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> |
Keith Busch | edce22e | 2022-01-05 09:05:15 -0800 | [diff] [blame] | 60 | #include <linux/blk-mq.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 | \ |
| 109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
| 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; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 477 | struct io_wq_work_list prior_task_list; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 478 | struct callback_head task_work; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 479 | bool task_running; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 480 | }; |
| 481 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 482 | /* |
| 483 | * First field must be the file pointer in all the |
| 484 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 485 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 486 | struct io_poll_iocb { |
| 487 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 488 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 489 | __poll_t events; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 490 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 491 | }; |
| 492 | |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 493 | struct io_poll_update { |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 494 | struct file *file; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 495 | u64 old_user_data; |
| 496 | u64 new_user_data; |
| 497 | __poll_t events; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 498 | bool update_events; |
| 499 | bool update_user_data; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 500 | }; |
| 501 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 502 | struct io_close { |
| 503 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 504 | int fd; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 505 | u32 file_slot; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 506 | }; |
| 507 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 508 | struct io_timeout_data { |
| 509 | struct io_kiocb *req; |
| 510 | struct hrtimer timer; |
| 511 | struct timespec64 ts; |
| 512 | enum hrtimer_mode mode; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 513 | u32 flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 516 | struct io_accept { |
| 517 | struct file *file; |
| 518 | struct sockaddr __user *addr; |
| 519 | int __user *addr_len; |
| 520 | int flags; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 521 | u32 file_slot; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 522 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | struct io_sync { |
| 526 | struct file *file; |
| 527 | loff_t len; |
| 528 | loff_t off; |
| 529 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 530 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 531 | }; |
| 532 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 533 | struct io_cancel { |
| 534 | struct file *file; |
| 535 | u64 addr; |
| 536 | }; |
| 537 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 538 | struct io_timeout { |
| 539 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 540 | u32 off; |
| 541 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 542 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 543 | /* head of the link, used by linked timeouts only */ |
| 544 | struct io_kiocb *head; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 545 | /* for linked completions */ |
| 546 | struct io_kiocb *prev; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 549 | struct io_timeout_rem { |
| 550 | struct file *file; |
| 551 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 552 | |
| 553 | /* timeout update */ |
| 554 | struct timespec64 ts; |
| 555 | u32 flags; |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 556 | bool ltimeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 557 | }; |
| 558 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 559 | struct io_rw { |
| 560 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 561 | struct kiocb kiocb; |
| 562 | u64 addr; |
| 563 | u64 len; |
| 564 | }; |
| 565 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 566 | struct io_connect { |
| 567 | struct file *file; |
| 568 | struct sockaddr __user *addr; |
| 569 | int addr_len; |
| 570 | }; |
| 571 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 572 | struct io_sr_msg { |
| 573 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 574 | union { |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 575 | struct compat_msghdr __user *umsg_compat; |
| 576 | struct user_msghdr __user *umsg; |
| 577 | void __user *buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 578 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 579 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 580 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 581 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 582 | }; |
| 583 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 584 | struct io_open { |
| 585 | struct file *file; |
| 586 | int dfd; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 587 | u32 file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 588 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 589 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 590 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 591 | }; |
| 592 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 593 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 594 | struct file *file; |
| 595 | u64 arg; |
| 596 | u32 nr_args; |
| 597 | u32 offset; |
| 598 | }; |
| 599 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 600 | struct io_fadvise { |
| 601 | struct file *file; |
| 602 | u64 offset; |
| 603 | u32 len; |
| 604 | u32 advice; |
| 605 | }; |
| 606 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 607 | struct io_madvise { |
| 608 | struct file *file; |
| 609 | u64 addr; |
| 610 | u32 len; |
| 611 | u32 advice; |
| 612 | }; |
| 613 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 614 | struct io_epoll { |
| 615 | struct file *file; |
| 616 | int epfd; |
| 617 | int op; |
| 618 | int fd; |
| 619 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 620 | }; |
| 621 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 622 | struct io_splice { |
| 623 | struct file *file_out; |
| 624 | struct file *file_in; |
| 625 | loff_t off_out; |
| 626 | loff_t off_in; |
| 627 | u64 len; |
| 628 | unsigned int flags; |
| 629 | }; |
| 630 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 631 | struct io_provide_buf { |
| 632 | struct file *file; |
| 633 | __u64 addr; |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 634 | __u32 len; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 635 | __u32 bgid; |
| 636 | __u16 nbufs; |
| 637 | __u16 bid; |
| 638 | }; |
| 639 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 640 | struct io_statx { |
| 641 | struct file *file; |
| 642 | int dfd; |
| 643 | unsigned int mask; |
| 644 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 645 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 646 | struct statx __user *buffer; |
| 647 | }; |
| 648 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 649 | struct io_shutdown { |
| 650 | struct file *file; |
| 651 | int how; |
| 652 | }; |
| 653 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 654 | struct io_rename { |
| 655 | struct file *file; |
| 656 | int old_dfd; |
| 657 | int new_dfd; |
| 658 | struct filename *oldpath; |
| 659 | struct filename *newpath; |
| 660 | int flags; |
| 661 | }; |
| 662 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 663 | struct io_unlink { |
| 664 | struct file *file; |
| 665 | int dfd; |
| 666 | int flags; |
| 667 | struct filename *filename; |
| 668 | }; |
| 669 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 670 | struct io_mkdir { |
| 671 | struct file *file; |
| 672 | int dfd; |
| 673 | umode_t mode; |
| 674 | struct filename *filename; |
| 675 | }; |
| 676 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 677 | struct io_symlink { |
| 678 | struct file *file; |
| 679 | int new_dfd; |
| 680 | struct filename *oldpath; |
| 681 | struct filename *newpath; |
| 682 | }; |
| 683 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 684 | struct io_hardlink { |
| 685 | struct file *file; |
| 686 | int old_dfd; |
| 687 | int new_dfd; |
| 688 | struct filename *oldpath; |
| 689 | struct filename *newpath; |
| 690 | int flags; |
| 691 | }; |
| 692 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 693 | struct io_async_connect { |
| 694 | struct sockaddr_storage address; |
| 695 | }; |
| 696 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 697 | struct io_async_msghdr { |
| 698 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 699 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 700 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 701 | struct sockaddr __user *uaddr; |
| 702 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 703 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 704 | }; |
| 705 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 706 | struct io_rw_state { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 707 | struct iov_iter iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 708 | struct iov_iter_state iter_state; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 709 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 710 | }; |
| 711 | |
| 712 | struct io_async_rw { |
| 713 | struct io_rw_state s; |
| 714 | const struct iovec *free_iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 715 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 716 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 717 | }; |
| 718 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 719 | enum { |
| 720 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 721 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 722 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 723 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 724 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 725 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 726 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 727 | |
Pavel Begunkov | dddca22 | 2021-04-27 16:13:52 +0100 | [diff] [blame] | 728 | /* first byte is taken by user flags, shift it to not overlap */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 729 | REQ_F_FAIL_BIT = 8, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 730 | REQ_F_INFLIGHT_BIT, |
| 731 | REQ_F_CUR_POS_BIT, |
| 732 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 733 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 734 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 735 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 736 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 737 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 738 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 739 | REQ_F_CREDS_BIT, |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 740 | REQ_F_REFCOUNT_BIT, |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 741 | REQ_F_ARM_LTIMEOUT_BIT, |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 742 | REQ_F_ASYNC_DATA_BIT, |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 743 | REQ_F_SKIP_LINK_CQES_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 744 | /* keep async read/write and isreg together and in order */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 745 | REQ_F_SUPPORT_NOWAIT_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 746 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 747 | |
| 748 | /* not a real bit, just to check we're not overflowing the space */ |
| 749 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 750 | }; |
| 751 | |
| 752 | enum { |
| 753 | /* ctx owns file */ |
| 754 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 755 | /* drain existing IO first */ |
| 756 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 757 | /* linked sqes */ |
| 758 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 759 | /* doesn't sever on completion < 0 */ |
| 760 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 761 | /* IOSQE_ASYNC */ |
| 762 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 763 | /* IOSQE_BUFFER_SELECT */ |
| 764 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 765 | /* IOSQE_CQE_SKIP_SUCCESS */ |
| 766 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 767 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 768 | /* fail rest of links */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 769 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 770 | /* on inflight list, should be cancelled and waited on exit reliably */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 771 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 772 | /* read/write uses file position */ |
| 773 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 774 | /* must not punt to workers */ |
| 775 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 776 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 777 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 778 | /* needs cleanup */ |
| 779 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 780 | /* already went through poll handler */ |
| 781 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 782 | /* buffer already selected */ |
| 783 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 784 | /* completion is deferred through io_comp_state */ |
| 785 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 786 | /* caller should reissue async */ |
| 787 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 788 | /* supports async reads/writes */ |
| 789 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 790 | /* regular file */ |
| 791 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 792 | /* has creds assigned */ |
| 793 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 794 | /* skip refcounting if not set */ |
| 795 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 796 | /* there is a linked timeout that has to be armed */ |
| 797 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 798 | /* ->async_data allocated */ |
| 799 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 800 | /* don't post CQEs while failing linked requests */ |
| 801 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 802 | }; |
| 803 | |
| 804 | struct async_poll { |
| 805 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 806 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 807 | }; |
| 808 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 809 | 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] | 810 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 811 | struct io_task_work { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 812 | union { |
| 813 | struct io_wq_work_node node; |
| 814 | struct llist_node fallback_node; |
| 815 | }; |
| 816 | io_req_tw_func_t func; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 817 | }; |
| 818 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 819 | enum { |
| 820 | IORING_RSRC_FILE = 0, |
| 821 | IORING_RSRC_BUFFER = 1, |
| 822 | }; |
| 823 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 824 | /* |
| 825 | * NOTE! Each of the iocb union members has the file pointer |
| 826 | * as the first entry in their struct definition. So you can |
| 827 | * access the file pointer through any of the sub-structs, |
| 828 | * or directly as just 'ki_filp' in this struct. |
| 829 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 830 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 831 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 832 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 833 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 834 | struct io_poll_iocb poll; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 835 | struct io_poll_update poll_update; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 836 | struct io_accept accept; |
| 837 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 838 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 839 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 840 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 841 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 842 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 843 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 844 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 845 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 846 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 847 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 848 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 849 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 850 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 851 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 852 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 853 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 854 | struct io_unlink unlink; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 855 | struct io_mkdir mkdir; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 856 | struct io_symlink symlink; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 857 | struct io_hardlink hardlink; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 858 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 859 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 860 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 861 | /* polled IO has completed */ |
| 862 | u8 iopoll_completed; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 863 | u16 buf_index; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 864 | unsigned int flags; |
| 865 | |
| 866 | u64 user_data; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 867 | u32 result; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 868 | u32 cflags; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 869 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 870 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 871 | struct task_struct *task; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 872 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 873 | struct percpu_ref *fixed_rsrc_refs; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 874 | /* store used ubuf, so we can prevent reloading */ |
| 875 | struct io_mapped_ubuf *imu; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 876 | |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 877 | /* used by request caches, completion batching and iopoll */ |
Pavel Begunkov | ef05d9e | 2021-09-24 22:00:02 +0100 | [diff] [blame] | 878 | struct io_wq_work_node comp_list; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 879 | atomic_t refs; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 880 | struct io_kiocb *link; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 881 | struct io_task_work io_task_work; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 882 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 883 | struct hlist_node hash_node; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 884 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 885 | struct async_poll *apoll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 886 | /* opcode allocated if it needs to store data for async defer */ |
| 887 | void *async_data; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 888 | struct io_wq_work work; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 889 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 890 | const struct cred *creds; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 891 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 892 | struct io_buffer *kbuf; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 893 | atomic_t poll_refs; |
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 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1195 | static inline void req_ref_get(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_zero_or_close_to_overflow(req)); |
| 1199 | atomic_inc(&req->refs); |
| 1200 | } |
| 1201 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1202 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
| 1203 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1204 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1205 | __io_submit_flush_completions(ctx); |
| 1206 | } |
| 1207 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1208 | 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] | 1209 | { |
| 1210 | if (!(req->flags & REQ_F_REFCOUNT)) { |
| 1211 | req->flags |= REQ_F_REFCOUNT; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1212 | atomic_set(&req->refs, nr); |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1216 | static inline void io_req_set_refcount(struct io_kiocb *req) |
| 1217 | { |
| 1218 | __io_req_set_refcount(req, 1); |
| 1219 | } |
| 1220 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1221 | #define IO_RSRC_REF_BATCH 100 |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1222 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1223 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, |
| 1224 | struct io_ring_ctx *ctx) |
| 1225 | __must_hold(&ctx->uring_lock) |
| 1226 | { |
| 1227 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
| 1228 | |
| 1229 | if (ref) { |
| 1230 | if (ref == &ctx->rsrc_node->refs) |
| 1231 | ctx->rsrc_cached_refs++; |
| 1232 | else |
| 1233 | percpu_ref_put(ref); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1238 | { |
| 1239 | if (req->fixed_rsrc_refs) |
| 1240 | percpu_ref_put(req->fixed_rsrc_refs); |
| 1241 | } |
| 1242 | |
| 1243 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) |
| 1244 | __must_hold(&ctx->uring_lock) |
| 1245 | { |
| 1246 | if (ctx->rsrc_cached_refs) { |
| 1247 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); |
| 1248 | ctx->rsrc_cached_refs = 0; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) |
| 1253 | __must_hold(&ctx->uring_lock) |
| 1254 | { |
| 1255 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; |
| 1256 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); |
| 1257 | } |
| 1258 | |
Pavel Begunkov | a46be97 | 2021-10-09 23:14:40 +0100 | [diff] [blame] | 1259 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
| 1260 | struct io_ring_ctx *ctx) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1261 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1262 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1263 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1264 | ctx->rsrc_cached_refs--; |
| 1265 | if (unlikely(ctx->rsrc_cached_refs < 0)) |
| 1266 | io_rsrc_refs_refill(ctx); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 1270 | static unsigned int __io_put_kbuf(struct io_kiocb *req) |
Hao Xu | 3648e52 | 2021-12-05 14:37:57 +0000 | [diff] [blame] | 1271 | { |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 1272 | struct io_buffer *kbuf = req->kbuf; |
Hao Xu | 3648e52 | 2021-12-05 14:37:57 +0000 | [diff] [blame] | 1273 | unsigned int cflags; |
| 1274 | |
| 1275 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 1276 | cflags |= IORING_CQE_F_BUFFER; |
| 1277 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
| 1278 | kfree(kbuf); |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 1279 | req->kbuf = NULL; |
Hao Xu | 3648e52 | 2021-12-05 14:37:57 +0000 | [diff] [blame] | 1280 | return cflags; |
| 1281 | } |
| 1282 | |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 1283 | static inline unsigned int io_put_kbuf(struct io_kiocb *req) |
Hao Xu | 3648e52 | 2021-12-05 14:37:57 +0000 | [diff] [blame] | 1284 | { |
| 1285 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 1286 | return 0; |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 1287 | return __io_put_kbuf(req); |
Hao Xu | 3648e52 | 2021-12-05 14:37:57 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1290 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1291 | { |
| 1292 | bool got = percpu_ref_tryget(ref); |
| 1293 | |
| 1294 | /* already at zero, wait for ->release() */ |
| 1295 | if (!got) |
| 1296 | wait_for_completion(compl); |
| 1297 | percpu_ref_resurrect(ref); |
| 1298 | if (got) |
| 1299 | percpu_ref_put(ref); |
| 1300 | } |
| 1301 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1302 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
| 1303 | bool cancel_all) |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 1304 | __must_hold(&req->ctx->timeout_lock) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1305 | { |
| 1306 | struct io_kiocb *req; |
| 1307 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1308 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1309 | return false; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1310 | if (cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1311 | return true; |
| 1312 | |
| 1313 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1314 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1315 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1316 | } |
| 1317 | return false; |
| 1318 | } |
| 1319 | |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 1320 | static bool io_match_linked(struct io_kiocb *head) |
| 1321 | { |
| 1322 | struct io_kiocb *req; |
| 1323 | |
| 1324 | io_for_each_link(req, head) { |
| 1325 | if (req->flags & REQ_F_INFLIGHT) |
| 1326 | return true; |
| 1327 | } |
| 1328 | return false; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * As io_match_task() but protected against racing with linked timeouts. |
| 1333 | * User must not hold timeout_lock. |
| 1334 | */ |
| 1335 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, |
| 1336 | bool cancel_all) |
| 1337 | { |
| 1338 | bool matched; |
| 1339 | |
| 1340 | if (task && head->task != task) |
| 1341 | return false; |
| 1342 | if (cancel_all) |
| 1343 | return true; |
| 1344 | |
| 1345 | if (head->flags & REQ_F_LINK_TIMEOUT) { |
| 1346 | struct io_ring_ctx *ctx = head->ctx; |
| 1347 | |
| 1348 | /* protect against races with linked timeouts */ |
| 1349 | spin_lock_irq(&ctx->timeout_lock); |
| 1350 | matched = io_match_linked(head); |
| 1351 | spin_unlock_irq(&ctx->timeout_lock); |
| 1352 | } else { |
| 1353 | matched = io_match_linked(head); |
| 1354 | } |
| 1355 | return matched; |
| 1356 | } |
| 1357 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 1358 | static inline bool req_has_async_data(struct io_kiocb *req) |
| 1359 | { |
| 1360 | return req->flags & REQ_F_ASYNC_DATA; |
| 1361 | } |
| 1362 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1363 | static inline void req_set_fail(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1364 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1365 | req->flags |= REQ_F_FAIL; |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 1366 | if (req->flags & REQ_F_CQE_SKIP) { |
| 1367 | req->flags &= ~REQ_F_CQE_SKIP; |
| 1368 | req->flags |= REQ_F_SKIP_LINK_CQES; |
| 1369 | } |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1370 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1371 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 1372 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
| 1373 | { |
| 1374 | req_set_fail(req); |
| 1375 | req->result = res; |
| 1376 | } |
| 1377 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1378 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1379 | { |
| 1380 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1381 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1382 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1385 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1386 | { |
| 1387 | return !req->timeout.off; |
| 1388 | } |
| 1389 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1390 | static __cold void io_fallback_req_func(struct work_struct *work) |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1391 | { |
| 1392 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 1393 | fallback_work.work); |
| 1394 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); |
| 1395 | struct io_kiocb *req, *tmp; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1396 | bool locked = false; |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1397 | |
| 1398 | percpu_ref_get(&ctx->refs); |
| 1399 | 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] | 1400 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 5636c00 | 2021-08-18 12:42:45 +0100 | [diff] [blame] | 1401 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1402 | if (locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1403 | io_submit_flush_completions(ctx); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1404 | mutex_unlock(&ctx->uring_lock); |
| 1405 | } |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1406 | percpu_ref_put(&ctx->refs); |
| 1407 | } |
| 1408 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1409 | 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] | 1410 | { |
| 1411 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1412 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1413 | |
| 1414 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1415 | if (!ctx) |
| 1416 | return NULL; |
| 1417 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1418 | /* |
| 1419 | * Use 5 bits less than the max cq entries, that should give us around |
| 1420 | * 32 entries per hash list if totally full and uniformly spread. |
| 1421 | */ |
| 1422 | hash_bits = ilog2(p->cq_entries); |
| 1423 | hash_bits -= 5; |
| 1424 | if (hash_bits <= 0) |
| 1425 | hash_bits = 1; |
| 1426 | ctx->cancel_hash_bits = hash_bits; |
| 1427 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1428 | GFP_KERNEL); |
| 1429 | if (!ctx->cancel_hash) |
| 1430 | goto err; |
| 1431 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1432 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1433 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
| 1434 | if (!ctx->dummy_ubuf) |
| 1435 | goto err; |
| 1436 | /* set invalid range, so io_import_fixed() fails meeting it */ |
| 1437 | ctx->dummy_ubuf->ubuf = -1UL; |
| 1438 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1439 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1440 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1441 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1442 | |
| 1443 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1444 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1445 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1446 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1447 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1448 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1449 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1450 | mutex_init(&ctx->uring_lock); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1451 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1452 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1453 | spin_lock_init(&ctx->timeout_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 1454 | INIT_WQ_LIST(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1455 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1456 | INIT_LIST_HEAD(&ctx->timeout_list); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 1457 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1458 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1459 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1460 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1461 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1462 | INIT_LIST_HEAD(&ctx->tctx_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1463 | ctx->submit_state.free_list.next = NULL; |
| 1464 | INIT_WQ_LIST(&ctx->locked_free_list); |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 1465 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1466 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1467 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1468 | err: |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1469 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1470 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1471 | kfree(ctx); |
| 1472 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1475 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
| 1476 | { |
| 1477 | struct io_rings *r = ctx->rings; |
| 1478 | |
| 1479 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); |
| 1480 | ctx->cq_extra--; |
| 1481 | } |
| 1482 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1483 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1484 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1485 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1486 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1487 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1488 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1489 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1490 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1491 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1492 | } |
| 1493 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 1494 | #define FFS_NOWAIT 0x1UL |
| 1495 | #define FFS_ISREG 0x2UL |
| 1496 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1497 | |
| 1498 | static inline bool io_req_ffs_set(struct io_kiocb *req) |
| 1499 | { |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 1500 | return req->flags & REQ_F_FIXED_FILE; |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1501 | } |
| 1502 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1503 | static inline void io_req_track_inflight(struct io_kiocb *req) |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1504 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1505 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1506 | req->flags |= REQ_F_INFLIGHT; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1507 | atomic_inc(¤t->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1511 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
| 1512 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1513 | if (WARN_ON_ONCE(!req->link)) |
| 1514 | return NULL; |
| 1515 | |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1516 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
| 1517 | req->flags |= REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1518 | |
| 1519 | /* linked timeouts should have two refs once prep'ed */ |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1520 | io_req_set_refcount(req); |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1521 | __io_req_set_refcount(req->link, 2); |
| 1522 | return req->link; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
| 1526 | { |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1527 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1528 | return NULL; |
| 1529 | return __io_prep_linked_timeout(req); |
| 1530 | } |
| 1531 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1532 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1533 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1534 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1535 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1536 | |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1537 | if (!(req->flags & REQ_F_CREDS)) { |
| 1538 | req->flags |= REQ_F_CREDS; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 1539 | req->creds = get_current_cred(); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1540 | } |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1541 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1542 | req->work.list.next = NULL; |
| 1543 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1544 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1545 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1546 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1547 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1548 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1549 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1550 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1551 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1552 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1553 | } |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1554 | |
| 1555 | switch (req->opcode) { |
| 1556 | case IORING_OP_SPLICE: |
| 1557 | case IORING_OP_TEE: |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1558 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
| 1559 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1560 | break; |
| 1561 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1562 | } |
| 1563 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1564 | static void io_prep_async_link(struct io_kiocb *req) |
| 1565 | { |
| 1566 | struct io_kiocb *cur; |
| 1567 | |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1568 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
| 1569 | struct io_ring_ctx *ctx = req->ctx; |
| 1570 | |
Pavel Begunkov | 674ee8e | 2021-11-23 01:45:35 +0000 | [diff] [blame] | 1571 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1572 | io_for_each_link(cur, req) |
| 1573 | io_prep_async_work(cur); |
Pavel Begunkov | 674ee8e | 2021-11-23 01:45:35 +0000 | [diff] [blame] | 1574 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1575 | } else { |
| 1576 | io_for_each_link(cur, req) |
| 1577 | io_prep_async_work(cur); |
| 1578 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1579 | } |
| 1580 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1581 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
| 1582 | { |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 1583 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 33ce2af | 2021-12-14 13:59:04 +0800 | [diff] [blame] | 1584 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1585 | |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 1586 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1587 | ctx->submit_state.flush_cqes = true; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1588 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
| 1589 | } |
| 1590 | |
Arnd Bergmann | 0016924 | 2021-10-19 17:34:53 +0200 | [diff] [blame] | 1591 | 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] | 1592 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1593 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1594 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1595 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1596 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1597 | BUG_ON(!tctx); |
| 1598 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1599 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1600 | /* init ->work of the whole link before punting */ |
| 1601 | io_prep_async_link(req); |
Jens Axboe | 991468d | 2021-07-23 11:53:54 -0600 | [diff] [blame] | 1602 | |
| 1603 | /* |
| 1604 | * Not expected to happen, but if we do have a bug where this _can_ |
| 1605 | * happen, catch it here and ensure the request is marked as |
| 1606 | * canceled. That will make io-wq go through the usual work cancel |
| 1607 | * procedure rather than attempt to run this request (or create a new |
| 1608 | * worker for it). |
| 1609 | */ |
| 1610 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) |
| 1611 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1612 | |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1613 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1614 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1615 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1616 | if (link) |
| 1617 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1618 | } |
| 1619 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1620 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1621 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1622 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1623 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1624 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1625 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1626 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 2ae2eb9 | 2021-09-09 13:56:27 +0100 | [diff] [blame] | 1627 | if (status) |
| 1628 | req_set_fail(req); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1629 | atomic_set(&req->ctx->cq_timeouts, |
| 1630 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1631 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1632 | io_fill_cqe_req(req, status, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1633 | io_put_req_deferred(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1637 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1638 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1639 | while (!list_empty(&ctx->defer_list)) { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1640 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1641 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1642 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1643 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1644 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1645 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1646 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1647 | kfree(de); |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1648 | } |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1649 | } |
| 1650 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1651 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1652 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1653 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1654 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1655 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1656 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1657 | while (!list_empty(&ctx->timeout_list)) { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1658 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1659 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1660 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1661 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1662 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1663 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1664 | |
| 1665 | /* |
| 1666 | * Since seq can easily wrap around over time, subtract |
| 1667 | * the last seq at which timeouts were flushed before comparing. |
| 1668 | * Assuming not more than 2^31-1 events have happened since, |
| 1669 | * these subtractions won't have wrapped, so we can check if |
| 1670 | * target is in [last_seq, current_seq] by comparing the two. |
| 1671 | */ |
| 1672 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1673 | events_got = seq - ctx->cq_last_tm_flush; |
| 1674 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1675 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1676 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1677 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1678 | io_kill_timeout(req, 0); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1679 | } |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1680 | ctx->cq_last_tm_flush = seq; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1681 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1682 | } |
| 1683 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1684 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1685 | { |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1686 | if (ctx->off_timeout_used) |
| 1687 | io_flush_timeouts(ctx); |
| 1688 | if (ctx->drain_active) |
| 1689 | io_queue_deferred(ctx); |
| 1690 | } |
| 1691 | |
| 1692 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1693 | { |
| 1694 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) |
| 1695 | __io_commit_cqring_flush(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1696 | /* order cqe stores with ring update */ |
| 1697 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1698 | } |
| 1699 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1700 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1701 | { |
| 1702 | struct io_rings *r = ctx->rings; |
| 1703 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1704 | 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] | 1705 | } |
| 1706 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1707 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1708 | { |
| 1709 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1710 | } |
| 1711 | |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1712 | 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] | 1713 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1714 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1715 | unsigned tail, mask = ctx->cq_entries - 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1716 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1717 | /* |
| 1718 | * writes to the cq entry need to come after reading head; the |
| 1719 | * control dependency is enough as we're using WRITE_ONCE to |
| 1720 | * fill the cq entry |
| 1721 | */ |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1722 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1723 | return NULL; |
| 1724 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1725 | tail = ctx->cached_cq_tail++; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1726 | return &rings->cqes[tail & mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1729 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1730 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1731 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1732 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1733 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1734 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1735 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Jens Axboe | 2c5d763 | 2021-08-21 07:21:19 -0600 | [diff] [blame] | 1738 | /* |
| 1739 | * This should only get called when at least one event has been posted. |
| 1740 | * Some applications rely on the eventfd notification count only changing |
| 1741 | * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1742 | * 1:1 relationship between how many times this function is called (and |
| 1743 | * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1744 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1745 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1746 | { |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1747 | /* |
| 1748 | * wake_up_all() may seem excessive, but io_wake_function() and |
| 1749 | * io_should_wake() handle the termination of the loop and only |
| 1750 | * wake as many waiters as we need to. |
| 1751 | */ |
| 1752 | if (wq_has_sleeper(&ctx->cq_wait)) |
| 1753 | wake_up_all(&ctx->cq_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1754 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1755 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1756 | } |
| 1757 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1758 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1759 | { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1760 | /* see waitqueue_active() comment */ |
| 1761 | smp_mb(); |
| 1762 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1763 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1764 | if (waitqueue_active(&ctx->cq_wait)) |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1765 | wake_up_all(&ctx->cq_wait); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1766 | } |
| 1767 | if (io_should_trigger_evfd(ctx)) |
| 1768 | eventfd_signal(ctx->cq_ev_fd, 1); |
| 1769 | } |
| 1770 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1771 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1772 | 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] | 1773 | { |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1774 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1775 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1776 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1777 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1778 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1779 | posted = false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1780 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1781 | while (!list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1782 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1783 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1784 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1785 | if (!cqe && !force) |
| 1786 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1787 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1788 | struct io_overflow_cqe, list); |
| 1789 | if (cqe) |
| 1790 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1791 | else |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1792 | io_account_cq_overflow(ctx); |
| 1793 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1794 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1795 | list_del(&ocqe->list); |
| 1796 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1799 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1800 | if (all_flushed) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1801 | clear_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1802 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1803 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1804 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1805 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1806 | if (posted) |
| 1807 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1808 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1809 | if (posted) |
| 1810 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1811 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1814 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1815 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1816 | bool ret = true; |
| 1817 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1818 | if (test_bit(0, &ctx->check_cq_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1819 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1820 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1821 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1822 | ret = __io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1823 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1824 | mutex_unlock(&ctx->uring_lock); |
| 1825 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1826 | |
| 1827 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1830 | /* must to be called somewhat shortly after putting a request */ |
| 1831 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1832 | { |
| 1833 | struct io_uring_task *tctx = task->io_uring; |
| 1834 | |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 1835 | if (likely(task == current)) { |
| 1836 | tctx->cached_refs += nr; |
| 1837 | } else { |
| 1838 | percpu_counter_sub(&tctx->inflight, nr); |
| 1839 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1840 | wake_up(&tctx->wait); |
| 1841 | put_task_struct_many(task, nr); |
| 1842 | } |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 1845 | static void io_task_refs_refill(struct io_uring_task *tctx) |
| 1846 | { |
| 1847 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; |
| 1848 | |
| 1849 | percpu_counter_add(&tctx->inflight, refill); |
| 1850 | refcount_add(refill, ¤t->usage); |
| 1851 | tctx->cached_refs += refill; |
| 1852 | } |
| 1853 | |
| 1854 | static inline void io_get_task_refs(int nr) |
| 1855 | { |
| 1856 | struct io_uring_task *tctx = current->io_uring; |
| 1857 | |
| 1858 | tctx->cached_refs -= nr; |
| 1859 | if (unlikely(tctx->cached_refs < 0)) |
| 1860 | io_task_refs_refill(tctx); |
| 1861 | } |
| 1862 | |
Pavel Begunkov | 3cc7fdb | 2022-01-09 00:53:22 +0000 | [diff] [blame] | 1863 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
| 1864 | { |
| 1865 | struct io_uring_task *tctx = task->io_uring; |
| 1866 | unsigned int refs = tctx->cached_refs; |
| 1867 | |
| 1868 | if (refs) { |
| 1869 | tctx->cached_refs = 0; |
| 1870 | percpu_counter_sub(&tctx->inflight, refs); |
| 1871 | put_task_struct_many(task, refs); |
| 1872 | } |
| 1873 | } |
| 1874 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1875 | 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] | 1876 | s32 res, u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1877 | { |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1878 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1879 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1880 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1881 | if (!ocqe) { |
| 1882 | /* |
| 1883 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1884 | * or cannot allocate an overflow entry, then we need to drop it |
| 1885 | * on the floor. |
| 1886 | */ |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1887 | io_account_cq_overflow(ctx); |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1888 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1889 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1890 | if (list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1891 | set_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1892 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1893 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); |
| 1894 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1895 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1896 | ocqe->cqe.user_data = user_data; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1897 | ocqe->cqe.res = res; |
| 1898 | ocqe->cqe.flags = cflags; |
| 1899 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1900 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1903 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
| 1904 | s32 res, u32 cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1905 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1906 | struct io_uring_cqe *cqe; |
| 1907 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1908 | trace_io_uring_complete(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1909 | |
| 1910 | /* |
| 1911 | * If we can't get a cq entry, userspace overflowed the |
| 1912 | * submission (by quite a lot). Increment the overflow count in |
| 1913 | * the ring. |
| 1914 | */ |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1915 | cqe = io_get_cqe(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1916 | if (likely(cqe)) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1917 | WRITE_ONCE(cqe->user_data, user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1918 | WRITE_ONCE(cqe->res, res); |
| 1919 | WRITE_ONCE(cqe->flags, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1920 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1921 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1922 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1925 | 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] | 1926 | { |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 1927 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1928 | __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 1931 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
| 1932 | s32 res, u32 cflags) |
| 1933 | { |
| 1934 | ctx->cq_extra++; |
| 1935 | return __io_fill_cqe(ctx, user_data, res, cflags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
Hao Xu | a37fae8 | 2021-12-07 17:39:50 +0800 | [diff] [blame] | 1938 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
| 1939 | u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1940 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1941 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1942 | |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 1943 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 1944 | __io_fill_cqe(ctx, req->user_data, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1945 | /* |
| 1946 | * If we're the last reference to this request, add to our locked |
| 1947 | * free_list cache. |
| 1948 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1949 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1950 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1951 | if (req->flags & IO_DISARM_MASK) |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1952 | io_disarm_next(req); |
| 1953 | if (req->link) { |
| 1954 | io_req_task_queue(req->link); |
| 1955 | req->link = NULL; |
| 1956 | } |
| 1957 | } |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1958 | io_req_put_rsrc(req, ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1959 | io_dismantle_req(req); |
| 1960 | io_put_task(req->task, 1); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1961 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1962 | ctx->locked_free_nr++; |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1963 | } |
Hao Xu | a37fae8 | 2021-12-07 17:39:50 +0800 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
| 1967 | u32 cflags) |
| 1968 | { |
| 1969 | struct io_ring_ctx *ctx = req->ctx; |
| 1970 | |
| 1971 | spin_lock(&ctx->completion_lock); |
| 1972 | __io_req_complete_post(req, res, cflags); |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1973 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1974 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | a3f34907 | 2021-09-15 12:04:20 +0100 | [diff] [blame] | 1975 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1978 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
| 1979 | u32 cflags) |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1980 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1981 | req->result = res; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 1982 | req->cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1983 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1984 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1985 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1986 | 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] | 1987 | s32 res, u32 cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1988 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1989 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1990 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1991 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1992 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1993 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1994 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1995 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1996 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1997 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 2000 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 2001 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2002 | req_set_fail(req); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 2003 | io_req_complete_post(req, res, 0); |
| 2004 | } |
| 2005 | |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 2006 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
| 2007 | { |
| 2008 | /* |
| 2009 | * We don't submit, fail them all, for that replace hardlinks with |
| 2010 | * normal links. Extra REQ_F_LINK is tolerated. |
| 2011 | */ |
| 2012 | req->flags &= ~REQ_F_HARDLINK; |
| 2013 | req->flags |= REQ_F_LINK; |
| 2014 | io_req_complete_failed(req, req->result); |
| 2015 | } |
| 2016 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2017 | /* |
| 2018 | * Don't initialise the fields below on every allocation, but do that in |
| 2019 | * advance and keep them valid across allocations. |
| 2020 | */ |
| 2021 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 2022 | { |
| 2023 | req->ctx = ctx; |
| 2024 | req->link = NULL; |
| 2025 | req->async_data = NULL; |
| 2026 | /* not necessary, but safer to zero */ |
| 2027 | req->result = 0; |
| 2028 | } |
| 2029 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 2030 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2031 | struct io_submit_state *state) |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 2032 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2033 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2034 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 2035 | ctx->locked_free_nr = 0; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2036 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 2039 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 2040 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2041 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 2042 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2043 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 2044 | /* |
| 2045 | * If we have more than a batch's worth of requests in our IRQ side |
| 2046 | * locked cache, grab the lock and move them over to our submission |
| 2047 | * side cache. |
| 2048 | */ |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 2049 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2050 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2051 | return !!state->free_list.next; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 2054 | /* |
| 2055 | * A request might get retired back into the request caches even before opcode |
| 2056 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. |
| 2057 | * Because of that, io_alloc_req() should be called only under ->uring_lock |
| 2058 | * and with extra caution to not get a request that is still worked on. |
| 2059 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2060 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 2061 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2062 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 2063 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2064 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2065 | void *reqs[IO_REQ_ALLOC_BATCH]; |
| 2066 | struct io_kiocb *req; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2067 | int ret, i; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2068 | |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2069 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2070 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2071 | |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2072 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2073 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2074 | /* |
| 2075 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 2076 | * retry single alloc to be on the safe side. |
| 2077 | */ |
| 2078 | if (unlikely(ret <= 0)) { |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2079 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 2080 | if (!reqs[0]) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2081 | return false; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2082 | ret = 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2083 | } |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2084 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 2085 | percpu_ref_get_many(&ctx->refs, ret); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2086 | for (i = 0; i < ret; i++) { |
| 2087 | req = reqs[i]; |
| 2088 | |
| 2089 | io_preinit_req(req, ctx); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2090 | wq_stack_add_head(&req->comp_list, &state->free_list); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2091 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2092 | return true; |
| 2093 | } |
| 2094 | |
| 2095 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) |
| 2096 | { |
| 2097 | if (unlikely(!ctx->submit_state.free_list.next)) |
| 2098 | return __io_alloc_req_refill(ctx); |
| 2099 | return true; |
| 2100 | } |
| 2101 | |
| 2102 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
| 2103 | { |
| 2104 | struct io_wq_work_node *node; |
| 2105 | |
| 2106 | node = wq_stack_extract(&ctx->submit_state.free_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2107 | return container_of(node, struct io_kiocb, comp_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2110 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2111 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2112 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2113 | fput(file); |
| 2114 | } |
| 2115 | |
Pavel Begunkov | 6b63952 | 2021-09-08 16:40:50 +0100 | [diff] [blame] | 2116 | static inline void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2117 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 2118 | unsigned int flags = req->flags; |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2119 | |
Pavel Begunkov | 867f8fa | 2021-10-04 20:02:58 +0100 | [diff] [blame] | 2120 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 2121 | io_clean_op(req); |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2122 | if (!(flags & REQ_F_FIXED_FILE)) |
| 2123 | io_put_file(req->file); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2124 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2125 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2126 | static __cold void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2127 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2128 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2129 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 2130 | io_req_put_rsrc(req, ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2131 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2132 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2133 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2134 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2135 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 2136 | ctx->locked_free_nr++; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2137 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2138 | } |
| 2139 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2140 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2141 | { |
| 2142 | struct io_kiocb *nxt = req->link; |
| 2143 | |
| 2144 | req->link = nxt->link; |
| 2145 | nxt->link = NULL; |
| 2146 | } |
| 2147 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2148 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 2149 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2150 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2151 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2152 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2153 | |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 2154 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2155 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2156 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2157 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2158 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 2159 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 2160 | list_del(&link->timeout.list); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2161 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 2162 | io_fill_cqe_req(link, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2163 | io_put_req_deferred(link); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2164 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2165 | } |
| 2166 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2167 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2168 | } |
| 2169 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2170 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2171 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2172 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2173 | struct io_kiocb *nxt, *link = req->link; |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2174 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2175 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2176 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2177 | while (link) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2178 | long res = -ECANCELED; |
| 2179 | |
| 2180 | if (link->flags & REQ_F_FAIL) |
| 2181 | res = link->result; |
| 2182 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2183 | nxt = link->link; |
| 2184 | link->link = NULL; |
| 2185 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2186 | trace_io_uring_fail_link(req, link); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2187 | if (!ignore_cqes) { |
| 2188 | link->flags &= ~REQ_F_CQE_SKIP; |
| 2189 | io_fill_cqe_req(link, res, 0); |
| 2190 | } |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2191 | io_put_req_deferred(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2192 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2193 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2194 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2195 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2196 | static bool io_disarm_next(struct io_kiocb *req) |
| 2197 | __must_hold(&req->ctx->completion_lock) |
| 2198 | { |
| 2199 | bool posted = false; |
| 2200 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2201 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
| 2202 | struct io_kiocb *link = req->link; |
| 2203 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 2204 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2205 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
| 2206 | io_remove_next_linked(req); |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 2207 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 2208 | io_fill_cqe_req(link, -ECANCELED, 0); |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2209 | io_put_req_deferred(link); |
| 2210 | posted = true; |
| 2211 | } |
| 2212 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2213 | struct io_ring_ctx *ctx = req->ctx; |
| 2214 | |
| 2215 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2216 | posted = io_kill_linked_timeout(req); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2217 | spin_unlock_irq(&ctx->timeout_lock); |
| 2218 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2219 | if (unlikely((req->flags & REQ_F_FAIL) && |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 2220 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2221 | posted |= (req->link != NULL); |
| 2222 | io_fail_links(req); |
| 2223 | } |
| 2224 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2225 | } |
| 2226 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2227 | static void __io_req_find_next_prep(struct io_kiocb *req) |
| 2228 | { |
| 2229 | struct io_ring_ctx *ctx = req->ctx; |
| 2230 | bool posted; |
| 2231 | |
| 2232 | spin_lock(&ctx->completion_lock); |
| 2233 | posted = io_disarm_next(req); |
| 2234 | if (posted) |
Hao Xu | 33ce2af | 2021-12-14 13:59:04 +0800 | [diff] [blame] | 2235 | io_commit_cqring(ctx); |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2236 | spin_unlock(&ctx->completion_lock); |
| 2237 | if (posted) |
| 2238 | io_cqring_ev_posted(ctx); |
| 2239 | } |
| 2240 | |
| 2241 | 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] | 2242 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2243 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2244 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2245 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
| 2246 | return NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2247 | /* |
| 2248 | * If LINK is set, we have dependent requests in this chain. If we |
| 2249 | * didn't fail this request, queue the first one up, moving any other |
| 2250 | * dependencies to the next request. In case of failure, fail the rest |
| 2251 | * of the chain. |
| 2252 | */ |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2253 | if (unlikely(req->flags & IO_DISARM_MASK)) |
| 2254 | __io_req_find_next_prep(req); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2255 | nxt = req->link; |
| 2256 | req->link = NULL; |
| 2257 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2258 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2259 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2260 | 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] | 2261 | { |
| 2262 | if (!ctx) |
| 2263 | return; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2264 | if (*locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2265 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2266 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2267 | *locked = false; |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2268 | } |
| 2269 | percpu_ref_put(&ctx->refs); |
| 2270 | } |
| 2271 | |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2272 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
| 2273 | { |
| 2274 | io_commit_cqring(ctx); |
| 2275 | spin_unlock(&ctx->completion_lock); |
| 2276 | io_cqring_ev_posted(ctx); |
| 2277 | } |
| 2278 | |
| 2279 | static void handle_prev_tw_list(struct io_wq_work_node *node, |
| 2280 | struct io_ring_ctx **ctx, bool *uring_locked) |
| 2281 | { |
| 2282 | if (*ctx && !*uring_locked) |
| 2283 | spin_lock(&(*ctx)->completion_lock); |
| 2284 | |
| 2285 | do { |
| 2286 | struct io_wq_work_node *next = node->next; |
| 2287 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2288 | io_task_work.node); |
| 2289 | |
| 2290 | if (req->ctx != *ctx) { |
| 2291 | if (unlikely(!*uring_locked && *ctx)) |
| 2292 | ctx_commit_and_unlock(*ctx); |
| 2293 | |
| 2294 | ctx_flush_and_put(*ctx, uring_locked); |
| 2295 | *ctx = req->ctx; |
| 2296 | /* if not contended, grab and improve batching */ |
| 2297 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); |
| 2298 | percpu_ref_get(&(*ctx)->refs); |
| 2299 | if (unlikely(!*uring_locked)) |
| 2300 | spin_lock(&(*ctx)->completion_lock); |
| 2301 | } |
| 2302 | if (likely(*uring_locked)) |
| 2303 | req->io_task_work.func(req, uring_locked); |
| 2304 | else |
| 2305 | __io_req_complete_post(req, req->result, io_put_kbuf(req)); |
| 2306 | node = next; |
| 2307 | } while (node); |
| 2308 | |
| 2309 | if (unlikely(!*uring_locked)) |
| 2310 | ctx_commit_and_unlock(*ctx); |
| 2311 | } |
| 2312 | |
| 2313 | static void handle_tw_list(struct io_wq_work_node *node, |
| 2314 | struct io_ring_ctx **ctx, bool *locked) |
Hao Xu | 9f8d032 | 2021-12-07 17:39:49 +0800 | [diff] [blame] | 2315 | { |
| 2316 | do { |
| 2317 | struct io_wq_work_node *next = node->next; |
| 2318 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2319 | io_task_work.node); |
| 2320 | |
| 2321 | if (req->ctx != *ctx) { |
| 2322 | ctx_flush_and_put(*ctx, locked); |
| 2323 | *ctx = req->ctx; |
| 2324 | /* if not contended, grab and improve batching */ |
| 2325 | *locked = mutex_trylock(&(*ctx)->uring_lock); |
| 2326 | percpu_ref_get(&(*ctx)->refs); |
| 2327 | } |
| 2328 | req->io_task_work.func(req, locked); |
| 2329 | node = next; |
| 2330 | } while (node); |
| 2331 | } |
| 2332 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2333 | static void tctx_task_work(struct callback_head *cb) |
| 2334 | { |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2335 | bool uring_locked = false; |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2336 | struct io_ring_ctx *ctx = NULL; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2337 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
| 2338 | task_work); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2339 | |
Pavel Begunkov | 16f7207 | 2021-06-17 18:14:09 +0100 | [diff] [blame] | 2340 | while (1) { |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2341 | struct io_wq_work_node *node1, *node2; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2342 | |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2343 | if (!tctx->task_list.first && |
| 2344 | !tctx->prior_task_list.first && uring_locked) |
Pavel Begunkov | 8d4ad41 | 2021-09-02 00:38:23 +0100 | [diff] [blame] | 2345 | io_submit_flush_completions(ctx); |
| 2346 | |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2347 | spin_lock_irq(&tctx->task_lock); |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2348 | node1 = tctx->prior_task_list.first; |
| 2349 | node2 = tctx->task_list.first; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2350 | INIT_WQ_LIST(&tctx->task_list); |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2351 | INIT_WQ_LIST(&tctx->prior_task_list); |
| 2352 | if (!node2 && !node1) |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2353 | tctx->task_running = false; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2354 | spin_unlock_irq(&tctx->task_lock); |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2355 | if (!node2 && !node1) |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2356 | break; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2357 | |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2358 | if (node1) |
| 2359 | handle_prev_tw_list(node1, &ctx, &uring_locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2360 | |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2361 | if (node2) |
| 2362 | handle_tw_list(node2, &ctx, &uring_locked); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2363 | cond_resched(); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2364 | } |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2365 | |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2366 | ctx_flush_and_put(ctx, &uring_locked); |
Pavel Begunkov | 3cc7fdb | 2022-01-09 00:53:22 +0000 | [diff] [blame] | 2367 | |
| 2368 | /* relaxed read is enough as only the task itself sets ->in_idle */ |
| 2369 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 2370 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 2373 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2374 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2375 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2376 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2377 | enum task_work_notify_mode notify; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2378 | struct io_wq_work_node *node; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2379 | unsigned long flags; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2380 | bool running; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2381 | |
| 2382 | WARN_ON_ONCE(!tctx); |
| 2383 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2384 | spin_lock_irqsave(&tctx->task_lock, flags); |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 2385 | if (priority) |
| 2386 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); |
| 2387 | else |
| 2388 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2389 | running = tctx->task_running; |
| 2390 | if (!running) |
| 2391 | tctx->task_running = true; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2392 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2393 | |
| 2394 | /* task_work already pending, we're done */ |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2395 | if (running) |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2396 | return; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2397 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2398 | /* |
| 2399 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2400 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2401 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2402 | * will do the job. |
| 2403 | */ |
| 2404 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
Pavel Begunkov | d97ec62 | 2021-09-08 16:40:53 +0100 | [diff] [blame] | 2405 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
| 2406 | if (notify == TWA_NONE) |
| 2407 | wake_up_process(tsk); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2408 | return; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2409 | } |
Pavel Begunkov | 2215bed | 2021-08-09 13:04:06 +0100 | [diff] [blame] | 2410 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2411 | spin_lock_irqsave(&tctx->task_lock, flags); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2412 | tctx->task_running = false; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 2413 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2414 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2415 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2416 | while (node) { |
| 2417 | req = container_of(node, struct io_kiocb, io_task_work.node); |
| 2418 | node = node->next; |
| 2419 | if (llist_add(&req->io_task_work.fallback_node, |
| 2420 | &req->ctx->fallback_llist)) |
| 2421 | schedule_delayed_work(&req->ctx->fallback_work, 1); |
| 2422 | } |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2425 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2426 | { |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2427 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2428 | |
Pavel Begunkov | b18a1a4 | 2021-08-25 20:51:39 +0100 | [diff] [blame] | 2429 | /* not needed for normal modes, but SQPOLL depends on it */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2430 | io_tw_lock(ctx, locked); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2431 | io_req_complete_failed(req, req->result); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2432 | } |
| 2433 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2434 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2435 | { |
| 2436 | struct io_ring_ctx *ctx = req->ctx; |
| 2437 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2438 | io_tw_lock(ctx, locked); |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 2439 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | af066f3 | 2021-08-09 13:04:19 +0100 | [diff] [blame] | 2440 | if (likely(!(req->task->flags & PF_EXITING))) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2441 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2442 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2443 | io_req_complete_failed(req, -EFAULT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2444 | } |
| 2445 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2446 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2447 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2448 | req->result = ret; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2449 | req->io_task_work.func = io_req_task_cancel; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 2450 | io_req_task_work_add(req, false); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2453 | static void io_req_task_queue(struct io_kiocb *req) |
| 2454 | { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2455 | req->io_task_work.func = io_req_task_submit; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 2456 | io_req_task_work_add(req, false); |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2459 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
| 2460 | { |
| 2461 | req->io_task_work.func = io_queue_async_work; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 2462 | io_req_task_work_add(req, false); |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2463 | } |
| 2464 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2465 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2466 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2467 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2468 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2469 | if (nxt) |
| 2470 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2471 | } |
| 2472 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2473 | static void io_free_req(struct io_kiocb *req) |
| 2474 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2475 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2476 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2477 | } |
| 2478 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2479 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
| 2480 | { |
| 2481 | io_free_req(req); |
| 2482 | } |
| 2483 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2484 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2485 | struct io_wq_work_node *node) |
Jens Axboe | a141dd8 | 2021-08-12 12:48:34 -0600 | [diff] [blame] | 2486 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2487 | { |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2488 | struct task_struct *task = NULL; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 2489 | int task_refs = 0; |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2490 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2491 | do { |
| 2492 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2493 | comp_list); |
| 2494 | |
Pavel Begunkov | def77ac | 2021-10-12 15:02:14 +0100 | [diff] [blame] | 2495 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2496 | node = req->comp_list.next; |
Pavel Begunkov | def77ac | 2021-10-12 15:02:14 +0100 | [diff] [blame] | 2497 | if (!req_ref_put_and_test(req)) |
| 2498 | continue; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2499 | } |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2500 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 2501 | io_req_put_rsrc_locked(req, ctx); |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2502 | io_queue_next(req); |
| 2503 | io_dismantle_req(req); |
| 2504 | |
| 2505 | if (req->task != task) { |
| 2506 | if (task) |
| 2507 | io_put_task(task, task_refs); |
| 2508 | task = req->task; |
| 2509 | task_refs = 0; |
| 2510 | } |
| 2511 | task_refs++; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2512 | node = req->comp_list.next; |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2513 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2514 | } while (node); |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2515 | |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2516 | if (task) |
| 2517 | io_put_task(task, task_refs); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2520 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2521 | __must_hold(&ctx->uring_lock) |
| 2522 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2523 | struct io_wq_work_node *node, *prev; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2524 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2525 | |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 2526 | if (state->flush_cqes) { |
| 2527 | spin_lock(&ctx->completion_lock); |
| 2528 | wq_list_for_each(node, prev, &state->compl_reqs) { |
| 2529 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2530 | comp_list); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2531 | |
Pavel Begunkov | 3d4aeb9 | 2021-11-10 15:49:33 +0000 | [diff] [blame] | 2532 | if (!(req->flags & REQ_F_CQE_SKIP)) |
| 2533 | __io_fill_cqe(ctx, req->user_data, req->result, |
| 2534 | req->cflags); |
| 2535 | } |
| 2536 | |
| 2537 | io_commit_cqring(ctx); |
| 2538 | spin_unlock(&ctx->completion_lock); |
| 2539 | io_cqring_ev_posted(ctx); |
| 2540 | state->flush_cqes = false; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2541 | } |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2542 | |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2543 | io_free_batch_list(ctx, state->compl_reqs.first); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2544 | INIT_WQ_LIST(&state->compl_reqs); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2545 | } |
| 2546 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2547 | /* |
| 2548 | * Drop reference to request, return next in chain (if there is one) if this |
| 2549 | * was the last reference to this request. |
| 2550 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2551 | 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] | 2552 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2553 | struct io_kiocb *nxt = NULL; |
| 2554 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2555 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2556 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2557 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2558 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2559 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2562 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2563 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2564 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2565 | io_free_req(req); |
| 2566 | } |
| 2567 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2568 | static inline void io_put_req_deferred(struct io_kiocb *req) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2569 | { |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2570 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2571 | req->io_task_work.func = io_free_req_work; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 2572 | io_req_task_work_add(req, false); |
Pavel Begunkov | 543af3a | 2021-08-09 13:04:15 +0100 | [diff] [blame] | 2573 | } |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2574 | } |
| 2575 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2576 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2577 | { |
| 2578 | /* See comment at the top of this file */ |
| 2579 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2580 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2581 | } |
| 2582 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2583 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2584 | { |
| 2585 | struct io_rings *rings = ctx->rings; |
| 2586 | |
| 2587 | /* make sure SQ entry isn't read before tail */ |
| 2588 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2589 | } |
| 2590 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2591 | static inline bool io_run_task_work(void) |
| 2592 | { |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2593 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2594 | __set_current_state(TASK_RUNNING); |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2595 | tracehook_notify_signal(); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2596 | return true; |
| 2597 | } |
| 2598 | |
| 2599 | return false; |
| 2600 | } |
| 2601 | |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2602 | 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] | 2603 | { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2604 | struct io_wq_work_node *pos, *start, *prev; |
Christoph Hellwig | d729cf9 | 2021-10-12 13:12:20 +0200 | [diff] [blame] | 2605 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2606 | DEFINE_IO_COMP_BATCH(iob); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2607 | int nr_events = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2608 | |
| 2609 | /* |
| 2610 | * Only spin for completions if we don't have multiple devices hanging |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2611 | * off our complete list. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2612 | */ |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2613 | if (ctx->poll_multi_queue || force_nonspin) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2614 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2615 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2616 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
| 2617 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2618 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2619 | int ret; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2620 | |
| 2621 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2622 | * Move completed and retryable entries to our local lists. |
| 2623 | * If we find a request that requires polling, break out |
| 2624 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2625 | */ |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2626 | if (READ_ONCE(req->iopoll_completed)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2627 | break; |
| 2628 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2629 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2630 | if (unlikely(ret < 0)) |
| 2631 | return ret; |
| 2632 | else if (ret) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2633 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2634 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2635 | /* iopoll may have completed current req */ |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2636 | if (!rq_list_empty(iob.req_list) || |
| 2637 | READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2638 | break; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2639 | } |
| 2640 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2641 | if (!rq_list_empty(iob.req_list)) |
| 2642 | iob.complete(&iob); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2643 | else if (!pos) |
| 2644 | return 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2645 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2646 | prev = start; |
| 2647 | wq_list_for_each_resume(pos, prev) { |
| 2648 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
| 2649 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2650 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
| 2651 | if (!smp_load_acquire(&req->iopoll_completed)) |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2652 | break; |
Pavel Begunkov | 83a13a4 | 2021-12-05 14:37:59 +0000 | [diff] [blame] | 2653 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
| 2654 | continue; |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 2655 | |
Pavel Begunkov | 83a13a4 | 2021-12-05 14:37:59 +0000 | [diff] [blame] | 2656 | __io_fill_cqe(ctx, req->user_data, req->result, io_put_kbuf(req)); |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2657 | nr_events++; |
| 2658 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2659 | |
Pavel Begunkov | f5ed3bc | 2021-09-24 21:59:52 +0100 | [diff] [blame] | 2660 | if (unlikely(!nr_events)) |
| 2661 | return 0; |
| 2662 | |
| 2663 | io_commit_cqring(ctx); |
| 2664 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2665 | pos = start ? start->next : ctx->iopoll_list.first; |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2666 | wq_list_cut(&ctx->iopoll_list, prev, start); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2667 | io_free_batch_list(ctx, pos); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2668 | return nr_events; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | /* |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2672 | * We can't just wait for polled events to come to us, we have to actively |
| 2673 | * find and complete them. |
| 2674 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2675 | 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] | 2676 | { |
| 2677 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2678 | return; |
| 2679 | |
| 2680 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2681 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2682 | /* let it sleep and repeat later if can't complete a request */ |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2683 | if (io_do_iopoll(ctx, true) == 0) |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2684 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2685 | /* |
| 2686 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2687 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2688 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2689 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2690 | if (need_resched()) { |
| 2691 | mutex_unlock(&ctx->uring_lock); |
| 2692 | cond_resched(); |
| 2693 | mutex_lock(&ctx->uring_lock); |
| 2694 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2695 | } |
| 2696 | mutex_unlock(&ctx->uring_lock); |
| 2697 | } |
| 2698 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2699 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2700 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2701 | unsigned int nr_events = 0; |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2702 | int ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2703 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2704 | /* |
| 2705 | * We disallow the app entering submit/complete with polling, but we |
| 2706 | * still need to lock the ring to prevent racing with polled issue |
| 2707 | * that got punted to a workqueue. |
| 2708 | */ |
| 2709 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2710 | /* |
| 2711 | * Don't enter poll loop if we already have events pending. |
| 2712 | * If we do, we can potentially be spinning for commands that |
| 2713 | * already triggered a CQE (eg in error). |
| 2714 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 2715 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2716 | __io_cqring_overflow_flush(ctx, false); |
| 2717 | if (io_cqring_events(ctx)) |
| 2718 | goto out; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2719 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2720 | /* |
| 2721 | * If a submit got punted to a workqueue, we can have the |
| 2722 | * application entering polling for a command before it gets |
| 2723 | * issued. That app will hold the uring_lock for the duration |
| 2724 | * of the poll right here, so we need to take a breather every |
| 2725 | * now and then to ensure that the issue has a chance to add |
| 2726 | * the poll to the issued list. Otherwise we can spin here |
| 2727 | * forever, while the workqueue is stuck trying to acquire the |
| 2728 | * very same mutex. |
| 2729 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2730 | if (wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2731 | u32 tail = ctx->cached_cq_tail; |
| 2732 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2733 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2734 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2735 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2736 | |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2737 | /* some requests don't go through iopoll_list */ |
| 2738 | if (tail != ctx->cached_cq_tail || |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2739 | wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2740 | break; |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2741 | } |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2742 | ret = io_do_iopoll(ctx, !min); |
| 2743 | if (ret < 0) |
| 2744 | break; |
| 2745 | nr_events += ret; |
| 2746 | ret = 0; |
| 2747 | } while (nr_events < min && !need_resched()); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2748 | out: |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2749 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2750 | return ret; |
| 2751 | } |
| 2752 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2753 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2754 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2755 | /* |
| 2756 | * Tell lockdep we inherited freeze protection from submission |
| 2757 | * thread. |
| 2758 | */ |
| 2759 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2760 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2761 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2762 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2763 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2764 | } |
| 2765 | } |
| 2766 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2767 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2768 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2769 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2770 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2771 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 2772 | if (!req_has_async_data(req)) |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2773 | return !io_req_prep_async(req); |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 2774 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2775 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2776 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2777 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2778 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2779 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2780 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2781 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2782 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2783 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2784 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2785 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2786 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2787 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2788 | /* |
| 2789 | * If ref is dying, we might be running poll reap from the exit work. |
| 2790 | * Don't attempt to reissue from that path, just let it fail with |
| 2791 | * -EAGAIN. |
| 2792 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2793 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2794 | return false; |
Jens Axboe | ef04688 | 2021-07-27 10:50:31 -0600 | [diff] [blame] | 2795 | /* |
| 2796 | * Play it safe and assume not safe to re-import and reissue if we're |
| 2797 | * not in the original thread group (or in task context). |
| 2798 | */ |
| 2799 | if (!same_thread_group(req->task, current) || !in_task()) |
| 2800 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2801 | return true; |
| 2802 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2803 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2804 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2805 | { |
| 2806 | return false; |
| 2807 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2808 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2809 | { |
| 2810 | return false; |
| 2811 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2812 | #endif |
| 2813 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2814 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2815 | { |
Pavel Begunkov | b65c128 | 2021-03-22 01:45:59 +0000 | [diff] [blame] | 2816 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2817 | kiocb_end_write(req); |
Pavel Begunkov | 258f3a7 | 2021-10-14 16:10:14 +0100 | [diff] [blame] | 2818 | if (unlikely(res != req->result)) { |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2819 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2820 | io_rw_should_reissue(req)) { |
| 2821 | req->flags |= REQ_F_REISSUE; |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2822 | return true; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2823 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2824 | req_set_fail(req); |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2825 | req->result = res; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2826 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2827 | return false; |
| 2828 | } |
| 2829 | |
Pavel Begunkov | cc8e9ba | 2021-12-15 22:08:50 +0000 | [diff] [blame] | 2830 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2831 | { |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 2832 | unsigned int cflags = io_put_kbuf(req); |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 2833 | int res = req->result; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2834 | |
| 2835 | if (*locked) { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2836 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 2837 | io_req_add_compl_list(req); |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2838 | } else { |
| 2839 | io_req_complete_post(req, res, cflags); |
| 2840 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2841 | } |
| 2842 | |
GuoYong Zheng | 00f6e68 | 2022-01-05 18:12:02 +0800 | [diff] [blame] | 2843 | static void __io_complete_rw(struct io_kiocb *req, long res, |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2844 | unsigned int issue_flags) |
| 2845 | { |
| 2846 | if (__io_complete_rw_common(req, res)) |
| 2847 | return; |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 2848 | __io_req_complete(req, issue_flags, req->result, io_put_kbuf(req)); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2849 | } |
| 2850 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2851 | static void io_complete_rw(struct kiocb *kiocb, long res) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2852 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2853 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2854 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2855 | if (__io_complete_rw_common(req, res)) |
| 2856 | return; |
| 2857 | req->result = res; |
| 2858 | req->io_task_work.func = io_req_task_complete; |
Hao Xu | f28c240e | 2021-12-08 13:21:25 +0800 | [diff] [blame] | 2859 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2862 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2863 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2864 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2865 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2866 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2867 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2868 | if (unlikely(res != req->result)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2869 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
| 2870 | req->flags |= REQ_F_REISSUE; |
| 2871 | return; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2872 | } |
Pavel Begunkov | 258f3a7 | 2021-10-14 16:10:14 +0100 | [diff] [blame] | 2873 | req->result = res; |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2874 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2875 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2876 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
| 2877 | smp_store_release(&req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | /* |
| 2881 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2882 | * 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] | 2883 | * 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] | 2884 | * accessing the kiocb cookie. |
| 2885 | */ |
Pavel Begunkov | 9882131 | 2021-10-15 17:09:12 +0100 | [diff] [blame] | 2886 | 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] | 2887 | { |
| 2888 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2889 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2890 | |
| 2891 | /* workqueue context doesn't hold uring_lock, grab it now */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2892 | if (unlikely(needs_lock)) |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2893 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2894 | |
| 2895 | /* |
| 2896 | * Track whether we have multiple files in our lists. This will impact |
| 2897 | * how we do polling eventually, not spinning if we're on potentially |
| 2898 | * different devices. |
| 2899 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2900 | if (wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2901 | ctx->poll_multi_queue = false; |
| 2902 | } else if (!ctx->poll_multi_queue) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2903 | struct io_kiocb *list_req; |
| 2904 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2905 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
| 2906 | comp_list); |
Christoph Hellwig | 30da1b4 | 2021-10-12 13:12:14 +0200 | [diff] [blame] | 2907 | if (list_req->file != req->file) |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2908 | ctx->poll_multi_queue = true; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | /* |
| 2912 | * For fast devices, IO may have already completed. If it has, add |
| 2913 | * it to the front so we find it first. |
| 2914 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2915 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2916 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2917 | else |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2918 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2919 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2920 | if (unlikely(needs_lock)) { |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2921 | /* |
| 2922 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle |
| 2923 | * in sq thread task context or in io worker task context. If |
| 2924 | * current task context is sq thread, we don't need to check |
| 2925 | * whether should wake up sq thread. |
| 2926 | */ |
| 2927 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2928 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2929 | wake_up(&ctx->sq_data->wait); |
| 2930 | |
| 2931 | mutex_unlock(&ctx->uring_lock); |
| 2932 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2935 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2936 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2937 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2938 | } |
| 2939 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2940 | /* |
| 2941 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2942 | * any file. For now, just ensure that anything potentially problematic is done |
| 2943 | * inline. |
| 2944 | */ |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2945 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2946 | { |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2947 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2948 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2949 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2950 | return true; |
| 2951 | return false; |
| 2952 | } |
Pavel Begunkov | 976517f | 2021-06-09 12:07:25 +0100 | [diff] [blame] | 2953 | if (S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2954 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2955 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2956 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2957 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2958 | file->f_op != &io_uring_fops) |
| 2959 | return true; |
| 2960 | return false; |
| 2961 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2962 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2963 | /* any ->read/write should understand O_NONBLOCK */ |
| 2964 | if (file->f_flags & O_NONBLOCK) |
| 2965 | return true; |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2966 | return file->f_mode & FMODE_NOWAIT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2967 | } |
| 2968 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2969 | /* |
| 2970 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2971 | * any file. For now, just ensure that anything potentially problematic is done |
| 2972 | * inline. |
| 2973 | */ |
| 2974 | static unsigned int io_file_get_flags(struct file *file) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2975 | { |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2976 | umode_t mode = file_inode(file)->i_mode; |
| 2977 | unsigned int res = 0; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2978 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2979 | if (S_ISREG(mode)) |
| 2980 | res |= FFS_ISREG; |
| 2981 | if (__io_file_supports_nowait(file, mode)) |
| 2982 | res |= FFS_NOWAIT; |
| 2983 | return res; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2986 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2987 | { |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2988 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
Pavel Begunkov | b9a6b8f | 2021-10-23 12:14:01 +0100 | [diff] [blame] | 2991 | 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] | 2992 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2993 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2994 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2995 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2996 | unsigned ioprio; |
| 2997 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2998 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2999 | if (!io_req_ffs_set(req)) |
| 3000 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 3001 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3002 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Jens Axboe | 7b9762a | 2021-12-22 20:26:56 -0700 | [diff] [blame] | 3003 | if (kiocb->ki_pos == -1) { |
| 3004 | if (!(file->f_mode & FMODE_STREAM)) { |
| 3005 | req->flags |= REQ_F_CUR_POS; |
| 3006 | kiocb->ki_pos = file->f_pos; |
| 3007 | } else { |
| 3008 | kiocb->ki_pos = 0; |
| 3009 | } |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3010 | } |
Pavel Begunkov | 5cb03d6 | 2021-10-15 17:09:16 +0100 | [diff] [blame] | 3011 | kiocb->ki_flags = iocb_flags(file); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 3012 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 3013 | if (unlikely(ret)) |
| 3014 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3015 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 3016 | /* |
| 3017 | * If the file is marked O_NONBLOCK, still allow retry for it if it |
| 3018 | * supports async. Otherwise it's impossible to use O_NONBLOCK files |
| 3019 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. |
| 3020 | */ |
| 3021 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3022 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3023 | req->flags |= REQ_F_NOWAIT; |
| 3024 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3025 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 5cb03d6 | 2021-10-15 17:09:16 +0100 | [diff] [blame] | 3026 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3027 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3028 | |
Jens Axboe | 394918e | 2021-03-08 11:40:23 -0700 | [diff] [blame] | 3029 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3030 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 3031 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3032 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 3033 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3034 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 3035 | kiocb->ki_complete = io_complete_rw; |
| 3036 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3037 | |
Pavel Begunkov | fb27274 | 2021-10-23 12:14:02 +0100 | [diff] [blame] | 3038 | ioprio = READ_ONCE(sqe->ioprio); |
| 3039 | if (ioprio) { |
| 3040 | ret = ioprio_check_cap(ioprio); |
| 3041 | if (ret) |
| 3042 | return ret; |
| 3043 | |
| 3044 | kiocb->ki_ioprio = ioprio; |
| 3045 | } else { |
| 3046 | kiocb->ki_ioprio = get_current_ioprio(); |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 3049 | req->imu = NULL; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3050 | req->rw.addr = READ_ONCE(sqe->addr); |
| 3051 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3052 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3053 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 3057 | { |
| 3058 | switch (ret) { |
| 3059 | case -EIOCBQUEUED: |
| 3060 | break; |
| 3061 | case -ERESTARTSYS: |
| 3062 | case -ERESTARTNOINTR: |
| 3063 | case -ERESTARTNOHAND: |
| 3064 | case -ERESTART_RESTARTBLOCK: |
| 3065 | /* |
| 3066 | * We can't just restart the syscall, since previously |
| 3067 | * submitted sqes may already be in progress. Just fail this |
| 3068 | * IO with EINTR. |
| 3069 | */ |
| 3070 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3071 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3072 | default: |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 3073 | kiocb->ki_complete(kiocb, ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 3077 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3078 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3079 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3080 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3081 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3082 | /* add previously done IO, if any */ |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3083 | if (req_has_async_data(req) && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3084 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3085 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3086 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3087 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3088 | } |
| 3089 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 3090 | if (req->flags & REQ_F_CUR_POS) |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 3091 | req->file->f_pos = req->rw.kiocb.ki_pos; |
| 3092 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) |
GuoYong Zheng | 00f6e68 | 2022-01-05 18:12:02 +0800 | [diff] [blame] | 3093 | __io_complete_rw(req, ret, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3094 | else |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 3095 | io_rw_done(&req->rw.kiocb, ret); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 3096 | |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 3097 | if (req->flags & REQ_F_REISSUE) { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 3098 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | a7be7c2 | 2021-04-15 11:31:14 -0600 | [diff] [blame] | 3099 | if (io_resubmit_prep(req)) { |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 3100 | io_req_task_queue_reissue(req); |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 3101 | } else { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3102 | req_set_fail(req); |
Pavel Begunkov | 06bdea2 | 2021-11-23 00:07:46 +0000 | [diff] [blame] | 3103 | req->result = ret; |
| 3104 | req->io_task_work.func = io_req_task_complete; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 3105 | io_req_task_work_add(req, false); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 3106 | } |
| 3107 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3108 | } |
| 3109 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3110 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
| 3111 | struct io_mapped_ubuf *imu) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3112 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3113 | size_t len = req->rw.len; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 3114 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3115 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3116 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 3117 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3118 | return -EFAULT; |
| 3119 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 3120 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3121 | return -EFAULT; |
| 3122 | |
| 3123 | /* |
| 3124 | * May not be a start of buffer, set size appropriately |
| 3125 | * and advance us to the beginning. |
| 3126 | */ |
| 3127 | offset = buf_addr - imu->ubuf; |
| 3128 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3129 | |
| 3130 | if (offset) { |
| 3131 | /* |
| 3132 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3133 | * using the latter parts of a big fixed buffer - it iterates |
| 3134 | * over each segment manually. We can cheat a bit here, because |
| 3135 | * we know that: |
| 3136 | * |
| 3137 | * 1) it's a BVEC iter, we set it up |
| 3138 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3139 | * first and last bvec |
| 3140 | * |
| 3141 | * So just find our index, and adjust the iterator afterwards. |
| 3142 | * If the offset is within the first bvec (or the whole first |
| 3143 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3144 | * since we can just skip the first segment, which may not |
| 3145 | * be PAGE_SIZE aligned. |
| 3146 | */ |
| 3147 | const struct bio_vec *bvec = imu->bvec; |
| 3148 | |
| 3149 | if (offset <= bvec->bv_len) { |
| 3150 | iov_iter_advance(iter, offset); |
| 3151 | } else { |
| 3152 | unsigned long seg_skip; |
| 3153 | |
| 3154 | /* skip first vec */ |
| 3155 | offset -= bvec->bv_len; |
| 3156 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3157 | |
| 3158 | iter->bvec = bvec + seg_skip; |
| 3159 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3160 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3161 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3165 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3166 | } |
| 3167 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3168 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
| 3169 | { |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3170 | struct io_mapped_ubuf *imu = req->imu; |
| 3171 | u16 index, buf_index = req->buf_index; |
| 3172 | |
| 3173 | if (likely(!imu)) { |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 3174 | struct io_ring_ctx *ctx = req->ctx; |
| 3175 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3176 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3177 | return -EFAULT; |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 3178 | io_req_set_rsrc_node(req, ctx); |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3179 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3180 | imu = READ_ONCE(ctx->user_bufs[index]); |
| 3181 | req->imu = imu; |
| 3182 | } |
| 3183 | return __io_import_fixed(req, rw, iter, imu); |
| 3184 | } |
| 3185 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3186 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3187 | { |
| 3188 | if (needs_lock) |
| 3189 | mutex_unlock(&ctx->uring_lock); |
| 3190 | } |
| 3191 | |
| 3192 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3193 | { |
| 3194 | /* |
| 3195 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3196 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3197 | * The only exception is when we've detached the request and issue it |
| 3198 | * from an async worker thread, grab the lock for that case. |
| 3199 | */ |
| 3200 | if (needs_lock) |
| 3201 | mutex_lock(&ctx->uring_lock); |
| 3202 | } |
| 3203 | |
| 3204 | 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] | 3205 | int bgid, unsigned int issue_flags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3206 | { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3207 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3208 | struct io_buffer *head; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 3209 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3210 | |
| 3211 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3212 | return kbuf; |
| 3213 | |
| 3214 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3215 | |
| 3216 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3217 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3218 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3219 | if (head) { |
| 3220 | if (!list_empty(&head->list)) { |
| 3221 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3222 | list); |
| 3223 | list_del(&kbuf->list); |
| 3224 | } else { |
| 3225 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3226 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3227 | } |
| 3228 | if (*len > kbuf->len) |
| 3229 | *len = kbuf->len; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3230 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3231 | req->kbuf = kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3232 | } else { |
| 3233 | kbuf = ERR_PTR(-ENOBUFS); |
| 3234 | } |
| 3235 | |
| 3236 | io_ring_submit_unlock(req->ctx, needs_lock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3237 | return kbuf; |
| 3238 | } |
| 3239 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3240 | 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] | 3241 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3242 | { |
| 3243 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3244 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3245 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3246 | bgid = req->buf_index; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3247 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3248 | if (IS_ERR(kbuf)) |
| 3249 | return kbuf; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3250 | return u64_to_user_ptr(kbuf->addr); |
| 3251 | } |
| 3252 | |
| 3253 | #ifdef CONFIG_COMPAT |
| 3254 | 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] | 3255 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3256 | { |
| 3257 | struct compat_iovec __user *uiov; |
| 3258 | compat_ssize_t clen; |
| 3259 | void __user *buf; |
| 3260 | ssize_t len; |
| 3261 | |
| 3262 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3263 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3264 | return -EFAULT; |
| 3265 | if (__get_user(clen, &uiov->iov_len)) |
| 3266 | return -EFAULT; |
| 3267 | if (clen < 0) |
| 3268 | return -EINVAL; |
| 3269 | |
| 3270 | len = clen; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3271 | buf = io_rw_buffer_select(req, &len, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3272 | if (IS_ERR(buf)) |
| 3273 | return PTR_ERR(buf); |
| 3274 | iov[0].iov_base = buf; |
| 3275 | iov[0].iov_len = (compat_size_t) len; |
| 3276 | return 0; |
| 3277 | } |
| 3278 | #endif |
| 3279 | |
| 3280 | 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] | 3281 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3282 | { |
| 3283 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3284 | void __user *buf; |
| 3285 | ssize_t len; |
| 3286 | |
| 3287 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3288 | return -EFAULT; |
| 3289 | |
| 3290 | len = iov[0].iov_len; |
| 3291 | if (len < 0) |
| 3292 | return -EINVAL; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3293 | buf = io_rw_buffer_select(req, &len, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3294 | if (IS_ERR(buf)) |
| 3295 | return PTR_ERR(buf); |
| 3296 | iov[0].iov_base = buf; |
| 3297 | iov[0].iov_len = len; |
| 3298 | return 0; |
| 3299 | } |
| 3300 | |
| 3301 | 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] | 3302 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3303 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3304 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3305 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3306 | |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3307 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3308 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3309 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3310 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3311 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3312 | return -EINVAL; |
| 3313 | |
| 3314 | #ifdef CONFIG_COMPAT |
| 3315 | if (req->ctx->compat) |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3316 | return io_compat_import(req, iov, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3317 | #endif |
| 3318 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3319 | return __io_iov_buffer_select(req, iov, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3322 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
| 3323 | struct io_rw_state *s, |
| 3324 | unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3325 | { |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3326 | struct iov_iter *iter = &s->iter; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3327 | u8 opcode = req->opcode; |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3328 | struct iovec *iovec; |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3329 | void __user *buf; |
| 3330 | size_t sqe_len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3331 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3332 | |
Pavel Begunkov | f325118 | 2021-11-23 00:07:48 +0000 | [diff] [blame] | 3333 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
| 3334 | ret = io_import_fixed(req, rw, iter); |
| 3335 | if (ret) |
| 3336 | return ERR_PTR(ret); |
| 3337 | return NULL; |
| 3338 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3339 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3340 | /* buffer index only valid with fixed read/write, or buffer select */ |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3341 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3342 | return ERR_PTR(-EINVAL); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3343 | |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3344 | buf = u64_to_user_ptr(req->rw.addr); |
| 3345 | sqe_len = req->rw.len; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3346 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3347 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3348 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3349 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3350 | if (IS_ERR(buf)) |
Changcheng Deng | 898df24 | 2021-10-20 08:49:48 +0000 | [diff] [blame] | 3351 | return ERR_CAST(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3352 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3353 | } |
| 3354 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3355 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
Pavel Begunkov | f325118 | 2021-11-23 00:07:48 +0000 | [diff] [blame] | 3356 | if (ret) |
| 3357 | return ERR_PTR(ret); |
| 3358 | return NULL; |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3359 | } |
| 3360 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3361 | iovec = s->fast_iov; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3362 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3363 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
Pavel Begunkov | f325118 | 2021-11-23 00:07:48 +0000 | [diff] [blame] | 3364 | if (ret) |
| 3365 | return ERR_PTR(ret); |
| 3366 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); |
| 3367 | return NULL; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3368 | } |
| 3369 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3370 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3371 | req->ctx->compat); |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3372 | if (unlikely(ret < 0)) |
| 3373 | return ERR_PTR(ret); |
| 3374 | return iovec; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3377 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
| 3378 | struct iovec **iovec, struct io_rw_state *s, |
| 3379 | unsigned int issue_flags) |
| 3380 | { |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3381 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
| 3382 | if (unlikely(IS_ERR(*iovec))) |
| 3383 | return PTR_ERR(*iovec); |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3384 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3385 | iov_iter_save_state(&s->iter, &s->iter_state); |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3386 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3389 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3390 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3391 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3392 | } |
| 3393 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3394 | /* |
| 3395 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3396 | * by looping over ->read() or ->write() manually. |
| 3397 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3398 | 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] | 3399 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3400 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3401 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3402 | ssize_t ret = 0; |
| 3403 | |
| 3404 | /* |
| 3405 | * Don't support polled IO through this interface, and we can't |
| 3406 | * support non-blocking either. For the latter, this just causes |
| 3407 | * the kiocb to be handled from an async context. |
| 3408 | */ |
| 3409 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3410 | return -EOPNOTSUPP; |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3411 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
| 3412 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3413 | return -EAGAIN; |
| 3414 | |
| 3415 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3416 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3417 | ssize_t nr; |
| 3418 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3419 | if (!iov_iter_is_bvec(iter)) { |
| 3420 | iovec = iov_iter_iovec(iter); |
| 3421 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3422 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3423 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3424 | } |
| 3425 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3426 | if (rw == READ) { |
| 3427 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3428 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3429 | } else { |
| 3430 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3431 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | if (nr < 0) { |
| 3435 | if (!ret) |
| 3436 | ret = nr; |
| 3437 | break; |
| 3438 | } |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3439 | if (!iov_iter_is_bvec(iter)) { |
| 3440 | iov_iter_advance(iter, nr); |
| 3441 | } else { |
| 3442 | req->rw.len -= nr; |
| 3443 | req->rw.addr += nr; |
| 3444 | } |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3445 | ret += nr; |
| 3446 | if (nr != iovec.iov_len) |
| 3447 | break; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | return ret; |
| 3451 | } |
| 3452 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3453 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3454 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3455 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3456 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3457 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3458 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3459 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3460 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3461 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3462 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3463 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3464 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3465 | unsigned iov_off = 0; |
| 3466 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3467 | rw->s.iter.iov = rw->s.fast_iov; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3468 | if (iter->iov != fast_iov) { |
| 3469 | iov_off = iter->iov - fast_iov; |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3470 | rw->s.iter.iov += iov_off; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3471 | } |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3472 | if (rw->s.fast_iov != fast_iov) |
| 3473 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3474 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3475 | } else { |
| 3476 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3477 | } |
| 3478 | } |
| 3479 | |
Hao Xu | 8d4af68 | 2021-09-22 18:15:22 +0800 | [diff] [blame] | 3480 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3481 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3482 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3483 | 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] | 3484 | if (req->async_data) { |
| 3485 | req->flags |= REQ_F_ASYNC_DATA; |
| 3486 | return false; |
| 3487 | } |
| 3488 | return true; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3489 | } |
| 3490 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3491 | 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] | 3492 | struct io_rw_state *s, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3493 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3494 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3495 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3496 | if (!req_has_async_data(req)) { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3497 | struct io_async_rw *iorw; |
| 3498 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3499 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3500 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3501 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3502 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3503 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3504 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3505 | iorw = req->async_data; |
| 3506 | /* we've copied and mapped the iter, ensure state is saved */ |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3507 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3508 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3509 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3512 | 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] | 3513 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3514 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3515 | struct iovec *iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3516 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3517 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3518 | /* submission path, ->uring_lock should already be taken */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 3519 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3520 | if (unlikely(ret < 0)) |
| 3521 | return ret; |
| 3522 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3523 | iorw->bytes_done = 0; |
| 3524 | iorw->free_iovec = iov; |
| 3525 | if (iov) |
| 3526 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3527 | return 0; |
| 3528 | } |
| 3529 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3530 | 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] | 3531 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3532 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3533 | return -EBADF; |
Pavel Begunkov | b9a6b8f | 2021-10-23 12:14:01 +0100 | [diff] [blame] | 3534 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3535 | } |
| 3536 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3537 | /* |
Matthew Wilcox (Oracle) | ffdc8da | 2020-12-30 17:58:40 -0500 | [diff] [blame] | 3538 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3539 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3540 | * This gets called when the page is unlocked, and we generally expect that to |
| 3541 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3542 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3543 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3544 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3545 | * slow path. |
| 3546 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3547 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3548 | int sync, void *arg) |
| 3549 | { |
| 3550 | struct wait_page_queue *wpq; |
| 3551 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3552 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3553 | |
| 3554 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3555 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3556 | if (!wake_page_match(wpq, key)) |
| 3557 | return 0; |
| 3558 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3559 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3560 | list_del_init(&wait->entry); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3561 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3562 | return 1; |
| 3563 | } |
| 3564 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3565 | /* |
| 3566 | * This controls whether a given IO request should be armed for async page |
| 3567 | * based retry. If we return false here, the request is handed to the async |
| 3568 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3569 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3570 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3571 | * will register a callback when the page is unlocked at IO completion. Through |
| 3572 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3573 | * That retry will attempt the buffered read again. The retry will generally |
| 3574 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3575 | * async worker threads for a blocking retry. |
| 3576 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3577 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3578 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3579 | struct io_async_rw *rw = req->async_data; |
| 3580 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3581 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3582 | |
| 3583 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3584 | if (req->flags & REQ_F_NOWAIT) |
| 3585 | return false; |
| 3586 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3587 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3588 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3589 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3590 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3591 | /* |
| 3592 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3593 | * support callback based unlocks |
| 3594 | */ |
| 3595 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3596 | return false; |
| 3597 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3598 | wait->wait.func = io_async_buf_func; |
| 3599 | wait->wait.private = req; |
| 3600 | wait->wait.flags = 0; |
| 3601 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3602 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3603 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3604 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3605 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3606 | } |
| 3607 | |
Pavel Begunkov | aeab950 | 2021-06-14 02:36:24 +0100 | [diff] [blame] | 3608 | 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] | 3609 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3610 | if (likely(req->file->f_op->read_iter)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3611 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3612 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3613 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3614 | else |
| 3615 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3616 | } |
| 3617 | |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3618 | static bool need_read_all(struct io_kiocb *req) |
| 3619 | { |
| 3620 | return req->flags & REQ_F_ISREG || |
| 3621 | S_ISBLK(file_inode(req->file)->i_mode); |
| 3622 | } |
| 3623 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3624 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3625 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3626 | struct io_rw_state __s, *s = &__s; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3627 | struct iovec *iovec; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3628 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3629 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3630 | struct io_async_rw *rw; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3631 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3632 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3633 | if (!req_has_async_data(req)) { |
| 3634 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); |
| 3635 | if (unlikely(ret < 0)) |
| 3636 | return ret; |
| 3637 | } else { |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3638 | rw = req->async_data; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3639 | s = &rw->s; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3640 | /* |
| 3641 | * We come here from an earlier attempt, restore our state to |
| 3642 | * match in case it doesn't. It's cheap enough that we don't |
| 3643 | * need to make this conditional. |
| 3644 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3645 | iov_iter_restore(&s->iter, &s->iter_state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3646 | iovec = NULL; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3647 | } |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3648 | req->result = iov_iter_count(&s->iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3649 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3650 | if (force_nonblock) { |
| 3651 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3652 | if (unlikely(!io_file_supports_nowait(req))) { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3653 | ret = io_setup_async_rw(req, iovec, s, true); |
| 3654 | return ret ?: -EAGAIN; |
| 3655 | } |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3656 | kiocb->ki_flags |= IOCB_NOWAIT; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3657 | } else { |
| 3658 | /* Ensure we clear previously set non-block flag */ |
| 3659 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3660 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3661 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3662 | 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] | 3663 | if (unlikely(ret)) { |
| 3664 | kfree(iovec); |
| 3665 | return ret; |
| 3666 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3667 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3668 | ret = io_iter_do_read(req, &s->iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3669 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3670 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3671 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3672 | /* IOPOLL retry should happen for io-wq threads */ |
| 3673 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3674 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3675 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3676 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3677 | goto done; |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3678 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3679 | } else if (ret == -EIOCBQUEUED) { |
| 3680 | goto out_free; |
Pavel Begunkov | f80a50a | 2021-10-14 16:10:13 +0100 | [diff] [blame] | 3681 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3682 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3683 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3684 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3685 | } |
| 3686 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3687 | /* |
| 3688 | * Don't depend on the iter state matching what was consumed, or being |
| 3689 | * untouched in case of error. Restore it and we'll advance it |
| 3690 | * manually if we need to. |
| 3691 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3692 | iov_iter_restore(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3693 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3694 | ret2 = io_setup_async_rw(req, iovec, s, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3695 | if (ret2) |
| 3696 | return ret2; |
| 3697 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3698 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3699 | rw = req->async_data; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3700 | s = &rw->s; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3701 | /* |
| 3702 | * Now use our persistent iterator and state, if we aren't already. |
| 3703 | * We've restored and mapped the iter to match. |
| 3704 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3705 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3706 | do { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3707 | /* |
| 3708 | * We end up here because of a partial read, either from |
| 3709 | * above or inside this loop. Advance the iter by the bytes |
| 3710 | * that were consumed. |
| 3711 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3712 | iov_iter_advance(&s->iter, ret); |
| 3713 | if (!iov_iter_count(&s->iter)) |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3714 | break; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3715 | rw->bytes_done += ret; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3716 | iov_iter_save_state(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3717 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3718 | /* if we can retry, do so with the callbacks armed */ |
| 3719 | if (!io_rw_should_retry(req)) { |
| 3720 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3721 | return -EAGAIN; |
| 3722 | } |
| 3723 | |
| 3724 | /* |
| 3725 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3726 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3727 | * desired page gets unlocked. We can also get a partial read |
| 3728 | * here, and if we do, then just retry at the new offset. |
| 3729 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3730 | ret = io_iter_do_read(req, &s->iter); |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3731 | if (ret == -EIOCBQUEUED) |
| 3732 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3733 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3734 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3735 | iov_iter_restore(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3736 | } while (ret > 0); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3737 | done: |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 3738 | kiocb_done(req, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3739 | out_free: |
| 3740 | /* it's faster to check here then delegate to kfree */ |
| 3741 | if (iovec) |
| 3742 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3743 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3746 | 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] | 3747 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3748 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3749 | return -EBADF; |
Jens Axboe | 3884b83 | 2021-10-25 13:45:12 -0600 | [diff] [blame] | 3750 | 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] | 3751 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3752 | } |
| 3753 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3754 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3755 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3756 | struct io_rw_state __s, *s = &__s; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3757 | struct iovec *iovec; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3758 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3759 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3760 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3761 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3762 | if (!req_has_async_data(req)) { |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3763 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
| 3764 | if (unlikely(ret < 0)) |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3765 | return ret; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3766 | } else { |
| 3767 | struct io_async_rw *rw = req->async_data; |
| 3768 | |
| 3769 | s = &rw->s; |
| 3770 | iov_iter_restore(&s->iter, &s->iter_state); |
| 3771 | iovec = NULL; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3772 | } |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3773 | req->result = iov_iter_count(&s->iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3774 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3775 | if (force_nonblock) { |
| 3776 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3777 | if (unlikely(!io_file_supports_nowait(req))) |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3778 | goto copy_iov; |
| 3779 | |
| 3780 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3781 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3782 | (req->flags & REQ_F_ISREG)) |
| 3783 | goto copy_iov; |
| 3784 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3785 | kiocb->ki_flags |= IOCB_NOWAIT; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3786 | } else { |
| 3787 | /* Ensure we clear previously set non-block flag */ |
| 3788 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3789 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3790 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3791 | 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] | 3792 | if (unlikely(ret)) |
| 3793 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3794 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3795 | /* |
| 3796 | * Open-code file_start_write here to grab freeze protection, |
| 3797 | * which will be released by another thread in |
| 3798 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3799 | * released so that it doesn't complain about the held lock when |
| 3800 | * we return to userspace. |
| 3801 | */ |
| 3802 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3803 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3804 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3805 | SB_FREEZE_WRITE); |
| 3806 | } |
| 3807 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3808 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3809 | if (likely(req->file->f_op->write_iter)) |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3810 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3811 | else if (req->file->f_op->write) |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3812 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3813 | else |
| 3814 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3815 | |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3816 | if (req->flags & REQ_F_REISSUE) { |
| 3817 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3818 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3819 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3820 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3821 | /* |
| 3822 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3823 | * retry them without IOCB_NOWAIT. |
| 3824 | */ |
| 3825 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3826 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3827 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3828 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3829 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3830 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3831 | /* IOPOLL retry should happen for io-wq threads */ |
Noah Goldstein | b10841c | 2021-10-16 20:32:29 -0500 | [diff] [blame] | 3832 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3833 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3834 | done: |
Pavel Begunkov | 2ea537c | 2021-11-23 00:07:49 +0000 | [diff] [blame] | 3835 | kiocb_done(req, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3836 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3837 | copy_iov: |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3838 | iov_iter_restore(&s->iter, &s->iter_state); |
| 3839 | ret = io_setup_async_rw(req, iovec, s, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3840 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3841 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3842 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3843 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3844 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3845 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3846 | return ret; |
| 3847 | } |
| 3848 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3849 | static int io_renameat_prep(struct io_kiocb *req, |
| 3850 | const struct io_uring_sqe *sqe) |
| 3851 | { |
| 3852 | struct io_rename *ren = &req->rename; |
| 3853 | const char __user *oldf, *newf; |
| 3854 | |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3855 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3856 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3857 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3858 | return -EINVAL; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3859 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3860 | return -EBADF; |
| 3861 | |
| 3862 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3863 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3864 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3865 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3866 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3867 | |
| 3868 | ren->oldpath = getname(oldf); |
| 3869 | if (IS_ERR(ren->oldpath)) |
| 3870 | return PTR_ERR(ren->oldpath); |
| 3871 | |
| 3872 | ren->newpath = getname(newf); |
| 3873 | if (IS_ERR(ren->newpath)) { |
| 3874 | putname(ren->oldpath); |
| 3875 | return PTR_ERR(ren->newpath); |
| 3876 | } |
| 3877 | |
| 3878 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3879 | return 0; |
| 3880 | } |
| 3881 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3882 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3883 | { |
| 3884 | struct io_rename *ren = &req->rename; |
| 3885 | int ret; |
| 3886 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3887 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3888 | return -EAGAIN; |
| 3889 | |
| 3890 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3891 | ren->newpath, ren->flags); |
| 3892 | |
| 3893 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3894 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3895 | req_set_fail(req); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3896 | io_req_complete(req, ret); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3900 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3901 | const struct io_uring_sqe *sqe) |
| 3902 | { |
| 3903 | struct io_unlink *un = &req->unlink; |
| 3904 | const char __user *fname; |
| 3905 | |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3906 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3907 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3908 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3909 | sqe->splice_fd_in) |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3910 | return -EINVAL; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3911 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3912 | return -EBADF; |
| 3913 | |
| 3914 | un->dfd = READ_ONCE(sqe->fd); |
| 3915 | |
| 3916 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3917 | if (un->flags & ~AT_REMOVEDIR) |
| 3918 | return -EINVAL; |
| 3919 | |
| 3920 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3921 | un->filename = getname(fname); |
| 3922 | if (IS_ERR(un->filename)) |
| 3923 | return PTR_ERR(un->filename); |
| 3924 | |
| 3925 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3926 | return 0; |
| 3927 | } |
| 3928 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3929 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3930 | { |
| 3931 | struct io_unlink *un = &req->unlink; |
| 3932 | int ret; |
| 3933 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3934 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3935 | return -EAGAIN; |
| 3936 | |
| 3937 | if (un->flags & AT_REMOVEDIR) |
| 3938 | ret = do_rmdir(un->dfd, un->filename); |
| 3939 | else |
| 3940 | ret = do_unlinkat(un->dfd, un->filename); |
| 3941 | |
| 3942 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3943 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3944 | req_set_fail(req); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3945 | io_req_complete(req, ret); |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3949 | static int io_mkdirat_prep(struct io_kiocb *req, |
| 3950 | const struct io_uring_sqe *sqe) |
| 3951 | { |
| 3952 | struct io_mkdir *mkd = &req->mkdir; |
| 3953 | const char __user *fname; |
| 3954 | |
| 3955 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3956 | return -EINVAL; |
| 3957 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || |
| 3958 | sqe->splice_fd_in) |
| 3959 | return -EINVAL; |
| 3960 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3961 | return -EBADF; |
| 3962 | |
| 3963 | mkd->dfd = READ_ONCE(sqe->fd); |
| 3964 | mkd->mode = READ_ONCE(sqe->len); |
| 3965 | |
| 3966 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3967 | mkd->filename = getname(fname); |
| 3968 | if (IS_ERR(mkd->filename)) |
| 3969 | return PTR_ERR(mkd->filename); |
| 3970 | |
| 3971 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3972 | return 0; |
| 3973 | } |
| 3974 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 3975 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3976 | { |
| 3977 | struct io_mkdir *mkd = &req->mkdir; |
| 3978 | int ret; |
| 3979 | |
| 3980 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3981 | return -EAGAIN; |
| 3982 | |
| 3983 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); |
| 3984 | |
| 3985 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3986 | if (ret < 0) |
| 3987 | req_set_fail(req); |
| 3988 | io_req_complete(req, ret); |
| 3989 | return 0; |
| 3990 | } |
| 3991 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3992 | static int io_symlinkat_prep(struct io_kiocb *req, |
| 3993 | const struct io_uring_sqe *sqe) |
| 3994 | { |
| 3995 | struct io_symlink *sl = &req->symlink; |
| 3996 | const char __user *oldpath, *newpath; |
| 3997 | |
| 3998 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3999 | return -EINVAL; |
| 4000 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || |
| 4001 | sqe->splice_fd_in) |
| 4002 | return -EINVAL; |
| 4003 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 4004 | return -EBADF; |
| 4005 | |
| 4006 | sl->new_dfd = READ_ONCE(sqe->fd); |
| 4007 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4008 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4009 | |
| 4010 | sl->oldpath = getname(oldpath); |
| 4011 | if (IS_ERR(sl->oldpath)) |
| 4012 | return PTR_ERR(sl->oldpath); |
| 4013 | |
| 4014 | sl->newpath = getname(newpath); |
| 4015 | if (IS_ERR(sl->newpath)) { |
| 4016 | putname(sl->oldpath); |
| 4017 | return PTR_ERR(sl->newpath); |
| 4018 | } |
| 4019 | |
| 4020 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4021 | return 0; |
| 4022 | } |
| 4023 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 4024 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 4025 | { |
| 4026 | struct io_symlink *sl = &req->symlink; |
| 4027 | int ret; |
| 4028 | |
| 4029 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 4030 | return -EAGAIN; |
| 4031 | |
| 4032 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); |
| 4033 | |
| 4034 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4035 | if (ret < 0) |
| 4036 | req_set_fail(req); |
| 4037 | io_req_complete(req, ret); |
| 4038 | return 0; |
| 4039 | } |
| 4040 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 4041 | static int io_linkat_prep(struct io_kiocb *req, |
| 4042 | const struct io_uring_sqe *sqe) |
| 4043 | { |
| 4044 | struct io_hardlink *lnk = &req->hardlink; |
| 4045 | const char __user *oldf, *newf; |
| 4046 | |
| 4047 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4048 | return -EINVAL; |
| 4049 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 4050 | return -EINVAL; |
| 4051 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 4052 | return -EBADF; |
| 4053 | |
| 4054 | lnk->old_dfd = READ_ONCE(sqe->fd); |
| 4055 | lnk->new_dfd = READ_ONCE(sqe->len); |
| 4056 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4057 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4058 | lnk->flags = READ_ONCE(sqe->hardlink_flags); |
| 4059 | |
| 4060 | lnk->oldpath = getname(oldf); |
| 4061 | if (IS_ERR(lnk->oldpath)) |
| 4062 | return PTR_ERR(lnk->oldpath); |
| 4063 | |
| 4064 | lnk->newpath = getname(newf); |
| 4065 | if (IS_ERR(lnk->newpath)) { |
| 4066 | putname(lnk->oldpath); |
| 4067 | return PTR_ERR(lnk->newpath); |
| 4068 | } |
| 4069 | |
| 4070 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 4074 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 4075 | { |
| 4076 | struct io_hardlink *lnk = &req->hardlink; |
| 4077 | int ret; |
| 4078 | |
| 4079 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 4080 | return -EAGAIN; |
| 4081 | |
| 4082 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, |
| 4083 | lnk->newpath, lnk->flags); |
| 4084 | |
| 4085 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4086 | if (ret < 0) |
| 4087 | req_set_fail(req); |
| 4088 | io_req_complete(req, ret); |
| 4089 | return 0; |
| 4090 | } |
| 4091 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4092 | static int io_shutdown_prep(struct io_kiocb *req, |
| 4093 | const struct io_uring_sqe *sqe) |
| 4094 | { |
| 4095 | #if defined(CONFIG_NET) |
| 4096 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4097 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4098 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 4099 | sqe->buf_index || sqe->splice_fd_in)) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4100 | return -EINVAL; |
| 4101 | |
| 4102 | req->shutdown.how = READ_ONCE(sqe->len); |
| 4103 | return 0; |
| 4104 | #else |
| 4105 | return -EOPNOTSUPP; |
| 4106 | #endif |
| 4107 | } |
| 4108 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4109 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4110 | { |
| 4111 | #if defined(CONFIG_NET) |
| 4112 | struct socket *sock; |
| 4113 | int ret; |
| 4114 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4115 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4116 | return -EAGAIN; |
| 4117 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4118 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4119 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4120 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4121 | |
| 4122 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 4123 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4124 | req_set_fail(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4125 | io_req_complete(req, ret); |
| 4126 | return 0; |
| 4127 | #else |
| 4128 | return -EOPNOTSUPP; |
| 4129 | #endif |
| 4130 | } |
| 4131 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4132 | static int __io_splice_prep(struct io_kiocb *req, |
| 4133 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4134 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4135 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4136 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4137 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4138 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4139 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4140 | |
| 4141 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4142 | sp->len = READ_ONCE(sqe->len); |
| 4143 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 4144 | |
| 4145 | if (unlikely(sp->flags & ~valid_flags)) |
| 4146 | return -EINVAL; |
| 4147 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 4148 | 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] | 4149 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 4150 | if (!sp->file_in) |
| 4151 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4152 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4153 | return 0; |
| 4154 | } |
| 4155 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4156 | static int io_tee_prep(struct io_kiocb *req, |
| 4157 | const struct io_uring_sqe *sqe) |
| 4158 | { |
| 4159 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 4160 | return -EINVAL; |
| 4161 | return __io_splice_prep(req, sqe); |
| 4162 | } |
| 4163 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4164 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4165 | { |
| 4166 | struct io_splice *sp = &req->splice; |
| 4167 | struct file *in = sp->file_in; |
| 4168 | struct file *out = sp->file_out; |
| 4169 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4170 | long ret = 0; |
| 4171 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4172 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4173 | return -EAGAIN; |
| 4174 | if (sp->len) |
| 4175 | ret = do_tee(in, out, sp->len, flags); |
| 4176 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4177 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4178 | io_put_file(in); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4179 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4180 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4181 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4182 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4183 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4184 | return 0; |
| 4185 | } |
| 4186 | |
| 4187 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4188 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4189 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4190 | |
| 4191 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 4192 | sp->off_out = READ_ONCE(sqe->off); |
| 4193 | return __io_splice_prep(req, sqe); |
| 4194 | } |
| 4195 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4196 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4197 | { |
| 4198 | struct io_splice *sp = &req->splice; |
| 4199 | struct file *in = sp->file_in; |
| 4200 | struct file *out = sp->file_out; |
| 4201 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4202 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4203 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4204 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4205 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 4206 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4207 | |
| 4208 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 4209 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4210 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 4211 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4212 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4213 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4214 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4215 | io_put_file(in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4216 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4217 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4218 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4219 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4220 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4221 | return 0; |
| 4222 | } |
| 4223 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4224 | /* |
| 4225 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4226 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4227 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4228 | { |
| 4229 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4230 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4231 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4232 | return -EINVAL; |
| 4233 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4234 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4235 | return 0; |
| 4236 | } |
| 4237 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4238 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4239 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4240 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4241 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4242 | if (!req->file) |
| 4243 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4244 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4245 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4246 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4247 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4248 | sqe->splice_fd_in)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4249 | return -EINVAL; |
| 4250 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4251 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4252 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4253 | return -EINVAL; |
| 4254 | |
| 4255 | req->sync.off = READ_ONCE(sqe->off); |
| 4256 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4257 | return 0; |
| 4258 | } |
| 4259 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4260 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4261 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4262 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4263 | int ret; |
| 4264 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4265 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4266 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4267 | return -EAGAIN; |
| 4268 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4269 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4270 | end > 0 ? end : LLONG_MAX, |
| 4271 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4272 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4273 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4274 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4275 | return 0; |
| 4276 | } |
| 4277 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4278 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4279 | const struct io_uring_sqe *sqe) |
| 4280 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4281 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
| 4282 | sqe->splice_fd_in) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4283 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4284 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4285 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4286 | |
| 4287 | req->sync.off = READ_ONCE(sqe->off); |
| 4288 | req->sync.len = READ_ONCE(sqe->addr); |
| 4289 | req->sync.mode = READ_ONCE(sqe->len); |
| 4290 | return 0; |
| 4291 | } |
| 4292 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4293 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4294 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4295 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4296 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4297 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4298 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4299 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4300 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4301 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4302 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4303 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4304 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4305 | return 0; |
| 4306 | } |
| 4307 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4308 | 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] | 4309 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4310 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4311 | int ret; |
| 4312 | |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4313 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4314 | return -EINVAL; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4315 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4316 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4317 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4318 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4319 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4320 | /* open.how should be already initialised */ |
| 4321 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4322 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4323 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4324 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4325 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4326 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4327 | if (IS_ERR(req->open.filename)) { |
| 4328 | ret = PTR_ERR(req->open.filename); |
| 4329 | req->open.filename = NULL; |
| 4330 | return ret; |
| 4331 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4332 | |
| 4333 | req->open.file_slot = READ_ONCE(sqe->file_index); |
| 4334 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) |
| 4335 | return -EINVAL; |
| 4336 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4337 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4338 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4339 | return 0; |
| 4340 | } |
| 4341 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4342 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4343 | { |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4344 | u64 mode = READ_ONCE(sqe->len); |
| 4345 | u64 flags = READ_ONCE(sqe->open_flags); |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4346 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4347 | req->open.how = build_open_how(flags, mode); |
| 4348 | return __io_openat_prep(req, sqe); |
| 4349 | } |
| 4350 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4351 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4352 | { |
| 4353 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4354 | size_t len; |
| 4355 | int ret; |
| 4356 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4357 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4358 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4359 | if (len < OPEN_HOW_SIZE_VER0) |
| 4360 | return -EINVAL; |
| 4361 | |
| 4362 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4363 | len); |
| 4364 | if (ret) |
| 4365 | return ret; |
| 4366 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4367 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4370 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4371 | { |
| 4372 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4373 | struct file *file; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4374 | bool resolve_nonblock, nonblock_set; |
| 4375 | bool fixed = !!req->open.file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4376 | int ret; |
| 4377 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4378 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4379 | if (ret) |
| 4380 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4381 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4382 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4383 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4384 | /* |
| 4385 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4386 | * it'll always -EAGAIN |
| 4387 | */ |
| 4388 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4389 | return -EAGAIN; |
| 4390 | op.lookup_flags |= LOOKUP_CACHED; |
| 4391 | op.open_flag |= O_NONBLOCK; |
| 4392 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4393 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4394 | if (!fixed) { |
| 4395 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
| 4396 | if (ret < 0) |
| 4397 | goto err; |
| 4398 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4399 | |
| 4400 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4401 | if (IS_ERR(file)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4402 | /* |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4403 | * We could hang on to this 'fd' on retrying, but seems like |
| 4404 | * marginal gain for something that is now known to be a slower |
| 4405 | * 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] | 4406 | */ |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4407 | if (!fixed) |
| 4408 | put_unused_fd(ret); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4409 | |
| 4410 | ret = PTR_ERR(file); |
| 4411 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
| 4412 | if (ret == -EAGAIN && |
| 4413 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) |
| 4414 | return -EAGAIN; |
| 4415 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4416 | } |
| 4417 | |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4418 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
| 4419 | file->f_flags &= ~O_NONBLOCK; |
| 4420 | fsnotify_open(file); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4421 | |
| 4422 | if (!fixed) |
| 4423 | fd_install(ret, file); |
| 4424 | else |
| 4425 | ret = io_install_fixed_file(req, file, issue_flags, |
| 4426 | req->open.file_slot - 1); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4427 | err: |
| 4428 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4429 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4430 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4431 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4432 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4433 | return 0; |
| 4434 | } |
| 4435 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4436 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4437 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 4438 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4439 | } |
| 4440 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4441 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4442 | const struct io_uring_sqe *sqe) |
| 4443 | { |
| 4444 | struct io_provide_buf *p = &req->pbuf; |
| 4445 | u64 tmp; |
| 4446 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4447 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
| 4448 | sqe->splice_fd_in) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4449 | return -EINVAL; |
| 4450 | |
| 4451 | tmp = READ_ONCE(sqe->fd); |
| 4452 | if (!tmp || tmp > USHRT_MAX) |
| 4453 | return -EINVAL; |
| 4454 | |
| 4455 | memset(p, 0, sizeof(*p)); |
| 4456 | p->nbufs = tmp; |
| 4457 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4458 | return 0; |
| 4459 | } |
| 4460 | |
| 4461 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4462 | int bgid, unsigned nbufs) |
| 4463 | { |
| 4464 | unsigned i = 0; |
| 4465 | |
| 4466 | /* shouldn't happen */ |
| 4467 | if (!nbufs) |
| 4468 | return 0; |
| 4469 | |
| 4470 | /* the head kbuf is the list itself */ |
| 4471 | while (!list_empty(&buf->list)) { |
| 4472 | struct io_buffer *nxt; |
| 4473 | |
| 4474 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4475 | list_del(&nxt->list); |
| 4476 | kfree(nxt); |
| 4477 | if (++i == nbufs) |
| 4478 | return i; |
Ye Bin | 1d0254e | 2021-11-22 10:47:37 +0800 | [diff] [blame] | 4479 | cond_resched(); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4480 | } |
| 4481 | i++; |
| 4482 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4483 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4484 | |
| 4485 | return i; |
| 4486 | } |
| 4487 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4488 | 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] | 4489 | { |
| 4490 | struct io_provide_buf *p = &req->pbuf; |
| 4491 | struct io_ring_ctx *ctx = req->ctx; |
| 4492 | struct io_buffer *head; |
| 4493 | int ret = 0; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4494 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4495 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4496 | io_ring_submit_lock(ctx, needs_lock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4497 | |
| 4498 | lockdep_assert_held(&ctx->uring_lock); |
| 4499 | |
| 4500 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4501 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4502 | if (head) |
| 4503 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4504 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4505 | req_set_fail(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4506 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4507 | /* complete before unlock, IOPOLL may need the lock */ |
| 4508 | __io_req_complete(req, issue_flags, ret, 0); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4509 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4510 | return 0; |
| 4511 | } |
| 4512 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4513 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4514 | const struct io_uring_sqe *sqe) |
| 4515 | { |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4516 | unsigned long size, tmp_check; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4517 | struct io_provide_buf *p = &req->pbuf; |
| 4518 | u64 tmp; |
| 4519 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4520 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4521 | return -EINVAL; |
| 4522 | |
| 4523 | tmp = READ_ONCE(sqe->fd); |
| 4524 | if (!tmp || tmp > USHRT_MAX) |
| 4525 | return -E2BIG; |
| 4526 | p->nbufs = tmp; |
| 4527 | p->addr = READ_ONCE(sqe->addr); |
| 4528 | p->len = READ_ONCE(sqe->len); |
| 4529 | |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4530 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
| 4531 | &size)) |
| 4532 | return -EOVERFLOW; |
| 4533 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) |
| 4534 | return -EOVERFLOW; |
| 4535 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 4536 | size = (unsigned long)p->len * p->nbufs; |
| 4537 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4538 | return -EFAULT; |
| 4539 | |
| 4540 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4541 | tmp = READ_ONCE(sqe->off); |
| 4542 | if (tmp > USHRT_MAX) |
| 4543 | return -E2BIG; |
| 4544 | p->bid = tmp; |
| 4545 | return 0; |
| 4546 | } |
| 4547 | |
| 4548 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4549 | { |
| 4550 | struct io_buffer *buf; |
| 4551 | u64 addr = pbuf->addr; |
| 4552 | int i, bid = pbuf->bid; |
| 4553 | |
| 4554 | for (i = 0; i < pbuf->nbufs; i++) { |
Jens Axboe | 9990da9 | 2021-09-24 07:39:08 -0600 | [diff] [blame] | 4555 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4556 | if (!buf) |
| 4557 | break; |
| 4558 | |
| 4559 | buf->addr = addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 4560 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4561 | buf->bid = bid; |
| 4562 | addr += pbuf->len; |
| 4563 | bid++; |
| 4564 | if (!*head) { |
| 4565 | INIT_LIST_HEAD(&buf->list); |
| 4566 | *head = buf; |
| 4567 | } else { |
| 4568 | list_add_tail(&buf->list, &(*head)->list); |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | return i ? i : -ENOMEM; |
| 4573 | } |
| 4574 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4575 | 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] | 4576 | { |
| 4577 | struct io_provide_buf *p = &req->pbuf; |
| 4578 | struct io_ring_ctx *ctx = req->ctx; |
| 4579 | struct io_buffer *head, *list; |
| 4580 | int ret = 0; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4581 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4582 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4583 | io_ring_submit_lock(ctx, needs_lock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4584 | |
| 4585 | lockdep_assert_held(&ctx->uring_lock); |
| 4586 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4587 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4588 | |
| 4589 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4590 | if (ret >= 0 && !list) { |
| 4591 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 4592 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4593 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4594 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4595 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4596 | req_set_fail(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4597 | /* complete before unlock, IOPOLL may need the lock */ |
| 4598 | __io_req_complete(req, issue_flags, ret, 0); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4599 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4600 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4601 | } |
| 4602 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4603 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4604 | const struct io_uring_sqe *sqe) |
| 4605 | { |
| 4606 | #if defined(CONFIG_EPOLL) |
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 | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4608 | return -EINVAL; |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4609 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4610 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4611 | |
| 4612 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4613 | req->epoll.op = READ_ONCE(sqe->len); |
| 4614 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4615 | |
| 4616 | if (ep_op_has_event(req->epoll.op)) { |
| 4617 | struct epoll_event __user *ev; |
| 4618 | |
| 4619 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4620 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4621 | return -EFAULT; |
| 4622 | } |
| 4623 | |
| 4624 | return 0; |
| 4625 | #else |
| 4626 | return -EOPNOTSUPP; |
| 4627 | #endif |
| 4628 | } |
| 4629 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4630 | 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] | 4631 | { |
| 4632 | #if defined(CONFIG_EPOLL) |
| 4633 | struct io_epoll *ie = &req->epoll; |
| 4634 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4635 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4636 | |
| 4637 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4638 | if (force_nonblock && ret == -EAGAIN) |
| 4639 | return -EAGAIN; |
| 4640 | |
| 4641 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4642 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4643 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4644 | return 0; |
| 4645 | #else |
| 4646 | return -EOPNOTSUPP; |
| 4647 | #endif |
| 4648 | } |
| 4649 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4650 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4651 | { |
| 4652 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4653 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4654 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4655 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4656 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4657 | |
| 4658 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4659 | req->madvise.len = READ_ONCE(sqe->len); |
| 4660 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4661 | return 0; |
| 4662 | #else |
| 4663 | return -EOPNOTSUPP; |
| 4664 | #endif |
| 4665 | } |
| 4666 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4667 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4668 | { |
| 4669 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4670 | struct io_madvise *ma = &req->madvise; |
| 4671 | int ret; |
| 4672 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4673 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4674 | return -EAGAIN; |
| 4675 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4676 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4677 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4678 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4679 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4680 | return 0; |
| 4681 | #else |
| 4682 | return -EOPNOTSUPP; |
| 4683 | #endif |
| 4684 | } |
| 4685 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4686 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4687 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4688 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4689 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4690 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4691 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4692 | |
| 4693 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4694 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4695 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4696 | return 0; |
| 4697 | } |
| 4698 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4699 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4700 | { |
| 4701 | struct io_fadvise *fa = &req->fadvise; |
| 4702 | int ret; |
| 4703 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4704 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4705 | switch (fa->advice) { |
| 4706 | case POSIX_FADV_NORMAL: |
| 4707 | case POSIX_FADV_RANDOM: |
| 4708 | case POSIX_FADV_SEQUENTIAL: |
| 4709 | break; |
| 4710 | default: |
| 4711 | return -EAGAIN; |
| 4712 | } |
| 4713 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4714 | |
| 4715 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4716 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4717 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4718 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4719 | return 0; |
| 4720 | } |
| 4721 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4722 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4723 | { |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4724 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4725 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4726 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4727 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4728 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4729 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4730 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4731 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4732 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4733 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4734 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4735 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4736 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4737 | return 0; |
| 4738 | } |
| 4739 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4740 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4741 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4742 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4743 | int ret; |
| 4744 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4745 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4746 | return -EAGAIN; |
| 4747 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4748 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4749 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4750 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4751 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4752 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4753 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4754 | return 0; |
| 4755 | } |
| 4756 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4757 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4758 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4759 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4760 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4761 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4762 | sqe->rw_flags || sqe->buf_index) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4763 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4764 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4765 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4766 | |
| 4767 | req->close.fd = READ_ONCE(sqe->fd); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4768 | req->close.file_slot = READ_ONCE(sqe->file_index); |
| 4769 | if (req->close.file_slot && req->close.fd) |
| 4770 | return -EINVAL; |
| 4771 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4772 | return 0; |
| 4773 | } |
| 4774 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4775 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4776 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4777 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4778 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4779 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4780 | struct file *file = NULL; |
| 4781 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4782 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4783 | if (req->close.file_slot) { |
| 4784 | ret = io_close_fixed(req, issue_flags); |
| 4785 | goto err; |
| 4786 | } |
| 4787 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4788 | spin_lock(&files->file_lock); |
| 4789 | fdt = files_fdtable(files); |
| 4790 | if (close->fd >= fdt->max_fds) { |
| 4791 | spin_unlock(&files->file_lock); |
| 4792 | goto err; |
| 4793 | } |
| 4794 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4795 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4796 | spin_unlock(&files->file_lock); |
| 4797 | file = NULL; |
| 4798 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4799 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4800 | |
| 4801 | /* 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] | 4802 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4803 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4804 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4805 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4806 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4807 | ret = __close_fd_get_file(close->fd, &file); |
| 4808 | spin_unlock(&files->file_lock); |
| 4809 | if (ret < 0) { |
| 4810 | if (ret == -ENOENT) |
| 4811 | ret = -EBADF; |
| 4812 | goto err; |
| 4813 | } |
| 4814 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4815 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4816 | ret = filp_close(file, current->files); |
| 4817 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4818 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4819 | req_set_fail(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4820 | if (file) |
| 4821 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4822 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4823 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4824 | } |
| 4825 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4826 | 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] | 4827 | { |
| 4828 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4829 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4830 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4831 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4832 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4833 | sqe->splice_fd_in)) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4834 | return -EINVAL; |
| 4835 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4836 | req->sync.off = READ_ONCE(sqe->off); |
| 4837 | req->sync.len = READ_ONCE(sqe->len); |
| 4838 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4839 | return 0; |
| 4840 | } |
| 4841 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4842 | 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] | 4843 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4844 | int ret; |
| 4845 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4846 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4847 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4848 | return -EAGAIN; |
| 4849 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4850 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4851 | req->sync.flags); |
| 4852 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4853 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4854 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4855 | return 0; |
| 4856 | } |
| 4857 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4858 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4859 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4860 | struct io_async_msghdr *kmsg) |
| 4861 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4862 | struct io_async_msghdr *async_msg = req->async_data; |
| 4863 | |
| 4864 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4865 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4866 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4867 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4868 | return -ENOMEM; |
| 4869 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4870 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4871 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4872 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4873 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4874 | /* if were using fast_iov, set it to the new one */ |
| 4875 | if (!async_msg->free_iov) |
| 4876 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4877 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4878 | return -EAGAIN; |
| 4879 | } |
| 4880 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4881 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4882 | struct io_async_msghdr *iomsg) |
| 4883 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4884 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4885 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4886 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4887 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4888 | } |
| 4889 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4890 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4891 | { |
| 4892 | int ret; |
| 4893 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4894 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4895 | if (!ret) |
| 4896 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4897 | return ret; |
| 4898 | } |
| 4899 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4900 | 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] | 4901 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4902 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4903 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4904 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4905 | return -EINVAL; |
| 4906 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4907 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4908 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4909 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4910 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4911 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4912 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4913 | #ifdef CONFIG_COMPAT |
| 4914 | if (req->ctx->compat) |
| 4915 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4916 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4917 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4920 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4921 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4922 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4923 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4924 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4925 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4926 | int ret; |
| 4927 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4928 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4929 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4930 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4931 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 4932 | if (req_has_async_data(req)) { |
| 4933 | kmsg = req->async_data; |
| 4934 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4935 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4936 | if (ret) |
| 4937 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4938 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4939 | } |
| 4940 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4941 | flags = req->sr_msg.msg_flags; |
| 4942 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4943 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4944 | if (flags & MSG_WAITALL) |
| 4945 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4946 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4947 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4948 | |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 4949 | if (ret < min_ret) { |
| 4950 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) |
| 4951 | return io_setup_async_msg(req, kmsg); |
| 4952 | if (ret == -ERESTARTSYS) |
| 4953 | ret = -EINTR; |
| 4954 | req_set_fail(req); |
| 4955 | } |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4956 | /* fast path, check for non-NULL to avoid function call */ |
| 4957 | if (kmsg->free_iov) |
| 4958 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4959 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4960 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4961 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4962 | } |
| 4963 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4964 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4965 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4966 | struct io_sr_msg *sr = &req->sr_msg; |
| 4967 | struct msghdr msg; |
| 4968 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4969 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4970 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4971 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4972 | int ret; |
| 4973 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4974 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4975 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4976 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4977 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4978 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4979 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4980 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4981 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4982 | msg.msg_name = NULL; |
| 4983 | msg.msg_control = NULL; |
| 4984 | msg.msg_controllen = 0; |
| 4985 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4986 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4987 | flags = req->sr_msg.msg_flags; |
| 4988 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4989 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4990 | if (flags & MSG_WAITALL) |
| 4991 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4992 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4993 | msg.msg_flags = flags; |
| 4994 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 4995 | if (ret < min_ret) { |
| 4996 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) |
| 4997 | return -EAGAIN; |
| 4998 | if (ret == -ERESTARTSYS) |
| 4999 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5000 | req_set_fail(req); |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5001 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5002 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5003 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5004 | } |
| 5005 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5006 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 5007 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5008 | { |
| 5009 | struct io_sr_msg *sr = &req->sr_msg; |
| 5010 | struct iovec __user *uiov; |
| 5011 | size_t iov_len; |
| 5012 | int ret; |
| 5013 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5014 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 5015 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5016 | if (ret) |
| 5017 | return ret; |
| 5018 | |
| 5019 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 5020 | if (iov_len > 1) |
| 5021 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5022 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5023 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5024 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5025 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5026 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5027 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5028 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5029 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5030 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5031 | if (ret > 0) |
| 5032 | ret = 0; |
| 5033 | } |
| 5034 | |
| 5035 | return ret; |
| 5036 | } |
| 5037 | |
| 5038 | #ifdef CONFIG_COMPAT |
| 5039 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5040 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5041 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5042 | struct io_sr_msg *sr = &req->sr_msg; |
| 5043 | struct compat_iovec __user *uiov; |
| 5044 | compat_uptr_t ptr; |
| 5045 | compat_size_t len; |
| 5046 | int ret; |
| 5047 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 5048 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 5049 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5050 | if (ret) |
| 5051 | return ret; |
| 5052 | |
| 5053 | uiov = compat_ptr(ptr); |
| 5054 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 5055 | compat_ssize_t clen; |
| 5056 | |
| 5057 | if (len > 1) |
| 5058 | return -EINVAL; |
| 5059 | if (!access_ok(uiov, sizeof(*uiov))) |
| 5060 | return -EFAULT; |
| 5061 | if (__get_user(clen, &uiov->iov_len)) |
| 5062 | return -EFAULT; |
| 5063 | if (clen < 0) |
| 5064 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 5065 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5066 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5067 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5068 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5069 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5070 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 5071 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5072 | if (ret < 0) |
| 5073 | return ret; |
| 5074 | } |
| 5075 | |
| 5076 | return 0; |
| 5077 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5078 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5079 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5080 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 5081 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5082 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5083 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5084 | |
| 5085 | #ifdef CONFIG_COMPAT |
| 5086 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5087 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5088 | #endif |
| 5089 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 5090 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5091 | } |
| 5092 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5093 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5094 | unsigned int issue_flags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5095 | { |
| 5096 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5097 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5098 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5099 | } |
| 5100 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5101 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5102 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5103 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 5104 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5105 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 5106 | if (!ret) |
| 5107 | req->flags |= REQ_F_NEED_CLEANUP; |
| 5108 | return ret; |
| 5109 | } |
| 5110 | |
| 5111 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5112 | { |
| 5113 | struct io_sr_msg *sr = &req->sr_msg; |
| 5114 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 5115 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5116 | return -EINVAL; |
| 5117 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 5118 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 5119 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5120 | sr->bgid = READ_ONCE(sqe->buf_group); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5121 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 5122 | if (sr->msg_flags & MSG_DONTWAIT) |
| 5123 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5124 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 5125 | #ifdef CONFIG_COMPAT |
| 5126 | if (req->ctx->compat) |
| 5127 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 5128 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5129 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5132 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5133 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5134 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5135 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5136 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5137 | unsigned flags; |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 5138 | int ret, min_ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5139 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5140 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5141 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5142 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5143 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5144 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5145 | if (req_has_async_data(req)) { |
| 5146 | kmsg = req->async_data; |
| 5147 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5148 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 5149 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 5150 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5151 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5152 | } |
| 5153 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5154 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5155 | kbuf = io_recv_buffer_select(req, issue_flags); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5156 | if (IS_ERR(kbuf)) |
| 5157 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5158 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5159 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 5160 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5161 | 1, req->sr_msg.len); |
| 5162 | } |
| 5163 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5164 | flags = req->sr_msg.msg_flags; |
| 5165 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5166 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5167 | if (flags & MSG_WAITALL) |
| 5168 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 5169 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5170 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 5171 | kmsg->uaddr, flags); |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5172 | if (ret < min_ret) { |
| 5173 | if (ret == -EAGAIN && force_nonblock) |
| 5174 | return io_setup_async_msg(req, kmsg); |
| 5175 | if (ret == -ERESTARTSYS) |
| 5176 | ret = -EINTR; |
| 5177 | req_set_fail(req); |
| 5178 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { |
| 5179 | req_set_fail(req); |
| 5180 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5181 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5182 | /* fast path, check for non-NULL to avoid function call */ |
| 5183 | if (kmsg->free_iov) |
| 5184 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5185 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 5186 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req)); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5187 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5188 | } |
| 5189 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5190 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5191 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5192 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5193 | struct io_sr_msg *sr = &req->sr_msg; |
| 5194 | struct msghdr msg; |
| 5195 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5196 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5197 | struct iovec iov; |
| 5198 | unsigned flags; |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 5199 | int ret, min_ret = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5200 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5201 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5202 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5203 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5204 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5205 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5206 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5207 | kbuf = io_recv_buffer_select(req, issue_flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5208 | if (IS_ERR(kbuf)) |
| 5209 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5210 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5211 | } |
| 5212 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5213 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5214 | if (unlikely(ret)) |
| 5215 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5216 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5217 | msg.msg_name = NULL; |
| 5218 | msg.msg_control = NULL; |
| 5219 | msg.msg_controllen = 0; |
| 5220 | msg.msg_namelen = 0; |
| 5221 | msg.msg_iocb = NULL; |
| 5222 | msg.msg_flags = 0; |
| 5223 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5224 | flags = req->sr_msg.msg_flags; |
| 5225 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5226 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5227 | if (flags & MSG_WAITALL) |
| 5228 | min_ret = iov_iter_count(&msg.msg_iter); |
| 5229 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5230 | ret = sock_recvmsg(sock, &msg, flags); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5231 | out_free: |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5232 | if (ret < min_ret) { |
| 5233 | if (ret == -EAGAIN && force_nonblock) |
| 5234 | return -EAGAIN; |
| 5235 | if (ret == -ERESTARTSYS) |
| 5236 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5237 | req_set_fail(req); |
Pavel Begunkov | 7297ce3 | 2021-11-23 00:07:47 +0000 | [diff] [blame] | 5238 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { |
| 5239 | req_set_fail(req); |
| 5240 | } |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 5241 | |
| 5242 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5243 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5244 | } |
| 5245 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5246 | 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] | 5247 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5248 | struct io_accept *accept = &req->accept; |
| 5249 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5250 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5251 | return -EINVAL; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5252 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5253 | return -EINVAL; |
| 5254 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5255 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5256 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5257 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5258 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5259 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5260 | accept->file_slot = READ_ONCE(sqe->file_index); |
| 5261 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || |
| 5262 | (accept->flags & SOCK_CLOEXEC))) |
| 5263 | return -EINVAL; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5264 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
| 5265 | return -EINVAL; |
| 5266 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) |
| 5267 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5268 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5269 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5270 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5271 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5272 | { |
| 5273 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5274 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5275 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5276 | bool fixed = !!accept->file_slot; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5277 | struct file *file; |
| 5278 | int ret, fd; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5279 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5280 | if (req->file->f_flags & O_NONBLOCK) |
| 5281 | req->flags |= REQ_F_NOWAIT; |
| 5282 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5283 | if (!fixed) { |
| 5284 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); |
| 5285 | if (unlikely(fd < 0)) |
| 5286 | return fd; |
| 5287 | } |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5288 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
| 5289 | accept->flags); |
| 5290 | if (IS_ERR(file)) { |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5291 | if (!fixed) |
| 5292 | put_unused_fd(fd); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5293 | ret = PTR_ERR(file); |
| 5294 | if (ret == -EAGAIN && force_nonblock) |
| 5295 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5296 | if (ret == -ERESTARTSYS) |
| 5297 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5298 | req_set_fail(req); |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5299 | } else if (!fixed) { |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5300 | fd_install(fd, file); |
| 5301 | ret = fd; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5302 | } else { |
| 5303 | ret = io_install_fixed_file(req, file, issue_flags, |
| 5304 | accept->file_slot - 1); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5305 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5306 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5307 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5308 | } |
| 5309 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5310 | static int io_connect_prep_async(struct io_kiocb *req) |
| 5311 | { |
| 5312 | struct io_async_connect *io = req->async_data; |
| 5313 | struct io_connect *conn = &req->connect; |
| 5314 | |
| 5315 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 5316 | } |
| 5317 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5318 | 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] | 5319 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5320 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5321 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5322 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5323 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5324 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
| 5325 | sqe->splice_fd_in) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5326 | return -EINVAL; |
| 5327 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5328 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5329 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5330 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5331 | } |
| 5332 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5333 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5334 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5335 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5336 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5337 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5338 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5339 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5340 | if (req_has_async_data(req)) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5341 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5342 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5343 | ret = move_addr_to_kernel(req->connect.addr, |
| 5344 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5345 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5346 | if (ret) |
| 5347 | goto out; |
| 5348 | io = &__io; |
| 5349 | } |
| 5350 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5351 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5352 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5353 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5354 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5355 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5356 | if (req_has_async_data(req)) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5357 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5358 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5359 | ret = -ENOMEM; |
| 5360 | goto out; |
| 5361 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5362 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5363 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5364 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5365 | if (ret == -ERESTARTSYS) |
| 5366 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5367 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5368 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5369 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5370 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5371 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5372 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5373 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5374 | #define IO_NETOP_FN(op) \ |
| 5375 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 5376 | { \ |
| 5377 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5378 | } |
| 5379 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5380 | #define IO_NETOP_PREP(op) \ |
| 5381 | IO_NETOP_FN(op) \ |
| 5382 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 5383 | { \ |
| 5384 | return -EOPNOTSUPP; \ |
| 5385 | } \ |
| 5386 | |
| 5387 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 5388 | IO_NETOP_PREP(op) \ |
| 5389 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 5390 | { \ |
| 5391 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5392 | } |
| 5393 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5394 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 5395 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 5396 | IO_NETOP_PREP_ASYNC(connect); |
| 5397 | IO_NETOP_PREP(accept); |
| 5398 | IO_NETOP_FN(send); |
| 5399 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5400 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5401 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5402 | struct io_poll_table { |
| 5403 | struct poll_table_struct pt; |
| 5404 | struct io_kiocb *req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5405 | int nr_entries; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5406 | int error; |
| 5407 | }; |
| 5408 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5409 | #define IO_POLL_CANCEL_FLAG BIT(31) |
| 5410 | #define IO_POLL_REF_MASK ((1u << 20)-1) |
| 5411 | |
| 5412 | /* |
| 5413 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can |
| 5414 | * bump it and acquire ownership. It's disallowed to modify requests while not |
| 5415 | * owning it, that prevents from races for enqueueing task_work's and b/w |
| 5416 | * arming poll and wakeups. |
| 5417 | */ |
| 5418 | static inline bool io_poll_get_ownership(struct io_kiocb *req) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5419 | { |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5420 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5421 | } |
| 5422 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5423 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5424 | { |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5425 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5426 | } |
| 5427 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5428 | 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] | 5429 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5430 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5431 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5432 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5433 | return req->apoll->double_poll; |
| 5434 | } |
| 5435 | |
| 5436 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5437 | { |
| 5438 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5439 | return &req->poll; |
| 5440 | return &req->apoll->poll; |
| 5441 | } |
| 5442 | |
Pavel Begunkov | 5641897 | 2021-12-15 22:08:46 +0000 | [diff] [blame] | 5443 | static void io_poll_req_insert(struct io_kiocb *req) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5444 | { |
| 5445 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 5641897 | 2021-12-15 22:08:46 +0000 | [diff] [blame] | 5446 | struct hlist_head *list; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5447 | |
Pavel Begunkov | 5641897 | 2021-12-15 22:08:46 +0000 | [diff] [blame] | 5448 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5449 | hlist_add_head(&req->hash_node, list); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5453 | wait_queue_func_t wake_func) |
| 5454 | { |
| 5455 | poll->head = NULL; |
Jens Axboe | 464dca6 | 2021-03-19 14:06:24 -0600 | [diff] [blame] | 5456 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 5457 | /* mask in events that we always want/need */ |
| 5458 | poll->events = events | IO_POLL_UNMASK; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5459 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5460 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5461 | } |
| 5462 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5463 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5464 | { |
Pavel Begunkov | 791f346 | 2022-01-14 11:59:10 +0000 | [diff] [blame] | 5465 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5466 | |
Pavel Begunkov | 791f346 | 2022-01-14 11:59:10 +0000 | [diff] [blame] | 5467 | if (head) { |
| 5468 | spin_lock_irq(&head->lock); |
| 5469 | list_del_init(&poll->wait.entry); |
| 5470 | poll->head = NULL; |
| 5471 | spin_unlock_irq(&head->lock); |
| 5472 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5473 | } |
| 5474 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5475 | static void io_poll_remove_entries(struct io_kiocb *req) |
| 5476 | { |
| 5477 | struct io_poll_iocb *poll = io_poll_get_single(req); |
| 5478 | struct io_poll_iocb *poll_double = io_poll_get_double(req); |
| 5479 | |
Pavel Begunkov | 791f346 | 2022-01-14 11:59:10 +0000 | [diff] [blame] | 5480 | /* |
| 5481 | * While we hold the waitqueue lock and the waitqueue is nonempty, |
| 5482 | * wake_up_pollfree() will wait for us. However, taking the waitqueue |
| 5483 | * lock in the first place can race with the waitqueue being freed. |
| 5484 | * |
| 5485 | * We solve this as eventpoll does: by taking advantage of the fact that |
| 5486 | * all users of wake_up_pollfree() will RCU-delay the actual free. If |
| 5487 | * we enter rcu_read_lock() and see that the pointer to the queue is |
| 5488 | * non-NULL, we can then lock it without the memory being freed out from |
| 5489 | * under us. |
| 5490 | * |
| 5491 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in |
| 5492 | * case the caller deletes the entry from the queue, leaving it empty. |
| 5493 | * In that case, only RCU prevents the queue memory from being freed. |
| 5494 | */ |
| 5495 | rcu_read_lock(); |
| 5496 | io_poll_remove_entry(poll); |
| 5497 | if (poll_double) |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5498 | io_poll_remove_entry(poll_double); |
Pavel Begunkov | 791f346 | 2022-01-14 11:59:10 +0000 | [diff] [blame] | 5499 | rcu_read_unlock(); |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | /* |
| 5503 | * All poll tw should go through this. Checks for poll events, manages |
| 5504 | * references, does rewait, etc. |
| 5505 | * |
| 5506 | * Returns a negative error on failure. >0 when no action require, which is |
| 5507 | * either spurious wakeup or multishot CQE is served. 0 when it's done with |
| 5508 | * the request, then the mask is stored in req->result. |
| 5509 | */ |
| 5510 | static int io_poll_check_events(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5511 | { |
| 5512 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5513 | struct io_poll_iocb *poll = io_poll_get_single(req); |
| 5514 | int v; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5515 | |
| 5516 | /* req->task == current here, checking PF_EXITING is safe */ |
| 5517 | if (unlikely(req->task->flags & PF_EXITING)) |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5518 | io_poll_mark_cancelled(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5519 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5520 | do { |
| 5521 | v = atomic_read(&req->poll_refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 5522 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5523 | /* tw handler should be the owner, and so have some references */ |
| 5524 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) |
| 5525 | return 0; |
| 5526 | if (v & IO_POLL_CANCEL_FLAG) |
| 5527 | return -ECANCELED; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5528 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5529 | if (!req->result) { |
| 5530 | struct poll_table_struct pt = { ._key = poll->events }; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5531 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5532 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5533 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5534 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5535 | /* multishot, just fill an CQE and proceed */ |
| 5536 | if (req->result && !(poll->events & EPOLLONESHOT)) { |
| 5537 | __poll_t mask = mangle_poll(req->result & poll->events); |
| 5538 | bool filled; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5539 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5540 | spin_lock(&ctx->completion_lock); |
| 5541 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, |
| 5542 | IORING_CQE_F_MORE); |
| 5543 | io_commit_cqring(ctx); |
| 5544 | spin_unlock(&ctx->completion_lock); |
| 5545 | if (unlikely(!filled)) |
| 5546 | return -ECANCELED; |
| 5547 | io_cqring_ev_posted(ctx); |
| 5548 | } else if (req->result) { |
| 5549 | return 0; |
| 5550 | } |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5551 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5552 | /* |
| 5553 | * Release all references, retry if someone tried to restart |
| 5554 | * task_work while we were executing it. |
| 5555 | */ |
| 5556 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5557 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5558 | return 1; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
| 5562 | { |
| 5563 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5564 | int ret; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5565 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5566 | ret = io_poll_check_events(req); |
| 5567 | if (ret > 0) |
| 5568 | return; |
| 5569 | |
| 5570 | if (!ret) { |
| 5571 | req->result = mangle_poll(req->result & req->poll.events); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5572 | } else { |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5573 | req->result = ret; |
| 5574 | req_set_fail(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5575 | } |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5576 | |
| 5577 | io_poll_remove_entries(req); |
| 5578 | spin_lock(&ctx->completion_lock); |
| 5579 | hash_del(&req->hash_node); |
| 5580 | __io_req_complete_post(req, req->result, 0); |
| 5581 | io_commit_cqring(ctx); |
| 5582 | spin_unlock(&ctx->completion_lock); |
| 5583 | io_cqring_ev_posted(ctx); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5584 | } |
| 5585 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5586 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
| 5587 | { |
| 5588 | struct io_ring_ctx *ctx = req->ctx; |
| 5589 | int ret; |
| 5590 | |
| 5591 | ret = io_poll_check_events(req); |
| 5592 | if (ret > 0) |
| 5593 | return; |
| 5594 | |
| 5595 | io_poll_remove_entries(req); |
| 5596 | spin_lock(&ctx->completion_lock); |
| 5597 | hash_del(&req->hash_node); |
| 5598 | spin_unlock(&ctx->completion_lock); |
| 5599 | |
| 5600 | if (!ret) |
| 5601 | io_req_task_submit(req, locked); |
| 5602 | else |
| 5603 | io_req_complete_failed(req, ret); |
| 5604 | } |
| 5605 | |
| 5606 | static void __io_poll_execute(struct io_kiocb *req, int mask) |
| 5607 | { |
| 5608 | req->result = mask; |
| 5609 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5610 | req->io_task_work.func = io_poll_task_func; |
| 5611 | else |
| 5612 | req->io_task_work.func = io_apoll_task_func; |
| 5613 | |
| 5614 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5615 | io_req_task_work_add(req, false); |
| 5616 | } |
| 5617 | |
| 5618 | static inline void io_poll_execute(struct io_kiocb *req, int res) |
| 5619 | { |
| 5620 | if (io_poll_get_ownership(req)) |
| 5621 | __io_poll_execute(req, res); |
| 5622 | } |
| 5623 | |
| 5624 | static void io_poll_cancel_req(struct io_kiocb *req) |
| 5625 | { |
| 5626 | io_poll_mark_cancelled(req); |
| 5627 | /* kick tw, which should complete the request */ |
| 5628 | io_poll_execute(req, 0); |
| 5629 | } |
| 5630 | |
| 5631 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5632 | void *key) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5633 | { |
| 5634 | struct io_kiocb *req = wait->private; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5635 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
| 5636 | wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5637 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5638 | |
Pavel Begunkov | 791f346 | 2022-01-14 11:59:10 +0000 | [diff] [blame] | 5639 | if (unlikely(mask & POLLFREE)) { |
| 5640 | io_poll_mark_cancelled(req); |
| 5641 | /* we have to kick tw in case it's not already */ |
| 5642 | io_poll_execute(req, 0); |
| 5643 | |
| 5644 | /* |
| 5645 | * If the waitqueue is being freed early but someone is already |
| 5646 | * holds ownership over it, we have to tear down the request as |
| 5647 | * best we can. That means immediately removing the request from |
| 5648 | * its waitqueue and preventing all further accesses to the |
| 5649 | * waitqueue via the request. |
| 5650 | */ |
| 5651 | list_del_init(&poll->wait.entry); |
| 5652 | |
| 5653 | /* |
| 5654 | * Careful: this *must* be the last step, since as soon |
| 5655 | * as req->head is NULL'ed out, the request can be |
| 5656 | * completed and freed, since aio_poll_complete_work() |
| 5657 | * will no longer need to take the waitqueue lock. |
| 5658 | */ |
| 5659 | smp_store_release(&poll->head, NULL); |
| 5660 | return 1; |
| 5661 | } |
| 5662 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5663 | /* for instances that support it check for an event match first */ |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5664 | if (mask && !(mask & poll->events)) |
| 5665 | return 0; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5666 | |
Pavel Begunkov | eb0089d | 2021-12-15 22:08:49 +0000 | [diff] [blame] | 5667 | if (io_poll_get_ownership(req)) { |
| 5668 | /* optional, saves extra locking for removal in tw handler */ |
| 5669 | if (mask && poll->events & EPOLLONESHOT) { |
| 5670 | list_del_init(&poll->wait.entry); |
| 5671 | poll->head = NULL; |
| 5672 | } |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5673 | __io_poll_execute(req, mask); |
Pavel Begunkov | eb0089d | 2021-12-15 22:08:49 +0000 | [diff] [blame] | 5674 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5675 | return 1; |
| 5676 | } |
| 5677 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5678 | 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] | 5679 | struct wait_queue_head *head, |
| 5680 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5681 | { |
| 5682 | struct io_kiocb *req = pt->req; |
| 5683 | |
| 5684 | /* |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5685 | * The file being polled uses multiple waitqueues for poll handling |
| 5686 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb |
| 5687 | * if this happens. |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5688 | */ |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5689 | if (unlikely(pt->nr_entries)) { |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5690 | struct io_poll_iocb *first = poll; |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5691 | |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5692 | /* double add on the same waitqueue head, ignore */ |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5693 | if (first->head == head) |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5694 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5695 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5696 | if (*poll_ptr) { |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5697 | if ((*poll_ptr)->head == head) |
| 5698 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5699 | pt->error = -EINVAL; |
| 5700 | return; |
| 5701 | } |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5702 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5703 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5704 | if (!poll) { |
| 5705 | pt->error = -ENOMEM; |
| 5706 | return; |
| 5707 | } |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5708 | io_init_poll_iocb(poll, first->events, first->wait.func); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5709 | *poll_ptr = poll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5710 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5711 | req->flags |= REQ_F_ASYNC_DATA; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5712 | } |
| 5713 | |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5714 | pt->nr_entries++; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5715 | poll->head = head; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5716 | poll->wait.private = req; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5717 | |
| 5718 | if (poll->events & EPOLLEXCLUSIVE) |
| 5719 | add_wait_queue_exclusive(head, &poll->wait); |
| 5720 | else |
| 5721 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5722 | } |
| 5723 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5724 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5725 | struct poll_table_struct *p) |
| 5726 | { |
| 5727 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5728 | |
| 5729 | __io_queue_proc(&pt->req->poll, pt, head, |
| 5730 | (struct io_poll_iocb **) &pt->req->async_data); |
| 5731 | } |
| 5732 | |
| 5733 | static int __io_arm_poll_handler(struct io_kiocb *req, |
| 5734 | struct io_poll_iocb *poll, |
| 5735 | struct io_poll_table *ipt, __poll_t mask) |
| 5736 | { |
| 5737 | struct io_ring_ctx *ctx = req->ctx; |
| 5738 | int v; |
| 5739 | |
| 5740 | INIT_HLIST_NODE(&req->hash_node); |
| 5741 | io_init_poll_iocb(poll, mask, io_poll_wake); |
| 5742 | poll->file = req->file; |
| 5743 | poll->wait.private = req; |
| 5744 | |
| 5745 | ipt->pt._key = mask; |
| 5746 | ipt->req = req; |
| 5747 | ipt->error = 0; |
| 5748 | ipt->nr_entries = 0; |
| 5749 | |
| 5750 | /* |
| 5751 | * Take the ownership to delay any tw execution up until we're done |
| 5752 | * with poll arming. see io_poll_get_ownership(). |
| 5753 | */ |
| 5754 | atomic_set(&req->poll_refs, 1); |
| 5755 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
| 5756 | |
| 5757 | if (mask && (poll->events & EPOLLONESHOT)) { |
| 5758 | io_poll_remove_entries(req); |
| 5759 | /* no one else has access to the req, forget about the ref */ |
| 5760 | return mask; |
| 5761 | } |
| 5762 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { |
| 5763 | io_poll_remove_entries(req); |
| 5764 | if (!ipt->error) |
| 5765 | ipt->error = -EINVAL; |
| 5766 | return 0; |
| 5767 | } |
| 5768 | |
| 5769 | spin_lock(&ctx->completion_lock); |
| 5770 | io_poll_req_insert(req); |
| 5771 | spin_unlock(&ctx->completion_lock); |
| 5772 | |
| 5773 | if (mask) { |
| 5774 | /* can't multishot if failed, just queue the event we've got */ |
| 5775 | if (unlikely(ipt->error || !ipt->nr_entries)) |
| 5776 | poll->events |= EPOLLONESHOT; |
| 5777 | __io_poll_execute(req, mask); |
| 5778 | return 0; |
| 5779 | } |
| 5780 | |
| 5781 | /* |
| 5782 | * Release ownership. If someone tried to queue a tw while it was |
| 5783 | * locked, kick it off for them. |
| 5784 | */ |
| 5785 | v = atomic_dec_return(&req->poll_refs); |
| 5786 | if (unlikely(v & IO_POLL_REF_MASK)) |
| 5787 | __io_poll_execute(req, 0); |
| 5788 | return 0; |
| 5789 | } |
| 5790 | |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5791 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5792 | struct poll_table_struct *p) |
| 5793 | { |
| 5794 | 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] | 5795 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5796 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5797 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5798 | } |
| 5799 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5800 | enum { |
| 5801 | IO_APOLL_OK, |
| 5802 | IO_APOLL_ABORTED, |
| 5803 | IO_APOLL_READY |
| 5804 | }; |
| 5805 | |
| 5806 | static int io_arm_poll_handler(struct io_kiocb *req) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5807 | { |
| 5808 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5809 | struct io_ring_ctx *ctx = req->ctx; |
| 5810 | struct async_poll *apoll; |
| 5811 | struct io_poll_table ipt; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5812 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
| 5813 | int ret; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5814 | |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5815 | if (!def->pollin && !def->pollout) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5816 | return IO_APOLL_ABORTED; |
Pavel Begunkov | 658d0a4 | 2021-10-23 12:13:58 +0100 | [diff] [blame] | 5817 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
| 5818 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5819 | |
| 5820 | if (def->pollin) { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5821 | mask |= POLLIN | POLLRDNORM; |
| 5822 | |
| 5823 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5824 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5825 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5826 | mask &= ~POLLIN; |
| 5827 | } else { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5828 | mask |= POLLOUT | POLLWRNORM; |
| 5829 | } |
| 5830 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5831 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5832 | if (unlikely(!apoll)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5833 | return IO_APOLL_ABORTED; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5834 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5835 | req->apoll = apoll; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5836 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5837 | ipt.pt._qproc = io_async_queue_proc; |
| 5838 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5839 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
Hao Xu | 41a5169 | 2021-08-12 15:47:02 +0800 | [diff] [blame] | 5840 | if (ret || ipt.error) |
| 5841 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; |
| 5842 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5843 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
| 5844 | mask, apoll->poll.events); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5845 | return IO_APOLL_OK; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5846 | } |
| 5847 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5848 | /* |
| 5849 | * Returns true if we found and killed one or more poll requests |
| 5850 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 5851 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
| 5852 | struct task_struct *tsk, bool cancel_all) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5853 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5854 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5855 | struct io_kiocb *req; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5856 | bool found = false; |
| 5857 | int i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5858 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5859 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5860 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5861 | struct hlist_head *list; |
| 5862 | |
| 5863 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5864 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Linus Torvalds | 42a7b4e | 2022-01-12 10:20:35 -0800 | [diff] [blame] | 5865 | if (io_match_task_safe(req, tsk, cancel_all)) { |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5866 | io_poll_cancel_req(req); |
| 5867 | found = true; |
| 5868 | } |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5869 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5870 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5871 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5872 | return found; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5873 | } |
| 5874 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5875 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5876 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5877 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5878 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5879 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5880 | struct io_kiocb *req; |
| 5881 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5882 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5883 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5884 | if (sqe_addr != req->user_data) |
| 5885 | continue; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5886 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
| 5887 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5888 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5889 | } |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5890 | return NULL; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5891 | } |
| 5892 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5893 | static bool io_poll_disarm(struct io_kiocb *req) |
| 5894 | __must_hold(&ctx->completion_lock) |
| 5895 | { |
| 5896 | if (!io_poll_get_ownership(req)) |
| 5897 | return false; |
| 5898 | io_poll_remove_entries(req); |
| 5899 | hash_del(&req->hash_node); |
| 5900 | return true; |
| 5901 | } |
| 5902 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5903 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5904 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5905 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5906 | { |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5907 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5908 | |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5909 | if (!req) |
| 5910 | return -ENOENT; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5911 | io_poll_cancel_req(req); |
| 5912 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5913 | } |
| 5914 | |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 5915 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
| 5916 | unsigned int flags) |
| 5917 | { |
| 5918 | u32 events; |
| 5919 | |
| 5920 | events = READ_ONCE(sqe->poll32_events); |
| 5921 | #ifdef __BIG_ENDIAN |
| 5922 | events = swahw32(events); |
| 5923 | #endif |
| 5924 | if (!(flags & IORING_POLL_ADD_MULTI)) |
| 5925 | events |= EPOLLONESHOT; |
| 5926 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
| 5927 | } |
| 5928 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5929 | static int io_poll_update_prep(struct io_kiocb *req, |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5930 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5931 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5932 | struct io_poll_update *upd = &req->poll_update; |
| 5933 | u32 flags; |
| 5934 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5935 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5936 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5937 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5938 | return -EINVAL; |
| 5939 | flags = READ_ONCE(sqe->len); |
| 5940 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | |
| 5941 | IORING_POLL_ADD_MULTI)) |
| 5942 | return -EINVAL; |
| 5943 | /* meaningless without update */ |
| 5944 | if (flags == IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5945 | return -EINVAL; |
| 5946 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5947 | upd->old_user_data = READ_ONCE(sqe->addr); |
| 5948 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 5949 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5950 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5951 | upd->new_user_data = READ_ONCE(sqe->off); |
| 5952 | if (!upd->update_user_data && upd->new_user_data) |
| 5953 | return -EINVAL; |
| 5954 | if (upd->update_events) |
| 5955 | upd->events = io_poll_parse_events(sqe, flags); |
| 5956 | else if (sqe->poll32_events) |
| 5957 | return -EINVAL; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5958 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5959 | return 0; |
| 5960 | } |
| 5961 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5962 | 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] | 5963 | { |
| 5964 | struct io_poll_iocb *poll = &req->poll; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5965 | u32 flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5966 | |
| 5967 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5968 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5969 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5970 | return -EINVAL; |
| 5971 | flags = READ_ONCE(sqe->len); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5972 | if (flags & ~IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5973 | return -EINVAL; |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 5974 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
| 5975 | return -EINVAL; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5976 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5977 | io_req_set_refcount(req); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5978 | poll->events = io_poll_parse_events(sqe, flags); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5979 | return 0; |
| 5980 | } |
| 5981 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5982 | 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] | 5983 | { |
| 5984 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5985 | struct io_poll_table ipt; |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5986 | int ret; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5987 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5988 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5989 | |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 5990 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
| 5991 | ret = ret ?: ipt.error; |
| 5992 | if (ret) |
| 5993 | __io_req_complete(req, issue_flags, ret, 0); |
| 5994 | return 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5995 | } |
| 5996 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5997 | 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] | 5998 | { |
| 5999 | struct io_ring_ctx *ctx = req->ctx; |
| 6000 | struct io_kiocb *preq; |
Pavel Begunkov | 2bbb146 | 2021-12-15 22:08:45 +0000 | [diff] [blame] | 6001 | int ret2, ret = 0; |
Pavel Begunkov | cc8e9ba | 2021-12-15 22:08:50 +0000 | [diff] [blame] | 6002 | bool locked; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6003 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6004 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 6005 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 6006 | if (!preq || !io_poll_disarm(preq)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6007 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | aa43477 | 2021-12-15 22:08:48 +0000 | [diff] [blame] | 6008 | ret = preq ? -EALREADY : -ENOENT; |
Pavel Begunkov | 2bbb146 | 2021-12-15 22:08:45 +0000 | [diff] [blame] | 6009 | goto out; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6010 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6011 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 6012 | |
Pavel Begunkov | 2bbb146 | 2021-12-15 22:08:45 +0000 | [diff] [blame] | 6013 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
| 6014 | /* only mask one event flags, keep behavior flags */ |
| 6015 | if (req->poll_update.update_events) { |
| 6016 | preq->poll.events &= ~0xffff; |
| 6017 | preq->poll.events |= req->poll_update.events & 0xffff; |
| 6018 | preq->poll.events |= IO_POLL_UNMASK; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 6019 | } |
Pavel Begunkov | 2bbb146 | 2021-12-15 22:08:45 +0000 | [diff] [blame] | 6020 | if (req->poll_update.update_user_data) |
| 6021 | preq->user_data = req->poll_update.new_user_data; |
| 6022 | |
| 6023 | ret2 = io_poll_add(preq, issue_flags); |
| 6024 | /* successfully updated, don't complete poll request */ |
| 6025 | if (!ret2) |
| 6026 | goto out; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6027 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 6028 | |
Pavel Begunkov | 2bbb146 | 2021-12-15 22:08:45 +0000 | [diff] [blame] | 6029 | req_set_fail(preq); |
Pavel Begunkov | cc8e9ba | 2021-12-15 22:08:50 +0000 | [diff] [blame] | 6030 | preq->result = -ECANCELED; |
| 6031 | locked = !(issue_flags & IO_URING_F_UNLOCKED); |
| 6032 | io_req_task_complete(preq, &locked); |
Pavel Begunkov | 2bbb146 | 2021-12-15 22:08:45 +0000 | [diff] [blame] | 6033 | out: |
| 6034 | if (ret < 0) |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 6035 | req_set_fail(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6036 | /* complete update request, we're done with it */ |
Pavel Begunkov | cc8e9ba | 2021-12-15 22:08:50 +0000 | [diff] [blame] | 6037 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6038 | return 0; |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6039 | } |
| 6040 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6041 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 6042 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6043 | struct io_timeout_data *data = container_of(timer, |
| 6044 | struct io_timeout_data, timer); |
| 6045 | struct io_kiocb *req = data->req; |
| 6046 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6047 | unsigned long flags; |
| 6048 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6049 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 6050 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 6051 | atomic_set(&req->ctx->cq_timeouts, |
| 6052 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6053 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 6054 | |
Pavel Begunkov | a90c8bf | 2021-12-05 14:38:00 +0000 | [diff] [blame] | 6055 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
| 6056 | req_set_fail(req); |
| 6057 | |
| 6058 | req->result = -ETIME; |
| 6059 | req->io_task_work.func = io_req_task_complete; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 6060 | io_req_task_work_add(req, false); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6061 | return HRTIMER_NORESTART; |
| 6062 | } |
| 6063 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6064 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 6065 | __u64 user_data) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6066 | __must_hold(&ctx->timeout_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6067 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6068 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6069 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6070 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6071 | |
| 6072 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6073 | found = user_data == req->user_data; |
| 6074 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6075 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6076 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6077 | if (!found) |
| 6078 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6079 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6080 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6081 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6082 | return ERR_PTR(-EALREADY); |
| 6083 | list_del_init(&req->timeout.list); |
| 6084 | return req; |
| 6085 | } |
| 6086 | |
| 6087 | 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] | 6088 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6089 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6090 | { |
| 6091 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6092 | |
| 6093 | if (IS_ERR(req)) |
| 6094 | return PTR_ERR(req); |
| 6095 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6096 | req_set_fail(req); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 6097 | io_fill_cqe_req(req, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 6098 | io_put_req_deferred(req); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6099 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6100 | } |
| 6101 | |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6102 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
| 6103 | { |
| 6104 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { |
| 6105 | case IORING_TIMEOUT_BOOTTIME: |
| 6106 | return CLOCK_BOOTTIME; |
| 6107 | case IORING_TIMEOUT_REALTIME: |
| 6108 | return CLOCK_REALTIME; |
| 6109 | default: |
| 6110 | /* can't happen, vetted at prep time */ |
| 6111 | WARN_ON_ONCE(1); |
| 6112 | fallthrough; |
| 6113 | case 0: |
| 6114 | return CLOCK_MONOTONIC; |
| 6115 | } |
| 6116 | } |
| 6117 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6118 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6119 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 6120 | __must_hold(&ctx->timeout_lock) |
| 6121 | { |
| 6122 | struct io_timeout_data *io; |
| 6123 | struct io_kiocb *req; |
| 6124 | bool found = false; |
| 6125 | |
| 6126 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { |
| 6127 | found = user_data == req->user_data; |
| 6128 | if (found) |
| 6129 | break; |
| 6130 | } |
| 6131 | if (!found) |
| 6132 | return -ENOENT; |
| 6133 | |
| 6134 | io = req->async_data; |
| 6135 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
| 6136 | return -EALREADY; |
| 6137 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); |
| 6138 | io->timer.function = io_link_timeout_fn; |
| 6139 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); |
| 6140 | return 0; |
| 6141 | } |
| 6142 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6143 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6144 | struct timespec64 *ts, enum hrtimer_mode mode) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6145 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6146 | { |
| 6147 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6148 | struct io_timeout_data *data; |
| 6149 | |
| 6150 | if (IS_ERR(req)) |
| 6151 | return PTR_ERR(req); |
| 6152 | |
| 6153 | req->timeout.off = 0; /* noseq */ |
| 6154 | data = req->async_data; |
| 6155 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6156 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6157 | data->timer.function = io_timeout_fn; |
| 6158 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 6159 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6160 | } |
| 6161 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6162 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 6163 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6164 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6165 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 6166 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6168 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6169 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6170 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6171 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6172 | return -EINVAL; |
| 6173 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6174 | tr->ltimeout = false; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6175 | tr->addr = READ_ONCE(sqe->addr); |
| 6176 | tr->flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6177 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
| 6178 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
| 6179 | return -EINVAL; |
| 6180 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) |
| 6181 | tr->ltimeout = true; |
| 6182 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6183 | return -EINVAL; |
| 6184 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 6185 | return -EFAULT; |
Ye Bin | 2087009 | 2021-11-29 12:15:37 +0800 | [diff] [blame] | 6186 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
| 6187 | return -EINVAL; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6188 | } else if (tr->flags) { |
| 6189 | /* timeout removal doesn't support flags */ |
| 6190 | return -EINVAL; |
| 6191 | } |
| 6192 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6193 | return 0; |
| 6194 | } |
| 6195 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6196 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 6197 | { |
| 6198 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 6199 | : HRTIMER_MODE_REL; |
| 6200 | } |
| 6201 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6202 | /* |
| 6203 | * Remove or update an existing timeout command |
| 6204 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6205 | 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] | 6206 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6207 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6208 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6209 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6210 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6211 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
| 6212 | spin_lock(&ctx->completion_lock); |
| 6213 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6214 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6215 | spin_unlock_irq(&ctx->timeout_lock); |
| 6216 | spin_unlock(&ctx->completion_lock); |
| 6217 | } else { |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6218 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
| 6219 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6220 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6221 | if (tr->ltimeout) |
| 6222 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 6223 | else |
| 6224 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6225 | spin_unlock_irq(&ctx->timeout_lock); |
| 6226 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6227 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6228 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6229 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6230 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6231 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6232 | } |
| 6233 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6234 | 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] | 6235 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6236 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6237 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6238 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6239 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6240 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6241 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6242 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6243 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
| 6244 | sqe->splice_fd_in) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6245 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6246 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6247 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6248 | flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 6249 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
| 6250 | IORING_TIMEOUT_ETIME_SUCCESS)) |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6251 | return -EINVAL; |
| 6252 | /* more than one clock specified is invalid, obviously */ |
| 6253 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6254 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 6255 | |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6256 | INIT_LIST_HEAD(&req->timeout.list); |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6257 | req->timeout.off = off; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 6258 | if (unlikely(off && !req->ctx->off_timeout_used)) |
| 6259 | req->ctx->off_timeout_used = true; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6260 | |
Pavel Begunkov | d6a644a | 2021-10-23 12:14:00 +0100 | [diff] [blame] | 6261 | if (WARN_ON_ONCE(req_has_async_data(req))) |
| 6262 | return -EFAULT; |
| 6263 | if (io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6264 | return -ENOMEM; |
| 6265 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6266 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6267 | data->req = req; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6268 | data->flags = flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6269 | |
| 6270 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6271 | return -EFAULT; |
| 6272 | |
Ye Bin | f6223ff | 2021-11-18 09:59:07 +0800 | [diff] [blame] | 6273 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
| 6274 | return -EINVAL; |
| 6275 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6276 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6277 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6278 | |
| 6279 | if (is_timeout_link) { |
| 6280 | struct io_submit_link *link = &req->ctx->submit_state.link; |
| 6281 | |
| 6282 | if (!link->head) |
| 6283 | return -EINVAL; |
| 6284 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) |
| 6285 | return -EINVAL; |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 6286 | req->timeout.head = link->last; |
| 6287 | link->last->flags |= REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6288 | } |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6289 | return 0; |
| 6290 | } |
| 6291 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6292 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6293 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6294 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6295 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6296 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6297 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6298 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6299 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6300 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6301 | /* |
| 6302 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6303 | * timeout event to be satisfied. If it isn't set, then this is |
| 6304 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6305 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6306 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6307 | entry = ctx->timeout_list.prev; |
| 6308 | goto add; |
| 6309 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6310 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6311 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 6312 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6313 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 6314 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 6315 | * This is safe because ->completion_lock is held, and submissions |
| 6316 | * and completions are never mixed in the same ->completion_lock section. |
| 6317 | */ |
| 6318 | ctx->cq_last_tm_flush = tail; |
| 6319 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6320 | /* |
| 6321 | * Insertion sort, ensuring the first entry in the list is always |
| 6322 | * the one we need first. |
| 6323 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6324 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6325 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 6326 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6327 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6328 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6329 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6330 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 6331 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6332 | break; |
| 6333 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6334 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6335 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6336 | data->timer.function = io_timeout_fn; |
| 6337 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6338 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
| 6341 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6342 | struct io_cancel_data { |
| 6343 | struct io_ring_ctx *ctx; |
| 6344 | u64 user_data; |
| 6345 | }; |
| 6346 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6347 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6348 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6349 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6350 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6351 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6352 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6353 | } |
| 6354 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6355 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 6356 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6357 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6358 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6359 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6360 | int ret = 0; |
| 6361 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6362 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 6363 | return -ENOENT; |
| 6364 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6365 | 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] | 6366 | switch (cancel_ret) { |
| 6367 | case IO_WQ_CANCEL_OK: |
| 6368 | ret = 0; |
| 6369 | break; |
| 6370 | case IO_WQ_CANCEL_RUNNING: |
| 6371 | ret = -EALREADY; |
| 6372 | break; |
| 6373 | case IO_WQ_CANCEL_NOTFOUND: |
| 6374 | ret = -ENOENT; |
| 6375 | break; |
| 6376 | } |
| 6377 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6378 | return ret; |
| 6379 | } |
| 6380 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6381 | 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] | 6382 | { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6383 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6384 | int ret; |
| 6385 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6386 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6387 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6388 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Jens Axboe | ccbf726 | 2022-01-18 19:11:11 -0700 | [diff] [blame] | 6389 | /* |
| 6390 | * Fall-through even for -EALREADY, as we may have poll armed |
| 6391 | * that need unarming. |
| 6392 | */ |
| 6393 | if (!ret) |
| 6394 | return 0; |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6395 | |
| 6396 | spin_lock(&ctx->completion_lock); |
Jens Axboe | ccbf726 | 2022-01-18 19:11:11 -0700 | [diff] [blame] | 6397 | ret = io_poll_cancel(ctx, sqe_addr, false); |
| 6398 | if (ret != -ENOENT) |
| 6399 | goto out; |
| 6400 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6401 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6402 | ret = io_timeout_cancel(ctx, sqe_addr); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6403 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6404 | out: |
| 6405 | spin_unlock(&ctx->completion_lock); |
| 6406 | return ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6407 | } |
| 6408 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6409 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6410 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6411 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6412 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6413 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6414 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6415 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6416 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
| 6417 | sqe->splice_fd_in) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6418 | return -EINVAL; |
| 6419 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6420 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6421 | return 0; |
| 6422 | } |
| 6423 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6424 | 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] | 6425 | { |
| 6426 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6427 | u64 sqe_addr = req->cancel.addr; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6428 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6429 | struct io_tctx_node *node; |
| 6430 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6431 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6432 | ret = io_try_cancel_userdata(req, sqe_addr); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6433 | if (ret != -ENOENT) |
| 6434 | goto done; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6435 | |
| 6436 | /* slow path, try all io-wq's */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6437 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6438 | ret = -ENOENT; |
| 6439 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 6440 | struct io_uring_task *tctx = node->task->io_uring; |
| 6441 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6442 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 6443 | if (ret != -ENOENT) |
| 6444 | break; |
| 6445 | } |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6446 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6447 | done: |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6448 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6449 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6450 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6451 | return 0; |
| 6452 | } |
| 6453 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6454 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6455 | const struct io_uring_sqe *sqe) |
| 6456 | { |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6457 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6458 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6459 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6460 | return -EINVAL; |
| 6461 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6462 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6463 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6464 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6465 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6466 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6467 | return 0; |
| 6468 | } |
| 6469 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6470 | 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] | 6471 | { |
| 6472 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6473 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6474 | struct io_uring_rsrc_update2 up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6475 | int ret; |
| 6476 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6477 | up.offset = req->rsrc_update.offset; |
| 6478 | up.data = req->rsrc_update.arg; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6479 | up.nr = 0; |
| 6480 | up.tags = 0; |
Colin Ian King | 615cee4 | 2021-04-26 10:47:35 +0100 | [diff] [blame] | 6481 | up.resv = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6482 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6483 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 6484 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 6485 | &up, req->rsrc_update.nr_args); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6486 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6487 | |
| 6488 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6489 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6490 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6491 | return 0; |
| 6492 | } |
| 6493 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6494 | 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] | 6495 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6496 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6497 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6498 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6499 | case IORING_OP_READV: |
| 6500 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6501 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6502 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6503 | case IORING_OP_WRITEV: |
| 6504 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6505 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6506 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6507 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6508 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6509 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6510 | return io_poll_update_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6511 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6512 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6513 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6514 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6515 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6516 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6517 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6518 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6519 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6520 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6521 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6522 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6523 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6524 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6525 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6526 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6527 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6528 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6529 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6530 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6531 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6532 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6533 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6534 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6535 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6536 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6537 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6538 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6539 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6540 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6541 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6542 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6543 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6544 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6545 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6546 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6547 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6548 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6549 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6550 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6551 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6552 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6553 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6554 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6555 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6556 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6557 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6558 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6559 | case IORING_OP_SHUTDOWN: |
| 6560 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6561 | case IORING_OP_RENAMEAT: |
| 6562 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6563 | case IORING_OP_UNLINKAT: |
| 6564 | return io_unlinkat_prep(req, sqe); |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6565 | case IORING_OP_MKDIRAT: |
| 6566 | return io_mkdirat_prep(req, sqe); |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6567 | case IORING_OP_SYMLINKAT: |
| 6568 | return io_symlinkat_prep(req, sqe); |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6569 | case IORING_OP_LINKAT: |
| 6570 | return io_linkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6571 | } |
| 6572 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6573 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6574 | req->opcode); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 6575 | return -EINVAL; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6576 | } |
| 6577 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6578 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6579 | { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6580 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 6581 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6582 | if (WARN_ON_ONCE(req_has_async_data(req))) |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6583 | return -EFAULT; |
| 6584 | if (io_alloc_async_data(req)) |
| 6585 | return -EAGAIN; |
| 6586 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6587 | switch (req->opcode) { |
| 6588 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6589 | return io_rw_prep_async(req, READ); |
| 6590 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6591 | return io_rw_prep_async(req, WRITE); |
| 6592 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6593 | return io_sendmsg_prep_async(req); |
| 6594 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6595 | return io_recvmsg_prep_async(req); |
| 6596 | case IORING_OP_CONNECT: |
| 6597 | return io_connect_prep_async(req); |
| 6598 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6599 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 6600 | req->opcode); |
| 6601 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6602 | } |
| 6603 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6604 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6605 | { |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6606 | u32 seq = req->ctx->cached_sq_head; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6607 | |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6608 | /* need original cached_sq_head, but it was increased for each req */ |
| 6609 | io_for_each_link(req, req) |
| 6610 | seq--; |
| 6611 | return seq; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6612 | } |
| 6613 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 6614 | static __cold void io_drain_req(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6615 | { |
| 6616 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6617 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6618 | int ret; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6619 | u32 seq = io_get_sequence(req); |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 6620 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6621 | /* Still need defer if there is pending req in defer list. */ |
Hao Xu | e302f10 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6622 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 5e37126 | 2021-09-24 22:00:04 +0100 | [diff] [blame] | 6623 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
Hao Xu | e302f10 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6624 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6625 | queue: |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6626 | ctx->drain_active = false; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6627 | io_req_task_queue(req); |
| 6628 | return; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6629 | } |
Hao Xu | e302f10 | 2021-11-25 17:21:02 +0800 | [diff] [blame] | 6630 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6631 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6632 | ret = io_req_prep_async(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6633 | if (ret) { |
| 6634 | fail: |
| 6635 | io_req_complete_failed(req, ret); |
| 6636 | return; |
| 6637 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6638 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6639 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6640 | if (!de) { |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6641 | ret = -ENOMEM; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6642 | goto fail; |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6643 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6644 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6645 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6646 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6647 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6648 | kfree(de); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6649 | goto queue; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6650 | } |
| 6651 | |
| 6652 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6653 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6654 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6655 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6656 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6657 | } |
| 6658 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 6659 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6660 | { |
Pavel Begunkov | d1fd1c2 | 2021-12-05 14:37:58 +0000 | [diff] [blame] | 6661 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 6662 | io_put_kbuf(req); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6663 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6664 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6665 | switch (req->opcode) { |
| 6666 | case IORING_OP_READV: |
| 6667 | case IORING_OP_READ_FIXED: |
| 6668 | case IORING_OP_READ: |
| 6669 | case IORING_OP_WRITEV: |
| 6670 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6671 | case IORING_OP_WRITE: { |
| 6672 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 1dacb4d | 2021-06-17 18:14:03 +0100 | [diff] [blame] | 6673 | |
| 6674 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6675 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6676 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6677 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6678 | case IORING_OP_SENDMSG: { |
| 6679 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6680 | |
| 6681 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6682 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6683 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6684 | case IORING_OP_SPLICE: |
| 6685 | case IORING_OP_TEE: |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 6686 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
| 6687 | io_put_file(req->splice.file_in); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6688 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6689 | case IORING_OP_OPENAT: |
| 6690 | case IORING_OP_OPENAT2: |
| 6691 | if (req->open.filename) |
| 6692 | putname(req->open.filename); |
| 6693 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6694 | case IORING_OP_RENAMEAT: |
| 6695 | putname(req->rename.oldpath); |
| 6696 | putname(req->rename.newpath); |
| 6697 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6698 | case IORING_OP_UNLINKAT: |
| 6699 | putname(req->unlink.filename); |
| 6700 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6701 | case IORING_OP_MKDIRAT: |
| 6702 | putname(req->mkdir.filename); |
| 6703 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6704 | case IORING_OP_SYMLINKAT: |
| 6705 | putname(req->symlink.oldpath); |
| 6706 | putname(req->symlink.newpath); |
| 6707 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6708 | case IORING_OP_LINKAT: |
| 6709 | putname(req->hardlink.oldpath); |
| 6710 | putname(req->hardlink.newpath); |
| 6711 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6712 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6713 | } |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 6714 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 6715 | kfree(req->apoll->double_poll); |
| 6716 | kfree(req->apoll); |
| 6717 | req->apoll = NULL; |
| 6718 | } |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6719 | if (req->flags & REQ_F_INFLIGHT) { |
| 6720 | struct io_uring_task *tctx = req->task->io_uring; |
| 6721 | |
| 6722 | atomic_dec(&tctx->inflight_tracked); |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6723 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6724 | if (req->flags & REQ_F_CREDS) |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 6725 | put_cred(req->creds); |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6726 | if (req->flags & REQ_F_ASYNC_DATA) { |
| 6727 | kfree(req->async_data); |
| 6728 | req->async_data = NULL; |
| 6729 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6730 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6731 | } |
| 6732 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6733 | 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] | 6734 | { |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6735 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6736 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6737 | |
Pavel Begunkov | 6878b40 | 2021-09-24 21:59:41 +0100 | [diff] [blame] | 6738 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 6739 | creds = override_creds(req->creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6740 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 6741 | if (!io_op_defs[req->opcode].audit_skip) |
| 6742 | audit_uring_entry(req->opcode); |
| 6743 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6744 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6745 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6746 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6747 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6748 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6749 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6750 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6751 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6752 | break; |
| 6753 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6754 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6755 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6756 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6757 | break; |
| 6758 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6759 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6760 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6761 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6762 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6763 | break; |
| 6764 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6765 | ret = io_poll_update(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6766 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6767 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6768 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6769 | break; |
| 6770 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6771 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6772 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6773 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6774 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6775 | break; |
| 6776 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6777 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6778 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6779 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6780 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6781 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6782 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6783 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6784 | break; |
| 6785 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6786 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6787 | break; |
| 6788 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6789 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6790 | break; |
| 6791 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6792 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6793 | break; |
| 6794 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6795 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6796 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6797 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6798 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6799 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6800 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6801 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6802 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6803 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6804 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6805 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6806 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6807 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6808 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6809 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6810 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6811 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6812 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6813 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6814 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6815 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6816 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6817 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6818 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6819 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6820 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6821 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6822 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6823 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6824 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6825 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6826 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6827 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6828 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6829 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6830 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6831 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6832 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6833 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6834 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6835 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6836 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6837 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6838 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6839 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6840 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6841 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6842 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6843 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6844 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6845 | case IORING_OP_MKDIRAT: |
| 6846 | ret = io_mkdirat(req, issue_flags); |
| 6847 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6848 | case IORING_OP_SYMLINKAT: |
| 6849 | ret = io_symlinkat(req, issue_flags); |
| 6850 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6851 | case IORING_OP_LINKAT: |
| 6852 | ret = io_linkat(req, issue_flags); |
| 6853 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6854 | default: |
| 6855 | ret = -EINVAL; |
| 6856 | break; |
| 6857 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6858 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 6859 | if (!io_op_defs[req->opcode].audit_skip) |
| 6860 | audit_uring_exit(!ret, ret); |
| 6861 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6862 | if (creds) |
| 6863 | revert_creds(creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6864 | if (ret) |
| 6865 | return ret; |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6866 | /* 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] | 6867 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
Pavel Begunkov | 9882131 | 2021-10-15 17:09:12 +0100 | [diff] [blame] | 6868 | io_iopoll_req_issued(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6869 | |
| 6870 | return 0; |
| 6871 | } |
| 6872 | |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 6873 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
| 6874 | { |
| 6875 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6876 | |
| 6877 | req = io_put_req_find_next(req); |
| 6878 | return req ? &req->work : NULL; |
| 6879 | } |
| 6880 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6881 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6882 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6883 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6884 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
| 6885 | bool needs_poll = false; |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6886 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6887 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6888 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 6889 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
| 6890 | if (!(req->flags & REQ_F_REFCOUNT)) |
| 6891 | __io_req_set_refcount(req, 2); |
| 6892 | else |
| 6893 | req_ref_get(req); |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6894 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6895 | timeout = io_prep_linked_timeout(req); |
| 6896 | if (timeout) |
| 6897 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6898 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6899 | /* 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] | 6900 | if (work->flags & IO_WQ_WORK_CANCEL) { |
| 6901 | io_req_task_queue_fail(req, -ECANCELED); |
| 6902 | return; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6903 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6904 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6905 | if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | afb7f56f | 2021-10-23 12:13:59 +0100 | [diff] [blame] | 6906 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 6907 | bool opcode_poll = def->pollin || def->pollout; |
| 6908 | |
| 6909 | if (opcode_poll && file_can_poll(req->file)) { |
| 6910 | needs_poll = true; |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6911 | issue_flags |= IO_URING_F_NONBLOCK; |
Pavel Begunkov | afb7f56f | 2021-10-23 12:13:59 +0100 | [diff] [blame] | 6912 | } |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6913 | } |
Hao Xu | 90fa028 | 2021-10-18 21:34:45 +0800 | [diff] [blame] | 6914 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6915 | do { |
| 6916 | ret = io_issue_sqe(req, issue_flags); |
| 6917 | if (ret != -EAGAIN) |
| 6918 | break; |
| 6919 | /* |
| 6920 | * We can get EAGAIN for iopolled IO even though we're |
| 6921 | * forcing a sync submission from here, since we can't |
| 6922 | * wait for request slots on the block side. |
| 6923 | */ |
| 6924 | if (!needs_poll) { |
| 6925 | cond_resched(); |
| 6926 | continue; |
Hao Xu | 90fa028 | 2021-10-18 21:34:45 +0800 | [diff] [blame] | 6927 | } |
| 6928 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6929 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
| 6930 | return; |
| 6931 | /* aborted or ready, in either case retry blocking */ |
| 6932 | needs_poll = false; |
| 6933 | issue_flags &= ~IO_URING_F_NONBLOCK; |
| 6934 | } while (1); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6935 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6936 | /* avoid locking problems by failing it from a clean context */ |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6937 | if (ret) |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6938 | io_req_task_queue_fail(req, ret); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6939 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6940 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6941 | 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] | 6942 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6943 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 6944 | return &table->files[i]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6947 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6948 | int index) |
| 6949 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6950 | 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] | 6951 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6952 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6953 | } |
| 6954 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6955 | 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] | 6956 | { |
| 6957 | unsigned long file_ptr = (unsigned long) file; |
| 6958 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 6959 | file_ptr |= io_file_get_flags(file); |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6960 | file_slot->file_ptr = file_ptr; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6961 | } |
| 6962 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6963 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
| 6964 | struct io_kiocb *req, int fd) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6965 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6966 | struct file *file; |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6967 | unsigned long file_ptr; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6968 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6969 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
| 6970 | return NULL; |
| 6971 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6972 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
| 6973 | file = (struct file *) (file_ptr & FFS_MASK); |
| 6974 | file_ptr &= ~FFS_MASK; |
| 6975 | /* mask in overlapping REQ_F and FFS bits */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 6976 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
Pavel Begunkov | a46be97 | 2021-10-09 23:14:40 +0100 | [diff] [blame] | 6977 | io_req_set_rsrc_node(req, ctx); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6978 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6979 | } |
| 6980 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6981 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6982 | struct io_kiocb *req, int fd) |
| 6983 | { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6984 | struct file *file = fget(fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6985 | |
| 6986 | trace_io_uring_file_get(ctx, fd); |
| 6987 | |
| 6988 | /* we don't allow fixed io_uring files */ |
| 6989 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6990 | io_req_track_inflight(req); |
| 6991 | return file; |
| 6992 | } |
| 6993 | |
| 6994 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6995 | struct io_kiocb *req, int fd, bool fixed) |
| 6996 | { |
| 6997 | if (fixed) |
| 6998 | return io_file_get_fixed(ctx, req, fd); |
| 6999 | else |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7000 | return io_file_get_normal(ctx, req, fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7001 | } |
| 7002 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7003 | 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] | 7004 | { |
| 7005 | struct io_kiocb *prev = req->timeout.prev; |
Pavel Begunkov | 617a894 | 2021-11-26 14:38:14 +0000 | [diff] [blame] | 7006 | int ret = -ENOENT; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7007 | |
| 7008 | if (prev) { |
Pavel Begunkov | 617a894 | 2021-11-26 14:38:14 +0000 | [diff] [blame] | 7009 | if (!(req->task->flags & PF_EXITING)) |
| 7010 | ret = io_try_cancel_userdata(req, prev->user_data); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 7011 | io_req_complete_post(req, ret ?: -ETIME, 0); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7012 | io_put_req(prev); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7013 | } else { |
| 7014 | io_req_complete_post(req, -ETIME, 0); |
| 7015 | } |
| 7016 | } |
| 7017 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7018 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 7019 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 7020 | struct io_timeout_data *data = container_of(timer, |
| 7021 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 7022 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7023 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7024 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7025 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7026 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 7027 | prev = req->timeout.head; |
| 7028 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7029 | |
| 7030 | /* |
| 7031 | * We don't expect the list to be empty, that will only happen if we |
| 7032 | * race with the completion of the linked work. |
| 7033 | */ |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 7034 | if (prev) { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 7035 | io_remove_next_linked(prev); |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 7036 | if (!req_ref_inc_not_zero(prev)) |
| 7037 | prev = NULL; |
| 7038 | } |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 7039 | list_del(&req->timeout.list); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7040 | req->timeout.prev = prev; |
| 7041 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7042 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7043 | req->io_task_work.func = io_req_task_link_timeout; |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 7044 | io_req_task_work_add(req, false); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7045 | return HRTIMER_NORESTART; |
| 7046 | } |
| 7047 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 7048 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7049 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 7050 | struct io_ring_ctx *ctx = req->ctx; |
| 7051 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7052 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 7053 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 7054 | * If the back reference is NULL, then our linked request finished |
| 7055 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 7056 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 7057 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 7058 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 7059 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 7060 | data->timer.function = io_link_timeout_fn; |
| 7061 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 7062 | data->mode); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 7063 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7064 | } |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 7065 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7066 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 7067 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 7068 | } |
| 7069 | |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 7070 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
| 7071 | __must_hold(&req->ctx->uring_lock) |
| 7072 | { |
| 7073 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
| 7074 | |
| 7075 | switch (io_arm_poll_handler(req)) { |
| 7076 | case IO_APOLL_READY: |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 7077 | io_req_task_queue(req); |
| 7078 | break; |
| 7079 | case IO_APOLL_ABORTED: |
| 7080 | /* |
| 7081 | * Queued up for async execution, worker will release |
| 7082 | * submit reference when the iocb is actually submitted. |
| 7083 | */ |
| 7084 | io_queue_async_work(req, NULL); |
| 7085 | break; |
| 7086 | } |
| 7087 | |
| 7088 | if (linked_timeout) |
| 7089 | io_queue_linked_timeout(linked_timeout); |
| 7090 | } |
| 7091 | |
| 7092 | static inline void __io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7093 | __must_hold(&req->ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7094 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7095 | struct io_kiocb *linked_timeout; |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 7096 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7097 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7098 | 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] | 7099 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 7100 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
| 7101 | io_req_add_compl_list(req); |
Pavel Begunkov | d9f9d28 | 2021-09-24 22:00:00 +0100 | [diff] [blame] | 7102 | return; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 7103 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 7104 | /* |
| 7105 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 7106 | * doesn't support non-blocking read/write attempts |
| 7107 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7108 | if (likely(!ret)) { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7109 | linked_timeout = io_prep_linked_timeout(req); |
| 7110 | if (linked_timeout) |
| 7111 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7112 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 7113 | io_queue_sqe_arm_apoll(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 7114 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 7115 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7116 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7117 | } |
| 7118 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7119 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7120 | __must_hold(&req->ctx->uring_lock) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7121 | { |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7122 | if (req->flags & REQ_F_FAIL) { |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 7123 | io_req_complete_fail_submit(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 7124 | } else if (unlikely(req->ctx->drain_active)) { |
| 7125 | io_drain_req(req); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 7126 | } else { |
| 7127 | int ret = io_req_prep_async(req); |
| 7128 | |
| 7129 | if (unlikely(ret)) |
| 7130 | io_req_complete_failed(req, ret); |
| 7131 | else |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7132 | io_queue_async_work(req, NULL); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 7133 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7134 | } |
| 7135 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7136 | static inline void io_queue_sqe(struct io_kiocb *req) |
| 7137 | __must_hold(&req->ctx->uring_lock) |
| 7138 | { |
| 7139 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) |
| 7140 | __io_queue_sqe(req); |
| 7141 | else |
| 7142 | io_queue_sqe_fallback(req); |
| 7143 | } |
| 7144 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7145 | /* |
| 7146 | * Check SQE restrictions (opcode and flags). |
| 7147 | * |
| 7148 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 7149 | */ |
| 7150 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 7151 | struct io_kiocb *req, |
| 7152 | unsigned int sqe_flags) |
| 7153 | { |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7154 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 7155 | return false; |
| 7156 | |
| 7157 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 7158 | ctx->restrictions.sqe_flags_required) |
| 7159 | return false; |
| 7160 | |
| 7161 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 7162 | ctx->restrictions.sqe_flags_required)) |
| 7163 | return false; |
| 7164 | |
| 7165 | return true; |
| 7166 | } |
| 7167 | |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7168 | static void io_init_req_drain(struct io_kiocb *req) |
| 7169 | { |
| 7170 | struct io_ring_ctx *ctx = req->ctx; |
| 7171 | struct io_kiocb *head = ctx->submit_state.link.head; |
| 7172 | |
| 7173 | ctx->drain_active = true; |
| 7174 | if (head) { |
| 7175 | /* |
| 7176 | * If we need to drain a request in the middle of a link, drain |
| 7177 | * the head request and the next request/link after the current |
| 7178 | * link. Considering sequential execution of links, |
Hao Xu | b6c7db3 | 2021-11-25 17:21:03 +0800 | [diff] [blame] | 7179 | * REQ_F_IO_DRAIN will be maintained for every request of our |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7180 | * link. |
| 7181 | */ |
Hao Xu | b6c7db3 | 2021-11-25 17:21:03 +0800 | [diff] [blame] | 7182 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7183 | ctx->drain_next = true; |
| 7184 | } |
| 7185 | } |
| 7186 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7187 | 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] | 7188 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7189 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7190 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7191 | unsigned int sqe_flags; |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7192 | int personality; |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7193 | u8 opcode; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7194 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 7195 | /* req is partially pre-initialised, see io_preinit_req() */ |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7196 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7197 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 7198 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7199 | req->user_data = READ_ONCE(sqe->user_data); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7200 | req->file = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7201 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 7202 | req->task = current; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7203 | |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7204 | if (unlikely(opcode >= IORING_OP_LAST)) { |
| 7205 | req->opcode = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7206 | return -EINVAL; |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7207 | } |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7208 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
| 7209 | /* enforce forwards compatibility on users */ |
| 7210 | if (sqe_flags & ~SQE_VALID_FLAGS) |
| 7211 | return -EINVAL; |
| 7212 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7213 | !io_op_defs[opcode].buffer_select) |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7214 | return -EOPNOTSUPP; |
Pavel Begunkov | 5562a8d | 2021-11-10 15:49:34 +0000 | [diff] [blame] | 7215 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
| 7216 | ctx->drain_disabled = true; |
| 7217 | if (sqe_flags & IOSQE_IO_DRAIN) { |
| 7218 | if (ctx->drain_disabled) |
| 7219 | return -EOPNOTSUPP; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7220 | io_init_req_drain(req); |
Pavel Begunkov | 5562a8d | 2021-11-10 15:49:34 +0000 | [diff] [blame] | 7221 | } |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7222 | } |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7223 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { |
| 7224 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) |
| 7225 | return -EACCES; |
| 7226 | /* knock it to the slow queue path, will be drained there */ |
| 7227 | if (ctx->drain_active) |
| 7228 | req->flags |= REQ_F_FORCE_ASYNC; |
| 7229 | /* if there is no link, we're at "next" request and need to drain */ |
| 7230 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { |
| 7231 | ctx->drain_next = false; |
| 7232 | ctx->drain_active = true; |
Hao Xu | b6c7db3 | 2021-11-25 17:21:03 +0800 | [diff] [blame] | 7233 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7234 | } |
| 7235 | } |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7236 | |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7237 | if (io_op_defs[opcode].needs_file) { |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7238 | struct io_submit_state *state = &ctx->submit_state; |
| 7239 | |
| 7240 | /* |
| 7241 | * Plug now if we have more than 2 IO left after this, and the |
| 7242 | * target is potentially a read/write to block based storage. |
| 7243 | */ |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7244 | if (state->need_plug && io_op_defs[opcode].plug) { |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7245 | state->plug_started = true; |
| 7246 | state->need_plug = false; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 7247 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7248 | } |
| 7249 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7250 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7251 | (sqe_flags & IOSQE_FIXED_FILE)); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 7252 | if (unlikely(!req->file)) |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7253 | return -EBADF; |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7254 | } |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 7255 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7256 | personality = READ_ONCE(sqe->personality); |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7257 | if (personality) { |
Linus Torvalds | cdab10b | 2021-11-01 21:06:18 -0700 | [diff] [blame] | 7258 | int ret; |
| 7259 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7260 | req->creds = xa_load(&ctx->personalities, personality); |
| 7261 | if (!req->creds) |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7262 | return -EINVAL; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7263 | get_cred(req->creds); |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 7264 | ret = security_uring_override_creds(req->creds); |
| 7265 | if (ret) { |
| 7266 | put_cred(req->creds); |
| 7267 | return ret; |
| 7268 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7269 | req->flags |= REQ_F_CREDS; |
| 7270 | } |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7271 | |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7272 | return io_req_prep(req, sqe); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7273 | } |
| 7274 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7275 | 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] | 7276 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7277 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7278 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7279 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7280 | int ret; |
| 7281 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7282 | ret = io_init_req(ctx, req, sqe); |
| 7283 | if (unlikely(ret)) { |
Jens Axboe | a87acfd | 2021-09-11 16:04:50 -0600 | [diff] [blame] | 7284 | trace_io_uring_req_failed(sqe, ret); |
| 7285 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7286 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7287 | if (link->head) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7288 | /* |
| 7289 | * we can judge a link req is failed or cancelled by if |
| 7290 | * REQ_F_FAIL is set, but the head is an exception since |
| 7291 | * it may be set REQ_F_FAIL because of other req's failure |
| 7292 | * so let's leverage req->result to distinguish if a head |
| 7293 | * is set REQ_F_FAIL because of its failure or other req's |
| 7294 | * failure so that we can set the correct ret code for it. |
| 7295 | * init result here to avoid affecting the normal path. |
| 7296 | */ |
| 7297 | if (!(link->head->flags & REQ_F_FAIL)) |
| 7298 | req_fail_link_node(link->head, -ECANCELED); |
| 7299 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7300 | /* |
| 7301 | * the current req is a normal req, we should return |
| 7302 | * error and thus break the submittion loop. |
| 7303 | */ |
| 7304 | io_req_complete_failed(req, ret); |
| 7305 | return ret; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7306 | } |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7307 | req_fail_link_node(req, ret); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7308 | } |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7309 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7310 | /* don't need @sqe from now on */ |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 7311 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
| 7312 | req->flags, true, |
| 7313 | ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7314 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7315 | /* |
| 7316 | * If we already have a head request, queue this one for async |
| 7317 | * submittal once the head completes. If we don't have a head but |
| 7318 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 7319 | * submitted sync once the chain is complete. If none of those |
| 7320 | * conditions are true (normal request), then just queue it. |
| 7321 | */ |
| 7322 | if (link->head) { |
| 7323 | struct io_kiocb *head = link->head; |
| 7324 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7325 | if (!(req->flags & REQ_F_FAIL)) { |
| 7326 | ret = io_req_prep_async(req); |
| 7327 | if (unlikely(ret)) { |
| 7328 | req_fail_link_node(req, ret); |
| 7329 | if (!(head->flags & REQ_F_FAIL)) |
| 7330 | req_fail_link_node(head, -ECANCELED); |
| 7331 | } |
| 7332 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7333 | trace_io_uring_link(ctx, req, head); |
| 7334 | link->last->link = req; |
| 7335 | link->last = req; |
| 7336 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7337 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
| 7338 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7339 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7340 | link->head = NULL; |
| 7341 | req = head; |
| 7342 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
| 7343 | link->head = req; |
| 7344 | link->last = req; |
| 7345 | return 0; |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7346 | } |
| 7347 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7348 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7349 | return 0; |
| 7350 | } |
| 7351 | |
| 7352 | /* |
| 7353 | * Batched submission is done, ensure local IO is flushed out. |
| 7354 | */ |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7355 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7356 | { |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7357 | struct io_submit_state *state = &ctx->submit_state; |
| 7358 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7359 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7360 | io_queue_sqe(state->link.head); |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7361 | /* flush only after queuing links as they can generate completions */ |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 7362 | io_submit_flush_completions(ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 7363 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7364 | blk_finish_plug(&state->plug); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7365 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7366 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7367 | /* |
| 7368 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7369 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7370 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7371 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7372 | { |
| 7373 | state->plug_started = false; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7374 | state->need_plug = max_ios > 2; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 7375 | state->submit_nr = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7376 | /* set only head, no need to init link_last in advance */ |
| 7377 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7378 | } |
| 7379 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7380 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 7381 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7382 | struct io_rings *rings = ctx->rings; |
| 7383 | |
| 7384 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7385 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7386 | * since once we write the new head, the application could |
| 7387 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 7388 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7389 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 7390 | } |
| 7391 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7392 | /* |
Fam Zheng | dd9ae8a | 2021-06-04 17:42:56 +0100 | [diff] [blame] | 7393 | * 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] | 7394 | * that is mapped by userspace. This means that care needs to be taken to |
| 7395 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 7396 | * being a good citizen. If members of the sqe are validated and then later |
| 7397 | * 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] | 7398 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7399 | */ |
| 7400 | 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] | 7401 | { |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 7402 | unsigned head, mask = ctx->sq_entries - 1; |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7403 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7404 | |
| 7405 | /* |
| 7406 | * The cached sq head (or cq tail) serves two purposes: |
| 7407 | * |
| 7408 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 7409 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7410 | * 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] | 7411 | * though the application is the one updating it. |
| 7412 | */ |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7413 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 7414 | if (likely(head < ctx->sq_entries)) |
| 7415 | return &ctx->sq_sqes[head]; |
| 7416 | |
| 7417 | /* drop invalid entries */ |
Pavel Begunkov | 15641e4 | 2021-06-14 23:37:24 +0100 | [diff] [blame] | 7418 | ctx->cq_extra--; |
| 7419 | WRITE_ONCE(ctx->rings->sq_dropped, |
| 7420 | READ_ONCE(ctx->rings->sq_dropped) + 1); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 7421 | return NULL; |
| 7422 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 7423 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7424 | 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] | 7425 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7426 | { |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7427 | unsigned int entries = io_sqring_entries(ctx); |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7428 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7429 | |
Pavel Begunkov | 51d48da | 2021-10-04 20:02:47 +0100 | [diff] [blame] | 7430 | if (unlikely(!entries)) |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7431 | return 0; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 7432 | /* make sure SQ entry isn't read before tail */ |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7433 | nr = min3(nr, ctx->sq_entries, entries); |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 7434 | io_get_task_refs(nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7435 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 7436 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7437 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7438 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7439 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7440 | |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7441 | if (unlikely(!io_alloc_req_refill(ctx))) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7442 | if (!submitted) |
| 7443 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7444 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7445 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7446 | req = io_alloc_req(ctx); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7447 | sqe = io_get_sqe(ctx); |
| 7448 | if (unlikely(!sqe)) { |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 7449 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7450 | break; |
| 7451 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7452 | /* will complete beyond this point, count as submitted */ |
| 7453 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7454 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7455 | break; |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7456 | } while (submitted < nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7457 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7458 | if (unlikely(submitted != nr)) { |
| 7459 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7460 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7461 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 7462 | current->io_uring->cached_refs += unused; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7463 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7464 | |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7465 | io_submit_state_end(ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7466 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 7467 | io_commit_sqring(ctx); |
| 7468 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7469 | return submitted; |
| 7470 | } |
| 7471 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7472 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
| 7473 | { |
| 7474 | return READ_ONCE(sqd->state); |
| 7475 | } |
| 7476 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7477 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 7478 | { |
| 7479 | /* Tell userspace we may need a wakeup call */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7480 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7481 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7482 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7483 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7484 | } |
| 7485 | |
| 7486 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 7487 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7488 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7489 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7490 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7491 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7492 | } |
| 7493 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7494 | 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] | 7495 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7496 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 7497 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7498 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7499 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7500 | /* if we're handling multiple rings, cap submit size for fairness */ |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 7501 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
| 7502 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7503 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7504 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7505 | const struct cred *creds = NULL; |
| 7506 | |
| 7507 | if (ctx->sq_creds != current_cred()) |
| 7508 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7509 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7510 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7511 | if (!wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 7512 | io_do_iopoll(ctx, true); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7513 | |
Pavel Begunkov | 3b763ba | 2021-04-18 14:52:08 +0100 | [diff] [blame] | 7514 | /* |
| 7515 | * Don't submit if refs are dying, good for io_uring_register(), |
| 7516 | * but also it is relied upon by io_ring_exit_work() |
| 7517 | */ |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 7518 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 7519 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7520 | ret = io_submit_sqes(ctx, to_submit); |
| 7521 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7522 | |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7523 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7524 | wake_up(&ctx->sqo_sq_wait); |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7525 | if (creds) |
| 7526 | revert_creds(creds); |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7527 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7528 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7529 | return ret; |
| 7530 | } |
| 7531 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7532 | 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] | 7533 | { |
| 7534 | struct io_ring_ctx *ctx; |
| 7535 | unsigned sq_thread_idle = 0; |
| 7536 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 7537 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7538 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7539 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7540 | } |
| 7541 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7542 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
| 7543 | { |
| 7544 | bool did_sig = false; |
| 7545 | struct ksignal ksig; |
| 7546 | |
| 7547 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 7548 | signal_pending(current)) { |
| 7549 | mutex_unlock(&sqd->lock); |
| 7550 | if (signal_pending(current)) |
| 7551 | did_sig = get_signal(&ksig); |
| 7552 | cond_resched(); |
| 7553 | mutex_lock(&sqd->lock); |
| 7554 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7555 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7556 | } |
| 7557 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7558 | static int io_sq_thread(void *data) |
| 7559 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7560 | struct io_sq_data *sqd = data; |
| 7561 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7562 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7563 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7564 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7565 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 7566 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7567 | set_task_comm(current, buf); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7568 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7569 | if (sqd->sq_cpu != -1) |
| 7570 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 7571 | else |
| 7572 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 7573 | current->flags |= PF_NO_SETAFFINITY; |
| 7574 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 7575 | audit_alloc_kernel(current); |
| 7576 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7577 | mutex_lock(&sqd->lock); |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7578 | while (1) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7579 | bool cap_entries, sqt_spin = false; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7580 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7581 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
| 7582 | if (io_sqd_handle_event(sqd)) |
Pavel Begunkov | c7d9561 | 2021-04-13 11:43:00 +0100 | [diff] [blame] | 7583 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7584 | timeout = jiffies + sqd->sq_thread_idle; |
| 7585 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7586 | |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7587 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7588 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7589 | int ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7590 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7591 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7592 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7593 | } |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7594 | if (io_run_task_work()) |
| 7595 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7596 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7597 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7598 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7599 | if (sqt_spin) |
| 7600 | timeout = jiffies + sqd->sq_thread_idle; |
| 7601 | continue; |
| 7602 | } |
| 7603 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7604 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7605 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7606 | bool needs_sched = true; |
| 7607 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7608 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | aaa9f0f | 2021-05-16 22:58:01 +0100 | [diff] [blame] | 7609 | io_ring_set_wakeup_flag(ctx); |
| 7610 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7611 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7612 | !wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7613 | needs_sched = false; |
| 7614 | break; |
| 7615 | } |
| 7616 | if (io_sqring_entries(ctx)) { |
| 7617 | needs_sched = false; |
| 7618 | break; |
| 7619 | } |
| 7620 | } |
| 7621 | |
| 7622 | if (needs_sched) { |
| 7623 | mutex_unlock(&sqd->lock); |
| 7624 | schedule(); |
| 7625 | mutex_lock(&sqd->lock); |
| 7626 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7627 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7628 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7629 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7630 | |
| 7631 | finish_wait(&sqd->wait, &wait); |
| 7632 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7633 | } |
| 7634 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 7635 | io_uring_cancel_generic(true, sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7636 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7637 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7638 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7639 | io_run_task_work(); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 7640 | mutex_unlock(&sqd->lock); |
| 7641 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 7642 | audit_free(current); |
| 7643 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7644 | complete(&sqd->exited); |
| 7645 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7646 | } |
| 7647 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7648 | struct io_wait_queue { |
| 7649 | struct wait_queue_entry wq; |
| 7650 | struct io_ring_ctx *ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7651 | unsigned cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7652 | unsigned nr_timeouts; |
| 7653 | }; |
| 7654 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7655 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7656 | { |
| 7657 | struct io_ring_ctx *ctx = iowq->ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7658 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7659 | |
| 7660 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7661 | * 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] | 7662 | * started waiting. For timeouts, we always want to return to userspace, |
| 7663 | * regardless of event count. |
| 7664 | */ |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7665 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7666 | } |
| 7667 | |
| 7668 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7669 | int wake_flags, void *key) |
| 7670 | { |
| 7671 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7672 | wq); |
| 7673 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7674 | /* |
| 7675 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7676 | * the task, and the next invocation will do it. |
| 7677 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7678 | 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] | 7679 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7680 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7681 | } |
| 7682 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7683 | static int io_run_task_work_sig(void) |
| 7684 | { |
| 7685 | if (io_run_task_work()) |
| 7686 | return 1; |
| 7687 | if (!signal_pending(current)) |
| 7688 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 7689 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7690 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7691 | return -EINTR; |
| 7692 | } |
| 7693 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7694 | /* when returns >0, the caller should retry */ |
| 7695 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7696 | struct io_wait_queue *iowq, |
| 7697 | signed long *timeout) |
| 7698 | { |
| 7699 | int ret; |
| 7700 | |
| 7701 | /* make sure we run task_work before checking for signals */ |
| 7702 | ret = io_run_task_work_sig(); |
| 7703 | if (ret || io_should_wake(iowq)) |
| 7704 | return ret; |
| 7705 | /* let the caller flush overflows, retry */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7706 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7707 | return 1; |
| 7708 | |
| 7709 | *timeout = schedule_timeout(*timeout); |
| 7710 | return !*timeout ? -ETIME : 1; |
| 7711 | } |
| 7712 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7713 | /* |
| 7714 | * Wait until events become available, if we don't already have some. The |
| 7715 | * application must reap them itself, as they reside on the shared cq ring. |
| 7716 | */ |
| 7717 | 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] | 7718 | const sigset_t __user *sig, size_t sigsz, |
| 7719 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7720 | { |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7721 | struct io_wait_queue iowq; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7722 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7723 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7724 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7725 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7726 | do { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7727 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7728 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7729 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7730 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7731 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7732 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7733 | |
Xiaoguang Wang | 44df58d | 2021-09-14 22:38:52 +0800 | [diff] [blame] | 7734 | if (uts) { |
| 7735 | struct timespec64 ts; |
| 7736 | |
| 7737 | if (get_timespec64(&ts, uts)) |
| 7738 | return -EFAULT; |
| 7739 | timeout = timespec64_to_jiffies(&ts); |
| 7740 | } |
| 7741 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7742 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7743 | #ifdef CONFIG_COMPAT |
| 7744 | if (in_compat_syscall()) |
| 7745 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7746 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7747 | else |
| 7748 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7749 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7750 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7751 | if (ret) |
| 7752 | return ret; |
| 7753 | } |
| 7754 | |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7755 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
| 7756 | iowq.wq.private = current; |
| 7757 | INIT_LIST_HEAD(&iowq.wq.entry); |
| 7758 | iowq.ctx = ctx; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7759 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7760 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7761 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7762 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7763 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7764 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7765 | if (!io_cqring_overflow_flush(ctx)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7766 | ret = -EBUSY; |
| 7767 | break; |
| 7768 | } |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7769 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7770 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7771 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7772 | finish_wait(&ctx->cq_wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7773 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7774 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7775 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7776 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7777 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7778 | 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] | 7779 | } |
| 7780 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7781 | static void io_free_page_table(void **table, size_t size) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7782 | { |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7783 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7784 | |
| 7785 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7786 | kfree(table[i]); |
| 7787 | kfree(table); |
| 7788 | } |
| 7789 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7790 | static __cold void **io_alloc_page_table(size_t size) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7791 | { |
| 7792 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
| 7793 | size_t init_size = size; |
| 7794 | void **table; |
| 7795 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7796 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7797 | if (!table) |
| 7798 | return NULL; |
| 7799 | |
| 7800 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 27f6b31 | 2021-06-15 13:20:13 +0100 | [diff] [blame] | 7801 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7802 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7803 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7804 | if (!table[i]) { |
| 7805 | io_free_page_table(table, init_size); |
| 7806 | return NULL; |
| 7807 | } |
| 7808 | size -= this_size; |
| 7809 | } |
| 7810 | return table; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7811 | } |
| 7812 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7813 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7814 | { |
| 7815 | percpu_ref_exit(&ref_node->refs); |
| 7816 | kfree(ref_node); |
| 7817 | } |
| 7818 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7819 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7820 | { |
| 7821 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
| 7822 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
| 7823 | unsigned long flags; |
| 7824 | bool first_add = false; |
Dylan Yudaken | b36a205 | 2022-01-21 04:38:56 -0800 | [diff] [blame] | 7825 | unsigned long delay = HZ; |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7826 | |
| 7827 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); |
| 7828 | node->done = true; |
| 7829 | |
Dylan Yudaken | b36a205 | 2022-01-21 04:38:56 -0800 | [diff] [blame] | 7830 | /* if we are mid-quiesce then do not delay */ |
| 7831 | if (node->rsrc_data->quiesce) |
| 7832 | delay = 0; |
| 7833 | |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7834 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7835 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7836 | struct io_rsrc_node, node); |
| 7837 | /* recycle ref nodes in order */ |
| 7838 | if (!node->done) |
| 7839 | break; |
| 7840 | list_del(&node->node); |
| 7841 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
| 7842 | } |
| 7843 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); |
| 7844 | |
| 7845 | if (first_add) |
Dylan Yudaken | b36a205 | 2022-01-21 04:38:56 -0800 | [diff] [blame] | 7846 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7847 | } |
| 7848 | |
Usama Arif | f6133fb | 2022-01-27 14:04:44 +0000 | [diff] [blame] | 7849 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7850 | { |
| 7851 | struct io_rsrc_node *ref_node; |
| 7852 | |
| 7853 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7854 | if (!ref_node) |
| 7855 | return NULL; |
| 7856 | |
| 7857 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
| 7858 | 0, GFP_KERNEL)) { |
| 7859 | kfree(ref_node); |
| 7860 | return NULL; |
| 7861 | } |
| 7862 | INIT_LIST_HEAD(&ref_node->node); |
| 7863 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
| 7864 | ref_node->done = false; |
| 7865 | return ref_node; |
| 7866 | } |
| 7867 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7868 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 7869 | struct io_rsrc_data *data_to_kill) |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 7870 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7871 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7872 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 7873 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7874 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 7875 | io_rsrc_refs_drop(ctx); |
| 7876 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7877 | if (data_to_kill) { |
| 7878 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7879 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7880 | rsrc_node->rsrc_data = data_to_kill; |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7881 | spin_lock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7882 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7883 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7884 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7885 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7886 | percpu_ref_kill(&rsrc_node->refs); |
| 7887 | ctx->rsrc_node = NULL; |
| 7888 | } |
| 7889 | |
| 7890 | if (!ctx->rsrc_node) { |
| 7891 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 7892 | ctx->rsrc_backup_node = NULL; |
| 7893 | } |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7894 | } |
| 7895 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7896 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7897 | { |
| 7898 | if (ctx->rsrc_backup_node) |
| 7899 | return 0; |
Usama Arif | f6133fb | 2022-01-27 14:04:44 +0000 | [diff] [blame] | 7900 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7901 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 7902 | } |
| 7903 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7904 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
| 7905 | struct io_ring_ctx *ctx) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7906 | { |
| 7907 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7908 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 7909 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7910 | if (data->quiesce) |
| 7911 | return -ENXIO; |
| 7912 | |
| 7913 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7914 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7915 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7916 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7917 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7918 | io_rsrc_node_switch(ctx, data); |
| 7919 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7920 | /* kill initial ref, already quiesced if zero */ |
| 7921 | if (atomic_dec_and_test(&data->refs)) |
| 7922 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7923 | mutex_unlock(&ctx->uring_lock); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7924 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7925 | ret = wait_for_completion_interruptible(&data->done); |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7926 | if (!ret) { |
| 7927 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7928 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7929 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7930 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7931 | atomic_inc(&data->refs); |
| 7932 | /* wait for all works potentially completing data->done */ |
| 7933 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 7934 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7935 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7936 | ret = io_run_task_work_sig(); |
| 7937 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7938 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7939 | data->quiesce = false; |
| 7940 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7941 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7942 | } |
| 7943 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7944 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
| 7945 | { |
| 7946 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; |
| 7947 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; |
| 7948 | |
| 7949 | return &data->tags[table_idx][off]; |
| 7950 | } |
| 7951 | |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7952 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
| 7953 | { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7954 | size_t size = data->nr * sizeof(data->tags[0][0]); |
| 7955 | |
| 7956 | if (data->tags) |
| 7957 | io_free_page_table((void **)data->tags, size); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7958 | kfree(data); |
| 7959 | } |
| 7960 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7961 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
| 7962 | u64 __user *utags, unsigned nr, |
| 7963 | struct io_rsrc_data **pdata) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7964 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7965 | struct io_rsrc_data *data; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7966 | int ret = -ENOMEM; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7967 | unsigned i; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7968 | |
| 7969 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7970 | if (!data) |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7971 | return -ENOMEM; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7972 | 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] | 7973 | if (!data->tags) { |
| 7974 | kfree(data); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7975 | return -ENOMEM; |
| 7976 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7977 | |
| 7978 | data->nr = nr; |
| 7979 | data->ctx = ctx; |
| 7980 | data->do_put = do_put; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7981 | if (utags) { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7982 | ret = -EFAULT; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7983 | for (i = 0; i < nr; i++) { |
Colin Ian King | fdd1dc3 | 2021-06-15 14:00:11 +0100 | [diff] [blame] | 7984 | u64 *tag_slot = io_get_tag_slot(data, i); |
| 7985 | |
| 7986 | if (copy_from_user(tag_slot, &utags[i], |
| 7987 | sizeof(*tag_slot))) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7988 | goto fail; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7989 | } |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 7990 | } |
| 7991 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7992 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7993 | init_completion(&data->done); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7994 | *pdata = data; |
| 7995 | return 0; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7996 | fail: |
| 7997 | io_rsrc_data_free(data); |
| 7998 | return ret; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7999 | } |
| 8000 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8001 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
| 8002 | { |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 8003 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
| 8004 | GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8005 | return !!table->files; |
| 8006 | } |
| 8007 | |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8008 | static void io_free_file_tables(struct io_file_table *table) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8009 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8010 | kvfree(table->files); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 8011 | table->files = NULL; |
| 8012 | } |
| 8013 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8014 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 8015 | { |
| 8016 | #if defined(CONFIG_UNIX) |
| 8017 | if (ctx->ring_sock) { |
| 8018 | struct sock *sock = ctx->ring_sock->sk; |
| 8019 | struct sk_buff *skb; |
| 8020 | |
| 8021 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 8022 | kfree_skb(skb); |
| 8023 | } |
| 8024 | #else |
| 8025 | int i; |
| 8026 | |
| 8027 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8028 | struct file *file; |
| 8029 | |
| 8030 | file = io_file_from_index(ctx, i); |
| 8031 | if (file) |
| 8032 | fput(file); |
| 8033 | } |
| 8034 | #endif |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8035 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8036 | io_rsrc_data_free(ctx->file_data); |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 8037 | ctx->file_data = NULL; |
| 8038 | ctx->nr_user_files = 0; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 8039 | } |
| 8040 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 8041 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 8042 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 8043 | int ret; |
| 8044 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8045 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 8046 | return -ENXIO; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8047 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
| 8048 | if (!ret) |
| 8049 | __io_sqe_files_unregister(ctx); |
| 8050 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8051 | } |
| 8052 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8053 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8054 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8055 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8056 | WARN_ON_ONCE(sqd->thread == current); |
| 8057 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8058 | /* |
| 8059 | * Do the dance but not conditional clear_bit() because it'd race with |
| 8060 | * other threads incrementing park_pending and setting the bit. |
| 8061 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8062 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8063 | if (atomic_dec_return(&sqd->park_pending)) |
| 8064 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8065 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8066 | } |
| 8067 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 8068 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8069 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8070 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8071 | WARN_ON_ONCE(sqd->thread == current); |
| 8072 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8073 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8074 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8075 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8076 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 8077 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8078 | } |
| 8079 | |
| 8080 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 8081 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8082 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 8083 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8084 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8085 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 8086 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8087 | if (sqd->thread) |
| 8088 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8089 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8090 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8091 | } |
| 8092 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8093 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8094 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8095 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8096 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 8097 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8098 | io_sq_thread_stop(sqd); |
| 8099 | kfree(sqd); |
| 8100 | } |
| 8101 | } |
| 8102 | |
| 8103 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 8104 | { |
| 8105 | struct io_sq_data *sqd = ctx->sq_data; |
| 8106 | |
| 8107 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8108 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8109 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8110 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8111 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8112 | |
| 8113 | io_put_sq_data(sqd); |
| 8114 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8115 | } |
| 8116 | } |
| 8117 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8118 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 8119 | { |
| 8120 | struct io_ring_ctx *ctx_attach; |
| 8121 | struct io_sq_data *sqd; |
| 8122 | struct fd f; |
| 8123 | |
| 8124 | f = fdget(p->wq_fd); |
| 8125 | if (!f.file) |
| 8126 | return ERR_PTR(-ENXIO); |
| 8127 | if (f.file->f_op != &io_uring_fops) { |
| 8128 | fdput(f); |
| 8129 | return ERR_PTR(-EINVAL); |
| 8130 | } |
| 8131 | |
| 8132 | ctx_attach = f.file->private_data; |
| 8133 | sqd = ctx_attach->sq_data; |
| 8134 | if (!sqd) { |
| 8135 | fdput(f); |
| 8136 | return ERR_PTR(-EINVAL); |
| 8137 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8138 | if (sqd->task_tgid != current->tgid) { |
| 8139 | fdput(f); |
| 8140 | return ERR_PTR(-EPERM); |
| 8141 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8142 | |
| 8143 | refcount_inc(&sqd->refs); |
| 8144 | fdput(f); |
| 8145 | return sqd; |
| 8146 | } |
| 8147 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8148 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 8149 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8150 | { |
| 8151 | struct io_sq_data *sqd; |
| 8152 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8153 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8154 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 8155 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8156 | if (!IS_ERR(sqd)) { |
| 8157 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8158 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8159 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8160 | /* fall through for EPERM case, setup new sqd/task */ |
| 8161 | if (PTR_ERR(sqd) != -EPERM) |
| 8162 | return sqd; |
| 8163 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8164 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8165 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 8166 | if (!sqd) |
| 8167 | return ERR_PTR(-ENOMEM); |
| 8168 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8169 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8170 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8171 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8172 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8173 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8174 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8175 | return sqd; |
| 8176 | } |
| 8177 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8178 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8179 | /* |
| 8180 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 8181 | * the io_uring can be safely unregistered on process exit, even if we have |
| 8182 | * loops in the file referencing. |
| 8183 | */ |
| 8184 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 8185 | { |
| 8186 | struct sock *sk = ctx->ring_sock->sk; |
| 8187 | struct scm_fp_list *fpl; |
| 8188 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8189 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8190 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8191 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 8192 | if (!fpl) |
| 8193 | return -ENOMEM; |
| 8194 | |
| 8195 | skb = alloc_skb(0, GFP_KERNEL); |
| 8196 | if (!skb) { |
| 8197 | kfree(fpl); |
| 8198 | return -ENOMEM; |
| 8199 | } |
| 8200 | |
| 8201 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8202 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8203 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8204 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8205 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8206 | struct file *file = io_file_from_index(ctx, i + offset); |
| 8207 | |
| 8208 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8209 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8210 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8211 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 8212 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8213 | } |
| 8214 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8215 | if (nr_files) { |
| 8216 | fpl->max = SCM_MAX_FD; |
| 8217 | fpl->count = nr_files; |
| 8218 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8219 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8220 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 8221 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8222 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8223 | for (i = 0; i < nr_files; i++) |
| 8224 | fput(fpl->fp[i]); |
| 8225 | } else { |
| 8226 | kfree_skb(skb); |
| 8227 | kfree(fpl); |
| 8228 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8229 | |
| 8230 | return 0; |
| 8231 | } |
| 8232 | |
| 8233 | /* |
| 8234 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 8235 | * causes regular reference counting to break down. We rely on the UNIX |
| 8236 | * garbage collection to take care of this problem for us. |
| 8237 | */ |
| 8238 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8239 | { |
| 8240 | unsigned left, total; |
| 8241 | int ret = 0; |
| 8242 | |
| 8243 | total = 0; |
| 8244 | left = ctx->nr_user_files; |
| 8245 | while (left) { |
| 8246 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8247 | |
| 8248 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 8249 | if (ret) |
| 8250 | break; |
| 8251 | left -= this_files; |
| 8252 | total += this_files; |
| 8253 | } |
| 8254 | |
| 8255 | if (!ret) |
| 8256 | return 0; |
| 8257 | |
| 8258 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8259 | struct file *file = io_file_from_index(ctx, total); |
| 8260 | |
| 8261 | if (file) |
| 8262 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8263 | total++; |
| 8264 | } |
| 8265 | |
| 8266 | return ret; |
| 8267 | } |
| 8268 | #else |
| 8269 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8270 | { |
| 8271 | return 0; |
| 8272 | } |
| 8273 | #endif |
| 8274 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 8275 | 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] | 8276 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 8277 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8278 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8279 | struct sock *sock = ctx->ring_sock->sk; |
| 8280 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 8281 | struct sk_buff *skb; |
| 8282 | int i; |
| 8283 | |
| 8284 | __skb_queue_head_init(&list); |
| 8285 | |
| 8286 | /* |
| 8287 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 8288 | * remove this entry and rearrange the file array. |
| 8289 | */ |
| 8290 | skb = skb_dequeue(head); |
| 8291 | while (skb) { |
| 8292 | struct scm_fp_list *fp; |
| 8293 | |
| 8294 | fp = UNIXCB(skb).fp; |
| 8295 | for (i = 0; i < fp->count; i++) { |
| 8296 | int left; |
| 8297 | |
| 8298 | if (fp->fp[i] != file) |
| 8299 | continue; |
| 8300 | |
| 8301 | unix_notinflight(fp->user, fp->fp[i]); |
| 8302 | left = fp->count - 1 - i; |
| 8303 | if (left) { |
| 8304 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 8305 | left * sizeof(struct file *)); |
| 8306 | } |
| 8307 | fp->count--; |
| 8308 | if (!fp->count) { |
| 8309 | kfree_skb(skb); |
| 8310 | skb = NULL; |
| 8311 | } else { |
| 8312 | __skb_queue_tail(&list, skb); |
| 8313 | } |
| 8314 | fput(file); |
| 8315 | file = NULL; |
| 8316 | break; |
| 8317 | } |
| 8318 | |
| 8319 | if (!file) |
| 8320 | break; |
| 8321 | |
| 8322 | __skb_queue_tail(&list, skb); |
| 8323 | |
| 8324 | skb = skb_dequeue(head); |
| 8325 | } |
| 8326 | |
| 8327 | if (skb_peek(&list)) { |
| 8328 | spin_lock_irq(&head->lock); |
| 8329 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 8330 | __skb_queue_tail(head, skb); |
| 8331 | spin_unlock_irq(&head->lock); |
| 8332 | } |
| 8333 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8334 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8335 | #endif |
| 8336 | } |
| 8337 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8338 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8339 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8340 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8341 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 8342 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8343 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8344 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 8345 | list_del(&prsrc->list); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8346 | |
| 8347 | if (prsrc->tag) { |
| 8348 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8349 | |
| 8350 | io_ring_submit_lock(ctx, lock_ring); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8351 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 913a571 | 2021-11-10 15:49:31 +0000 | [diff] [blame] | 8352 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8353 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8354 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8355 | io_cqring_ev_posted(ctx); |
| 8356 | io_ring_submit_unlock(ctx, lock_ring); |
| 8357 | } |
| 8358 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 8359 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8360 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8361 | } |
| 8362 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 8363 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8364 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 8365 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8366 | } |
| 8367 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8368 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8369 | { |
| 8370 | struct io_ring_ctx *ctx; |
| 8371 | struct llist_node *node; |
| 8372 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8373 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 8374 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8375 | |
| 8376 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8377 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8378 | struct llist_node *next = node->next; |
| 8379 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8380 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8381 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8382 | node = next; |
| 8383 | } |
| 8384 | } |
| 8385 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8386 | 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] | 8387 | unsigned nr_args, u64 __user *tags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8388 | { |
| 8389 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8390 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8391 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 8392 | unsigned i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8393 | |
| 8394 | if (ctx->file_data) |
| 8395 | return -EBUSY; |
| 8396 | if (!nr_args) |
| 8397 | return -EINVAL; |
| 8398 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 8399 | return -EMFILE; |
Pavel Begunkov | 3a1b8a4 | 2021-08-20 10:36:35 +0100 | [diff] [blame] | 8400 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
| 8401 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8402 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8403 | if (ret) |
| 8404 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8405 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
| 8406 | &ctx->file_data); |
| 8407 | if (ret) |
| 8408 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8409 | |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8410 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8411 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8412 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8413 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8414 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8415 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8416 | ret = -EFAULT; |
| 8417 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8418 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8419 | /* allow sparse sets */ |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8420 | if (fd == -1) { |
| 8421 | ret = -EINVAL; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8422 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8423 | goto out_fput; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8424 | continue; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8425 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8426 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8427 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8428 | ret = -EBADF; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8429 | if (unlikely(!file)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8430 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8431 | |
| 8432 | /* |
| 8433 | * Don't allow io_uring instances to be registered. If UNIX |
| 8434 | * isn't enabled, then this causes a reference cycle and this |
| 8435 | * instance can never get freed. If UNIX is enabled we'll |
| 8436 | * handle it just fine, but there's still no point in allowing |
| 8437 | * a ring fd as it doesn't support regular read/write anyway. |
| 8438 | */ |
| 8439 | if (file->f_op == &io_uring_fops) { |
| 8440 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8441 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8442 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8443 | 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] | 8444 | } |
| 8445 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8446 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8447 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8448 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8449 | return ret; |
| 8450 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8451 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8452 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8453 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8454 | out_fput: |
| 8455 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8456 | file = io_file_from_index(ctx, i); |
| 8457 | if (file) |
| 8458 | fput(file); |
| 8459 | } |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8460 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8461 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8462 | out_free: |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8463 | io_rsrc_data_free(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 8464 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8465 | return ret; |
| 8466 | } |
| 8467 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8468 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 8469 | int index) |
| 8470 | { |
| 8471 | #if defined(CONFIG_UNIX) |
| 8472 | struct sock *sock = ctx->ring_sock->sk; |
| 8473 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 8474 | struct sk_buff *skb; |
| 8475 | |
| 8476 | /* |
| 8477 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 8478 | * file set. If there's no room, fall back to allocating a new skb |
| 8479 | * and filling it in. |
| 8480 | */ |
| 8481 | spin_lock_irq(&head->lock); |
| 8482 | skb = skb_peek(head); |
| 8483 | if (skb) { |
| 8484 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 8485 | |
| 8486 | if (fpl->count < SCM_MAX_FD) { |
| 8487 | __skb_unlink(skb, head); |
| 8488 | spin_unlock_irq(&head->lock); |
| 8489 | fpl->fp[fpl->count] = get_file(file); |
| 8490 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 8491 | fpl->count++; |
| 8492 | spin_lock_irq(&head->lock); |
| 8493 | __skb_queue_head(head, skb); |
| 8494 | } else { |
| 8495 | skb = NULL; |
| 8496 | } |
| 8497 | } |
| 8498 | spin_unlock_irq(&head->lock); |
| 8499 | |
| 8500 | if (skb) { |
| 8501 | fput(file); |
| 8502 | return 0; |
| 8503 | } |
| 8504 | |
| 8505 | return __io_sqe_files_scm(ctx, 1, index); |
| 8506 | #else |
| 8507 | return 0; |
| 8508 | #endif |
| 8509 | } |
| 8510 | |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8511 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
| 8512 | struct io_rsrc_node *node, void *rsrc) |
| 8513 | { |
| 8514 | struct io_rsrc_put *prsrc; |
| 8515 | |
| 8516 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 8517 | if (!prsrc) |
| 8518 | return -ENOMEM; |
| 8519 | |
| 8520 | prsrc->tag = *io_get_tag_slot(data, idx); |
| 8521 | prsrc->rsrc = rsrc; |
| 8522 | list_add(&prsrc->list, &node->rsrc_list); |
| 8523 | return 0; |
| 8524 | } |
| 8525 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8526 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 8527 | unsigned int issue_flags, u32 slot_index) |
| 8528 | { |
| 8529 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8530 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8531 | bool needs_switch = false; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8532 | struct io_fixed_file *file_slot; |
| 8533 | int ret = -EBADF; |
| 8534 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8535 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8536 | if (file->f_op == &io_uring_fops) |
| 8537 | goto err; |
| 8538 | ret = -ENXIO; |
| 8539 | if (!ctx->file_data) |
| 8540 | goto err; |
| 8541 | ret = -EINVAL; |
| 8542 | if (slot_index >= ctx->nr_user_files) |
| 8543 | goto err; |
| 8544 | |
| 8545 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); |
| 8546 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8547 | |
| 8548 | if (file_slot->file_ptr) { |
| 8549 | struct file *old_file; |
| 8550 | |
| 8551 | ret = io_rsrc_node_switch_start(ctx); |
| 8552 | if (ret) |
| 8553 | goto err; |
| 8554 | |
| 8555 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8556 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, |
| 8557 | ctx->rsrc_node, old_file); |
| 8558 | if (ret) |
| 8559 | goto err; |
| 8560 | file_slot->file_ptr = 0; |
| 8561 | needs_switch = true; |
| 8562 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8563 | |
| 8564 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; |
| 8565 | io_fixed_file_set(file_slot, file); |
| 8566 | ret = io_sqe_file_register(ctx, file, slot_index); |
| 8567 | if (ret) { |
| 8568 | file_slot->file_ptr = 0; |
| 8569 | goto err; |
| 8570 | } |
| 8571 | |
| 8572 | ret = 0; |
| 8573 | err: |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8574 | if (needs_switch) |
| 8575 | io_rsrc_node_switch(ctx, ctx->file_data); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8576 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8577 | if (ret) |
| 8578 | fput(file); |
| 8579 | return ret; |
| 8580 | } |
| 8581 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8582 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 8583 | { |
| 8584 | unsigned int offset = req->close.file_slot - 1; |
| 8585 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8586 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8587 | struct io_fixed_file *file_slot; |
| 8588 | struct file *file; |
| 8589 | int ret, i; |
| 8590 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8591 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8592 | ret = -ENXIO; |
| 8593 | if (unlikely(!ctx->file_data)) |
| 8594 | goto out; |
| 8595 | ret = -EINVAL; |
| 8596 | if (offset >= ctx->nr_user_files) |
| 8597 | goto out; |
| 8598 | ret = io_rsrc_node_switch_start(ctx); |
| 8599 | if (ret) |
| 8600 | goto out; |
| 8601 | |
| 8602 | i = array_index_nospec(offset, ctx->nr_user_files); |
| 8603 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
| 8604 | ret = -EBADF; |
| 8605 | if (!file_slot->file_ptr) |
| 8606 | goto out; |
| 8607 | |
| 8608 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8609 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); |
| 8610 | if (ret) |
| 8611 | goto out; |
| 8612 | |
| 8613 | file_slot->file_ptr = 0; |
| 8614 | io_rsrc_node_switch(ctx, ctx->file_data); |
| 8615 | ret = 0; |
| 8616 | out: |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8617 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8618 | return ret; |
| 8619 | } |
| 8620 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8621 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8622 | struct io_uring_rsrc_update2 *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8623 | unsigned nr_args) |
| 8624 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8625 | u64 __user *tags = u64_to_user_ptr(up->tags); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8626 | __s32 __user *fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8627 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8628 | struct io_fixed_file *file_slot; |
| 8629 | struct file *file; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8630 | int fd, i, err = 0; |
| 8631 | unsigned int done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8632 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8633 | |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8634 | if (!ctx->file_data) |
| 8635 | return -ENXIO; |
| 8636 | if (up->offset + nr_args > ctx->nr_user_files) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8637 | return -EINVAL; |
| 8638 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8639 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8640 | u64 tag = 0; |
| 8641 | |
| 8642 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || |
| 8643 | copy_from_user(&fd, &fds[done], sizeof(fd))) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8644 | err = -EFAULT; |
| 8645 | break; |
| 8646 | } |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8647 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
| 8648 | err = -EINVAL; |
| 8649 | break; |
| 8650 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8651 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8652 | continue; |
| 8653 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8654 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8655 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8656 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8657 | if (file_slot->file_ptr) { |
| 8658 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8659 | err = io_queue_rsrc_removal(data, up->offset + done, |
| 8660 | ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8661 | if (err) |
| 8662 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8663 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8664 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8665 | } |
| 8666 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8667 | file = fget(fd); |
| 8668 | if (!file) { |
| 8669 | err = -EBADF; |
| 8670 | break; |
| 8671 | } |
| 8672 | /* |
| 8673 | * Don't allow io_uring instances to be registered. If |
| 8674 | * UNIX isn't enabled, then this causes a reference |
| 8675 | * cycle and this instance can never get freed. If UNIX |
| 8676 | * is enabled we'll handle it just fine, but there's |
| 8677 | * still no point in allowing a ring fd as it doesn't |
| 8678 | * support regular read/write anyway. |
| 8679 | */ |
| 8680 | if (file->f_op == &io_uring_fops) { |
| 8681 | fput(file); |
| 8682 | err = -EBADF; |
| 8683 | break; |
| 8684 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8685 | *io_get_tag_slot(data, up->offset + done) = tag; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 8686 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8687 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8688 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8689 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8690 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8691 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8692 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8693 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8694 | } |
| 8695 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8696 | if (needs_switch) |
| 8697 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8698 | return done ? done : err; |
| 8699 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8700 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8701 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8702 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8703 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8704 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8705 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8706 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8707 | |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8708 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8709 | hash = ctx->hash_map; |
| 8710 | if (!hash) { |
| 8711 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8712 | if (!hash) { |
| 8713 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8714 | return ERR_PTR(-ENOMEM); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8715 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8716 | refcount_set(&hash->refs, 1); |
| 8717 | init_waitqueue_head(&hash->wait); |
| 8718 | ctx->hash_map = hash; |
| 8719 | } |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8720 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8721 | |
| 8722 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8723 | data.task = task; |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 8724 | data.free_work = io_wq_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8725 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8726 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8727 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8728 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8729 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8730 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8731 | } |
| 8732 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8733 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
| 8734 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8735 | { |
| 8736 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8737 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8738 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8739 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8740 | if (unlikely(!tctx)) |
| 8741 | return -ENOMEM; |
| 8742 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8743 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8744 | if (unlikely(ret)) { |
| 8745 | kfree(tctx); |
| 8746 | return ret; |
| 8747 | } |
| 8748 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8749 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8750 | if (IS_ERR(tctx->io_wq)) { |
| 8751 | ret = PTR_ERR(tctx->io_wq); |
| 8752 | percpu_counter_destroy(&tctx->inflight); |
| 8753 | kfree(tctx); |
| 8754 | return ret; |
| 8755 | } |
| 8756 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8757 | xa_init(&tctx->xa); |
| 8758 | init_waitqueue_head(&tctx->wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8759 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 8760 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8761 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8762 | spin_lock_init(&tctx->task_lock); |
| 8763 | INIT_WQ_LIST(&tctx->task_list); |
Hao Xu | 4813c37 | 2021-12-07 17:39:48 +0800 | [diff] [blame] | 8764 | INIT_WQ_LIST(&tctx->prior_task_list); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8765 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8766 | return 0; |
| 8767 | } |
| 8768 | |
| 8769 | void __io_uring_free(struct task_struct *tsk) |
| 8770 | { |
| 8771 | struct io_uring_task *tctx = tsk->io_uring; |
| 8772 | |
| 8773 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8774 | WARN_ON_ONCE(tctx->io_wq); |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8775 | WARN_ON_ONCE(tctx->cached_refs); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8776 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8777 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8778 | kfree(tctx); |
| 8779 | tsk->io_uring = NULL; |
| 8780 | } |
| 8781 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8782 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8783 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8784 | { |
| 8785 | int ret; |
| 8786 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8787 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 8788 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 8789 | IORING_SETUP_ATTACH_WQ) { |
| 8790 | struct fd f; |
| 8791 | |
| 8792 | f = fdget(p->wq_fd); |
| 8793 | if (!f.file) |
| 8794 | return -ENXIO; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8795 | if (f.file->f_op != &io_uring_fops) { |
| 8796 | fdput(f); |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8797 | return -EINVAL; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8798 | } |
| 8799 | fdput(f); |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8800 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8801 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8802 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8803 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8804 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8805 | |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 8806 | ret = security_uring_sqpoll(); |
| 8807 | if (ret) |
| 8808 | return ret; |
| 8809 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8810 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8811 | if (IS_ERR(sqd)) { |
| 8812 | ret = PTR_ERR(sqd); |
| 8813 | goto err; |
| 8814 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8815 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 8816 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8817 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8818 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8819 | if (!ctx->sq_thread_idle) |
| 8820 | ctx->sq_thread_idle = HZ; |
| 8821 | |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8822 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8823 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 8824 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8825 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8826 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8827 | io_sq_thread_unpark(sqd); |
| 8828 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8829 | if (ret < 0) |
| 8830 | goto err; |
| 8831 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8832 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8833 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8834 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8835 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8836 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8837 | ret = -EINVAL; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8838 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8839 | goto err_sqpoll; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8840 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8841 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8842 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8843 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8844 | |
| 8845 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8846 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8847 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 8848 | if (IS_ERR(tsk)) { |
| 8849 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8850 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8851 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8852 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8853 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8854 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8855 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8856 | if (ret) |
| 8857 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8858 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8859 | /* Can't have SQ_AFF without SQPOLL */ |
| 8860 | ret = -EINVAL; |
| 8861 | goto err; |
| 8862 | } |
| 8863 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8864 | return 0; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8865 | err_sqpoll: |
| 8866 | complete(&ctx->sq_data->exited); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8867 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8868 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8869 | return ret; |
| 8870 | } |
| 8871 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8872 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8873 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8874 | { |
| 8875 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8876 | } |
| 8877 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8878 | static inline int __io_account_mem(struct user_struct *user, |
| 8879 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8880 | { |
| 8881 | unsigned long page_limit, cur_pages, new_pages; |
| 8882 | |
| 8883 | /* Don't allow more pages than we can safely lock */ |
| 8884 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8885 | |
| 8886 | do { |
| 8887 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8888 | new_pages = cur_pages + nr_pages; |
| 8889 | if (new_pages > page_limit) |
| 8890 | return -ENOMEM; |
| 8891 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8892 | new_pages) != cur_pages); |
| 8893 | |
| 8894 | return 0; |
| 8895 | } |
| 8896 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8897 | 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] | 8898 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8899 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8900 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8901 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8902 | if (ctx->mm_account) |
| 8903 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8904 | } |
| 8905 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8906 | 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] | 8907 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8908 | int ret; |
| 8909 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8910 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8911 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8912 | if (ret) |
| 8913 | return ret; |
| 8914 | } |
| 8915 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8916 | if (ctx->mm_account) |
| 8917 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8918 | |
| 8919 | return 0; |
| 8920 | } |
| 8921 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8922 | static void io_mem_free(void *ptr) |
| 8923 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8924 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8925 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8926 | if (!ptr) |
| 8927 | return; |
| 8928 | |
| 8929 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8930 | if (put_page_testzero(page)) |
| 8931 | free_compound_page(page); |
| 8932 | } |
| 8933 | |
| 8934 | static void *io_mem_alloc(size_t size) |
| 8935 | { |
| 8936 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8937 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8938 | |
| 8939 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8940 | } |
| 8941 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8942 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8943 | size_t *sq_offset) |
| 8944 | { |
| 8945 | struct io_rings *rings; |
| 8946 | size_t off, sq_array_size; |
| 8947 | |
| 8948 | off = struct_size(rings, cqes, cq_entries); |
| 8949 | if (off == SIZE_MAX) |
| 8950 | return SIZE_MAX; |
| 8951 | |
| 8952 | #ifdef CONFIG_SMP |
| 8953 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8954 | if (off == 0) |
| 8955 | return SIZE_MAX; |
| 8956 | #endif |
| 8957 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8958 | if (sq_offset) |
| 8959 | *sq_offset = off; |
| 8960 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8961 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8962 | if (sq_array_size == SIZE_MAX) |
| 8963 | return SIZE_MAX; |
| 8964 | |
| 8965 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8966 | return SIZE_MAX; |
| 8967 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8968 | return off; |
| 8969 | } |
| 8970 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8971 | 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] | 8972 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8973 | struct io_mapped_ubuf *imu = *slot; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8974 | unsigned int i; |
| 8975 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8976 | if (imu != ctx->dummy_ubuf) { |
| 8977 | for (i = 0; i < imu->nr_bvecs; i++) |
| 8978 | unpin_user_page(imu->bvec[i].bv_page); |
| 8979 | if (imu->acct_pages) |
| 8980 | io_unaccount_mem(ctx, imu->acct_pages); |
| 8981 | kvfree(imu); |
| 8982 | } |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8983 | *slot = NULL; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8984 | } |
| 8985 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8986 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
| 8987 | { |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 8988 | io_buffer_unmap(ctx, &prsrc->buf); |
| 8989 | prsrc->buf = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8990 | } |
| 8991 | |
| 8992 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8993 | { |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8994 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8995 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8996 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 8997 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8998 | kfree(ctx->user_bufs); |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 8999 | io_rsrc_data_free(ctx->buf_data); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9000 | ctx->user_bufs = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9001 | ctx->buf_data = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9002 | ctx->nr_user_bufs = 0; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9003 | } |
| 9004 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9005 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
| 9006 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9007 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9008 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9009 | if (!ctx->buf_data) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9010 | return -ENXIO; |
| 9011 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9012 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
| 9013 | if (!ret) |
| 9014 | __io_sqe_buffers_unregister(ctx); |
| 9015 | return ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9016 | } |
| 9017 | |
| 9018 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 9019 | void __user *arg, unsigned index) |
| 9020 | { |
| 9021 | struct iovec __user *src; |
| 9022 | |
| 9023 | #ifdef CONFIG_COMPAT |
| 9024 | if (ctx->compat) { |
| 9025 | struct compat_iovec __user *ciovs; |
| 9026 | struct compat_iovec ciov; |
| 9027 | |
| 9028 | ciovs = (struct compat_iovec __user *) arg; |
| 9029 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 9030 | return -EFAULT; |
| 9031 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 9032 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9033 | dst->iov_len = ciov.iov_len; |
| 9034 | return 0; |
| 9035 | } |
| 9036 | #endif |
| 9037 | src = (struct iovec __user *) arg; |
| 9038 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 9039 | return -EFAULT; |
| 9040 | return 0; |
| 9041 | } |
| 9042 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9043 | /* |
| 9044 | * Not super efficient, but this is just a registration time. And we do cache |
| 9045 | * the last compound head, so generally we'll only do a full search if we don't |
| 9046 | * match that one. |
| 9047 | * |
| 9048 | * We check if the given compound head page has already been accounted, to |
| 9049 | * avoid double accounting it. This allows us to account the full size of the |
| 9050 | * page, not just the constituent pages of a huge page. |
| 9051 | */ |
| 9052 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 9053 | int nr_pages, struct page *hpage) |
| 9054 | { |
| 9055 | int i, j; |
| 9056 | |
| 9057 | /* check current page array */ |
| 9058 | for (i = 0; i < nr_pages; i++) { |
| 9059 | if (!PageCompound(pages[i])) |
| 9060 | continue; |
| 9061 | if (compound_head(pages[i]) == hpage) |
| 9062 | return true; |
| 9063 | } |
| 9064 | |
| 9065 | /* check previously registered pages */ |
| 9066 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9067 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9068 | |
| 9069 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 9070 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 9071 | continue; |
| 9072 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 9073 | return true; |
| 9074 | } |
| 9075 | } |
| 9076 | |
| 9077 | return false; |
| 9078 | } |
| 9079 | |
| 9080 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 9081 | int nr_pages, struct io_mapped_ubuf *imu, |
| 9082 | struct page **last_hpage) |
| 9083 | { |
| 9084 | int i, ret; |
| 9085 | |
Pavel Begunkov | 216e583 | 2021-05-29 12:01:02 +0100 | [diff] [blame] | 9086 | imu->acct_pages = 0; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9087 | for (i = 0; i < nr_pages; i++) { |
| 9088 | if (!PageCompound(pages[i])) { |
| 9089 | imu->acct_pages++; |
| 9090 | } else { |
| 9091 | struct page *hpage; |
| 9092 | |
| 9093 | hpage = compound_head(pages[i]); |
| 9094 | if (hpage == *last_hpage) |
| 9095 | continue; |
| 9096 | *last_hpage = hpage; |
| 9097 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 9098 | continue; |
| 9099 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 9100 | } |
| 9101 | } |
| 9102 | |
| 9103 | if (!imu->acct_pages) |
| 9104 | return 0; |
| 9105 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9106 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9107 | if (ret) |
| 9108 | imu->acct_pages = 0; |
| 9109 | return ret; |
| 9110 | } |
| 9111 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9112 | 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] | 9113 | struct io_mapped_ubuf **pimu, |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9114 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9115 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9116 | struct io_mapped_ubuf *imu = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9117 | struct vm_area_struct **vmas = NULL; |
| 9118 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9119 | unsigned long off, start, end, ubuf; |
| 9120 | size_t size; |
| 9121 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9122 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9123 | if (!iov->iov_base) { |
| 9124 | *pimu = ctx->dummy_ubuf; |
| 9125 | return 0; |
| 9126 | } |
| 9127 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9128 | ubuf = (unsigned long) iov->iov_base; |
| 9129 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 9130 | start = ubuf >> PAGE_SHIFT; |
| 9131 | nr_pages = end - start; |
| 9132 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9133 | *pimu = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9134 | ret = -ENOMEM; |
| 9135 | |
| 9136 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 9137 | if (!pages) |
| 9138 | goto done; |
| 9139 | |
| 9140 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 9141 | GFP_KERNEL); |
| 9142 | if (!vmas) |
| 9143 | goto done; |
| 9144 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9145 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
Pavel Begunkov | a2b4198 | 2021-04-26 00:16:31 +0100 | [diff] [blame] | 9146 | if (!imu) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9147 | goto done; |
| 9148 | |
| 9149 | ret = 0; |
| 9150 | mmap_read_lock(current->mm); |
| 9151 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 9152 | pages, vmas); |
| 9153 | if (pret == nr_pages) { |
| 9154 | /* don't support file backed memory */ |
| 9155 | for (i = 0; i < nr_pages; i++) { |
| 9156 | struct vm_area_struct *vma = vmas[i]; |
| 9157 | |
Pavel Begunkov | 40dad76 | 2021-06-09 15:26:54 +0100 | [diff] [blame] | 9158 | if (vma_is_shmem(vma)) |
| 9159 | continue; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9160 | if (vma->vm_file && |
| 9161 | !is_file_hugepages(vma->vm_file)) { |
| 9162 | ret = -EOPNOTSUPP; |
| 9163 | break; |
| 9164 | } |
| 9165 | } |
| 9166 | } else { |
| 9167 | ret = pret < 0 ? pret : -EFAULT; |
| 9168 | } |
| 9169 | mmap_read_unlock(current->mm); |
| 9170 | if (ret) { |
| 9171 | /* |
| 9172 | * if we did partial map, or found file backed vmas, |
| 9173 | * release any pages we did get |
| 9174 | */ |
| 9175 | if (pret > 0) |
| 9176 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9177 | goto done; |
| 9178 | } |
| 9179 | |
| 9180 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 9181 | if (ret) { |
| 9182 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9183 | goto done; |
| 9184 | } |
| 9185 | |
| 9186 | off = ubuf & ~PAGE_MASK; |
| 9187 | size = iov->iov_len; |
| 9188 | for (i = 0; i < nr_pages; i++) { |
| 9189 | size_t vec_len; |
| 9190 | |
| 9191 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 9192 | imu->bvec[i].bv_page = pages[i]; |
| 9193 | imu->bvec[i].bv_len = vec_len; |
| 9194 | imu->bvec[i].bv_offset = off; |
| 9195 | off = 0; |
| 9196 | size -= vec_len; |
| 9197 | } |
| 9198 | /* store original address for later verification */ |
| 9199 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9200 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9201 | imu->nr_bvecs = nr_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9202 | *pimu = imu; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9203 | ret = 0; |
| 9204 | done: |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9205 | if (ret) |
| 9206 | kvfree(imu); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9207 | kvfree(pages); |
| 9208 | kvfree(vmas); |
| 9209 | return ret; |
| 9210 | } |
| 9211 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9212 | 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] | 9213 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9214 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 9215 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9216 | } |
| 9217 | |
| 9218 | static int io_buffer_validate(struct iovec *iov) |
| 9219 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9220 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 9221 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9222 | /* |
| 9223 | * Don't impose further limits on the size and buffer |
| 9224 | * constraints here, we'll -EINVAL later when IO is |
| 9225 | * submitted if they are wrong. |
| 9226 | */ |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9227 | if (!iov->iov_base) |
| 9228 | return iov->iov_len ? -EFAULT : 0; |
| 9229 | if (!iov->iov_len) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9230 | return -EFAULT; |
| 9231 | |
| 9232 | /* arbitrary limit, but we need something */ |
| 9233 | if (iov->iov_len > SZ_1G) |
| 9234 | return -EFAULT; |
| 9235 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9236 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 9237 | return -EOVERFLOW; |
| 9238 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9239 | return 0; |
| 9240 | } |
| 9241 | |
| 9242 | 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] | 9243 | unsigned int nr_args, u64 __user *tags) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9244 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9245 | struct page *last_hpage = NULL; |
| 9246 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9247 | int i, ret; |
| 9248 | struct iovec iov; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9249 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9250 | if (ctx->user_bufs) |
| 9251 | return -EBUSY; |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 9252 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9253 | return -EINVAL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9254 | ret = io_rsrc_node_switch_start(ctx); |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9255 | if (ret) |
| 9256 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 9257 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
| 9258 | if (ret) |
| 9259 | return ret; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9260 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 9261 | if (ret) { |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 9262 | io_rsrc_data_free(data); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9263 | return ret; |
| 9264 | } |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9265 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9266 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9267 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 9268 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9269 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9270 | ret = io_buffer_validate(&iov); |
| 9271 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9272 | break; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9273 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9274 | ret = -EINVAL; |
| 9275 | break; |
| 9276 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9277 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9278 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
| 9279 | &last_hpage); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9280 | if (ret) |
| 9281 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9282 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9283 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9284 | WARN_ON_ONCE(ctx->buf_data); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9285 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9286 | ctx->buf_data = data; |
| 9287 | if (ret) |
| 9288 | __io_sqe_buffers_unregister(ctx); |
| 9289 | else |
| 9290 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9291 | return ret; |
| 9292 | } |
| 9293 | |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9294 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
| 9295 | struct io_uring_rsrc_update2 *up, |
| 9296 | unsigned int nr_args) |
| 9297 | { |
| 9298 | u64 __user *tags = u64_to_user_ptr(up->tags); |
| 9299 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9300 | struct page *last_hpage = NULL; |
| 9301 | bool needs_switch = false; |
| 9302 | __u32 done; |
| 9303 | int i, err; |
| 9304 | |
| 9305 | if (!ctx->buf_data) |
| 9306 | return -ENXIO; |
| 9307 | if (up->offset + nr_args > ctx->nr_user_bufs) |
| 9308 | return -EINVAL; |
| 9309 | |
| 9310 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9311 | struct io_mapped_ubuf *imu; |
| 9312 | int offset = up->offset + done; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9313 | u64 tag = 0; |
| 9314 | |
| 9315 | err = io_copy_iov(ctx, &iov, iovs, done); |
| 9316 | if (err) |
| 9317 | break; |
| 9318 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { |
| 9319 | err = -EFAULT; |
| 9320 | break; |
| 9321 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9322 | err = io_buffer_validate(&iov); |
| 9323 | if (err) |
| 9324 | break; |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9325 | if (!iov.iov_base && tag) { |
| 9326 | err = -EINVAL; |
| 9327 | break; |
| 9328 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9329 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
| 9330 | if (err) |
| 9331 | break; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9332 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9333 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9334 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9335 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
| 9336 | ctx->rsrc_node, ctx->user_bufs[i]); |
| 9337 | if (unlikely(err)) { |
| 9338 | io_buffer_unmap(ctx, &imu); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9339 | break; |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9340 | } |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9341 | ctx->user_bufs[i] = NULL; |
| 9342 | needs_switch = true; |
| 9343 | } |
| 9344 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9345 | ctx->user_bufs[i] = imu; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9346 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9347 | } |
| 9348 | |
| 9349 | if (needs_switch) |
| 9350 | io_rsrc_node_switch(ctx, ctx->buf_data); |
| 9351 | return done ? done : err; |
| 9352 | } |
| 9353 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9354 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 9355 | { |
| 9356 | __s32 __user *fds = arg; |
| 9357 | int fd; |
| 9358 | |
| 9359 | if (ctx->cq_ev_fd) |
| 9360 | return -EBUSY; |
| 9361 | |
| 9362 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 9363 | return -EFAULT; |
| 9364 | |
| 9365 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9366 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 9367 | int ret = PTR_ERR(ctx->cq_ev_fd); |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 9368 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9369 | ctx->cq_ev_fd = NULL; |
| 9370 | return ret; |
| 9371 | } |
| 9372 | |
| 9373 | return 0; |
| 9374 | } |
| 9375 | |
| 9376 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 9377 | { |
| 9378 | if (ctx->cq_ev_fd) { |
| 9379 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 9380 | ctx->cq_ev_fd = NULL; |
| 9381 | return 0; |
| 9382 | } |
| 9383 | |
| 9384 | return -ENXIO; |
| 9385 | } |
| 9386 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9387 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 9388 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9389 | struct io_buffer *buf; |
| 9390 | unsigned long index; |
| 9391 | |
Ye Bin | 1d0254e | 2021-11-22 10:47:37 +0800 | [diff] [blame] | 9392 | xa_for_each(&ctx->io_buffers, index, buf) |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9393 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9394 | } |
| 9395 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9396 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9397 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9398 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9399 | int nr = 0; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 9400 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9401 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9402 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9403 | |
| 9404 | while (state->free_list.next) { |
| 9405 | struct io_wq_work_node *node; |
| 9406 | struct io_kiocb *req; |
| 9407 | |
| 9408 | node = wq_stack_extract(&state->free_list); |
| 9409 | req = container_of(node, struct io_kiocb, comp_list); |
| 9410 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9411 | nr++; |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9412 | } |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9413 | if (nr) |
| 9414 | percpu_ref_put_many(&ctx->refs, nr); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9415 | mutex_unlock(&ctx->uring_lock); |
| 9416 | } |
| 9417 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9418 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9419 | { |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9420 | if (data && !atomic_dec_and_test(&data->refs)) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9421 | wait_for_completion(&data->done); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9422 | } |
| 9423 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9424 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9425 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9426 | io_sq_thread_finish(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9427 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9428 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9429 | mmdrop(ctx->mm_account); |
| 9430 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9431 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9432 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 9433 | io_rsrc_refs_drop(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9434 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
| 9435 | io_wait_rsrc_data(ctx->buf_data); |
| 9436 | io_wait_rsrc_data(ctx->file_data); |
| 9437 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9438 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9439 | if (ctx->buf_data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9440 | __io_sqe_buffers_unregister(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9441 | if (ctx->file_data) |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 9442 | __io_sqe_files_unregister(ctx); |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 9443 | if (ctx->rings) |
| 9444 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9445 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9446 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9447 | io_destroy_buffers(ctx); |
Pavel Begunkov | 07db298 | 2021-04-20 12:03:32 +0100 | [diff] [blame] | 9448 | if (ctx->sq_creds) |
| 9449 | put_cred(ctx->sq_creds); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9450 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9451 | /* there are no registered resources left, nobody uses it */ |
| 9452 | if (ctx->rsrc_node) |
| 9453 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 9454 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 9455 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9456 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 756ab7c | 2021-10-06 16:06:47 +0100 | [diff] [blame] | 9457 | flush_delayed_work(&ctx->fallback_work); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9458 | |
| 9459 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 9460 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9461 | |
| 9462 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9463 | if (ctx->ring_sock) { |
| 9464 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9465 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9466 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9467 | #endif |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 9468 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9469 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9470 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9471 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9472 | |
| 9473 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9474 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9475 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 9476 | if (ctx->hash_map) |
| 9477 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 9478 | kfree(ctx->cancel_hash); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9479 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9480 | kfree(ctx); |
| 9481 | } |
| 9482 | |
| 9483 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 9484 | { |
| 9485 | struct io_ring_ctx *ctx = file->private_data; |
| 9486 | __poll_t mask = 0; |
| 9487 | |
Pavel Begunkov | d60aa65 | 2021-10-04 20:02:52 +0100 | [diff] [blame] | 9488 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 9489 | /* |
| 9490 | * synchronizes with barrier from wq_has_sleeper call in |
| 9491 | * io_commit_cqring |
| 9492 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9493 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9494 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9495 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 9496 | |
| 9497 | /* |
| 9498 | * Don't flush cqring overflow list here, just do a simple check. |
| 9499 | * Otherwise there could possible be ABBA deadlock: |
| 9500 | * CPU0 CPU1 |
| 9501 | * ---- ---- |
| 9502 | * lock(&ctx->uring_lock); |
| 9503 | * lock(&ep->mtx); |
| 9504 | * lock(&ctx->uring_lock); |
| 9505 | * lock(&ep->mtx); |
| 9506 | * |
| 9507 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 9508 | * pushs them to do the flush. |
| 9509 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 9510 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9511 | mask |= EPOLLIN | EPOLLRDNORM; |
| 9512 | |
| 9513 | return mask; |
| 9514 | } |
| 9515 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9516 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9517 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9518 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9519 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9520 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9521 | if (creds) { |
| 9522 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9523 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9524 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9525 | |
| 9526 | return -EINVAL; |
| 9527 | } |
| 9528 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9529 | struct io_tctx_exit { |
| 9530 | struct callback_head task_work; |
| 9531 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9532 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9533 | }; |
| 9534 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9535 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9536 | { |
| 9537 | struct io_uring_task *tctx = current->io_uring; |
| 9538 | struct io_tctx_exit *work; |
| 9539 | |
| 9540 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 9541 | /* |
| 9542 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 9543 | * node. It'll be removed by the end of cancellation, just ignore it. |
| 9544 | */ |
| 9545 | if (!atomic_read(&tctx->in_idle)) |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9546 | io_uring_del_tctx_node((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9547 | complete(&work->completion); |
| 9548 | } |
| 9549 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9550 | 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] | 9551 | { |
| 9552 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 9553 | |
| 9554 | return req->ctx == data; |
| 9555 | } |
| 9556 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9557 | static __cold void io_ring_exit_work(struct work_struct *work) |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9558 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9559 | 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] | 9560 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9561 | unsigned long interval = HZ / 20; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9562 | struct io_tctx_exit exit; |
| 9563 | struct io_tctx_node *node; |
| 9564 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9565 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 9566 | /* |
| 9567 | * If we're doing polled IO and end up having requests being |
| 9568 | * submitted async (out-of-line), then completions can come in while |
| 9569 | * we're waiting for refs to drop. We need to reap these manually, |
| 9570 | * as nobody else will be looking for them. |
| 9571 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 9572 | do { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9573 | io_uring_try_cancel_requests(ctx, NULL, true); |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9574 | if (ctx->sq_data) { |
| 9575 | struct io_sq_data *sqd = ctx->sq_data; |
| 9576 | struct task_struct *tsk; |
| 9577 | |
| 9578 | io_sq_thread_park(sqd); |
| 9579 | tsk = sqd->thread; |
| 9580 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) |
| 9581 | io_wq_cancel_cb(tsk->io_uring->io_wq, |
| 9582 | io_cancel_ctx_cb, ctx, true); |
| 9583 | io_sq_thread_unpark(sqd); |
| 9584 | } |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9585 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9586 | io_req_caches_free(ctx); |
| 9587 | |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9588 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
| 9589 | /* there is little hope left, don't run it too often */ |
| 9590 | interval = HZ * 60; |
| 9591 | } |
| 9592 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9593 | |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9594 | init_completion(&exit.completion); |
| 9595 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 9596 | exit.ctx = ctx; |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9597 | /* |
| 9598 | * Some may use context even when all refs and requests have been put, |
| 9599 | * 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] | 9600 | * completion_lock, see io_req_task_submit(). Apart from other work, |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9601 | * this lock/unlock section also waits them to finish. |
| 9602 | */ |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9603 | mutex_lock(&ctx->uring_lock); |
| 9604 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9605 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 9606 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9607 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 9608 | ctx_node); |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9609 | /* don't spin on a single task if cancellation failed */ |
| 9610 | list_rotate_left(&ctx->tctx_list); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9611 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 9612 | if (WARN_ON_ONCE(ret)) |
| 9613 | continue; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9614 | |
| 9615 | mutex_unlock(&ctx->uring_lock); |
| 9616 | wait_for_completion(&exit.completion); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9617 | mutex_lock(&ctx->uring_lock); |
| 9618 | } |
| 9619 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9620 | spin_lock(&ctx->completion_lock); |
| 9621 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9622 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9623 | io_ring_ctx_free(ctx); |
| 9624 | } |
| 9625 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9626 | /* Returns true if we found and killed one or more timeouts */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9627 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
| 9628 | struct task_struct *tsk, bool cancel_all) |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9629 | { |
| 9630 | struct io_kiocb *req, *tmp; |
| 9631 | int canceled = 0; |
| 9632 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9633 | spin_lock(&ctx->completion_lock); |
| 9634 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9635 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9636 | if (io_match_task(req, tsk, cancel_all)) { |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9637 | io_kill_timeout(req, -ECANCELED); |
| 9638 | canceled++; |
| 9639 | } |
| 9640 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9641 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 9642 | if (canceled != 0) |
| 9643 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9644 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9645 | if (canceled != 0) |
| 9646 | io_cqring_ev_posted(ctx); |
| 9647 | return canceled != 0; |
| 9648 | } |
| 9649 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9650 | 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] | 9651 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9652 | unsigned long index; |
| 9653 | struct creds *creds; |
| 9654 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9655 | mutex_lock(&ctx->uring_lock); |
| 9656 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 9657 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9658 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9659 | xa_for_each(&ctx->personalities, index, creds) |
| 9660 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9661 | mutex_unlock(&ctx->uring_lock); |
| 9662 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9663 | io_kill_timeouts(ctx, NULL, true); |
| 9664 | io_poll_remove_all(ctx, NULL, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 9665 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 9666 | /* 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] | 9667 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 9668 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9669 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 9670 | /* |
| 9671 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 9672 | * if we're exiting a ton of rings at the same time. It just adds |
| 9673 | * noise and overhead, there's no discernable change in runtime |
| 9674 | * over using system_wq. |
| 9675 | */ |
| 9676 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9677 | } |
| 9678 | |
| 9679 | static int io_uring_release(struct inode *inode, struct file *file) |
| 9680 | { |
| 9681 | struct io_ring_ctx *ctx = file->private_data; |
| 9682 | |
| 9683 | file->private_data = NULL; |
| 9684 | io_ring_ctx_wait_and_kill(ctx); |
| 9685 | return 0; |
| 9686 | } |
| 9687 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9688 | struct io_task_cancel { |
| 9689 | struct task_struct *task; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9690 | bool all; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9691 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 9692 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9693 | 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] | 9694 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9695 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9696 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9697 | |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 9698 | return io_match_task_safe(req, cancel->task, cancel->all); |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 9699 | } |
| 9700 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9701 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
| 9702 | struct task_struct *task, |
| 9703 | bool cancel_all) |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9704 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9705 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9706 | LIST_HEAD(list); |
| 9707 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9708 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9709 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 9710 | if (io_match_task_safe(de->req, task, cancel_all)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9711 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 9712 | break; |
| 9713 | } |
| 9714 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9715 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9716 | if (list_empty(&list)) |
| 9717 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9718 | |
| 9719 | while (!list_empty(&list)) { |
| 9720 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 9721 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 9722 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9723 | kfree(de); |
| 9724 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9725 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9726 | } |
| 9727 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9728 | 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] | 9729 | { |
| 9730 | struct io_tctx_node *node; |
| 9731 | enum io_wq_cancel cret; |
| 9732 | bool ret = false; |
| 9733 | |
| 9734 | mutex_lock(&ctx->uring_lock); |
| 9735 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 9736 | struct io_uring_task *tctx = node->task->io_uring; |
| 9737 | |
| 9738 | /* |
| 9739 | * io_wq will stay alive while we hold uring_lock, because it's |
| 9740 | * killed after ctx nodes, which requires to take the lock. |
| 9741 | */ |
| 9742 | if (!tctx || !tctx->io_wq) |
| 9743 | continue; |
| 9744 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 9745 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9746 | } |
| 9747 | mutex_unlock(&ctx->uring_lock); |
| 9748 | |
| 9749 | return ret; |
| 9750 | } |
| 9751 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9752 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 9753 | struct task_struct *task, |
| 9754 | bool cancel_all) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9755 | { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9756 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9757 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9758 | |
| 9759 | while (1) { |
| 9760 | enum io_wq_cancel cret; |
| 9761 | bool ret = false; |
| 9762 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9763 | if (!task) { |
| 9764 | ret |= io_uring_try_cancel_iowq(ctx); |
| 9765 | } else if (tctx && tctx->io_wq) { |
| 9766 | /* |
| 9767 | * Cancels requests of all rings, not only @ctx, but |
| 9768 | * it's fine as the task is in exit/exec. |
| 9769 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9770 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9771 | &cancel, true); |
| 9772 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9773 | } |
| 9774 | |
| 9775 | /* SQPOLL thread does its own polling */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9776 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 9777 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 9778 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9779 | io_iopoll_try_reap_events(ctx); |
| 9780 | ret = true; |
| 9781 | } |
| 9782 | } |
| 9783 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9784 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
| 9785 | ret |= io_poll_remove_all(ctx, task, cancel_all); |
| 9786 | ret |= io_kill_timeouts(ctx, task, cancel_all); |
Pavel Begunkov | e5dc480 | 2021-06-26 21:40:46 +0100 | [diff] [blame] | 9787 | if (task) |
| 9788 | ret |= io_run_task_work(); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9789 | if (!ret) |
| 9790 | break; |
| 9791 | cond_resched(); |
| 9792 | } |
| 9793 | } |
| 9794 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9795 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9796 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9797 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9798 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9799 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9800 | |
| 9801 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9802 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9803 | if (unlikely(ret)) |
| 9804 | return ret; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9805 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9806 | tctx = current->io_uring; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9807 | if (ctx->iowq_limits_set) { |
| 9808 | unsigned int limits[2] = { ctx->iowq_limits[0], |
| 9809 | ctx->iowq_limits[1], }; |
| 9810 | |
| 9811 | ret = io_wq_max_workers(tctx->io_wq, limits); |
| 9812 | if (ret) |
| 9813 | return ret; |
| 9814 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9815 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9816 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 9817 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 9818 | if (!node) |
| 9819 | return -ENOMEM; |
| 9820 | node->ctx = ctx; |
| 9821 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9822 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9823 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 9824 | node, GFP_KERNEL)); |
| 9825 | if (ret) { |
| 9826 | kfree(node); |
| 9827 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9828 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9829 | |
| 9830 | mutex_lock(&ctx->uring_lock); |
| 9831 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 9832 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9833 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9834 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9835 | return 0; |
| 9836 | } |
| 9837 | |
| 9838 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9839 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9840 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9841 | 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] | 9842 | { |
| 9843 | struct io_uring_task *tctx = current->io_uring; |
| 9844 | |
| 9845 | if (likely(tctx && tctx->last == ctx)) |
| 9846 | return 0; |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9847 | return __io_uring_add_tctx_node(ctx); |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9848 | } |
| 9849 | |
| 9850 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9851 | * Remove this io_uring_file -> task mapping. |
| 9852 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9853 | static __cold void io_uring_del_tctx_node(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9854 | { |
| 9855 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9856 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9857 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 9858 | if (!tctx) |
| 9859 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9860 | node = xa_erase(&tctx->xa, index); |
| 9861 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9862 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9863 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9864 | WARN_ON_ONCE(current != node->task); |
| 9865 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 9866 | |
| 9867 | mutex_lock(&node->ctx->uring_lock); |
| 9868 | list_del(&node->ctx_node); |
| 9869 | mutex_unlock(&node->ctx->uring_lock); |
| 9870 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9871 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9872 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9873 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9874 | } |
| 9875 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9876 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9877 | { |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9878 | struct io_wq *wq = tctx->io_wq; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9879 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9880 | unsigned long index; |
| 9881 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9882 | xa_for_each(&tctx->xa, index, node) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9883 | io_uring_del_tctx_node(index); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9884 | cond_resched(); |
| 9885 | } |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9886 | if (wq) { |
| 9887 | /* |
Kamal Mostafa | f6f9b27 | 2021-11-16 09:55:30 -0800 | [diff] [blame] | 9888 | * Must be after io_uring_del_tctx_node() (removes nodes under |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9889 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
| 9890 | */ |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9891 | io_wq_put_and_exit(wq); |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 9892 | tctx->io_wq = NULL; |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9893 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9894 | } |
| 9895 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9896 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9897 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9898 | if (tracked) |
| 9899 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9900 | return percpu_counter_sum(&tctx->inflight); |
| 9901 | } |
| 9902 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9903 | /* |
| 9904 | * Find any io_uring ctx that this task has registered or done IO on, and cancel |
Jens Axboe | 78a7806 | 2021-12-09 08:54:29 -0700 | [diff] [blame] | 9905 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9906 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9907 | static __cold void io_uring_cancel_generic(bool cancel_all, |
| 9908 | struct io_sq_data *sqd) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9909 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9910 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 9911 | struct io_ring_ctx *ctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9912 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9913 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9914 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9915 | WARN_ON_ONCE(sqd && sqd->thread != current); |
| 9916 | |
Palash Oswal | 6d042ff | 2021-04-27 18:21:49 +0530 | [diff] [blame] | 9917 | if (!current->io_uring) |
| 9918 | return; |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 9919 | if (tctx->io_wq) |
| 9920 | io_wq_exit_start(tctx->io_wq); |
| 9921 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9922 | atomic_inc(&tctx->in_idle); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9923 | do { |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9924 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9925 | /* read completions before cancelations */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9926 | inflight = tctx_inflight(tctx, !cancel_all); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9927 | if (!inflight) |
| 9928 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9929 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9930 | if (!sqd) { |
| 9931 | struct io_tctx_node *node; |
| 9932 | unsigned long index; |
| 9933 | |
| 9934 | xa_for_each(&tctx->xa, index, node) { |
| 9935 | /* sqpoll task will cancel all its requests */ |
| 9936 | if (node->ctx->sq_data) |
| 9937 | continue; |
| 9938 | io_uring_try_cancel_requests(node->ctx, current, |
| 9939 | cancel_all); |
| 9940 | } |
| 9941 | } else { |
| 9942 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 9943 | io_uring_try_cancel_requests(ctx, current, |
| 9944 | cancel_all); |
| 9945 | } |
| 9946 | |
Jens Axboe | 78a7806 | 2021-12-09 08:54:29 -0700 | [diff] [blame] | 9947 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
| 9948 | io_run_task_work(); |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9949 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 78a7806 | 2021-12-09 08:54:29 -0700 | [diff] [blame] | 9950 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9951 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9952 | * If we've seen completions, retry without waiting. This |
| 9953 | * avoids a race where a completion comes in before we did |
| 9954 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9955 | */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9956 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9957 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9958 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9959 | } while (1); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9960 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9961 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9962 | if (cancel_all) { |
Pavel Begunkov | 3cc7fdb | 2022-01-09 00:53:22 +0000 | [diff] [blame] | 9963 | /* |
| 9964 | * We shouldn't run task_works after cancel, so just leave |
| 9965 | * ->in_idle set for normal exit. |
| 9966 | */ |
| 9967 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9968 | /* for exec all current's requests should be gone, kill tctx */ |
| 9969 | __io_uring_free(current); |
| 9970 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9971 | } |
| 9972 | |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9973 | void __io_uring_cancel(bool cancel_all) |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9974 | { |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9975 | io_uring_cancel_generic(cancel_all, NULL); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9976 | } |
| 9977 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9978 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9979 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9980 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9981 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9982 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9983 | struct page *page; |
| 9984 | void *ptr; |
| 9985 | |
| 9986 | switch (offset) { |
| 9987 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9988 | case IORING_OFF_CQ_RING: |
| 9989 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9990 | break; |
| 9991 | case IORING_OFF_SQES: |
| 9992 | ptr = ctx->sq_sqes; |
| 9993 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9994 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9995 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9996 | } |
| 9997 | |
| 9998 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9999 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10000 | return ERR_PTR(-EINVAL); |
| 10001 | |
| 10002 | return ptr; |
| 10003 | } |
| 10004 | |
| 10005 | #ifdef CONFIG_MMU |
| 10006 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10007 | 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] | 10008 | { |
| 10009 | size_t sz = vma->vm_end - vma->vm_start; |
| 10010 | unsigned long pfn; |
| 10011 | void *ptr; |
| 10012 | |
| 10013 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 10014 | if (IS_ERR(ptr)) |
| 10015 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10016 | |
| 10017 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 10018 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 10019 | } |
| 10020 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10021 | #else /* !CONFIG_MMU */ |
| 10022 | |
| 10023 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 10024 | { |
| 10025 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 10026 | } |
| 10027 | |
| 10028 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 10029 | { |
| 10030 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 10031 | } |
| 10032 | |
| 10033 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 10034 | unsigned long addr, unsigned long len, |
| 10035 | unsigned long pgoff, unsigned long flags) |
| 10036 | { |
| 10037 | void *ptr; |
| 10038 | |
| 10039 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 10040 | if (IS_ERR(ptr)) |
| 10041 | return PTR_ERR(ptr); |
| 10042 | |
| 10043 | return (unsigned long) ptr; |
| 10044 | } |
| 10045 | |
| 10046 | #endif /* !CONFIG_MMU */ |
| 10047 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 10048 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10049 | { |
| 10050 | DEFINE_WAIT(wait); |
| 10051 | |
| 10052 | do { |
| 10053 | if (!io_sqring_full(ctx)) |
| 10054 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10055 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 10056 | |
| 10057 | if (!io_sqring_full(ctx)) |
| 10058 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10059 | schedule(); |
| 10060 | } while (!signal_pending(current)); |
| 10061 | |
| 10062 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 10063 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 10064 | } |
| 10065 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10066 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 10067 | struct __kernel_timespec __user **ts, |
| 10068 | const sigset_t __user **sig) |
| 10069 | { |
| 10070 | struct io_uring_getevents_arg arg; |
| 10071 | |
| 10072 | /* |
| 10073 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 10074 | * is just a pointer to the sigset_t. |
| 10075 | */ |
| 10076 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 10077 | *sig = (const sigset_t __user *) argp; |
| 10078 | *ts = NULL; |
| 10079 | return 0; |
| 10080 | } |
| 10081 | |
| 10082 | /* |
| 10083 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 10084 | * timespec and sigset_t pointers if good. |
| 10085 | */ |
| 10086 | if (*argsz != sizeof(arg)) |
| 10087 | return -EINVAL; |
| 10088 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 10089 | return -EFAULT; |
| 10090 | *sig = u64_to_user_ptr(arg.sigmask); |
| 10091 | *argsz = arg.sigmask_sz; |
| 10092 | *ts = u64_to_user_ptr(arg.ts); |
| 10093 | return 0; |
| 10094 | } |
| 10095 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10096 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10097 | u32, min_complete, u32, flags, const void __user *, argp, |
| 10098 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10099 | { |
| 10100 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10101 | int submitted = 0; |
| 10102 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10103 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10104 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 10105 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 10106 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10107 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 10108 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10109 | return -EINVAL; |
| 10110 | |
| 10111 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10112 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10113 | return -EBADF; |
| 10114 | |
| 10115 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10116 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10117 | goto out_fput; |
| 10118 | |
| 10119 | ret = -ENXIO; |
| 10120 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10121 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10122 | goto out_fput; |
| 10123 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10124 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10125 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10126 | goto out; |
| 10127 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10128 | /* |
| 10129 | * For SQ polling, the thread will do all submissions and completions. |
| 10130 | * Just return the requested submit count, and wake the thread if |
| 10131 | * we were asked to. |
| 10132 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10133 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10134 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 10135 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 10136 | |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10137 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 10138 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 10139 | goto out; |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10140 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10141 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 10142 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 10143 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 10144 | ret = io_sqpoll_wait_sq(ctx); |
| 10145 | if (ret) |
| 10146 | goto out; |
| 10147 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10148 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10149 | } else if (to_submit) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10150 | ret = io_uring_add_tctx_node(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10151 | if (unlikely(ret)) |
| 10152 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10153 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10154 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10155 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10156 | |
| 10157 | if (submitted != to_submit) |
| 10158 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10159 | } |
| 10160 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10161 | const sigset_t __user *sig; |
| 10162 | struct __kernel_timespec __user *ts; |
| 10163 | |
| 10164 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 10165 | if (unlikely(ret)) |
| 10166 | goto out; |
| 10167 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10168 | min_complete = min(min_complete, ctx->cq_entries); |
| 10169 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 10170 | /* |
| 10171 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 10172 | * space applications don't need to do io completion events |
| 10173 | * polling again, they can rely on io_sq_thread to do polling |
| 10174 | * work, which can reduce cpu usage and uring_lock contention. |
| 10175 | */ |
| 10176 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 10177 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 10178 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10179 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10180 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10181 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10182 | } |
| 10183 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10184 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 10185 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10186 | out_fput: |
| 10187 | fdput(f); |
| 10188 | return submitted ? submitted : ret; |
| 10189 | } |
| 10190 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10191 | #ifdef CONFIG_PROC_FS |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10192 | 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] | 10193 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10194 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10195 | struct user_namespace *uns = seq_user_ns(m); |
| 10196 | struct group_info *gi; |
| 10197 | kernel_cap_t cap; |
| 10198 | unsigned __capi; |
| 10199 | int g; |
| 10200 | |
| 10201 | seq_printf(m, "%5d\n", id); |
| 10202 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 10203 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 10204 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 10205 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 10206 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 10207 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 10208 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 10209 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 10210 | seq_puts(m, "\n\tGroups:\t"); |
| 10211 | gi = cred->group_info; |
| 10212 | for (g = 0; g < gi->ngroups; g++) { |
| 10213 | seq_put_decimal_ull(m, g ? " " : "", |
| 10214 | from_kgid_munged(uns, gi->gid[g])); |
| 10215 | } |
| 10216 | seq_puts(m, "\n\tCapEff:\t"); |
| 10217 | cap = cred->cap_effective; |
| 10218 | CAP_FOR_EACH_U32(__capi) |
| 10219 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 10220 | seq_putc(m, '\n'); |
| 10221 | return 0; |
| 10222 | } |
| 10223 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10224 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
| 10225 | struct seq_file *m) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10226 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10227 | struct io_sq_data *sq = NULL; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10228 | struct io_overflow_cqe *ocqe; |
| 10229 | struct io_rings *r = ctx->rings; |
| 10230 | 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] | 10231 | unsigned int sq_head = READ_ONCE(r->sq.head); |
| 10232 | unsigned int sq_tail = READ_ONCE(r->sq.tail); |
| 10233 | unsigned int cq_head = READ_ONCE(r->cq.head); |
| 10234 | unsigned int cq_tail = READ_ONCE(r->cq.tail); |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10235 | unsigned int sq_entries, cq_entries; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10236 | bool has_lock; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10237 | unsigned int i; |
| 10238 | |
| 10239 | /* |
| 10240 | * we may get imprecise sqe and cqe info if uring is actively running |
| 10241 | * since we get cached_sq_head and cached_cq_tail without uring_lock |
| 10242 | * and sq_tail and cq_head are changed by userspace. But it's ok since |
| 10243 | * we usually use these info when it is stuck. |
| 10244 | */ |
GuoYong Zheng | c023565 | 2022-01-05 18:13:05 +0800 | [diff] [blame] | 10245 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10246 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
| 10247 | seq_printf(m, "SqTail:\t%u\n", sq_tail); |
| 10248 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); |
| 10249 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); |
| 10250 | seq_printf(m, "CqHead:\t%u\n", cq_head); |
| 10251 | seq_printf(m, "CqTail:\t%u\n", cq_tail); |
| 10252 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); |
| 10253 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); |
| 10254 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); |
| 10255 | for (i = 0; i < sq_entries; i++) { |
| 10256 | unsigned int entry = i + sq_head; |
| 10257 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); |
Jens Axboe | a195778 | 2021-11-05 09:31:05 -0600 | [diff] [blame] | 10258 | struct io_uring_sqe *sqe; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10259 | |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10260 | if (sq_idx > sq_mask) |
| 10261 | continue; |
| 10262 | sqe = &ctx->sq_sqes[sq_idx]; |
| 10263 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", |
| 10264 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, |
| 10265 | sqe->user_data); |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10266 | } |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10267 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
| 10268 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); |
| 10269 | for (i = 0; i < cq_entries; i++) { |
| 10270 | unsigned int entry = i + cq_head; |
| 10271 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10272 | |
| 10273 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10274 | entry & cq_mask, cqe->user_data, cqe->res, |
| 10275 | cqe->flags); |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10276 | } |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10277 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10278 | /* |
| 10279 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 10280 | * since fdinfo case grabs it in the opposite direction of normal use |
| 10281 | * cases. If we fail to get the lock, we just don't iterate any |
| 10282 | * structures that could be going away outside the io_uring mutex. |
| 10283 | */ |
| 10284 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 10285 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10286 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10287 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10288 | if (!sq->thread) |
| 10289 | sq = NULL; |
| 10290 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10291 | |
| 10292 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 10293 | 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] | 10294 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10295 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 10296 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10297 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10298 | if (f) |
| 10299 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 10300 | else |
| 10301 | seq_printf(m, "%5u: <none>\n", i); |
| 10302 | } |
| 10303 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10304 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 10305 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10306 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10307 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10308 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10309 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10310 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 10311 | unsigned long index; |
| 10312 | const struct cred *cred; |
| 10313 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10314 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10315 | xa_for_each(&ctx->personalities, index, cred) |
| 10316 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10317 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10318 | if (has_lock) |
| 10319 | mutex_unlock(&ctx->uring_lock); |
| 10320 | |
| 10321 | seq_puts(m, "PollList:\n"); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10322 | spin_lock(&ctx->completion_lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 10323 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 10324 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 10325 | struct io_kiocb *req; |
| 10326 | |
| 10327 | hlist_for_each_entry(req, list, hash_node) |
| 10328 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 10329 | req->task->task_works != NULL); |
| 10330 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10331 | |
| 10332 | seq_puts(m, "CqOverflowList:\n"); |
| 10333 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { |
| 10334 | struct io_uring_cqe *cqe = &ocqe->cqe; |
| 10335 | |
| 10336 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", |
| 10337 | cqe->user_data, cqe->res, cqe->flags); |
| 10338 | |
| 10339 | } |
| 10340 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10341 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10342 | } |
| 10343 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10344 | 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] | 10345 | { |
| 10346 | struct io_ring_ctx *ctx = f->private_data; |
| 10347 | |
| 10348 | if (percpu_ref_tryget(&ctx->refs)) { |
| 10349 | __io_uring_show_fdinfo(ctx, m); |
| 10350 | percpu_ref_put(&ctx->refs); |
| 10351 | } |
| 10352 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10353 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10354 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10355 | static const struct file_operations io_uring_fops = { |
| 10356 | .release = io_uring_release, |
| 10357 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10358 | #ifndef CONFIG_MMU |
| 10359 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 10360 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 10361 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10362 | .poll = io_uring_poll, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10363 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10364 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10365 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10366 | }; |
| 10367 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10368 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 10369 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10370 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10371 | struct io_rings *rings; |
| 10372 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10373 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 10374 | /* make sure these are sane, as we already accounted them */ |
| 10375 | ctx->sq_entries = p->sq_entries; |
| 10376 | ctx->cq_entries = p->cq_entries; |
| 10377 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10378 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 10379 | if (size == SIZE_MAX) |
| 10380 | return -EOVERFLOW; |
| 10381 | |
| 10382 | rings = io_mem_alloc(size); |
| 10383 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10384 | return -ENOMEM; |
| 10385 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10386 | ctx->rings = rings; |
| 10387 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 10388 | rings->sq_ring_mask = p->sq_entries - 1; |
| 10389 | rings->cq_ring_mask = p->cq_entries - 1; |
| 10390 | rings->sq_ring_entries = p->sq_entries; |
| 10391 | rings->cq_ring_entries = p->cq_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10392 | |
| 10393 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10394 | if (size == SIZE_MAX) { |
| 10395 | io_mem_free(ctx->rings); |
| 10396 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10397 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10398 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10399 | |
| 10400 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10401 | if (!ctx->sq_sqes) { |
| 10402 | io_mem_free(ctx->rings); |
| 10403 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10404 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10405 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10406 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10407 | return 0; |
| 10408 | } |
| 10409 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10410 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 10411 | { |
| 10412 | int ret, fd; |
| 10413 | |
| 10414 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 10415 | if (fd < 0) |
| 10416 | return fd; |
| 10417 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10418 | ret = io_uring_add_tctx_node(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10419 | if (ret) { |
| 10420 | put_unused_fd(fd); |
| 10421 | return ret; |
| 10422 | } |
| 10423 | fd_install(fd, file); |
| 10424 | return fd; |
| 10425 | } |
| 10426 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10427 | /* |
| 10428 | * Allocate an anonymous fd, this is what constitutes the application |
| 10429 | * visible backing of an io_uring instance. The application mmaps this |
| 10430 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 10431 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 10432 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10433 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10434 | { |
| 10435 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10436 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10437 | int ret; |
| 10438 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10439 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 10440 | &ctx->ring_sock); |
| 10441 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10442 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10443 | #endif |
| 10444 | |
Paul Moore | 91a9ab7 | 2021-02-01 19:33:52 -0500 | [diff] [blame] | 10445 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
| 10446 | O_RDWR | O_CLOEXEC, NULL); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10447 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10448 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10449 | sock_release(ctx->ring_sock); |
| 10450 | ctx->ring_sock = NULL; |
| 10451 | } else { |
| 10452 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10453 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10454 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10455 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10456 | } |
| 10457 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10458 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 10459 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10460 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10461 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10462 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10463 | int ret; |
| 10464 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10465 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10466 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10467 | if (entries > IORING_MAX_ENTRIES) { |
| 10468 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10469 | return -EINVAL; |
| 10470 | entries = IORING_MAX_ENTRIES; |
| 10471 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10472 | |
| 10473 | /* |
| 10474 | * Use twice as many entries for the CQ ring. It's possible for the |
| 10475 | * application to drive a higher depth than the size of the SQ ring, |
| 10476 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10477 | * some flexibility in overcommitting a bit. If the application has |
| 10478 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 10479 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10480 | */ |
| 10481 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10482 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 10483 | /* |
| 10484 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 10485 | * to a power-of-two, if it isn't already. We do NOT impose |
| 10486 | * any cq vs sq ring sizing. |
| 10487 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10488 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10489 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10490 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 10491 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10492 | return -EINVAL; |
| 10493 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 10494 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10495 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 10496 | if (p->cq_entries < p->sq_entries) |
| 10497 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10498 | } else { |
| 10499 | p->cq_entries = 2 * p->sq_entries; |
| 10500 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10501 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10502 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10503 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10504 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10505 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10506 | if (!capable(CAP_IPC_LOCK)) |
| 10507 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10508 | |
| 10509 | /* |
| 10510 | * This is just grabbed for accounting purposes. When a process exits, |
| 10511 | * the mm is exited and dropped before the files, hence we need to hang |
| 10512 | * on to this mm purely for the purposes of being able to unaccount |
| 10513 | * memory (locked/pinned vm). It's not used for anything else. |
| 10514 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10515 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10516 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10517 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10518 | ret = io_allocate_scq_urings(ctx, p); |
| 10519 | if (ret) |
| 10520 | goto err; |
| 10521 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10522 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10523 | if (ret) |
| 10524 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10525 | /* always set a rsrc node */ |
Pavel Begunkov | 47b228c | 2021-04-29 11:46:48 +0100 | [diff] [blame] | 10526 | ret = io_rsrc_node_switch_start(ctx); |
| 10527 | if (ret) |
| 10528 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10529 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10530 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10531 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10532 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 10533 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 10534 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 10535 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 10536 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 10537 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 10538 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10539 | |
| 10540 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10541 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 10542 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 10543 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 10544 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 10545 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 10546 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 10547 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 10548 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10549 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 10550 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10551 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10552 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Pavel Begunkov | 9690557 | 2021-06-10 16:37:38 +0100 | [diff] [blame] | 10553 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
Pavel Begunkov | 04c76b4 | 2021-11-10 15:49:32 +0000 | [diff] [blame] | 10554 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10555 | |
| 10556 | if (copy_to_user(params, p, sizeof(*p))) { |
| 10557 | ret = -EFAULT; |
| 10558 | goto err; |
| 10559 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10560 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10561 | file = io_uring_get_file(ctx); |
| 10562 | if (IS_ERR(file)) { |
| 10563 | ret = PTR_ERR(file); |
| 10564 | goto err; |
| 10565 | } |
| 10566 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10567 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10568 | * Install ring fd as the very last thing, so we don't risk someone |
| 10569 | * having closed it before we finish setup |
| 10570 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10571 | ret = io_uring_install_fd(ctx, file); |
| 10572 | if (ret < 0) { |
| 10573 | /* fput will clean it up */ |
| 10574 | fput(file); |
| 10575 | return ret; |
| 10576 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10577 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10578 | 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] | 10579 | return ret; |
| 10580 | err: |
| 10581 | io_ring_ctx_wait_and_kill(ctx); |
| 10582 | return ret; |
| 10583 | } |
| 10584 | |
| 10585 | /* |
| 10586 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 10587 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 10588 | * params structure passed in. |
| 10589 | */ |
| 10590 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 10591 | { |
| 10592 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10593 | int i; |
| 10594 | |
| 10595 | if (copy_from_user(&p, params, sizeof(p))) |
| 10596 | return -EFAULT; |
| 10597 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 10598 | if (p.resv[i]) |
| 10599 | return -EINVAL; |
| 10600 | } |
| 10601 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10602 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10603 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10604 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 10605 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10606 | return -EINVAL; |
| 10607 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10608 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10609 | } |
| 10610 | |
| 10611 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 10612 | struct io_uring_params __user *, params) |
| 10613 | { |
| 10614 | return io_uring_setup(entries, params); |
| 10615 | } |
| 10616 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10617 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
| 10618 | unsigned nr_args) |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10619 | { |
| 10620 | struct io_uring_probe *p; |
| 10621 | size_t size; |
| 10622 | int i, ret; |
| 10623 | |
| 10624 | size = struct_size(p, ops, nr_args); |
| 10625 | if (size == SIZE_MAX) |
| 10626 | return -EOVERFLOW; |
| 10627 | p = kzalloc(size, GFP_KERNEL); |
| 10628 | if (!p) |
| 10629 | return -ENOMEM; |
| 10630 | |
| 10631 | ret = -EFAULT; |
| 10632 | if (copy_from_user(p, arg, size)) |
| 10633 | goto out; |
| 10634 | ret = -EINVAL; |
| 10635 | if (memchr_inv(p, 0, size)) |
| 10636 | goto out; |
| 10637 | |
| 10638 | p->last_op = IORING_OP_LAST - 1; |
| 10639 | if (nr_args > IORING_OP_LAST) |
| 10640 | nr_args = IORING_OP_LAST; |
| 10641 | |
| 10642 | for (i = 0; i < nr_args; i++) { |
| 10643 | p->ops[i].op = i; |
| 10644 | if (!io_op_defs[i].not_supported) |
| 10645 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 10646 | } |
| 10647 | p->ops_len = i; |
| 10648 | |
| 10649 | ret = 0; |
| 10650 | if (copy_to_user(arg, p, size)) |
| 10651 | ret = -EFAULT; |
| 10652 | out: |
| 10653 | kfree(p); |
| 10654 | return ret; |
| 10655 | } |
| 10656 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10657 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 10658 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10659 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10660 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10661 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10662 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10663 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10664 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10665 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 10666 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
Jens Axboe | a30f895 | 2021-08-20 14:53:59 -0600 | [diff] [blame] | 10667 | if (ret < 0) { |
| 10668 | put_cred(creds); |
| 10669 | return ret; |
| 10670 | } |
| 10671 | return id; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10672 | } |
| 10673 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10674 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
| 10675 | void __user *arg, unsigned int nr_args) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10676 | { |
| 10677 | struct io_uring_restriction *res; |
| 10678 | size_t size; |
| 10679 | int i, ret; |
| 10680 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10681 | /* Restrictions allowed only if rings started disabled */ |
| 10682 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10683 | return -EBADFD; |
| 10684 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10685 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10686 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10687 | return -EBUSY; |
| 10688 | |
| 10689 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 10690 | return -EINVAL; |
| 10691 | |
| 10692 | size = array_size(nr_args, sizeof(*res)); |
| 10693 | if (size == SIZE_MAX) |
| 10694 | return -EOVERFLOW; |
| 10695 | |
| 10696 | res = memdup_user(arg, size); |
| 10697 | if (IS_ERR(res)) |
| 10698 | return PTR_ERR(res); |
| 10699 | |
| 10700 | ret = 0; |
| 10701 | |
| 10702 | for (i = 0; i < nr_args; i++) { |
| 10703 | switch (res[i].opcode) { |
| 10704 | case IORING_RESTRICTION_REGISTER_OP: |
| 10705 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 10706 | ret = -EINVAL; |
| 10707 | goto out; |
| 10708 | } |
| 10709 | |
| 10710 | __set_bit(res[i].register_op, |
| 10711 | ctx->restrictions.register_op); |
| 10712 | break; |
| 10713 | case IORING_RESTRICTION_SQE_OP: |
| 10714 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 10715 | ret = -EINVAL; |
| 10716 | goto out; |
| 10717 | } |
| 10718 | |
| 10719 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 10720 | break; |
| 10721 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 10722 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 10723 | break; |
| 10724 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 10725 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 10726 | break; |
| 10727 | default: |
| 10728 | ret = -EINVAL; |
| 10729 | goto out; |
| 10730 | } |
| 10731 | } |
| 10732 | |
| 10733 | out: |
| 10734 | /* Reset all restrictions if an error happened */ |
| 10735 | if (ret != 0) |
| 10736 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10737 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10738 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10739 | |
| 10740 | kfree(res); |
| 10741 | return ret; |
| 10742 | } |
| 10743 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10744 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10745 | { |
| 10746 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10747 | return -EBADFD; |
| 10748 | |
| 10749 | if (ctx->restrictions.registered) |
| 10750 | ctx->restricted = 1; |
| 10751 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 10752 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10753 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 10754 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10755 | return 0; |
| 10756 | } |
| 10757 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10758 | 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] | 10759 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10760 | unsigned nr_args) |
| 10761 | { |
| 10762 | __u32 tmp; |
| 10763 | int err; |
| 10764 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10765 | if (up->resv) |
| 10766 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10767 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
| 10768 | return -EOVERFLOW; |
| 10769 | err = io_rsrc_node_switch_start(ctx); |
| 10770 | if (err) |
| 10771 | return err; |
| 10772 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10773 | switch (type) { |
| 10774 | case IORING_RSRC_FILE: |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10775 | return __io_sqe_files_update(ctx, up, nr_args); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10776 | case IORING_RSRC_BUFFER: |
| 10777 | return __io_sqe_buffers_update(ctx, up, nr_args); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10778 | } |
| 10779 | return -EINVAL; |
| 10780 | } |
| 10781 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10782 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 10783 | unsigned nr_args) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10784 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10785 | struct io_uring_rsrc_update2 up; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10786 | |
| 10787 | if (!nr_args) |
| 10788 | return -EINVAL; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10789 | memset(&up, 0, sizeof(up)); |
| 10790 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) |
| 10791 | return -EFAULT; |
| 10792 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
| 10793 | } |
| 10794 | |
| 10795 | 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] | 10796 | unsigned size, unsigned type) |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10797 | { |
| 10798 | struct io_uring_rsrc_update2 up; |
| 10799 | |
| 10800 | if (size != sizeof(up)) |
| 10801 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10802 | if (copy_from_user(&up, arg, sizeof(up))) |
| 10803 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10804 | if (!up.nr || up.resv) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10805 | return -EINVAL; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10806 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10807 | } |
| 10808 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10809 | 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] | 10810 | unsigned int size, unsigned int type) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10811 | { |
| 10812 | struct io_uring_rsrc_register rr; |
| 10813 | |
| 10814 | /* keep it extendible */ |
| 10815 | if (size != sizeof(rr)) |
| 10816 | return -EINVAL; |
| 10817 | |
| 10818 | memset(&rr, 0, sizeof(rr)); |
| 10819 | if (copy_from_user(&rr, arg, size)) |
| 10820 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10821 | if (!rr.nr || rr.resv || rr.resv2) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10822 | return -EINVAL; |
| 10823 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10824 | switch (type) { |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10825 | case IORING_RSRC_FILE: |
| 10826 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), |
| 10827 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10828 | case IORING_RSRC_BUFFER: |
| 10829 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), |
| 10830 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10831 | } |
| 10832 | return -EINVAL; |
| 10833 | } |
| 10834 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10835 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
| 10836 | void __user *arg, unsigned len) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10837 | { |
| 10838 | struct io_uring_task *tctx = current->io_uring; |
| 10839 | cpumask_var_t new_mask; |
| 10840 | int ret; |
| 10841 | |
| 10842 | if (!tctx || !tctx->io_wq) |
| 10843 | return -EINVAL; |
| 10844 | |
| 10845 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 10846 | return -ENOMEM; |
| 10847 | |
| 10848 | cpumask_clear(new_mask); |
| 10849 | if (len > cpumask_size()) |
| 10850 | len = cpumask_size(); |
| 10851 | |
| 10852 | if (copy_from_user(new_mask, arg, len)) { |
| 10853 | free_cpumask_var(new_mask); |
| 10854 | return -EFAULT; |
| 10855 | } |
| 10856 | |
| 10857 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); |
| 10858 | free_cpumask_var(new_mask); |
| 10859 | return ret; |
| 10860 | } |
| 10861 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10862 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10863 | { |
| 10864 | struct io_uring_task *tctx = current->io_uring; |
| 10865 | |
| 10866 | if (!tctx || !tctx->io_wq) |
| 10867 | return -EINVAL; |
| 10868 | |
| 10869 | return io_wq_cpu_affinity(tctx->io_wq, NULL); |
| 10870 | } |
| 10871 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10872 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
| 10873 | void __user *arg) |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10874 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10875 | { |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10876 | struct io_tctx_node *node; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10877 | struct io_uring_task *tctx = NULL; |
| 10878 | struct io_sq_data *sqd = NULL; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10879 | __u32 new_count[2]; |
| 10880 | int i, ret; |
| 10881 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10882 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
| 10883 | return -EFAULT; |
| 10884 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10885 | if (new_count[i] > INT_MAX) |
| 10886 | return -EINVAL; |
| 10887 | |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10888 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 10889 | sqd = ctx->sq_data; |
| 10890 | if (sqd) { |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10891 | /* |
| 10892 | * Observe the correct sqd->lock -> ctx->uring_lock |
| 10893 | * ordering. Fine to drop uring_lock here, we hold |
| 10894 | * a ref to the ctx. |
| 10895 | */ |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10896 | refcount_inc(&sqd->refs); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10897 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10898 | mutex_lock(&sqd->lock); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10899 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10900 | if (sqd->thread) |
| 10901 | tctx = sqd->thread->io_uring; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10902 | } |
| 10903 | } else { |
| 10904 | tctx = current->io_uring; |
| 10905 | } |
| 10906 | |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10907 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10908 | |
Pavel Begunkov | bad119b | 2021-11-08 15:10:03 +0000 | [diff] [blame] | 10909 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10910 | if (new_count[i]) |
| 10911 | ctx->iowq_limits[i] = new_count[i]; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10912 | ctx->iowq_limits_set = true; |
| 10913 | |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10914 | if (tctx && tctx->io_wq) { |
| 10915 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
| 10916 | if (ret) |
| 10917 | goto err; |
| 10918 | } else { |
| 10919 | memset(new_count, 0, sizeof(new_count)); |
| 10920 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10921 | |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10922 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10923 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10924 | io_put_sq_data(sqd); |
| 10925 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10926 | |
| 10927 | if (copy_to_user(arg, new_count, sizeof(new_count))) |
| 10928 | return -EFAULT; |
| 10929 | |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10930 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
| 10931 | if (sqd) |
| 10932 | return 0; |
| 10933 | |
| 10934 | /* now propagate the restriction to all registered users */ |
| 10935 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 10936 | struct io_uring_task *tctx = node->task->io_uring; |
| 10937 | |
| 10938 | if (WARN_ON_ONCE(!tctx->io_wq)) |
| 10939 | continue; |
| 10940 | |
| 10941 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10942 | new_count[i] = ctx->iowq_limits[i]; |
| 10943 | /* ignore errors, it always returns zero anyway */ |
| 10944 | (void)io_wq_max_workers(tctx->io_wq, new_count); |
| 10945 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10946 | return 0; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10947 | err: |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10948 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10949 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10950 | io_put_sq_data(sqd); |
| 10951 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10952 | return ret; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10953 | } |
| 10954 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10955 | static bool io_register_op_must_quiesce(int op) |
| 10956 | { |
| 10957 | switch (op) { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 10958 | case IORING_REGISTER_BUFFERS: |
| 10959 | case IORING_UNREGISTER_BUFFERS: |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 10960 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10961 | case IORING_UNREGISTER_FILES: |
| 10962 | case IORING_REGISTER_FILES_UPDATE: |
| 10963 | case IORING_REGISTER_PROBE: |
| 10964 | case IORING_REGISTER_PERSONALITY: |
| 10965 | case IORING_UNREGISTER_PERSONALITY: |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10966 | case IORING_REGISTER_FILES2: |
| 10967 | case IORING_REGISTER_FILES_UPDATE2: |
| 10968 | case IORING_REGISTER_BUFFERS2: |
| 10969 | case IORING_REGISTER_BUFFERS_UPDATE: |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10970 | case IORING_REGISTER_IOWQ_AFF: |
| 10971 | case IORING_UNREGISTER_IOWQ_AFF: |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10972 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10973 | return false; |
| 10974 | default: |
| 10975 | return true; |
| 10976 | } |
| 10977 | } |
| 10978 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10979 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10980 | { |
| 10981 | long ret; |
| 10982 | |
| 10983 | percpu_ref_kill(&ctx->refs); |
| 10984 | |
| 10985 | /* |
| 10986 | * Drop uring mutex before waiting for references to exit. If another |
| 10987 | * thread is currently inside io_uring_enter() it might need to grab the |
| 10988 | * uring_lock to make progress. If we hold it here across the drain |
| 10989 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 10990 | * no new references will come in after we've killed the percpu ref. |
| 10991 | */ |
| 10992 | mutex_unlock(&ctx->uring_lock); |
| 10993 | do { |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10994 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
| 10995 | if (ret) { |
| 10996 | ret = min(0L, ret); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10997 | break; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10998 | } |
| 10999 | |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 11000 | ret = io_run_task_work_sig(); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 11001 | io_req_caches_free(ctx); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 11002 | } while (ret >= 0); |
| 11003 | mutex_lock(&ctx->uring_lock); |
| 11004 | |
| 11005 | if (ret) |
| 11006 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 11007 | return ret; |
| 11008 | } |
| 11009 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11010 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 11011 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 11012 | __releases(ctx->uring_lock) |
| 11013 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11014 | { |
| 11015 | int ret; |
| 11016 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 11017 | /* |
| 11018 | * We're inside the ring mutex, if the ref is already dying, then |
| 11019 | * someone else killed the ctx or is already going through |
| 11020 | * io_uring_register(). |
| 11021 | */ |
| 11022 | if (percpu_ref_is_dying(&ctx->refs)) |
| 11023 | return -ENXIO; |
| 11024 | |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 11025 | if (ctx->restricted) { |
| 11026 | if (opcode >= IORING_REGISTER_LAST) |
| 11027 | return -EINVAL; |
| 11028 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); |
| 11029 | if (!test_bit(opcode, ctx->restrictions.register_op)) |
| 11030 | return -EACCES; |
| 11031 | } |
| 11032 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11033 | if (io_register_op_must_quiesce(opcode)) { |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 11034 | ret = io_ctx_quiesce(ctx); |
| 11035 | if (ret) |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 11036 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11037 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11038 | |
| 11039 | switch (opcode) { |
| 11040 | case IORING_REGISTER_BUFFERS: |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 11041 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11042 | break; |
| 11043 | case IORING_UNREGISTER_BUFFERS: |
| 11044 | ret = -EINVAL; |
| 11045 | if (arg || nr_args) |
| 11046 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 11047 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11048 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 11049 | case IORING_REGISTER_FILES: |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 11050 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 11051 | break; |
| 11052 | case IORING_UNREGISTER_FILES: |
| 11053 | ret = -EINVAL; |
| 11054 | if (arg || nr_args) |
| 11055 | break; |
| 11056 | ret = io_sqe_files_unregister(ctx); |
| 11057 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 11058 | case IORING_REGISTER_FILES_UPDATE: |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 11059 | ret = io_register_files_update(ctx, arg, nr_args); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 11060 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 11061 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 11062 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 11063 | ret = -EINVAL; |
| 11064 | if (nr_args != 1) |
| 11065 | break; |
| 11066 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 11067 | if (ret) |
| 11068 | break; |
| 11069 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 11070 | ctx->eventfd_async = 1; |
| 11071 | else |
| 11072 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 11073 | break; |
| 11074 | case IORING_UNREGISTER_EVENTFD: |
| 11075 | ret = -EINVAL; |
| 11076 | if (arg || nr_args) |
| 11077 | break; |
| 11078 | ret = io_eventfd_unregister(ctx); |
| 11079 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 11080 | case IORING_REGISTER_PROBE: |
| 11081 | ret = -EINVAL; |
| 11082 | if (!arg || nr_args > 256) |
| 11083 | break; |
| 11084 | ret = io_probe(ctx, arg, nr_args); |
| 11085 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11086 | case IORING_REGISTER_PERSONALITY: |
| 11087 | ret = -EINVAL; |
| 11088 | if (arg || nr_args) |
| 11089 | break; |
| 11090 | ret = io_register_personality(ctx); |
| 11091 | break; |
| 11092 | case IORING_UNREGISTER_PERSONALITY: |
| 11093 | ret = -EINVAL; |
| 11094 | if (arg) |
| 11095 | break; |
| 11096 | ret = io_unregister_personality(ctx, nr_args); |
| 11097 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 11098 | case IORING_REGISTER_ENABLE_RINGS: |
| 11099 | ret = -EINVAL; |
| 11100 | if (arg || nr_args) |
| 11101 | break; |
| 11102 | ret = io_register_enable_rings(ctx); |
| 11103 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 11104 | case IORING_REGISTER_RESTRICTIONS: |
| 11105 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 11106 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11107 | case IORING_REGISTER_FILES2: |
| 11108 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 11109 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11110 | case IORING_REGISTER_FILES_UPDATE2: |
| 11111 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11112 | IORING_RSRC_FILE); |
| 11113 | break; |
| 11114 | case IORING_REGISTER_BUFFERS2: |
| 11115 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); |
| 11116 | break; |
| 11117 | case IORING_REGISTER_BUFFERS_UPDATE: |
| 11118 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11119 | IORING_RSRC_BUFFER); |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 11120 | break; |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 11121 | case IORING_REGISTER_IOWQ_AFF: |
| 11122 | ret = -EINVAL; |
| 11123 | if (!arg || !nr_args) |
| 11124 | break; |
| 11125 | ret = io_register_iowq_aff(ctx, arg, nr_args); |
| 11126 | break; |
| 11127 | case IORING_UNREGISTER_IOWQ_AFF: |
| 11128 | ret = -EINVAL; |
| 11129 | if (arg || nr_args) |
| 11130 | break; |
| 11131 | ret = io_unregister_iowq_aff(ctx); |
| 11132 | break; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11133 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
| 11134 | ret = -EINVAL; |
| 11135 | if (!arg || nr_args != 2) |
| 11136 | break; |
| 11137 | ret = io_register_iowq_max_workers(ctx, arg); |
| 11138 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11139 | default: |
| 11140 | ret = -EINVAL; |
| 11141 | break; |
| 11142 | } |
| 11143 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11144 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11145 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11146 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 11147 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11148 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11149 | return ret; |
| 11150 | } |
| 11151 | |
| 11152 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 11153 | void __user *, arg, unsigned int, nr_args) |
| 11154 | { |
| 11155 | struct io_ring_ctx *ctx; |
| 11156 | long ret = -EBADF; |
| 11157 | struct fd f; |
| 11158 | |
| 11159 | f = fdget(fd); |
| 11160 | if (!f.file) |
| 11161 | return -EBADF; |
| 11162 | |
| 11163 | ret = -EOPNOTSUPP; |
| 11164 | if (f.file->f_op != &io_uring_fops) |
| 11165 | goto out_fput; |
| 11166 | |
| 11167 | ctx = f.file->private_data; |
| 11168 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 11169 | io_run_task_work(); |
| 11170 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11171 | mutex_lock(&ctx->uring_lock); |
| 11172 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 11173 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 11174 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 11175 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11176 | out_fput: |
| 11177 | fdput(f); |
| 11178 | return ret; |
| 11179 | } |
| 11180 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11181 | static int __init io_uring_init(void) |
| 11182 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11183 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 11184 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 11185 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 11186 | } while (0) |
| 11187 | |
| 11188 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 11189 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 11190 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 11191 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 11192 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 11193 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 11194 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 11195 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 11196 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 11197 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11198 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11199 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 11200 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 11201 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 11202 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 11203 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 11204 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 11205 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11206 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 11207 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 11208 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 11209 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 11210 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 11211 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 11212 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 11213 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11214 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11215 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 11216 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11217 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11218 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11219 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 11220 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11221 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11222 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
| 11223 | sizeof(struct io_uring_rsrc_update)); |
| 11224 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > |
| 11225 | sizeof(struct io_uring_rsrc_update2)); |
Pavel Begunkov | 90499ad | 2021-08-25 20:51:40 +0100 | [diff] [blame] | 11226 | |
| 11227 | /* ->buf_index is u16 */ |
| 11228 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); |
| 11229 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11230 | /* should fit into one byte */ |
| 11231 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 11232 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
| 11233 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11234 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 11235 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Hao Xu | 32c2d33 | 2021-09-07 11:22:43 +0800 | [diff] [blame] | 11236 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11237 | |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 11238 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 11239 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11240 | return 0; |
| 11241 | }; |
| 11242 | __initcall(io_uring_init); |