Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 47 | #include <net/compat.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 48 | #include <linux/refcount.h> |
| 49 | #include <linux/uio.h> |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 50 | #include <linux/bits.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 51 | |
| 52 | #include <linux/sched/signal.h> |
| 53 | #include <linux/fs.h> |
| 54 | #include <linux/file.h> |
| 55 | #include <linux/fdtable.h> |
| 56 | #include <linux/mm.h> |
| 57 | #include <linux/mman.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 58 | #include <linux/percpu.h> |
| 59 | #include <linux/slab.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 60 | #include <linux/blkdev.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 61 | #include <linux/bvec.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 62 | #include <linux/net.h> |
| 63 | #include <net/sock.h> |
| 64 | #include <net/af_unix.h> |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 65 | #include <net/scm.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 66 | #include <linux/anon_inodes.h> |
| 67 | #include <linux/sched/mm.h> |
| 68 | #include <linux/uaccess.h> |
| 69 | #include <linux/nospec.h> |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 70 | #include <linux/sizes.h> |
| 71 | #include <linux/hugetlb.h> |
Jens Axboe | aa4c396 | 2019-11-29 10:14:00 -0700 | [diff] [blame] | 72 | #include <linux/highmem.h> |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 73 | #include <linux/namei.h> |
| 74 | #include <linux/fsnotify.h> |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 75 | #include <linux/fadvise.h> |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 76 | #include <linux/eventpoll.h> |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 77 | #include <linux/splice.h> |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 78 | #include <linux/task_work.h> |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 79 | #include <linux/pagemap.h> |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 80 | #include <linux/io_uring.h> |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 81 | #include <linux/tracehook.h> |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 82 | #include <linux/audit.h> |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 83 | #include <linux/security.h> |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 84 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 85 | #define CREATE_TRACE_POINTS |
| 86 | #include <trace/events/io_uring.h> |
| 87 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 88 | #include <uapi/linux/io_uring.h> |
| 89 | |
| 90 | #include "internal.h" |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 91 | #include "io-wq.h" |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 92 | |
Daniel Xu | 5277dea | 2019-09-14 14:23:45 -0700 | [diff] [blame] | 93 | #define IORING_MAX_ENTRIES 32768 |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 95 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 96 | |
wangyangbo | 187f08c | 2021-08-19 13:56:57 +0800 | [diff] [blame] | 97 | /* only define max */ |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 98 | #define IORING_MAX_FIXED_FILES (1U << 15) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 99 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| 100 | IORING_REGISTER_LAST + IORING_OP_LAST) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 101 | |
wangyangbo | 187f08c | 2021-08-19 13:56:57 +0800 | [diff] [blame] | 102 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 103 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
| 104 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) |
| 105 | |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 106 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
| 107 | |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 108 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
| 109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
| 110 | |
| 111 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS|IOSQE_BUFFER_SELECT|IOSQE_IO_DRAIN) |
| 112 | |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 113 | #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] | 114 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
| 115 | REQ_F_ASYNC_DATA) |
Pavel Begunkov | b16fed66 | 2021-02-18 18:29:40 +0000 | [diff] [blame] | 116 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 117 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
| 118 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 119 | struct io_uring { |
| 120 | u32 head ____cacheline_aligned_in_smp; |
| 121 | u32 tail ____cacheline_aligned_in_smp; |
| 122 | }; |
| 123 | |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 124 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 125 | * This data is shared with the application through the mmap at offsets |
| 126 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 127 | * |
| 128 | * The offsets to the member fields are published through struct |
| 129 | * io_sqring_offsets when calling io_uring_setup. |
| 130 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 131 | struct io_rings { |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 132 | /* |
| 133 | * Head and tail offsets into the ring; the offsets need to be |
| 134 | * masked to get valid indices. |
| 135 | * |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 136 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 137 | * and the application controls tail of the sq ring and the head of the |
| 138 | * cq ring. |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 139 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 140 | struct io_uring sq, cq; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 141 | /* |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 142 | * Bitmasks to apply to head and tail offsets (constant, equals |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 143 | * ring_entries - 1) |
| 144 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 145 | u32 sq_ring_mask, cq_ring_mask; |
| 146 | /* Ring sizes (constant, power of 2) */ |
| 147 | u32 sq_ring_entries, cq_ring_entries; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Number of invalid entries dropped by the kernel due to |
| 150 | * invalid index stored in array |
| 151 | * |
| 152 | * Written by the kernel, shouldn't be modified by the |
| 153 | * application (i.e. get number of "new events" by comparing to |
| 154 | * cached value). |
| 155 | * |
| 156 | * After a new SQ head value was read by the application this |
| 157 | * counter includes all submissions that were dropped reaching |
| 158 | * the new SQ head (and possibly more). |
| 159 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 160 | u32 sq_dropped; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 161 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 162 | * Runtime SQ flags |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 163 | * |
| 164 | * Written by the kernel, shouldn't be modified by the |
| 165 | * application. |
| 166 | * |
| 167 | * The application needs a full memory barrier before checking |
| 168 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 169 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 170 | u32 sq_flags; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 171 | /* |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 172 | * Runtime CQ flags |
| 173 | * |
| 174 | * Written by the application, shouldn't be modified by the |
| 175 | * kernel. |
| 176 | */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 177 | u32 cq_flags; |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 178 | /* |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 179 | * Number of completion events lost because the queue was full; |
| 180 | * this should be avoided by the application by making sure |
LimingWu | 0b4295b | 2019-12-05 20:18:18 +0800 | [diff] [blame] | 181 | * there are not more requests pending than there is space in |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 182 | * the completion queue. |
| 183 | * |
| 184 | * Written by the kernel, shouldn't be modified by the |
| 185 | * application (i.e. get number of "new events" by comparing to |
| 186 | * cached value). |
| 187 | * |
| 188 | * As completion events come in out of order this counter is not |
| 189 | * ordered with any other data. |
| 190 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 191 | u32 cq_overflow; |
Stefan Bühler | 1e84b97 | 2019-04-24 23:54:16 +0200 | [diff] [blame] | 192 | /* |
| 193 | * Ring buffer of completion events. |
| 194 | * |
| 195 | * The kernel writes completion events fresh every time they are |
| 196 | * produced, so the application is allowed to modify pending |
| 197 | * entries. |
| 198 | */ |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 199 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 202 | enum io_uring_cmd_flags { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 203 | IO_URING_F_COMPLETE_DEFER = 1, |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 204 | IO_URING_F_UNLOCKED = 2, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 205 | /* int's last bit, sign checks are usually faster than a bit test */ |
| 206 | IO_URING_F_NONBLOCK = INT_MIN, |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 209 | struct io_mapped_ubuf { |
| 210 | u64 ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 211 | u64 ubuf_end; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 212 | unsigned int nr_bvecs; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 213 | unsigned long acct_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 214 | struct bio_vec bvec[]; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 217 | struct io_ring_ctx; |
| 218 | |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 219 | struct io_overflow_cqe { |
| 220 | struct io_uring_cqe cqe; |
| 221 | struct list_head list; |
| 222 | }; |
| 223 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 224 | struct io_fixed_file { |
| 225 | /* file * with additional FFS_* flags */ |
| 226 | unsigned long file_ptr; |
| 227 | }; |
| 228 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 229 | struct io_rsrc_put { |
| 230 | struct list_head list; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 231 | u64 tag; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 232 | union { |
| 233 | void *rsrc; |
| 234 | struct file *file; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 235 | struct io_mapped_ubuf *buf; |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 236 | }; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 239 | struct io_file_table { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 240 | struct io_fixed_file *files; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 243 | struct io_rsrc_node { |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 244 | struct percpu_ref refs; |
| 245 | struct list_head node; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 246 | struct list_head rsrc_list; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 247 | struct io_rsrc_data *rsrc_data; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 248 | struct llist_node llist; |
Pavel Begunkov | e297822 | 2020-11-18 14:56:26 +0000 | [diff] [blame] | 249 | bool done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 250 | }; |
| 251 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 252 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
| 253 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 254 | struct io_rsrc_data { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 255 | struct io_ring_ctx *ctx; |
| 256 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 257 | u64 **tags; |
| 258 | unsigned int nr; |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 259 | rsrc_put_fn *do_put; |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 260 | atomic_t refs; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 261 | struct completion done; |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 262 | bool quiesce; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 265 | struct io_buffer { |
| 266 | struct list_head list; |
| 267 | __u64 addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 268 | __u32 len; |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 269 | __u16 bid; |
| 270 | }; |
| 271 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 272 | struct io_restriction { |
| 273 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| 274 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| 275 | u8 sqe_flags_allowed; |
| 276 | u8 sqe_flags_required; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 277 | bool registered; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 278 | }; |
| 279 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 280 | enum { |
| 281 | IO_SQ_THREAD_SHOULD_STOP = 0, |
| 282 | IO_SQ_THREAD_SHOULD_PARK, |
| 283 | }; |
| 284 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 285 | struct io_sq_data { |
| 286 | refcount_t refs; |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 287 | atomic_t park_pending; |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 288 | struct mutex lock; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 289 | |
| 290 | /* ctx's that are using this sqd */ |
| 291 | struct list_head ctx_list; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 292 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 293 | struct task_struct *thread; |
| 294 | struct wait_queue_head wait; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 295 | |
| 296 | unsigned sq_thread_idle; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 297 | int sq_cpu; |
| 298 | pid_t task_pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 299 | pid_t task_tgid; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 300 | |
| 301 | unsigned long state; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 302 | struct completion exited; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 303 | }; |
| 304 | |
Pavel Begunkov | 6dd0be1 | 2021-02-10 00:03:13 +0000 | [diff] [blame] | 305 | #define IO_COMPL_BATCH 32 |
Pavel Begunkov | 6ff119a | 2021-02-10 00:03:18 +0000 | [diff] [blame] | 306 | #define IO_REQ_CACHE_SIZE 32 |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 307 | #define IO_REQ_ALLOC_BATCH 8 |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 308 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 309 | struct io_submit_link { |
| 310 | struct io_kiocb *head; |
| 311 | struct io_kiocb *last; |
| 312 | }; |
| 313 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 314 | struct io_submit_state { |
Pavel Begunkov | 5a158c6 | 2021-10-06 16:06:48 +0100 | [diff] [blame] | 315 | /* inline/task_work completion list, under ->uring_lock */ |
| 316 | struct io_wq_work_node free_list; |
| 317 | /* batch completion logic */ |
| 318 | struct io_wq_work_list compl_reqs; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 319 | struct io_submit_link link; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 320 | |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 321 | bool plug_started; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 322 | bool need_plug; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 323 | unsigned short submit_nr; |
Pavel Begunkov | 5a158c6 | 2021-10-06 16:06:48 +0100 | [diff] [blame] | 324 | struct blk_plug plug; |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 327 | struct io_ring_ctx { |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 328 | /* const or read-mostly hot data */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 329 | struct { |
| 330 | struct percpu_ref refs; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 331 | |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 332 | struct io_rings *rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 333 | unsigned int flags; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 334 | unsigned int compat: 1; |
Randy Dunlap | e1d8533 | 2020-02-05 20:57:10 -0800 | [diff] [blame] | 335 | unsigned int drain_next: 1; |
| 336 | unsigned int eventfd_async: 1; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 337 | unsigned int restricted: 1; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 338 | unsigned int off_timeout_used: 1; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 339 | unsigned int drain_active: 1; |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 340 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 341 | |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 342 | /* submission data */ |
Pavel Begunkov | b52ecf8 | 2021-06-14 23:37:21 +0100 | [diff] [blame] | 343 | struct { |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 344 | struct mutex uring_lock; |
| 345 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 346 | /* |
| 347 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 348 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 349 | * |
| 350 | * This indirection could e.g. be used to assign fixed |
| 351 | * io_uring_sqe entries to operations and only submit them to |
| 352 | * the queue when needed. |
| 353 | * |
| 354 | * The kernel modifies neither the indices array nor the entries |
| 355 | * array. |
| 356 | */ |
| 357 | u32 *sq_array; |
Pavel Begunkov | c7af47c | 2021-06-14 23:37:20 +0100 | [diff] [blame] | 358 | struct io_uring_sqe *sq_sqes; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 359 | unsigned cached_sq_head; |
| 360 | unsigned sq_entries; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 361 | struct list_head defer_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * Fixed resources fast path, should be accessed only under |
| 365 | * uring_lock, and updated through io_uring_register(2) |
| 366 | */ |
| 367 | struct io_rsrc_node *rsrc_node; |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 368 | int rsrc_cached_refs; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 369 | struct io_file_table file_table; |
| 370 | unsigned nr_user_files; |
| 371 | unsigned nr_user_bufs; |
| 372 | struct io_mapped_ubuf **user_bufs; |
| 373 | |
| 374 | struct io_submit_state submit_state; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 375 | struct list_head timeout_list; |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 376 | struct list_head ltimeout_list; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 377 | struct list_head cq_overflow_list; |
Pavel Begunkov | 7f1129d | 2021-06-14 23:37:22 +0100 | [diff] [blame] | 378 | struct xarray io_buffers; |
| 379 | struct xarray personalities; |
| 380 | u32 pers_next; |
| 381 | unsigned sq_thread_idle; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 382 | } ____cacheline_aligned_in_smp; |
| 383 | |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 384 | /* IRQ completion list, under ->completion_lock */ |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 385 | struct io_wq_work_list locked_free_list; |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 386 | unsigned int locked_free_nr; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 387 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 388 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 389 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
| 390 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 391 | struct wait_queue_head sqo_sq_wait; |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 392 | struct list_head sqd_list; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 393 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 394 | unsigned long check_cq_overflow; |
| 395 | |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 396 | struct { |
| 397 | unsigned cached_cq_tail; |
| 398 | unsigned cq_entries; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 399 | struct eventfd_ctx *cq_ev_fd; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 400 | struct wait_queue_head cq_wait; |
| 401 | unsigned cq_extra; |
| 402 | atomic_t cq_timeouts; |
Pavel Begunkov | 0499e58 | 2021-06-14 23:37:29 +0100 | [diff] [blame] | 403 | unsigned cq_last_tm_flush; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 404 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 405 | |
| 406 | struct { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 407 | spinlock_t completion_lock; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 408 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 409 | spinlock_t timeout_lock; |
| 410 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 411 | /* |
Pavel Begunkov | 540e32a | 2020-07-13 23:37:09 +0300 | [diff] [blame] | 412 | * ->iopoll_list is protected by the ctx->uring_lock for |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 413 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 414 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 415 | * manipulate the list, hence no extra locking is needed there. |
| 416 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 417 | struct io_wq_work_list iopoll_list; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 418 | struct hlist_head *cancel_hash; |
| 419 | unsigned cancel_hash_bits; |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 420 | bool poll_multi_queue; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 421 | } ____cacheline_aligned_in_smp; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 422 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 423 | struct io_restriction restrictions; |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 424 | |
Pavel Begunkov | b13a891 | 2021-05-16 22:58:07 +0100 | [diff] [blame] | 425 | /* slow path rsrc auxilary data, used by update/register */ |
| 426 | struct { |
| 427 | struct io_rsrc_node *rsrc_backup_node; |
| 428 | struct io_mapped_ubuf *dummy_ubuf; |
| 429 | struct io_rsrc_data *file_data; |
| 430 | struct io_rsrc_data *buf_data; |
| 431 | |
| 432 | struct delayed_work rsrc_put_work; |
| 433 | struct llist_head rsrc_put_llist; |
| 434 | struct list_head rsrc_ref_list; |
| 435 | spinlock_t rsrc_ref_lock; |
| 436 | }; |
| 437 | |
Jens Axboe | 3c1a2ea | 2021-02-11 10:48:03 -0700 | [diff] [blame] | 438 | /* Keep this last, we don't need it for the fast path */ |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 439 | struct { |
| 440 | #if defined(CONFIG_UNIX) |
| 441 | struct socket *ring_sock; |
| 442 | #endif |
| 443 | /* hashed buffered write serialization */ |
| 444 | struct io_wq_hash *hash_map; |
| 445 | |
| 446 | /* Only used for accounting purposes */ |
| 447 | struct user_struct *user; |
| 448 | struct mm_struct *mm_account; |
| 449 | |
| 450 | /* ctx exit and cancelation */ |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 451 | struct llist_head fallback_llist; |
| 452 | struct delayed_work fallback_work; |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 453 | struct work_struct exit_work; |
| 454 | struct list_head tctx_list; |
| 455 | struct completion ref_comp; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 456 | u32 iowq_limits[2]; |
| 457 | bool iowq_limits_set; |
Pavel Begunkov | b986af7 | 2021-05-16 22:58:06 +0100 | [diff] [blame] | 458 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 459 | }; |
| 460 | |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 461 | struct io_uring_task { |
| 462 | /* submission side */ |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 463 | int cached_refs; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 464 | struct xarray xa; |
| 465 | struct wait_queue_head wait; |
Stefan Metzmacher | ee53fb2 | 2021-03-15 12:56:57 +0100 | [diff] [blame] | 466 | const struct io_ring_ctx *last; |
| 467 | struct io_wq *io_wq; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 468 | struct percpu_counter inflight; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 469 | atomic_t inflight_tracked; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 470 | atomic_t in_idle; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 471 | |
| 472 | spinlock_t task_lock; |
| 473 | struct io_wq_work_list task_list; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 474 | struct callback_head task_work; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 475 | bool task_running; |
Stefan Metzmacher | 53e043b | 2021-03-15 12:56:56 +0100 | [diff] [blame] | 476 | }; |
| 477 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 478 | /* |
| 479 | * First field must be the file pointer in all the |
| 480 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 481 | */ |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 482 | struct io_poll_iocb { |
| 483 | struct file *file; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 484 | struct wait_queue_head *head; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 485 | __poll_t events; |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 486 | bool done; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 487 | bool canceled; |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 488 | struct wait_queue_entry wait; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 489 | }; |
| 490 | |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 491 | struct io_poll_update { |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 492 | struct file *file; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 493 | u64 old_user_data; |
| 494 | u64 new_user_data; |
| 495 | __poll_t events; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 496 | bool update_events; |
| 497 | bool update_user_data; |
Pavel Begunkov | 018043b | 2020-10-27 23:17:18 +0000 | [diff] [blame] | 498 | }; |
| 499 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 500 | struct io_close { |
| 501 | struct file *file; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 502 | int fd; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 503 | u32 file_slot; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 504 | }; |
| 505 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 506 | struct io_timeout_data { |
| 507 | struct io_kiocb *req; |
| 508 | struct hrtimer timer; |
| 509 | struct timespec64 ts; |
| 510 | enum hrtimer_mode mode; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 511 | u32 flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 512 | }; |
| 513 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 514 | struct io_accept { |
| 515 | struct file *file; |
| 516 | struct sockaddr __user *addr; |
| 517 | int __user *addr_len; |
| 518 | int flags; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 519 | u32 file_slot; |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 520 | unsigned long nofile; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | struct io_sync { |
| 524 | struct file *file; |
| 525 | loff_t len; |
| 526 | loff_t off; |
| 527 | int flags; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 528 | int mode; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 529 | }; |
| 530 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 531 | struct io_cancel { |
| 532 | struct file *file; |
| 533 | u64 addr; |
| 534 | }; |
| 535 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 536 | struct io_timeout { |
| 537 | struct file *file; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 538 | u32 off; |
| 539 | u32 target_seq; |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 540 | struct list_head list; |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 541 | /* head of the link, used by linked timeouts only */ |
| 542 | struct io_kiocb *head; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 543 | /* for linked completions */ |
| 544 | struct io_kiocb *prev; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 545 | }; |
| 546 | |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 547 | struct io_timeout_rem { |
| 548 | struct file *file; |
| 549 | u64 addr; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 550 | |
| 551 | /* timeout update */ |
| 552 | struct timespec64 ts; |
| 553 | u32 flags; |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 554 | bool ltimeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 555 | }; |
| 556 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 557 | struct io_rw { |
| 558 | /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| 559 | struct kiocb kiocb; |
| 560 | u64 addr; |
| 561 | u64 len; |
| 562 | }; |
| 563 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 564 | struct io_connect { |
| 565 | struct file *file; |
| 566 | struct sockaddr __user *addr; |
| 567 | int addr_len; |
| 568 | }; |
| 569 | |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 570 | struct io_sr_msg { |
| 571 | struct file *file; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 572 | union { |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 573 | struct compat_msghdr __user *umsg_compat; |
| 574 | struct user_msghdr __user *umsg; |
| 575 | void __user *buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 576 | }; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 577 | int msg_flags; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 578 | int bgid; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 579 | size_t len; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 580 | }; |
| 581 | |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 582 | struct io_open { |
| 583 | struct file *file; |
| 584 | int dfd; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 585 | u32 file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 586 | struct filename *filename; |
Jens Axboe | c12cedf | 2020-01-08 17:41:21 -0700 | [diff] [blame] | 587 | struct open_how how; |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 588 | unsigned long nofile; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 589 | }; |
| 590 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 591 | struct io_rsrc_update { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 592 | struct file *file; |
| 593 | u64 arg; |
| 594 | u32 nr_args; |
| 595 | u32 offset; |
| 596 | }; |
| 597 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 598 | struct io_fadvise { |
| 599 | struct file *file; |
| 600 | u64 offset; |
| 601 | u32 len; |
| 602 | u32 advice; |
| 603 | }; |
| 604 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 605 | struct io_madvise { |
| 606 | struct file *file; |
| 607 | u64 addr; |
| 608 | u32 len; |
| 609 | u32 advice; |
| 610 | }; |
| 611 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 612 | struct io_epoll { |
| 613 | struct file *file; |
| 614 | int epfd; |
| 615 | int op; |
| 616 | int fd; |
| 617 | struct epoll_event event; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 618 | }; |
| 619 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 620 | struct io_splice { |
| 621 | struct file *file_out; |
| 622 | struct file *file_in; |
| 623 | loff_t off_out; |
| 624 | loff_t off_in; |
| 625 | u64 len; |
| 626 | unsigned int flags; |
| 627 | }; |
| 628 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 629 | struct io_provide_buf { |
| 630 | struct file *file; |
| 631 | __u64 addr; |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 632 | __u32 len; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 633 | __u32 bgid; |
| 634 | __u16 nbufs; |
| 635 | __u16 bid; |
| 636 | }; |
| 637 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 638 | struct io_statx { |
| 639 | struct file *file; |
| 640 | int dfd; |
| 641 | unsigned int mask; |
| 642 | unsigned int flags; |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 643 | const char __user *filename; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 644 | struct statx __user *buffer; |
| 645 | }; |
| 646 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 647 | struct io_shutdown { |
| 648 | struct file *file; |
| 649 | int how; |
| 650 | }; |
| 651 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 652 | struct io_rename { |
| 653 | struct file *file; |
| 654 | int old_dfd; |
| 655 | int new_dfd; |
| 656 | struct filename *oldpath; |
| 657 | struct filename *newpath; |
| 658 | int flags; |
| 659 | }; |
| 660 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 661 | struct io_unlink { |
| 662 | struct file *file; |
| 663 | int dfd; |
| 664 | int flags; |
| 665 | struct filename *filename; |
| 666 | }; |
| 667 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 668 | struct io_mkdir { |
| 669 | struct file *file; |
| 670 | int dfd; |
| 671 | umode_t mode; |
| 672 | struct filename *filename; |
| 673 | }; |
| 674 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 675 | struct io_symlink { |
| 676 | struct file *file; |
| 677 | int new_dfd; |
| 678 | struct filename *oldpath; |
| 679 | struct filename *newpath; |
| 680 | }; |
| 681 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 682 | struct io_hardlink { |
| 683 | struct file *file; |
| 684 | int old_dfd; |
| 685 | int new_dfd; |
| 686 | struct filename *oldpath; |
| 687 | struct filename *newpath; |
| 688 | int flags; |
| 689 | }; |
| 690 | |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 691 | struct io_async_connect { |
| 692 | struct sockaddr_storage address; |
| 693 | }; |
| 694 | |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 695 | struct io_async_msghdr { |
| 696 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 697 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
| 698 | struct iovec *free_iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 699 | struct sockaddr __user *uaddr; |
| 700 | struct msghdr msg; |
Jens Axboe | b537916 | 2020-02-09 11:29:15 -0700 | [diff] [blame] | 701 | struct sockaddr_storage addr; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 702 | }; |
| 703 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 704 | struct io_rw_state { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 705 | struct iov_iter iter; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 706 | struct iov_iter_state iter_state; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 707 | struct iovec fast_iov[UIO_FASTIOV]; |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 708 | }; |
| 709 | |
| 710 | struct io_async_rw { |
| 711 | struct io_rw_state s; |
| 712 | const struct iovec *free_iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 713 | size_t bytes_done; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 714 | struct wait_page_queue wpq; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 715 | }; |
| 716 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 717 | enum { |
| 718 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| 719 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| 720 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| 721 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| 722 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 723 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 724 | |
Pavel Begunkov | dddca22 | 2021-04-27 16:13:52 +0100 | [diff] [blame] | 725 | /* first byte is taken by user flags, shift it to not overlap */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 726 | REQ_F_FAIL_BIT = 8, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 727 | REQ_F_INFLIGHT_BIT, |
| 728 | REQ_F_CUR_POS_BIT, |
| 729 | REQ_F_NOWAIT_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 730 | REQ_F_LINK_TIMEOUT_BIT, |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 731 | REQ_F_NEED_CLEANUP_BIT, |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 732 | REQ_F_POLLED_BIT, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 733 | REQ_F_BUFFER_SELECTED_BIT, |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 734 | REQ_F_COMPLETE_INLINE_BIT, |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 735 | REQ_F_REISSUE_BIT, |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 736 | REQ_F_CREDS_BIT, |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 737 | REQ_F_REFCOUNT_BIT, |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 738 | REQ_F_ARM_LTIMEOUT_BIT, |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 739 | REQ_F_ASYNC_DATA_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 740 | /* keep async read/write and isreg together and in order */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 741 | REQ_F_SUPPORT_NOWAIT_BIT, |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 742 | REQ_F_ISREG_BIT, |
Jens Axboe | 8455787 | 2020-03-03 15:28:17 -0700 | [diff] [blame] | 743 | |
| 744 | /* not a real bit, just to check we're not overflowing the space */ |
| 745 | __REQ_F_LAST_BIT, |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 746 | }; |
| 747 | |
| 748 | enum { |
| 749 | /* ctx owns file */ |
| 750 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| 751 | /* drain existing IO first */ |
| 752 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| 753 | /* linked sqes */ |
| 754 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| 755 | /* doesn't sever on completion < 0 */ |
| 756 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| 757 | /* IOSQE_ASYNC */ |
| 758 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 759 | /* IOSQE_BUFFER_SELECT */ |
| 760 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 761 | |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 762 | /* fail rest of links */ |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 763 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 764 | /* on inflight list, should be cancelled and waited on exit reliably */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 765 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| 766 | /* read/write uses file position */ |
| 767 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| 768 | /* must not punt to workers */ |
| 769 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
Pavel Begunkov | 900fad4 | 2020-10-19 16:39:16 +0100 | [diff] [blame] | 770 | /* has or had linked timeout */ |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 771 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 772 | /* needs cleanup */ |
| 773 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 774 | /* already went through poll handler */ |
| 775 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 776 | /* buffer already selected */ |
| 777 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 778 | /* completion is deferred through io_comp_state */ |
| 779 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 780 | /* caller should reissue async */ |
| 781 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 782 | /* supports async reads/writes */ |
| 783 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 784 | /* regular file */ |
| 785 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 786 | /* has creds assigned */ |
| 787 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 788 | /* skip refcounting if not set */ |
| 789 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 790 | /* there is a linked timeout that has to be armed */ |
| 791 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 792 | /* ->async_data allocated */ |
| 793 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 794 | }; |
| 795 | |
| 796 | struct async_poll { |
| 797 | struct io_poll_iocb poll; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 798 | struct io_poll_iocb *double_poll; |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 799 | }; |
| 800 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 801 | 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] | 802 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 803 | struct io_task_work { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 804 | union { |
| 805 | struct io_wq_work_node node; |
| 806 | struct llist_node fallback_node; |
| 807 | }; |
| 808 | io_req_tw_func_t func; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 809 | }; |
| 810 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 811 | enum { |
| 812 | IORING_RSRC_FILE = 0, |
| 813 | IORING_RSRC_BUFFER = 1, |
| 814 | }; |
| 815 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 816 | /* |
| 817 | * NOTE! Each of the iocb union members has the file pointer |
| 818 | * as the first entry in their struct definition. So you can |
| 819 | * access the file pointer through any of the sub-structs, |
| 820 | * or directly as just 'ki_filp' in this struct. |
| 821 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 822 | struct io_kiocb { |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 823 | union { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 824 | struct file *file; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 825 | struct io_rw rw; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 826 | struct io_poll_iocb poll; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 827 | struct io_poll_update poll_update; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 828 | struct io_accept accept; |
| 829 | struct io_sync sync; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 830 | struct io_cancel cancel; |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 831 | struct io_timeout timeout; |
Pavel Begunkov | 0bdf7a2 | 2020-10-10 18:34:10 +0100 | [diff] [blame] | 832 | struct io_timeout_rem timeout_rem; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 833 | struct io_connect connect; |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 834 | struct io_sr_msg sr_msg; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 835 | struct io_open open; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 836 | struct io_close close; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 837 | struct io_rsrc_update rsrc_update; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 838 | struct io_fadvise fadvise; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 839 | struct io_madvise madvise; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 840 | struct io_epoll epoll; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 841 | struct io_splice splice; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 842 | struct io_provide_buf pbuf; |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 843 | struct io_statx statx; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 844 | struct io_shutdown shutdown; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 845 | struct io_rename rename; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 846 | struct io_unlink unlink; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 847 | struct io_mkdir mkdir; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 848 | struct io_symlink symlink; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 849 | struct io_hardlink hardlink; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 850 | }; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 851 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 852 | u8 opcode; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 853 | /* polled IO has completed */ |
| 854 | u8 iopoll_completed; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 855 | u16 buf_index; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 856 | unsigned int flags; |
| 857 | |
| 858 | u64 user_data; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 859 | u32 result; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 860 | u32 cflags; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 861 | |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 862 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 863 | struct task_struct *task; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 864 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 865 | struct percpu_ref *fixed_rsrc_refs; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 866 | /* store used ubuf, so we can prevent reloading */ |
| 867 | struct io_mapped_ubuf *imu; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 868 | |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 869 | /* used by request caches, completion batching and iopoll */ |
Pavel Begunkov | ef05d9e | 2021-09-24 22:00:02 +0100 | [diff] [blame] | 870 | struct io_wq_work_node comp_list; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 871 | atomic_t refs; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 872 | struct io_kiocb *link; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 873 | struct io_task_work io_task_work; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 874 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| 875 | struct hlist_node hash_node; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 876 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 877 | struct async_poll *apoll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 878 | /* opcode allocated if it needs to store data for async defer */ |
| 879 | void *async_data; |
Pavel Begunkov | 010e8e6 | 2020-07-30 18:43:45 +0300 | [diff] [blame] | 880 | struct io_wq_work work; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 881 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 882 | const struct cred *creds; |
Pavel Begunkov | 7e3709d | 2021-10-04 20:02:46 +0100 | [diff] [blame] | 883 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 884 | struct io_buffer *kbuf; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 885 | }; |
| 886 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 887 | struct io_tctx_node { |
| 888 | struct list_head ctx_node; |
| 889 | struct task_struct *task; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 890 | struct io_ring_ctx *ctx; |
| 891 | }; |
| 892 | |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 893 | struct io_defer_entry { |
| 894 | struct list_head list; |
| 895 | struct io_kiocb *req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 896 | u32 seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 897 | }; |
| 898 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 899 | struct io_op_def { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 900 | /* needs req->file assigned */ |
| 901 | unsigned needs_file : 1; |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 902 | /* should block plug */ |
| 903 | unsigned plug : 1; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 904 | /* hash wq insertion if file is a regular file */ |
| 905 | unsigned hash_reg_file : 1; |
| 906 | /* unbound wq insertion if file is a non-regular file */ |
| 907 | unsigned unbound_nonreg_file : 1; |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 908 | /* set if opcode supports polled "wait" */ |
| 909 | unsigned pollin : 1; |
| 910 | unsigned pollout : 1; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 911 | /* op supports buffer selection */ |
| 912 | unsigned buffer_select : 1; |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 913 | /* do prep async if is going to be punted */ |
| 914 | unsigned needs_async_setup : 1; |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 915 | /* opcode is not supported by this kernel */ |
| 916 | unsigned not_supported : 1; |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 917 | /* skip auditing */ |
| 918 | unsigned audit_skip : 1; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 919 | /* size of async data needed, if any */ |
| 920 | unsigned short async_size; |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 921 | }; |
| 922 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 923 | static const struct io_op_def io_op_defs[] = { |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 924 | [IORING_OP_NOP] = {}, |
| 925 | [IORING_OP_READV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 926 | .needs_file = 1, |
| 927 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 928 | .pollin = 1, |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 929 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 930 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 931 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 932 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 933 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 934 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 935 | [IORING_OP_WRITEV] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 936 | .needs_file = 1, |
| 937 | .hash_reg_file = 1, |
| 938 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 939 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 940 | .needs_async_setup = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 941 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 942 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 943 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 944 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 945 | [IORING_OP_FSYNC] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 946 | .needs_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 947 | .audit_skip = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 948 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 949 | [IORING_OP_READ_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 950 | .needs_file = 1, |
| 951 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 952 | .pollin = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 953 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 954 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 955 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 956 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 957 | [IORING_OP_WRITE_FIXED] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 958 | .needs_file = 1, |
| 959 | .hash_reg_file = 1, |
| 960 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 961 | .pollout = 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_POLL_ADD] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 967 | .needs_file = 1, |
| 968 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 969 | .audit_skip = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 970 | }, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 971 | [IORING_OP_POLL_REMOVE] = { |
| 972 | .audit_skip = 1, |
| 973 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 974 | [IORING_OP_SYNC_FILE_RANGE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 975 | .needs_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 976 | .audit_skip = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 977 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 978 | [IORING_OP_SENDMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 979 | .needs_file = 1, |
| 980 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 981 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 982 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 983 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 984 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 985 | [IORING_OP_RECVMSG] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 986 | .needs_file = 1, |
| 987 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 988 | .pollin = 1, |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 989 | .buffer_select = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 990 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 991 | .async_size = sizeof(struct io_async_msghdr), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 992 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 993 | [IORING_OP_TIMEOUT] = { |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 994 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 995 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 996 | }, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 997 | [IORING_OP_TIMEOUT_REMOVE] = { |
| 998 | /* used by timeout updates' prep() */ |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 999 | .audit_skip = 1, |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 1000 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1001 | [IORING_OP_ACCEPT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1002 | .needs_file = 1, |
| 1003 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1004 | .pollin = 1, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1005 | }, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1006 | [IORING_OP_ASYNC_CANCEL] = { |
| 1007 | .audit_skip = 1, |
| 1008 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1009 | [IORING_OP_LINK_TIMEOUT] = { |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1010 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1011 | .async_size = sizeof(struct io_timeout_data), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1012 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1013 | [IORING_OP_CONNECT] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1014 | .needs_file = 1, |
| 1015 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1016 | .pollout = 1, |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 1017 | .needs_async_setup = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1018 | .async_size = sizeof(struct io_async_connect), |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1019 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1020 | [IORING_OP_FALLOCATE] = { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1021 | .needs_file = 1, |
| 1022 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1023 | [IORING_OP_OPENAT] = {}, |
| 1024 | [IORING_OP_CLOSE] = {}, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1025 | [IORING_OP_FILES_UPDATE] = { |
| 1026 | .audit_skip = 1, |
| 1027 | }, |
| 1028 | [IORING_OP_STATX] = { |
| 1029 | .audit_skip = 1, |
| 1030 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1031 | [IORING_OP_READ] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1032 | .needs_file = 1, |
| 1033 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1034 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1035 | .buffer_select = 1, |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 1036 | .plug = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1037 | .audit_skip = 1, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1038 | .async_size = sizeof(struct io_async_rw), |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1039 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1040 | [IORING_OP_WRITE] = { |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1041 | .needs_file = 1, |
Jens Axboe | 7b3188e | 2021-08-30 19:37:41 -0600 | [diff] [blame] | 1042 | .hash_reg_file = 1, |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 1043 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1044 | .pollout = 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_FADVISE] = { |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 1050 | .needs_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1051 | .audit_skip = 1, |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 1052 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1053 | [IORING_OP_MADVISE] = {}, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1054 | [IORING_OP_SEND] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1055 | .needs_file = 1, |
| 1056 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1057 | .pollout = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1058 | .audit_skip = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1059 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1060 | [IORING_OP_RECV] = { |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1061 | .needs_file = 1, |
| 1062 | .unbound_nonreg_file = 1, |
Jens Axboe | 8a72758 | 2020-02-20 09:59:44 -0700 | [diff] [blame] | 1063 | .pollin = 1, |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1064 | .buffer_select = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1065 | .audit_skip = 1, |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 1066 | }, |
Pavel Begunkov | 0463b6c | 2020-01-18 21:35:38 +0300 | [diff] [blame] | 1067 | [IORING_OP_OPENAT2] = { |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 1068 | }, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1069 | [IORING_OP_EPOLL_CTL] = { |
| 1070 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1071 | .audit_skip = 1, |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 1072 | }, |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 1073 | [IORING_OP_SPLICE] = { |
| 1074 | .needs_file = 1, |
| 1075 | .hash_reg_file = 1, |
| 1076 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1077 | .audit_skip = 1, |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 1078 | }, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1079 | [IORING_OP_PROVIDE_BUFFERS] = { |
| 1080 | .audit_skip = 1, |
| 1081 | }, |
| 1082 | [IORING_OP_REMOVE_BUFFERS] = { |
| 1083 | .audit_skip = 1, |
| 1084 | }, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1085 | [IORING_OP_TEE] = { |
| 1086 | .needs_file = 1, |
| 1087 | .hash_reg_file = 1, |
| 1088 | .unbound_nonreg_file = 1, |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 1089 | .audit_skip = 1, |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 1090 | }, |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 1091 | [IORING_OP_SHUTDOWN] = { |
| 1092 | .needs_file = 1, |
| 1093 | }, |
Jens Axboe | 44526be | 2021-02-15 13:32:18 -0700 | [diff] [blame] | 1094 | [IORING_OP_RENAMEAT] = {}, |
| 1095 | [IORING_OP_UNLINKAT] = {}, |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 1096 | [IORING_OP_MKDIRAT] = {}, |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 1097 | [IORING_OP_SYMLINKAT] = {}, |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 1098 | [IORING_OP_LINKAT] = {}, |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1099 | }; |
| 1100 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1101 | /* requests with any of those set should undergo io_disarm_next() */ |
| 1102 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) |
| 1103 | |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1104 | static bool io_disarm_next(struct io_kiocb *req); |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 1105 | static void io_uring_del_tctx_node(unsigned long index); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 1106 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 1107 | struct task_struct *task, |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1108 | bool cancel_all); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 1109 | 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] | 1110 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1111 | static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1112 | s32 res, u32 cflags); |
Jackie Liu | ec9c02a | 2019-11-08 23:50:36 +0800 | [diff] [blame] | 1113 | static void io_put_req(struct io_kiocb *req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1114 | static void io_put_req_deferred(struct io_kiocb *req); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1115 | static void io_dismantle_req(struct io_kiocb *req); |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 1116 | static void io_queue_linked_timeout(struct io_kiocb *req); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 1117 | 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] | 1118 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 1119 | unsigned nr_args); |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 1120 | static void io_clean_op(struct io_kiocb *req); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 1121 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 1122 | struct io_kiocb *req, int fd, bool fixed); |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 1123 | static void __io_queue_sqe(struct io_kiocb *req); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1124 | static void io_rsrc_put_work(struct work_struct *work); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1125 | |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1126 | static void io_req_task_queue(struct io_kiocb *req); |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1127 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
Pavel Begunkov | 179ae0d | 2021-02-28 22:35:20 +0000 | [diff] [blame] | 1128 | static int io_req_prep_async(struct io_kiocb *req); |
Jens Axboe | 9a56a23 | 2019-01-09 09:06:50 -0700 | [diff] [blame] | 1129 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1130 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 1131 | unsigned int issue_flags, u32 slot_index); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 1132 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
| 1133 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 1134 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 1135 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1136 | static struct kmem_cache *req_cachep; |
| 1137 | |
Jens Axboe | 0918682 | 2020-10-13 15:01:40 -0600 | [diff] [blame] | 1138 | static const struct file_operations io_uring_fops; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1139 | |
| 1140 | struct sock *io_uring_get_socket(struct file *file) |
| 1141 | { |
| 1142 | #if defined(CONFIG_UNIX) |
| 1143 | if (file->f_op == &io_uring_fops) { |
| 1144 | struct io_ring_ctx *ctx = file->private_data; |
| 1145 | |
| 1146 | return ctx->ring_sock->sk; |
| 1147 | } |
| 1148 | #endif |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | EXPORT_SYMBOL(io_uring_get_socket); |
| 1152 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1153 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
| 1154 | { |
| 1155 | if (!*locked) { |
| 1156 | mutex_lock(&ctx->uring_lock); |
| 1157 | *locked = true; |
| 1158 | } |
| 1159 | } |
| 1160 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 1161 | #define io_for_each_link(pos, head) \ |
| 1162 | for (pos = (head); pos; pos = pos->link) |
| 1163 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1164 | /* |
| 1165 | * Shamelessly stolen from the mm implementation of page reference checking, |
| 1166 | * see commit f958d7b528b1 for details. |
| 1167 | */ |
| 1168 | #define req_ref_zero_or_close_to_overflow(req) \ |
| 1169 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) |
| 1170 | |
| 1171 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) |
| 1172 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1173 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1174 | return atomic_inc_not_zero(&req->refs); |
| 1175 | } |
| 1176 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1177 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
| 1178 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1179 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
| 1180 | return true; |
| 1181 | |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1182 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1183 | return atomic_dec_and_test(&req->refs); |
| 1184 | } |
| 1185 | |
| 1186 | static inline void req_ref_put(struct io_kiocb *req) |
| 1187 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1188 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1189 | WARN_ON_ONCE(req_ref_put_and_test(req)); |
| 1190 | } |
| 1191 | |
| 1192 | static inline void req_ref_get(struct io_kiocb *req) |
| 1193 | { |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1194 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
Pavel Begunkov | 21c843d | 2021-08-11 19:28:27 +0100 | [diff] [blame] | 1195 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
| 1196 | atomic_inc(&req->refs); |
| 1197 | } |
| 1198 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1199 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
| 1200 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1201 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1202 | __io_submit_flush_completions(ctx); |
| 1203 | } |
| 1204 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1205 | 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] | 1206 | { |
| 1207 | if (!(req->flags & REQ_F_REFCOUNT)) { |
| 1208 | req->flags |= REQ_F_REFCOUNT; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1209 | atomic_set(&req->refs, nr); |
Pavel Begunkov | 20e60a3 | 2021-08-11 19:28:30 +0100 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1213 | static inline void io_req_set_refcount(struct io_kiocb *req) |
| 1214 | { |
| 1215 | __io_req_set_refcount(req, 1); |
| 1216 | } |
| 1217 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1218 | #define IO_RSRC_REF_BATCH 100 |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1219 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1220 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, |
| 1221 | struct io_ring_ctx *ctx) |
| 1222 | __must_hold(&ctx->uring_lock) |
| 1223 | { |
| 1224 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
| 1225 | |
| 1226 | if (ref) { |
| 1227 | if (ref == &ctx->rsrc_node->refs) |
| 1228 | ctx->rsrc_cached_refs++; |
| 1229 | else |
| 1230 | percpu_ref_put(ref); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1235 | { |
| 1236 | if (req->fixed_rsrc_refs) |
| 1237 | percpu_ref_put(req->fixed_rsrc_refs); |
| 1238 | } |
| 1239 | |
| 1240 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) |
| 1241 | __must_hold(&ctx->uring_lock) |
| 1242 | { |
| 1243 | if (ctx->rsrc_cached_refs) { |
| 1244 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); |
| 1245 | ctx->rsrc_cached_refs = 0; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) |
| 1250 | __must_hold(&ctx->uring_lock) |
| 1251 | { |
| 1252 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; |
| 1253 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); |
| 1254 | } |
| 1255 | |
Pavel Begunkov | a46be97 | 2021-10-09 23:14:40 +0100 | [diff] [blame] | 1256 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
| 1257 | struct io_ring_ctx *ctx) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1258 | { |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1259 | if (!req->fixed_rsrc_refs) { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 1260 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1261 | ctx->rsrc_cached_refs--; |
| 1262 | if (unlikely(ctx->rsrc_cached_refs < 0)) |
| 1263 | io_rsrc_refs_refill(ctx); |
Pavel Begunkov | 36f72fe | 2020-11-18 19:57:26 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 1267 | static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| 1268 | { |
| 1269 | bool got = percpu_ref_tryget(ref); |
| 1270 | |
| 1271 | /* already at zero, wait for ->release() */ |
| 1272 | if (!got) |
| 1273 | wait_for_completion(compl); |
| 1274 | percpu_ref_resurrect(ref); |
| 1275 | if (got) |
| 1276 | percpu_ref_put(ref); |
| 1277 | } |
| 1278 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1279 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
| 1280 | bool cancel_all) |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 1281 | __must_hold(&req->ctx->timeout_lock) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1282 | { |
| 1283 | struct io_kiocb *req; |
| 1284 | |
Pavel Begunkov | 6820768 | 2021-03-22 01:58:25 +0000 | [diff] [blame] | 1285 | if (task && head->task != task) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1286 | return false; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 1287 | if (cancel_all) |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1288 | return true; |
| 1289 | |
| 1290 | io_for_each_link(req, head) { |
Pavel Begunkov | b05a1bc | 2021-03-04 13:59:24 +0000 | [diff] [blame] | 1291 | if (req->flags & REQ_F_INFLIGHT) |
Jens Axboe | 02a1367 | 2021-01-23 15:49:31 -0700 | [diff] [blame] | 1292 | return true; |
Pavel Begunkov | 08d2363 | 2020-11-06 13:00:22 +0000 | [diff] [blame] | 1293 | } |
| 1294 | return false; |
| 1295 | } |
| 1296 | |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 1297 | static bool io_match_linked(struct io_kiocb *head) |
| 1298 | { |
| 1299 | struct io_kiocb *req; |
| 1300 | |
| 1301 | io_for_each_link(req, head) { |
| 1302 | if (req->flags & REQ_F_INFLIGHT) |
| 1303 | return true; |
| 1304 | } |
| 1305 | return false; |
| 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | * As io_match_task() but protected against racing with linked timeouts. |
| 1310 | * User must not hold timeout_lock. |
| 1311 | */ |
| 1312 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, |
| 1313 | bool cancel_all) |
| 1314 | { |
| 1315 | bool matched; |
| 1316 | |
| 1317 | if (task && head->task != task) |
| 1318 | return false; |
| 1319 | if (cancel_all) |
| 1320 | return true; |
| 1321 | |
| 1322 | if (head->flags & REQ_F_LINK_TIMEOUT) { |
| 1323 | struct io_ring_ctx *ctx = head->ctx; |
| 1324 | |
| 1325 | /* protect against races with linked timeouts */ |
| 1326 | spin_lock_irq(&ctx->timeout_lock); |
| 1327 | matched = io_match_linked(head); |
| 1328 | spin_unlock_irq(&ctx->timeout_lock); |
| 1329 | } else { |
| 1330 | matched = io_match_linked(head); |
| 1331 | } |
| 1332 | return matched; |
| 1333 | } |
| 1334 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 1335 | static inline bool req_has_async_data(struct io_kiocb *req) |
| 1336 | { |
| 1337 | return req->flags & REQ_F_ASYNC_DATA; |
| 1338 | } |
| 1339 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1340 | static inline void req_set_fail(struct io_kiocb *req) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1341 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1342 | req->flags |= REQ_F_FAIL; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 1343 | } |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 1344 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 1345 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
| 1346 | { |
| 1347 | req_set_fail(req); |
| 1348 | req->result = res; |
| 1349 | } |
| 1350 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1351 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1352 | { |
| 1353 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 1354 | |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1355 | complete(&ctx->ref_comp); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1358 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| 1359 | { |
| 1360 | return !req->timeout.off; |
| 1361 | } |
| 1362 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1363 | static __cold void io_fallback_req_func(struct work_struct *work) |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1364 | { |
| 1365 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, |
| 1366 | fallback_work.work); |
| 1367 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); |
| 1368 | struct io_kiocb *req, *tmp; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1369 | bool locked = false; |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1370 | |
| 1371 | percpu_ref_get(&ctx->refs); |
| 1372 | 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] | 1373 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 5636c00 | 2021-08-18 12:42:45 +0100 | [diff] [blame] | 1374 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1375 | if (locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 1376 | io_submit_flush_completions(ctx); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 1377 | mutex_unlock(&ctx->uring_lock); |
| 1378 | } |
Pavel Begunkov | f56165e | 2021-08-09 20:18:07 +0100 | [diff] [blame] | 1379 | percpu_ref_put(&ctx->refs); |
| 1380 | } |
| 1381 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1382 | 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] | 1383 | { |
| 1384 | struct io_ring_ctx *ctx; |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1385 | int hash_bits; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1386 | |
| 1387 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 1388 | if (!ctx) |
| 1389 | return NULL; |
| 1390 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1391 | /* |
| 1392 | * Use 5 bits less than the max cq entries, that should give us around |
| 1393 | * 32 entries per hash list if totally full and uniformly spread. |
| 1394 | */ |
| 1395 | hash_bits = ilog2(p->cq_entries); |
| 1396 | hash_bits -= 5; |
| 1397 | if (hash_bits <= 0) |
| 1398 | hash_bits = 1; |
| 1399 | ctx->cancel_hash_bits = hash_bits; |
| 1400 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| 1401 | GFP_KERNEL); |
| 1402 | if (!ctx->cancel_hash) |
| 1403 | goto err; |
| 1404 | __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| 1405 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1406 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
| 1407 | if (!ctx->dummy_ubuf) |
| 1408 | goto err; |
| 1409 | /* set invalid range, so io_import_fixed() fails meeting it */ |
| 1410 | ctx->dummy_ubuf->ubuf = -1UL; |
| 1411 | |
Roman Gushchin | 2148289 | 2019-05-07 10:01:48 -0700 | [diff] [blame] | 1412 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1413 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| 1414 | goto err; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1415 | |
| 1416 | ctx->flags = p->flags; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1417 | init_waitqueue_head(&ctx->sqo_sq_wait); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 1418 | INIT_LIST_HEAD(&ctx->sqd_list); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1419 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 1420 | init_completion(&ctx->ref_comp); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 1421 | xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 1422 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1423 | mutex_init(&ctx->uring_lock); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 1424 | init_waitqueue_head(&ctx->cq_wait); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1425 | spin_lock_init(&ctx->completion_lock); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1426 | spin_lock_init(&ctx->timeout_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 1427 | INIT_WQ_LIST(&ctx->iopoll_list); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1428 | INIT_LIST_HEAD(&ctx->defer_list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1429 | INIT_LIST_HEAD(&ctx->timeout_list); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 1430 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
Bijan Mottahedeh | d67d226 | 2021-01-15 17:37:46 +0000 | [diff] [blame] | 1431 | spin_lock_init(&ctx->rsrc_ref_lock); |
| 1432 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 1433 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
| 1434 | init_llist_head(&ctx->rsrc_put_llist); |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 1435 | INIT_LIST_HEAD(&ctx->tctx_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1436 | ctx->submit_state.free_list.next = NULL; |
| 1437 | INIT_WQ_LIST(&ctx->locked_free_list); |
Pavel Begunkov | 9011bf9 | 2021-06-30 21:54:03 +0100 | [diff] [blame] | 1438 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 1439 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1440 | return ctx; |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1441 | err: |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 1442 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 1443 | kfree(ctx->cancel_hash); |
Jens Axboe | 206aefd | 2019-11-07 18:27:42 -0700 | [diff] [blame] | 1444 | kfree(ctx); |
| 1445 | return NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1448 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
| 1449 | { |
| 1450 | struct io_rings *r = ctx->rings; |
| 1451 | |
| 1452 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); |
| 1453 | ctx->cq_extra--; |
| 1454 | } |
| 1455 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1456 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1457 | { |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1458 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| 1459 | struct io_ring_ctx *ctx = req->ctx; |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1460 | |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1461 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
Jens Axboe | 2bc9930 | 2020-07-09 09:43:27 -0600 | [diff] [blame] | 1462 | } |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1463 | |
Bob Liu | 9d858b2 | 2019-11-13 18:06:25 +0800 | [diff] [blame] | 1464 | return false; |
Jens Axboe | 7adf4ea | 2019-10-10 21:42:58 -0600 | [diff] [blame] | 1465 | } |
| 1466 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 1467 | #define FFS_NOWAIT 0x1UL |
| 1468 | #define FFS_ISREG 0x2UL |
| 1469 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1470 | |
| 1471 | static inline bool io_req_ffs_set(struct io_kiocb *req) |
| 1472 | { |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 1473 | return req->flags & REQ_F_FIXED_FILE; |
Pavel Begunkov | c97d8a0 | 2021-08-09 13:04:04 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1476 | static inline void io_req_track_inflight(struct io_kiocb *req) |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1477 | { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1478 | if (!(req->flags & REQ_F_INFLIGHT)) { |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1479 | req->flags |= REQ_F_INFLIGHT; |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 1480 | atomic_inc(¤t->io_uring->inflight_tracked); |
Pavel Begunkov | ce3d5aa | 2021-02-01 18:59:55 +0000 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1484 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
| 1485 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 1486 | if (WARN_ON_ONCE(!req->link)) |
| 1487 | return NULL; |
| 1488 | |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1489 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
| 1490 | req->flags |= REQ_F_LINK_TIMEOUT; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1491 | |
| 1492 | /* linked timeouts should have two refs once prep'ed */ |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 1493 | io_req_set_refcount(req); |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1494 | __io_req_set_refcount(req->link, 2); |
| 1495 | return req->link; |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
| 1499 | { |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 1500 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
Pavel Begunkov | fd08e53 | 2021-08-11 19:28:31 +0100 | [diff] [blame] | 1501 | return NULL; |
| 1502 | return __io_prep_linked_timeout(req); |
| 1503 | } |
| 1504 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1505 | static void io_prep_async_work(struct io_kiocb *req) |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1506 | { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1507 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1508 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1509 | |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1510 | if (!(req->flags & REQ_F_CREDS)) { |
| 1511 | req->flags |= REQ_F_CREDS; |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 1512 | req->creds = get_current_cred(); |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 1513 | } |
Jens Axboe | 003e8dc | 2021-03-06 09:22:27 -0700 | [diff] [blame] | 1514 | |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1515 | req->work.list.next = NULL; |
| 1516 | req->work.flags = 0; |
Pavel Begunkov | feaadc4 | 2020-10-22 16:47:16 +0100 | [diff] [blame] | 1517 | if (req->flags & REQ_F_FORCE_ASYNC) |
| 1518 | req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| 1519 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1520 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 2332951 | 2020-10-10 18:34:06 +0100 | [diff] [blame] | 1521 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 8766dd5 | 2020-03-14 00:31:04 +0300 | [diff] [blame] | 1522 | io_wq_hash_work(&req->work, file_inode(req->file)); |
Jens Axboe | 4b982bd | 2021-04-01 08:38:34 -0600 | [diff] [blame] | 1523 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 1524 | if (def->unbound_nonreg_file) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 1525 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
Jens Axboe | 54a91f3 | 2019-09-10 09:15:04 -0600 | [diff] [blame] | 1526 | } |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1527 | |
| 1528 | switch (req->opcode) { |
| 1529 | case IORING_OP_SPLICE: |
| 1530 | case IORING_OP_TEE: |
Pavel Begunkov | e1d675d | 2021-03-22 01:58:29 +0000 | [diff] [blame] | 1531 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
| 1532 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
| 1533 | break; |
| 1534 | } |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1535 | } |
| 1536 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1537 | static void io_prep_async_link(struct io_kiocb *req) |
| 1538 | { |
| 1539 | struct io_kiocb *cur; |
| 1540 | |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1541 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
| 1542 | struct io_ring_ctx *ctx = req->ctx; |
| 1543 | |
Pavel Begunkov | 674ee8e | 2021-11-23 01:45:35 +0000 | [diff] [blame] | 1544 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1545 | io_for_each_link(cur, req) |
| 1546 | io_prep_async_work(cur); |
Pavel Begunkov | 674ee8e | 2021-11-23 01:45:35 +0000 | [diff] [blame] | 1547 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 44eff40 | 2021-07-26 14:14:31 +0100 | [diff] [blame] | 1548 | } else { |
| 1549 | io_for_each_link(cur, req) |
| 1550 | io_prep_async_work(cur); |
| 1551 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1552 | } |
| 1553 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 1554 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
| 1555 | { |
| 1556 | struct io_submit_state *state = &req->ctx->submit_state; |
| 1557 | |
| 1558 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
| 1559 | } |
| 1560 | |
Arnd Bergmann | 0016924 | 2021-10-19 17:34:53 +0200 | [diff] [blame] | 1561 | 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] | 1562 | { |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 1563 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1564 | struct io_kiocb *link = io_prep_linked_timeout(req); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 1565 | struct io_uring_task *tctx = req->task->io_uring; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1566 | |
Jens Axboe | 3bfe610 | 2021-02-16 14:15:30 -0700 | [diff] [blame] | 1567 | BUG_ON(!tctx); |
| 1568 | BUG_ON(!tctx->io_wq); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 1569 | |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1570 | /* init ->work of the whole link before punting */ |
| 1571 | io_prep_async_link(req); |
Jens Axboe | 991468d | 2021-07-23 11:53:54 -0600 | [diff] [blame] | 1572 | |
| 1573 | /* |
| 1574 | * Not expected to happen, but if we do have a bug where this _can_ |
| 1575 | * happen, catch it here and ensure the request is marked as |
| 1576 | * canceled. That will make io-wq go through the usual work cancel |
| 1577 | * procedure rather than attempt to run this request (or create a new |
| 1578 | * worker for it). |
| 1579 | */ |
| 1580 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) |
| 1581 | req->work.flags |= IO_WQ_WORK_CANCEL; |
| 1582 | |
Pavel Begunkov | d07f1e8a | 2021-03-22 01:45:58 +0000 | [diff] [blame] | 1583 | trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| 1584 | &req->work, req->flags); |
Pavel Begunkov | ebf9366 | 2021-03-01 18:20:47 +0000 | [diff] [blame] | 1585 | io_wq_enqueue(tctx->io_wq, &req->work); |
Jens Axboe | 7271ef3 | 2020-08-10 09:55:22 -0600 | [diff] [blame] | 1586 | if (link) |
| 1587 | io_queue_linked_timeout(link); |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 1588 | } |
| 1589 | |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1590 | static void io_kill_timeout(struct io_kiocb *req, int status) |
Pavel Begunkov | 8c85588 | 2021-04-13 02:58:41 +0100 | [diff] [blame] | 1591 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1592 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1593 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 1594 | struct io_timeout_data *io = req->async_data; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1595 | |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 1596 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | 2ae2eb9 | 2021-09-09 13:56:27 +0100 | [diff] [blame] | 1597 | if (status) |
| 1598 | req_set_fail(req); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 1599 | atomic_set(&req->ctx->cq_timeouts, |
| 1600 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1601 | list_del_init(&req->timeout.list); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1602 | io_cqring_fill_event(req->ctx, req->user_data, status, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 1603 | io_put_req_deferred(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1607 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1608 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1609 | while (!list_empty(&ctx->defer_list)) { |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1610 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| 1611 | struct io_defer_entry, list); |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1612 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 1613 | if (req_need_defer(de->req, de->seq)) |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1614 | break; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1615 | list_del_init(&de->list); |
Pavel Begunkov | 907d1df | 2021-01-26 23:35:10 +0000 | [diff] [blame] | 1616 | io_req_task_queue(de->req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 1617 | kfree(de); |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1618 | } |
Pavel Begunkov | 0451894 | 2020-05-26 20:34:05 +0300 | [diff] [blame] | 1619 | } |
| 1620 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1621 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 1622 | __must_hold(&ctx->completion_lock) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1623 | { |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 1624 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1625 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1626 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1627 | while (!list_empty(&ctx->timeout_list)) { |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1628 | u32 events_needed, events_got; |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1629 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1630 | struct io_kiocb, timeout.list); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1631 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 1632 | if (io_is_timeout_noseq(req)) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1633 | break; |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1634 | |
| 1635 | /* |
| 1636 | * Since seq can easily wrap around over time, subtract |
| 1637 | * the last seq at which timeouts were flushed before comparing. |
| 1638 | * Assuming not more than 2^31-1 events have happened since, |
| 1639 | * these subtractions won't have wrapped, so we can check if |
| 1640 | * target is in [last_seq, current_seq] by comparing the two. |
| 1641 | */ |
| 1642 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| 1643 | events_got = seq - ctx->cq_last_tm_flush; |
| 1644 | if (events_got < events_needed) |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1645 | break; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 1646 | |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 1647 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 1ee4160 | 2021-03-25 18:32:42 +0000 | [diff] [blame] | 1648 | io_kill_timeout(req, 0); |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 1649 | } |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 1650 | ctx->cq_last_tm_flush = seq; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1651 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 360428f | 2020-05-30 14:54:17 +0300 | [diff] [blame] | 1652 | } |
| 1653 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 1654 | static __cold void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1655 | { |
Pavel Begunkov | 2335f6f | 2021-06-15 16:47:58 +0100 | [diff] [blame] | 1656 | if (ctx->off_timeout_used) |
| 1657 | io_flush_timeouts(ctx); |
| 1658 | if (ctx->drain_active) |
| 1659 | io_queue_deferred(ctx); |
| 1660 | } |
| 1661 | |
| 1662 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
| 1663 | { |
| 1664 | if (unlikely(ctx->off_timeout_used || ctx->drain_active)) |
| 1665 | __io_commit_cqring_flush(ctx); |
Pavel Begunkov | ec30e04 | 2021-01-19 13:32:38 +0000 | [diff] [blame] | 1666 | /* order cqe stores with ring update */ |
| 1667 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 1668 | } |
| 1669 | |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 1670 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| 1671 | { |
| 1672 | struct io_rings *r = ctx->rings; |
| 1673 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1674 | 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] | 1675 | } |
| 1676 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1677 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
| 1678 | { |
| 1679 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); |
| 1680 | } |
| 1681 | |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1682 | 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] | 1683 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 1684 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1685 | unsigned tail, mask = ctx->cq_entries - 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1686 | |
Stefan Bühler | 115e12e | 2019-04-24 23:54:18 +0200 | [diff] [blame] | 1687 | /* |
| 1688 | * writes to the cq entry need to come after reading head; the |
| 1689 | * control dependency is enough as we're using WRITE_ONCE to |
| 1690 | * fill the cq entry |
| 1691 | */ |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1692 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1693 | return NULL; |
| 1694 | |
Pavel Begunkov | 888aae2 | 2021-01-19 13:32:39 +0000 | [diff] [blame] | 1695 | tail = ctx->cached_cq_tail++; |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 1696 | return &rings->cqes[tail & mask]; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1699 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| 1700 | { |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1701 | if (likely(!ctx->cq_ev_fd)) |
Jens Axboe | f0b493e | 2020-02-01 21:30:11 -0700 | [diff] [blame] | 1702 | return false; |
Stefano Garzarella | 7e55a19 | 2020-05-15 18:38:05 +0200 | [diff] [blame] | 1703 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| 1704 | return false; |
Pavel Begunkov | 44c769d | 2021-04-11 01:46:31 +0100 | [diff] [blame] | 1705 | return !ctx->eventfd_async || io_wq_current_is_worker(); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
Jens Axboe | 2c5d763 | 2021-08-21 07:21:19 -0600 | [diff] [blame] | 1708 | /* |
| 1709 | * This should only get called when at least one event has been posted. |
| 1710 | * Some applications rely on the eventfd notification count only changing |
| 1711 | * IFF a new CQE has been added to the CQ ring. There's no depedency on |
| 1712 | * 1:1 relationship between how many times this function is called (and |
| 1713 | * hence the eventfd count) and number of CQEs posted to the CQ ring. |
| 1714 | */ |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1715 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1716 | { |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1717 | /* |
| 1718 | * wake_up_all() may seem excessive, but io_wake_function() and |
| 1719 | * io_should_wake() handle the termination of the loop and only |
| 1720 | * wake as many waiters as we need to. |
| 1721 | */ |
| 1722 | if (wq_has_sleeper(&ctx->cq_wait)) |
| 1723 | wake_up_all(&ctx->cq_wait); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 1724 | if (io_should_trigger_evfd(ctx)) |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 1725 | eventfd_signal(ctx->cq_ev_fd, 1); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 1726 | } |
| 1727 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1728 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
| 1729 | { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1730 | /* see waitqueue_active() comment */ |
| 1731 | smp_mb(); |
| 1732 | |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1733 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | c57a91fb | 2021-09-08 20:49:17 +0100 | [diff] [blame] | 1734 | if (waitqueue_active(&ctx->cq_wait)) |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 1735 | wake_up_all(&ctx->cq_wait); |
Pavel Begunkov | 80c18e4 | 2021-01-07 03:15:41 +0000 | [diff] [blame] | 1736 | } |
| 1737 | if (io_should_trigger_evfd(ctx)) |
| 1738 | eventfd_signal(ctx->cq_ev_fd, 1); |
| 1739 | } |
| 1740 | |
Jens Axboe | c4a2ed7 | 2019-11-21 21:01:26 -0700 | [diff] [blame] | 1741 | /* Returns true if there are no backlogged entries after the flush */ |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1742 | 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] | 1743 | { |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1744 | bool all_flushed, posted; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1745 | |
Pavel Begunkov | a566c55 | 2021-05-16 22:58:08 +0100 | [diff] [blame] | 1746 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 1747 | return false; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1748 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1749 | posted = false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1750 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1751 | while (!list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1752 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1753 | struct io_overflow_cqe *ocqe; |
Jens Axboe | e6c8aa9 | 2020-09-28 13:10:13 -0600 | [diff] [blame] | 1754 | |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1755 | if (!cqe && !force) |
| 1756 | break; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1757 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
| 1758 | struct io_overflow_cqe, list); |
| 1759 | if (cqe) |
| 1760 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); |
| 1761 | else |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1762 | io_account_cq_overflow(ctx); |
| 1763 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1764 | posted = true; |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 1765 | list_del(&ocqe->list); |
| 1766 | kfree(ocqe); |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1769 | all_flushed = list_empty(&ctx->cq_overflow_list); |
| 1770 | if (all_flushed) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1771 | clear_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1772 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1773 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1774 | } |
Pavel Begunkov | 4693014 | 2020-07-30 18:43:49 +0300 | [diff] [blame] | 1775 | |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1776 | if (posted) |
| 1777 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1778 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b18032b | 2021-01-24 16:58:56 -0700 | [diff] [blame] | 1779 | if (posted) |
| 1780 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 09e8840 | 2020-12-17 00:24:38 +0000 | [diff] [blame] | 1781 | return all_flushed; |
Jens Axboe | 1d7bb1d | 2019-11-06 11:31:17 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1784 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1785 | { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1786 | bool ret = true; |
| 1787 | |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1788 | if (test_bit(0, &ctx->check_cq_overflow)) { |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1789 | /* iopoll syncs against uring_lock, not completion_lock */ |
| 1790 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1791 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 1792 | ret = __io_cqring_overflow_flush(ctx, false); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1793 | if (ctx->flags & IORING_SETUP_IOPOLL) |
| 1794 | mutex_unlock(&ctx->uring_lock); |
| 1795 | } |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 1796 | |
| 1797 | return ret; |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1800 | /* must to be called somewhat shortly after putting a request */ |
| 1801 | static inline void io_put_task(struct task_struct *task, int nr) |
| 1802 | { |
| 1803 | struct io_uring_task *tctx = task->io_uring; |
| 1804 | |
Pavel Begunkov | e98e49b | 2021-08-18 17:01:43 +0100 | [diff] [blame] | 1805 | if (likely(task == current)) { |
| 1806 | tctx->cached_refs += nr; |
| 1807 | } else { |
| 1808 | percpu_counter_sub(&tctx->inflight, nr); |
| 1809 | if (unlikely(atomic_read(&tctx->in_idle))) |
| 1810 | wake_up(&tctx->wait); |
| 1811 | put_task_struct_many(task, nr); |
| 1812 | } |
Pavel Begunkov | 6a290a1 | 2021-08-09 13:04:13 +0100 | [diff] [blame] | 1813 | } |
| 1814 | |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 1815 | static void io_task_refs_refill(struct io_uring_task *tctx) |
| 1816 | { |
| 1817 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; |
| 1818 | |
| 1819 | percpu_counter_add(&tctx->inflight, refill); |
| 1820 | refcount_add(refill, ¤t->usage); |
| 1821 | tctx->cached_refs += refill; |
| 1822 | } |
| 1823 | |
| 1824 | static inline void io_get_task_refs(int nr) |
| 1825 | { |
| 1826 | struct io_uring_task *tctx = current->io_uring; |
| 1827 | |
| 1828 | tctx->cached_refs -= nr; |
| 1829 | if (unlikely(tctx->cached_refs < 0)) |
| 1830 | io_task_refs_refill(tctx); |
| 1831 | } |
| 1832 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1833 | 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] | 1834 | s32 res, u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1835 | { |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1836 | struct io_overflow_cqe *ocqe; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1837 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1838 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
| 1839 | if (!ocqe) { |
| 1840 | /* |
| 1841 | * If we're in ring overflow flush mode, or in task cancel mode, |
| 1842 | * or cannot allocate an overflow entry, then we need to drop it |
| 1843 | * on the floor. |
| 1844 | */ |
Pavel Begunkov | 8f6ed49 | 2021-05-16 22:58:10 +0100 | [diff] [blame] | 1845 | io_account_cq_overflow(ctx); |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1846 | return false; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1847 | } |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1848 | if (list_empty(&ctx->cq_overflow_list)) { |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 1849 | set_bit(0, &ctx->check_cq_overflow); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 1850 | WRITE_ONCE(ctx->rings->sq_flags, |
| 1851 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); |
| 1852 | |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1853 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1854 | ocqe->cqe.user_data = user_data; |
Pavel Begunkov | cce4b8b | 2021-04-13 02:58:44 +0100 | [diff] [blame] | 1855 | ocqe->cqe.res = res; |
| 1856 | ocqe->cqe.flags = cflags; |
| 1857 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); |
| 1858 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1861 | static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1862 | s32 res, u32 cflags) |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1863 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1864 | struct io_uring_cqe *cqe; |
| 1865 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1866 | trace_io_uring_complete(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1867 | |
| 1868 | /* |
| 1869 | * If we can't get a cq entry, userspace overflowed the |
| 1870 | * submission (by quite a lot). Increment the overflow count in |
| 1871 | * the ring. |
| 1872 | */ |
Pavel Begunkov | d068b50 | 2021-05-16 22:58:11 +0100 | [diff] [blame] | 1873 | cqe = io_get_cqe(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1874 | if (likely(cqe)) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1875 | WRITE_ONCE(cqe->user_data, user_data); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1876 | WRITE_ONCE(cqe->res, res); |
| 1877 | WRITE_ONCE(cqe->flags, cflags); |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1878 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1879 | } |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1880 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Pavel Begunkov | 8d13326 | 2021-04-11 01:46:33 +0100 | [diff] [blame] | 1883 | /* not as hot to bloat with inlining */ |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1884 | static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data, |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1885 | s32 res, u32 cflags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1886 | { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1887 | return __io_cqring_fill_event(ctx, user_data, res, cflags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1890 | static void io_req_complete_post(struct io_kiocb *req, s32 res, |
| 1891 | u32 cflags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1892 | { |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 1893 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1894 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1895 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 1896 | __io_cqring_fill_event(ctx, req->user_data, res, cflags); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1897 | /* |
| 1898 | * If we're the last reference to this request, add to our locked |
| 1899 | * free_list cache. |
| 1900 | */ |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 1901 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1902 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 1903 | if (req->flags & IO_DISARM_MASK) |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1904 | io_disarm_next(req); |
| 1905 | if (req->link) { |
| 1906 | io_req_task_queue(req->link); |
| 1907 | req->link = NULL; |
| 1908 | } |
| 1909 | } |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 1910 | io_req_put_rsrc(req, ctx); |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1911 | io_dismantle_req(req); |
| 1912 | io_put_task(req->task, 1); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1913 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1914 | ctx->locked_free_nr++; |
Pavel Begunkov | 180f829 | 2021-03-14 20:57:09 +0000 | [diff] [blame] | 1915 | } |
Pavel Begunkov | 7a61235 | 2021-03-09 00:37:59 +0000 | [diff] [blame] | 1916 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1917 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | a3f34907 | 2021-09-15 12:04:20 +0100 | [diff] [blame] | 1918 | io_cqring_ev_posted(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1921 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
| 1922 | u32 cflags) |
Jens Axboe | 4e3d9ff | 2021-04-15 17:44:34 -0600 | [diff] [blame] | 1923 | { |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1924 | req->result = res; |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 1925 | req->cflags = cflags; |
Pavel Begunkov | e342c80 | 2021-01-19 13:32:47 +0000 | [diff] [blame] | 1926 | req->flags |= REQ_F_COMPLETE_INLINE; |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1927 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1928 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1929 | 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] | 1930 | s32 res, u32 cflags) |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1931 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1932 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
| 1933 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1934 | else |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1935 | io_req_complete_post(req, res, cflags); |
Pavel Begunkov | a38d68d | 2021-01-19 13:32:45 +0000 | [diff] [blame] | 1936 | } |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1937 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1938 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 1939 | { |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 1940 | __io_req_complete(req, 0, res, 0); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 1943 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1944 | { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 1945 | req_set_fail(req); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 1946 | io_req_complete_post(req, res, 0); |
| 1947 | } |
| 1948 | |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 1949 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
| 1950 | { |
| 1951 | /* |
| 1952 | * We don't submit, fail them all, for that replace hardlinks with |
| 1953 | * normal links. Extra REQ_F_LINK is tolerated. |
| 1954 | */ |
| 1955 | req->flags &= ~REQ_F_HARDLINK; |
| 1956 | req->flags |= REQ_F_LINK; |
| 1957 | io_req_complete_failed(req, req->result); |
| 1958 | } |
| 1959 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 1960 | /* |
| 1961 | * Don't initialise the fields below on every allocation, but do that in |
| 1962 | * advance and keep them valid across allocations. |
| 1963 | */ |
| 1964 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) |
| 1965 | { |
| 1966 | req->ctx = ctx; |
| 1967 | req->link = NULL; |
| 1968 | req->async_data = NULL; |
| 1969 | /* not necessary, but safer to zero */ |
| 1970 | req->result = 0; |
| 1971 | } |
| 1972 | |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1973 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1974 | struct io_submit_state *state) |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1975 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1976 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1977 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1978 | ctx->locked_free_nr = 0; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 1979 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dac7a09 | 2021-03-19 17:22:39 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Pavel Begunkov | dd78f49 | 2021-03-19 17:22:35 +0000 | [diff] [blame] | 1982 | /* Returns true IFF there are requests in the cache */ |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1983 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1984 | { |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1985 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 1986 | |
Jens Axboe | c7dae4b | 2021-02-09 19:53:37 -0700 | [diff] [blame] | 1987 | /* |
| 1988 | * If we have more than a batch's worth of requests in our IRQ side |
| 1989 | * locked cache, grab the lock and move them over to our submission |
| 1990 | * side cache. |
| 1991 | */ |
Pavel Begunkov | d0acdee | 2021-05-16 22:58:12 +0100 | [diff] [blame] | 1992 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 1993 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 1994 | return !!state->free_list.next; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 1997 | /* |
| 1998 | * A request might get retired back into the request caches even before opcode |
| 1999 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. |
| 2000 | * Because of that, io_alloc_req() should be called only under ->uring_lock |
| 2001 | * and with extra caution to not get a request that is still worked on. |
| 2002 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2003 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 2004 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2005 | { |
Pavel Begunkov | 258b29a | 2021-02-10 00:03:10 +0000 | [diff] [blame] | 2006 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2007 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2008 | void *reqs[IO_REQ_ALLOC_BATCH]; |
| 2009 | struct io_kiocb *req; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2010 | int ret, i; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2011 | |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2012 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2013 | return true; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2014 | |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2015 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
Pavel Begunkov | e5d1bc0 | 2021-02-10 00:03:23 +0000 | [diff] [blame] | 2016 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2017 | /* |
| 2018 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 2019 | * retry single alloc to be on the safe side. |
| 2020 | */ |
| 2021 | if (unlikely(ret <= 0)) { |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2022 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 2023 | if (!reqs[0]) |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2024 | return false; |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2025 | ret = 1; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2026 | } |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 2027 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 2028 | percpu_ref_get_many(&ctx->refs, ret); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2029 | for (i = 0; i < ret; i++) { |
| 2030 | req = reqs[i]; |
| 2031 | |
| 2032 | io_preinit_req(req, ctx); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2033 | wq_stack_add_head(&req->comp_list, &state->free_list); |
Pavel Begunkov | 3ab665b | 2021-09-24 21:59:45 +0100 | [diff] [blame] | 2034 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) |
| 2039 | { |
| 2040 | if (unlikely(!ctx->submit_state.free_list.next)) |
| 2041 | return __io_alloc_req_refill(ctx); |
| 2042 | return true; |
| 2043 | } |
| 2044 | |
| 2045 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) |
| 2046 | { |
| 2047 | struct io_wq_work_node *node; |
| 2048 | |
| 2049 | node = wq_stack_extract(&ctx->submit_state.free_list); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2050 | return container_of(node, struct io_kiocb, comp_list); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2053 | static inline void io_put_file(struct file *file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2054 | { |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2055 | if (file) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 2056 | fput(file); |
| 2057 | } |
| 2058 | |
Pavel Begunkov | 6b63952 | 2021-09-08 16:40:50 +0100 | [diff] [blame] | 2059 | static inline void io_dismantle_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2060 | { |
Pavel Begunkov | 094bae4 | 2021-03-19 17:22:42 +0000 | [diff] [blame] | 2061 | unsigned int flags = req->flags; |
Pavel Begunkov | 929a3af | 2020-02-19 00:19:09 +0300 | [diff] [blame] | 2062 | |
Pavel Begunkov | 867f8fa | 2021-10-04 20:02:58 +0100 | [diff] [blame] | 2063 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 2064 | io_clean_op(req); |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 2065 | if (!(flags & REQ_F_FIXED_FILE)) |
| 2066 | io_put_file(req->file); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2067 | } |
Pavel Begunkov | 2b85edf | 2019-12-28 14:13:03 +0300 | [diff] [blame] | 2068 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2069 | static __cold void __io_free_req(struct io_kiocb *req) |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2070 | { |
Jens Axboe | 51a4cc1 | 2020-08-10 10:55:56 -0600 | [diff] [blame] | 2071 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | ecfc517 | 2020-06-29 13:13:03 +0300 | [diff] [blame] | 2072 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 2073 | io_req_put_rsrc(req, ctx); |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2074 | io_dismantle_req(req); |
Pavel Begunkov | 7c66073 | 2021-01-25 11:42:21 +0000 | [diff] [blame] | 2075 | io_put_task(req->task, 1); |
Pavel Begunkov | e6543a8 | 2020-06-28 12:52:30 +0300 | [diff] [blame] | 2076 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2077 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 2078 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
Pavel Begunkov | c34b025 | 2021-08-09 20:18:08 +0100 | [diff] [blame] | 2079 | ctx->locked_free_nr++; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2080 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2081 | } |
| 2082 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2083 | static inline void io_remove_next_linked(struct io_kiocb *req) |
| 2084 | { |
| 2085 | struct io_kiocb *nxt = req->link; |
| 2086 | |
| 2087 | req->link = nxt->link; |
| 2088 | nxt->link = NULL; |
| 2089 | } |
| 2090 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2091 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
| 2092 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2093 | __must_hold(&req->ctx->timeout_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2094 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2095 | struct io_kiocb *link = req->link; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2096 | |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 2097 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2098 | struct io_timeout_data *io = link->async_data; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2099 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2100 | io_remove_next_linked(req); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 2101 | link->timeout.head = NULL; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 2102 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 2103 | list_del(&link->timeout.list); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2104 | io_cqring_fill_event(link->ctx, link->user_data, |
| 2105 | -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2106 | io_put_req_deferred(link); |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2107 | return true; |
Pavel Begunkov | c9abd7a | 2020-10-22 16:43:11 +0100 | [diff] [blame] | 2108 | } |
| 2109 | } |
Pavel Begunkov | d4729fb | 2021-03-22 01:58:24 +0000 | [diff] [blame] | 2110 | return false; |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2111 | } |
| 2112 | |
Pavel Begunkov | d148ca4 | 2020-10-18 10:17:39 +0100 | [diff] [blame] | 2113 | static void io_fail_links(struct io_kiocb *req) |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2114 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2115 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2116 | struct io_kiocb *nxt, *link = req->link; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2117 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2118 | req->link = NULL; |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2119 | while (link) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2120 | long res = -ECANCELED; |
| 2121 | |
| 2122 | if (link->flags & REQ_F_FAIL) |
| 2123 | res = link->result; |
| 2124 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2125 | nxt = link->link; |
| 2126 | link->link = NULL; |
| 2127 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 2128 | trace_io_uring_fail_link(req, link); |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 2129 | io_cqring_fill_event(link->ctx, link->user_data, res, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2130 | io_put_req_deferred(link); |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2131 | link = nxt; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2132 | } |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2133 | } |
Pavel Begunkov | 7c86ffe | 2020-06-29 13:12:59 +0300 | [diff] [blame] | 2134 | |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2135 | static bool io_disarm_next(struct io_kiocb *req) |
| 2136 | __must_hold(&req->ctx->completion_lock) |
| 2137 | { |
| 2138 | bool posted = false; |
| 2139 | |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2140 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
| 2141 | struct io_kiocb *link = req->link; |
| 2142 | |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 2143 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | 0756a86 | 2021-08-15 10:40:25 +0100 | [diff] [blame] | 2144 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
| 2145 | io_remove_next_linked(req); |
| 2146 | io_cqring_fill_event(link->ctx, link->user_data, |
| 2147 | -ECANCELED, 0); |
| 2148 | io_put_req_deferred(link); |
| 2149 | posted = true; |
| 2150 | } |
| 2151 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2152 | struct io_ring_ctx *ctx = req->ctx; |
| 2153 | |
| 2154 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2155 | posted = io_kill_linked_timeout(req); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 2156 | spin_unlock_irq(&ctx->timeout_lock); |
| 2157 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2158 | if (unlikely((req->flags & REQ_F_FAIL) && |
Pavel Begunkov | e4335ed | 2021-04-11 01:46:39 +0100 | [diff] [blame] | 2159 | !(req->flags & REQ_F_HARDLINK))) { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2160 | posted |= (req->link != NULL); |
| 2161 | io_fail_links(req); |
| 2162 | } |
| 2163 | return posted; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2164 | } |
| 2165 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2166 | static void __io_req_find_next_prep(struct io_kiocb *req) |
| 2167 | { |
| 2168 | struct io_ring_ctx *ctx = req->ctx; |
| 2169 | bool posted; |
| 2170 | |
| 2171 | spin_lock(&ctx->completion_lock); |
| 2172 | posted = io_disarm_next(req); |
| 2173 | if (posted) |
| 2174 | io_commit_cqring(req->ctx); |
| 2175 | spin_unlock(&ctx->completion_lock); |
| 2176 | if (posted) |
| 2177 | io_cqring_ev_posted(ctx); |
| 2178 | } |
| 2179 | |
| 2180 | 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] | 2181 | { |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2182 | struct io_kiocb *nxt; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2183 | |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2184 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
| 2185 | return NULL; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2186 | /* |
| 2187 | * If LINK is set, we have dependent requests in this chain. If we |
| 2188 | * didn't fail this request, queue the first one up, moving any other |
| 2189 | * dependencies to the next request. In case of failure, fail the rest |
| 2190 | * of the chain. |
| 2191 | */ |
Pavel Begunkov | d81499b | 2021-09-08 16:40:51 +0100 | [diff] [blame] | 2192 | if (unlikely(req->flags & IO_DISARM_MASK)) |
| 2193 | __io_req_find_next_prep(req); |
Pavel Begunkov | 33cc89a | 2021-03-09 00:37:58 +0000 | [diff] [blame] | 2194 | nxt = req->link; |
| 2195 | req->link = NULL; |
| 2196 | return nxt; |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2197 | } |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 2198 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2199 | 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] | 2200 | { |
| 2201 | if (!ctx) |
| 2202 | return; |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2203 | if (*locked) { |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2204 | io_submit_flush_completions(ctx); |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2205 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2206 | *locked = false; |
Pavel Begunkov | 2c32395 | 2021-02-28 22:04:53 +0000 | [diff] [blame] | 2207 | } |
| 2208 | percpu_ref_put(&ctx->refs); |
| 2209 | } |
| 2210 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2211 | static void tctx_task_work(struct callback_head *cb) |
| 2212 | { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2213 | bool locked = false; |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2214 | struct io_ring_ctx *ctx = NULL; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2215 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
| 2216 | task_work); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2217 | |
Pavel Begunkov | 16f7207 | 2021-06-17 18:14:09 +0100 | [diff] [blame] | 2218 | while (1) { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2219 | struct io_wq_work_node *node; |
| 2220 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2221 | if (!tctx->task_list.first && locked) |
Pavel Begunkov | 8d4ad41 | 2021-09-02 00:38:23 +0100 | [diff] [blame] | 2222 | io_submit_flush_completions(ctx); |
| 2223 | |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2224 | spin_lock_irq(&tctx->task_lock); |
Pavel Begunkov | c6538be | 2021-06-17 18:14:08 +0100 | [diff] [blame] | 2225 | node = tctx->task_list.first; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2226 | INIT_WQ_LIST(&tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2227 | if (!node) |
| 2228 | tctx->task_running = false; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2229 | spin_unlock_irq(&tctx->task_lock); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2230 | if (!node) |
| 2231 | break; |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2232 | |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2233 | do { |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2234 | struct io_wq_work_node *next = node->next; |
| 2235 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2236 | io_task_work.node); |
| 2237 | |
| 2238 | if (req->ctx != ctx) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2239 | ctx_flush_and_put(ctx, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2240 | ctx = req->ctx; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2241 | /* if not contended, grab and improve batching */ |
| 2242 | locked = mutex_trylock(&ctx->uring_lock); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2243 | percpu_ref_get(&ctx->refs); |
| 2244 | } |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2245 | req->io_task_work.func(req, &locked); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2246 | node = next; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2247 | } while (node); |
| 2248 | |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2249 | cond_resched(); |
Pavel Begunkov | 3f18407 | 2021-06-17 18:14:06 +0100 | [diff] [blame] | 2250 | } |
Pavel Begunkov | ebd0df2 | 2021-06-17 18:14:07 +0100 | [diff] [blame] | 2251 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2252 | ctx_flush_and_put(ctx, &locked); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2255 | static void io_req_task_work_add(struct io_kiocb *req) |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2256 | { |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2257 | struct task_struct *tsk = req->task; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2258 | struct io_uring_task *tctx = tsk->io_uring; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2259 | enum task_work_notify_mode notify; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2260 | struct io_wq_work_node *node; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2261 | unsigned long flags; |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2262 | bool running; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2263 | |
| 2264 | WARN_ON_ONCE(!tctx); |
| 2265 | |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2266 | spin_lock_irqsave(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2267 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2268 | running = tctx->task_running; |
| 2269 | if (!running) |
| 2270 | tctx->task_running = true; |
Jens Axboe | 0b81e80 | 2021-02-16 10:33:53 -0700 | [diff] [blame] | 2271 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2272 | |
| 2273 | /* task_work already pending, we're done */ |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2274 | if (running) |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2275 | return; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2276 | |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2277 | /* |
| 2278 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| 2279 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| 2280 | * processing task_work. There's no reliable way to tell if TWA_RESUME |
| 2281 | * will do the job. |
| 2282 | */ |
| 2283 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; |
Pavel Begunkov | d97ec62 | 2021-09-08 16:40:53 +0100 | [diff] [blame] | 2284 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
| 2285 | if (notify == TWA_NONE) |
| 2286 | wake_up_process(tsk); |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2287 | return; |
Pavel Begunkov | c15b79d | 2021-03-19 17:22:44 +0000 | [diff] [blame] | 2288 | } |
Pavel Begunkov | 2215bed | 2021-08-09 13:04:06 +0100 | [diff] [blame] | 2289 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2290 | spin_lock_irqsave(&tctx->task_lock, flags); |
Pavel Begunkov | 6294f36 | 2021-08-10 17:53:55 +0100 | [diff] [blame] | 2291 | tctx->task_running = false; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2292 | node = tctx->task_list.first; |
| 2293 | INIT_WQ_LIST(&tctx->task_list); |
| 2294 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 2295 | |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2296 | while (node) { |
| 2297 | req = container_of(node, struct io_kiocb, io_task_work.node); |
| 2298 | node = node->next; |
| 2299 | if (llist_add(&req->io_task_work.fallback_node, |
| 2300 | &req->ctx->fallback_llist)) |
| 2301 | schedule_delayed_work(&req->ctx->fallback_work, 1); |
| 2302 | } |
Pavel Begunkov | eab30c4 | 2021-01-19 13:32:42 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2305 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2306 | { |
Jens Axboe | 87ceb6a | 2020-09-14 08:20:12 -0600 | [diff] [blame] | 2307 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2308 | |
Pavel Begunkov | b18a1a4 | 2021-08-25 20:51:39 +0100 | [diff] [blame] | 2309 | /* not needed for normal modes, but SQPOLL depends on it */ |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2310 | io_tw_lock(ctx, locked); |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2311 | io_req_complete_failed(req, req->result); |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2312 | } |
| 2313 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2314 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
Jens Axboe | c40f637 | 2020-06-25 15:39:59 -0600 | [diff] [blame] | 2315 | { |
| 2316 | struct io_ring_ctx *ctx = req->ctx; |
| 2317 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2318 | io_tw_lock(ctx, locked); |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 2319 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | af066f3 | 2021-08-09 13:04:19 +0100 | [diff] [blame] | 2320 | if (likely(!(req->task->flags & PF_EXITING))) |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 2321 | __io_queue_sqe(req); |
Pavel Begunkov | 81b6d05 | 2021-01-04 20:36:35 +0000 | [diff] [blame] | 2322 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 2323 | io_req_complete_failed(req, -EFAULT); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2324 | } |
| 2325 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2326 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
| 2327 | { |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2328 | req->result = ret; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2329 | req->io_task_work.func = io_req_task_cancel; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2330 | io_req_task_work_add(req); |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2333 | static void io_req_task_queue(struct io_kiocb *req) |
| 2334 | { |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 2335 | req->io_task_work.func = io_req_task_submit; |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 2336 | io_req_task_work_add(req); |
Pavel Begunkov | 2c4b8eb | 2021-02-28 22:35:10 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2339 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
| 2340 | { |
| 2341 | req->io_task_work.func = io_queue_async_work; |
| 2342 | io_req_task_work_add(req); |
| 2343 | } |
| 2344 | |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 2345 | static inline void io_queue_next(struct io_kiocb *req) |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2346 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2347 | struct io_kiocb *nxt = io_req_find_next(req); |
Pavel Begunkov | 944e58b | 2019-11-21 23:21:01 +0300 | [diff] [blame] | 2348 | |
Pavel Begunkov | 906a8c3 | 2020-06-27 14:04:55 +0300 | [diff] [blame] | 2349 | if (nxt) |
| 2350 | io_req_task_queue(nxt); |
Jackie Liu | c69f8db | 2019-11-09 11:00:08 +0800 | [diff] [blame] | 2351 | } |
| 2352 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2353 | static void io_free_req(struct io_kiocb *req) |
| 2354 | { |
Pavel Begunkov | c352438 | 2020-06-28 12:52:32 +0300 | [diff] [blame] | 2355 | io_queue_next(req); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 2356 | __io_free_req(req); |
Jens Axboe | e65ef56 | 2019-03-12 10:16:44 -0600 | [diff] [blame] | 2357 | } |
| 2358 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2359 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
| 2360 | { |
| 2361 | io_free_req(req); |
| 2362 | } |
| 2363 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2364 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2365 | struct io_wq_work_node *node) |
Jens Axboe | a141dd8 | 2021-08-12 12:48:34 -0600 | [diff] [blame] | 2366 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2367 | { |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2368 | struct task_struct *task = NULL; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 2369 | int task_refs = 0; |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2370 | |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2371 | do { |
| 2372 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2373 | comp_list); |
| 2374 | |
Pavel Begunkov | def77ac | 2021-10-12 15:02:14 +0100 | [diff] [blame] | 2375 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2376 | node = req->comp_list.next; |
Pavel Begunkov | def77ac | 2021-10-12 15:02:14 +0100 | [diff] [blame] | 2377 | if (!req_ref_put_and_test(req)) |
| 2378 | continue; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2379 | } |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2380 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 2381 | io_req_put_rsrc_locked(req, ctx); |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2382 | io_queue_next(req); |
| 2383 | io_dismantle_req(req); |
| 2384 | |
| 2385 | if (req->task != task) { |
| 2386 | if (task) |
| 2387 | io_put_task(task, task_refs); |
| 2388 | task = req->task; |
| 2389 | task_refs = 0; |
| 2390 | } |
| 2391 | task_refs++; |
Pavel Begunkov | c1e53a6 | 2021-10-04 20:02:55 +0100 | [diff] [blame] | 2392 | node = req->comp_list.next; |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2393 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2394 | } while (node); |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2395 | |
Pavel Begunkov | d4b7a5e | 2021-09-24 21:59:53 +0100 | [diff] [blame] | 2396 | if (task) |
| 2397 | io_put_task(task, task_refs); |
Pavel Begunkov | 3aa83bf | 2021-09-24 21:59:50 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 2400 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2401 | __must_hold(&ctx->uring_lock) |
| 2402 | { |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2403 | struct io_wq_work_node *node, *prev; |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 2404 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2405 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2406 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2407 | wq_list_for_each(node, prev, &state->compl_reqs) { |
| 2408 | struct io_kiocb *req = container_of(node, struct io_kiocb, |
| 2409 | comp_list); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2410 | |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 2411 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
Pavel Begunkov | d17e56e | 2021-10-04 20:02:57 +0100 | [diff] [blame] | 2412 | req->cflags); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2413 | } |
| 2414 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 2415 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 905c172 | 2021-02-10 00:03:14 +0000 | [diff] [blame] | 2416 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | 5182ed2 | 2021-06-26 21:40:48 +0100 | [diff] [blame] | 2417 | |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2418 | io_free_batch_list(ctx, state->compl_reqs.first); |
Pavel Begunkov | 6f33b0b | 2021-09-24 21:59:44 +0100 | [diff] [blame] | 2419 | INIT_WQ_LIST(&state->compl_reqs); |
Pavel Begunkov | 7a743e2 | 2020-03-03 21:33:13 +0300 | [diff] [blame] | 2420 | } |
| 2421 | |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2422 | /* |
| 2423 | * Drop reference to request, return next in chain (if there is one) if this |
| 2424 | * was the last reference to this request. |
| 2425 | */ |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2426 | 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] | 2427 | { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2428 | struct io_kiocb *nxt = NULL; |
| 2429 | |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2430 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2431 | nxt = io_req_find_next(req); |
Jens Axboe | 4d7dd46 | 2019-11-20 13:03:52 -0700 | [diff] [blame] | 2432 | __io_free_req(req); |
Jens Axboe | 2a44f46 | 2020-02-25 13:25:41 -0700 | [diff] [blame] | 2433 | } |
Pavel Begunkov | 9b5f7bd9 | 2020-06-29 13:13:00 +0300 | [diff] [blame] | 2434 | return nxt; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Pavel Begunkov | 0d85035 | 2021-03-19 17:22:37 +0000 | [diff] [blame] | 2437 | static inline void io_put_req(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2438 | { |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 2439 | if (req_ref_put_and_test(req)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2440 | io_free_req(req); |
| 2441 | } |
| 2442 | |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2443 | static inline void io_put_req_deferred(struct io_kiocb *req) |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2444 | { |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 2445 | if (req_ref_put_and_test(req)) { |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2446 | req->io_task_work.func = io_free_req_work; |
Pavel Begunkov | 543af3a | 2021-08-09 13:04:15 +0100 | [diff] [blame] | 2447 | io_req_task_work_add(req); |
| 2448 | } |
Pavel Begunkov | 216578e | 2020-10-13 09:44:00 +0100 | [diff] [blame] | 2449 | } |
| 2450 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 2451 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2452 | { |
| 2453 | /* See comment at the top of this file */ |
| 2454 | smp_rmb(); |
Pavel Begunkov | e23de15 | 2020-12-17 00:24:37 +0000 | [diff] [blame] | 2455 | return __io_cqring_events(ctx); |
Jens Axboe | a3a0e43 | 2019-08-20 11:03:11 -0600 | [diff] [blame] | 2456 | } |
| 2457 | |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 2458 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 2459 | { |
| 2460 | struct io_rings *rings = ctx->rings; |
| 2461 | |
| 2462 | /* make sure SQ entry isn't read before tail */ |
| 2463 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 2464 | } |
| 2465 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2466 | static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2467 | { |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2468 | unsigned int cflags; |
Jens Axboe | e94f141 | 2019-12-19 12:06:02 -0700 | [diff] [blame] | 2469 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2470 | cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| 2471 | cflags |= IORING_CQE_F_BUFFER; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 2472 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 2473 | kfree(kbuf); |
| 2474 | return cflags; |
| 2475 | } |
| 2476 | |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2477 | static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| 2478 | { |
Pavel Begunkov | ae421d9 | 2021-08-17 20:28:08 +0100 | [diff] [blame] | 2479 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
| 2480 | return 0; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 2481 | return io_put_kbuf(req, req->kbuf); |
Pavel Begunkov | 8ff069b | 2020-07-16 23:28:04 +0300 | [diff] [blame] | 2482 | } |
| 2483 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2484 | static inline bool io_run_task_work(void) |
| 2485 | { |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2486 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) { |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2487 | __set_current_state(TASK_RUNNING); |
Nadav Amit | ef98eb0 | 2021-08-07 17:13:41 -0700 | [diff] [blame] | 2488 | tracehook_notify_signal(); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2489 | return true; |
| 2490 | } |
| 2491 | |
| 2492 | return false; |
| 2493 | } |
| 2494 | |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2495 | 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] | 2496 | { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2497 | struct io_wq_work_node *pos, *start, *prev; |
Christoph Hellwig | d729cf9 | 2021-10-12 13:12:20 +0200 | [diff] [blame] | 2498 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2499 | DEFINE_IO_COMP_BATCH(iob); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2500 | int nr_events = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2501 | |
| 2502 | /* |
| 2503 | * Only spin for completions if we don't have multiple devices hanging |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2504 | * off our complete list. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2505 | */ |
Pavel Begunkov | 87a115f | 2021-09-24 21:59:42 +0100 | [diff] [blame] | 2506 | if (ctx->poll_multi_queue || force_nonspin) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2507 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2508 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2509 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
| 2510 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2511 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2512 | int ret; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2513 | |
| 2514 | /* |
Bijan Mottahedeh | 581f981 | 2020-04-03 13:51:33 -0700 | [diff] [blame] | 2515 | * Move completed and retryable entries to our local lists. |
| 2516 | * If we find a request that requires polling, break out |
| 2517 | * and complete those lists first, if we have entries there. |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2518 | */ |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2519 | if (READ_ONCE(req->iopoll_completed)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2520 | break; |
| 2521 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2522 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
Pavel Begunkov | a2416e1 | 2021-08-09 13:04:09 +0100 | [diff] [blame] | 2523 | if (unlikely(ret < 0)) |
| 2524 | return ret; |
| 2525 | else if (ret) |
Christoph Hellwig | ef99b2d | 2021-10-12 13:12:19 +0200 | [diff] [blame] | 2526 | poll_flags |= BLK_POLL_ONESHOT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2527 | |
Pavel Begunkov | 3aadc23 | 2020-07-06 17:59:29 +0300 | [diff] [blame] | 2528 | /* iopoll may have completed current req */ |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2529 | if (!rq_list_empty(iob.req_list) || |
| 2530 | READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2531 | break; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
Jens Axboe | b688f11 | 2021-10-12 09:28:46 -0600 | [diff] [blame] | 2534 | if (!rq_list_empty(iob.req_list)) |
| 2535 | iob.complete(&iob); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2536 | else if (!pos) |
| 2537 | return 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2538 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2539 | prev = start; |
| 2540 | wq_list_for_each_resume(pos, prev) { |
| 2541 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); |
| 2542 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2543 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
| 2544 | if (!smp_load_acquire(&req->iopoll_completed)) |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2545 | break; |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2546 | __io_cqring_fill_event(ctx, req->user_data, req->result, |
Pavel Begunkov | f5ed3bc | 2021-09-24 21:59:52 +0100 | [diff] [blame] | 2547 | io_put_rw_kbuf(req)); |
Pavel Begunkov | e3f721e | 2021-09-24 21:59:48 +0100 | [diff] [blame] | 2548 | nr_events++; |
| 2549 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2550 | |
Pavel Begunkov | f5ed3bc | 2021-09-24 21:59:52 +0100 | [diff] [blame] | 2551 | if (unlikely(!nr_events)) |
| 2552 | return 0; |
| 2553 | |
| 2554 | io_commit_cqring(ctx); |
| 2555 | io_cqring_ev_posted_iopoll(ctx); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2556 | pos = start ? start->next : ctx->iopoll_list.first; |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2557 | wq_list_cut(&ctx->iopoll_list, prev, start); |
Pavel Begunkov | 1cce17a | 2021-09-24 21:59:54 +0100 | [diff] [blame] | 2558 | io_free_batch_list(ctx, pos); |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2559 | return nr_events; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /* |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2563 | * We can't just wait for polled events to come to us, we have to actively |
| 2564 | * find and complete them. |
| 2565 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 2566 | 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] | 2567 | { |
| 2568 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 2569 | return; |
| 2570 | |
| 2571 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2572 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2573 | /* let it sleep and repeat later if can't complete a request */ |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2574 | if (io_do_iopoll(ctx, true) == 0) |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 2575 | break; |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2576 | /* |
| 2577 | * Ensure we allow local-to-the-cpu processing to take place, |
| 2578 | * in this case we need to ensure that we reap all events. |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2579 | * Also let task_work, etc. to progress by releasing the mutex |
Jens Axboe | 08f5439 | 2019-08-21 22:19:11 -0600 | [diff] [blame] | 2580 | */ |
Pavel Begunkov | 3fcee5a | 2020-07-06 17:59:31 +0300 | [diff] [blame] | 2581 | if (need_resched()) { |
| 2582 | mutex_unlock(&ctx->uring_lock); |
| 2583 | cond_resched(); |
| 2584 | mutex_lock(&ctx->uring_lock); |
| 2585 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2586 | } |
| 2587 | mutex_unlock(&ctx->uring_lock); |
| 2588 | } |
| 2589 | |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2590 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2591 | { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 2592 | unsigned int nr_events = 0; |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2593 | int ret = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2594 | |
Xiaoguang Wang | c7849be | 2020-02-22 14:46:05 +0800 | [diff] [blame] | 2595 | /* |
| 2596 | * We disallow the app entering submit/complete with polling, but we |
| 2597 | * still need to lock the ring to prevent racing with polled issue |
| 2598 | * that got punted to a workqueue. |
| 2599 | */ |
| 2600 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2601 | /* |
| 2602 | * Don't enter poll loop if we already have events pending. |
| 2603 | * If we do, we can potentially be spinning for commands that |
| 2604 | * already triggered a CQE (eg in error). |
| 2605 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 2606 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2607 | __io_cqring_overflow_flush(ctx, false); |
| 2608 | if (io_cqring_events(ctx)) |
| 2609 | goto out; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2610 | do { |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2611 | /* |
| 2612 | * If a submit got punted to a workqueue, we can have the |
| 2613 | * application entering polling for a command before it gets |
| 2614 | * issued. That app will hold the uring_lock for the duration |
| 2615 | * of the poll right here, so we need to take a breather every |
| 2616 | * now and then to ensure that the issue has a chance to add |
| 2617 | * the poll to the issued list. Otherwise we can spin here |
| 2618 | * forever, while the workqueue is stuck trying to acquire the |
| 2619 | * very same mutex. |
| 2620 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2621 | if (wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2622 | u32 tail = ctx->cached_cq_tail; |
| 2623 | |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2624 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 2625 | io_run_task_work(); |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2626 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2627 | |
Pavel Begunkov | 8f487ef | 2021-07-08 13:37:06 +0100 | [diff] [blame] | 2628 | /* some requests don't go through iopoll_list */ |
| 2629 | if (tail != ctx->cached_cq_tail || |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2630 | wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | e9979b3 | 2021-04-13 02:58:45 +0100 | [diff] [blame] | 2631 | break; |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2632 | } |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 2633 | ret = io_do_iopoll(ctx, !min); |
| 2634 | if (ret < 0) |
| 2635 | break; |
| 2636 | nr_events += ret; |
| 2637 | ret = 0; |
| 2638 | } while (nr_events < min && !need_resched()); |
Pavel Begunkov | f39c8a5 | 2021-04-13 02:58:46 +0100 | [diff] [blame] | 2639 | out: |
Jens Axboe | 500f9fb | 2019-08-19 12:15:59 -0600 | [diff] [blame] | 2640 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2641 | return ret; |
| 2642 | } |
| 2643 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2644 | static void kiocb_end_write(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2645 | { |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2646 | /* |
| 2647 | * Tell lockdep we inherited freeze protection from submission |
| 2648 | * thread. |
| 2649 | */ |
| 2650 | if (req->flags & REQ_F_ISREG) { |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2651 | struct super_block *sb = file_inode(req->file)->i_sb; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2652 | |
Pavel Begunkov | 1c98679 | 2021-03-22 01:58:31 +0000 | [diff] [blame] | 2653 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
| 2654 | sb_end_write(sb); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2658 | #ifdef CONFIG_BLOCK |
Pavel Begunkov | dc2a6e9 | 2021-01-19 13:32:35 +0000 | [diff] [blame] | 2659 | static bool io_resubmit_prep(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2660 | { |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2661 | struct io_async_rw *rw = req->async_data; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2662 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 2663 | if (!req_has_async_data(req)) |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2664 | return !io_req_prep_async(req); |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 2665 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
Pavel Begunkov | ab45443 | 2021-03-22 01:58:33 +0000 | [diff] [blame] | 2666 | return true; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2667 | } |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2668 | |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2669 | static bool io_rw_should_reissue(struct io_kiocb *req) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2670 | { |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2671 | umode_t mode = file_inode(req->file)->i_mode; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2672 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2673 | |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 2674 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| 2675 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2676 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
| 2677 | !(ctx->flags & IORING_SETUP_IOPOLL))) |
Jens Axboe | b63534c | 2020-06-04 11:28:00 -0600 | [diff] [blame] | 2678 | return false; |
Jens Axboe | 7c977a5 | 2021-02-23 19:17:35 -0700 | [diff] [blame] | 2679 | /* |
| 2680 | * If ref is dying, we might be running poll reap from the exit work. |
| 2681 | * Don't attempt to reissue from that path, just let it fail with |
| 2682 | * -EAGAIN. |
| 2683 | */ |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2684 | if (percpu_ref_is_dying(&ctx->refs)) |
| 2685 | return false; |
Jens Axboe | ef04688 | 2021-07-27 10:50:31 -0600 | [diff] [blame] | 2686 | /* |
| 2687 | * Play it safe and assume not safe to re-import and reissue if we're |
| 2688 | * not in the original thread group (or in task context). |
| 2689 | */ |
| 2690 | if (!same_thread_group(req->task, current) || !in_task()) |
| 2691 | return false; |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2692 | return true; |
| 2693 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2694 | #else |
Jens Axboe | a1ff1e3 | 2021-04-12 06:40:02 -0600 | [diff] [blame] | 2695 | static bool io_resubmit_prep(struct io_kiocb *req) |
| 2696 | { |
| 2697 | return false; |
| 2698 | } |
Jens Axboe | e82ad48 | 2021-04-02 19:45:34 -0600 | [diff] [blame] | 2699 | static bool io_rw_should_reissue(struct io_kiocb *req) |
| 2700 | { |
| 2701 | return false; |
| 2702 | } |
Jens Axboe | 3e6a0d3 | 2021-03-01 13:56:00 -0700 | [diff] [blame] | 2703 | #endif |
| 2704 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2705 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2706 | { |
Pavel Begunkov | b65c128 | 2021-03-22 01:45:59 +0000 | [diff] [blame] | 2707 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) |
| 2708 | kiocb_end_write(req); |
Pavel Begunkov | 258f3a7 | 2021-10-14 16:10:14 +0100 | [diff] [blame] | 2709 | if (unlikely(res != req->result)) { |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2710 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
| 2711 | io_rw_should_reissue(req)) { |
| 2712 | req->flags |= REQ_F_REISSUE; |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2713 | return true; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2714 | } |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2715 | req_set_fail(req); |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2716 | req->result = res; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2717 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2718 | return false; |
| 2719 | } |
| 2720 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 2721 | static void io_req_task_complete(struct io_kiocb *req, bool *locked) |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2722 | { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2723 | unsigned int cflags = io_put_rw_kbuf(req); |
Pavel Begunkov | 54daa9b | 2021-10-04 20:03:00 +0100 | [diff] [blame] | 2724 | int res = req->result; |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2725 | |
| 2726 | if (*locked) { |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2727 | io_req_complete_state(req, res, cflags); |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 2728 | io_req_add_compl_list(req); |
Pavel Begunkov | 126180b | 2021-08-18 12:42:47 +0100 | [diff] [blame] | 2729 | } else { |
| 2730 | io_req_complete_post(req, res, cflags); |
| 2731 | } |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| 2735 | unsigned int issue_flags) |
| 2736 | { |
| 2737 | if (__io_complete_rw_common(req, res)) |
| 2738 | return; |
Pavel Begunkov | 6363785 | 2021-09-02 00:38:22 +0100 | [diff] [blame] | 2739 | __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req)); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2740 | } |
| 2741 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2742 | static void io_complete_rw(struct kiocb *kiocb, long res) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2743 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2744 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2745 | |
Jens Axboe | 8ef12ef | 2021-08-10 15:15:25 -0600 | [diff] [blame] | 2746 | if (__io_complete_rw_common(req, res)) |
| 2747 | return; |
| 2748 | req->result = res; |
| 2749 | req->io_task_work.func = io_req_task_complete; |
| 2750 | io_req_task_work_add(req); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2753 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2754 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2755 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2756 | |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2757 | if (kiocb->ki_flags & IOCB_WRITE) |
| 2758 | kiocb_end_write(req); |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2759 | if (unlikely(res != req->result)) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2760 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
| 2761 | req->flags |= REQ_F_REISSUE; |
| 2762 | return; |
Pavel Begunkov | 9532b99 | 2021-03-22 01:58:34 +0000 | [diff] [blame] | 2763 | } |
Pavel Begunkov | 258f3a7 | 2021-10-14 16:10:14 +0100 | [diff] [blame] | 2764 | req->result = res; |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2765 | } |
Xiaoguang Wang | bbde017 | 2020-06-16 02:06:38 +0800 | [diff] [blame] | 2766 | |
Pavel Begunkov | b3fa03f | 2021-09-24 21:59:51 +0100 | [diff] [blame] | 2767 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
| 2768 | smp_store_release(&req->iopoll_completed, 1); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | /* |
| 2772 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 2773 | * 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] | 2774 | * 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] | 2775 | * accessing the kiocb cookie. |
| 2776 | */ |
Pavel Begunkov | 9882131 | 2021-10-15 17:09:12 +0100 | [diff] [blame] | 2777 | 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] | 2778 | { |
| 2779 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2780 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2781 | |
| 2782 | /* workqueue context doesn't hold uring_lock, grab it now */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2783 | if (unlikely(needs_lock)) |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2784 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2785 | |
| 2786 | /* |
| 2787 | * Track whether we have multiple files in our lists. This will impact |
| 2788 | * how we do polling eventually, not spinning if we're on potentially |
| 2789 | * different devices. |
| 2790 | */ |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2791 | if (wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2792 | ctx->poll_multi_queue = false; |
| 2793 | } else if (!ctx->poll_multi_queue) { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2794 | struct io_kiocb *list_req; |
| 2795 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2796 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
| 2797 | comp_list); |
Christoph Hellwig | 30da1b4 | 2021-10-12 13:12:14 +0200 | [diff] [blame] | 2798 | if (list_req->file != req->file) |
Hao Xu | 915b3dd | 2021-06-28 05:37:30 +0800 | [diff] [blame] | 2799 | ctx->poll_multi_queue = true; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | /* |
| 2803 | * For fast devices, IO may have already completed. If it has, add |
| 2804 | * it to the front so we find it first. |
| 2805 | */ |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2806 | if (READ_ONCE(req->iopoll_completed)) |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2807 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2808 | else |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 2809 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 2810 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2811 | if (unlikely(needs_lock)) { |
Pavel Begunkov | cb3d897 | 2021-06-14 02:36:14 +0100 | [diff] [blame] | 2812 | /* |
| 2813 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle |
| 2814 | * in sq thread task context or in io worker task context. If |
| 2815 | * current task context is sq thread, we don't need to check |
| 2816 | * whether should wake up sq thread. |
| 2817 | */ |
| 2818 | if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| 2819 | wq_has_sleeper(&ctx->sq_data->wait)) |
| 2820 | wake_up(&ctx->sq_data->wait); |
| 2821 | |
| 2822 | mutex_unlock(&ctx->uring_lock); |
| 2823 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2824 | } |
| 2825 | |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2826 | static bool io_bdev_nowait(struct block_device *bdev) |
| 2827 | { |
Jeffle Xu | 9ba0d0c | 2020-10-19 16:59:42 +0800 | [diff] [blame] | 2828 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2829 | } |
| 2830 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2831 | /* |
| 2832 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2833 | * any file. For now, just ensure that anything potentially problematic is done |
| 2834 | * inline. |
| 2835 | */ |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2836 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2837 | { |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2838 | if (S_ISBLK(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2839 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2840 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2841 | return true; |
| 2842 | return false; |
| 2843 | } |
Pavel Begunkov | 976517f | 2021-06-09 12:07:25 +0100 | [diff] [blame] | 2844 | if (S_ISSOCK(mode)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2845 | return true; |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2846 | if (S_ISREG(mode)) { |
Christoph Hellwig | 4e7b567 | 2020-11-23 13:38:40 +0100 | [diff] [blame] | 2847 | if (IS_ENABLED(CONFIG_BLOCK) && |
| 2848 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
Jens Axboe | 4503b76 | 2020-06-01 10:00:27 -0600 | [diff] [blame] | 2849 | file->f_op != &io_uring_fops) |
| 2850 | return true; |
| 2851 | return false; |
| 2852 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2853 | |
Jens Axboe | c5b8562 | 2020-06-09 19:23:05 -0600 | [diff] [blame] | 2854 | /* any ->read/write should understand O_NONBLOCK */ |
| 2855 | if (file->f_flags & O_NONBLOCK) |
| 2856 | return true; |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2857 | return file->f_mode & FMODE_NOWAIT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2860 | /* |
| 2861 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 2862 | * any file. For now, just ensure that anything potentially problematic is done |
| 2863 | * inline. |
| 2864 | */ |
| 2865 | static unsigned int io_file_get_flags(struct file *file) |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2866 | { |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2867 | umode_t mode = file_inode(file)->i_mode; |
| 2868 | unsigned int res = 0; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2869 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2870 | if (S_ISREG(mode)) |
| 2871 | res |= FFS_ISREG; |
| 2872 | if (__io_file_supports_nowait(file, mode)) |
| 2873 | res |= FFS_NOWAIT; |
| 2874 | return res; |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2877 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2878 | { |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2879 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2880 | } |
| 2881 | |
Pavel Begunkov | b9a6b8f | 2021-10-23 12:14:01 +0100 | [diff] [blame] | 2882 | 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] | 2883 | { |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2884 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2885 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2886 | struct file *file = req->file; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2887 | unsigned ioprio; |
| 2888 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2889 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 2890 | if (!io_req_ffs_set(req)) |
| 2891 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 2892 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2893 | kiocb->ki_pos = READ_ONCE(sqe->off); |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2894 | if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2895 | req->flags |= REQ_F_CUR_POS; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2896 | kiocb->ki_pos = file->f_pos; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2897 | } |
Pavel Begunkov | 5cb03d6 | 2021-10-15 17:09:16 +0100 | [diff] [blame] | 2898 | kiocb->ki_flags = iocb_flags(file); |
Pavel Begunkov | 3e577dc | 2020-02-01 03:58:42 +0300 | [diff] [blame] | 2899 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 2900 | if (unlikely(ret)) |
| 2901 | return ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2902 | |
Jens Axboe | 5d329e1 | 2021-09-14 11:08:37 -0600 | [diff] [blame] | 2903 | /* |
| 2904 | * If the file is marked O_NONBLOCK, still allow retry for it if it |
| 2905 | * supports async. Otherwise it's impossible to use O_NONBLOCK files |
| 2906 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. |
| 2907 | */ |
| 2908 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 2909 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 2910 | req->flags |= REQ_F_NOWAIT; |
| 2911 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2912 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
Pavel Begunkov | 5cb03d6 | 2021-10-15 17:09:16 +0100 | [diff] [blame] | 2913 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2914 | return -EOPNOTSUPP; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2915 | |
Jens Axboe | 394918e | 2021-03-08 11:40:23 -0700 | [diff] [blame] | 2916 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2917 | kiocb->ki_complete = io_complete_rw_iopoll; |
Xiaoguang Wang | 65a6543 | 2020-06-11 23:39:36 +0800 | [diff] [blame] | 2918 | req->iopoll_completed = 0; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2919 | } else { |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 2920 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 2921 | return -EINVAL; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 2922 | kiocb->ki_complete = io_complete_rw; |
| 2923 | } |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 2924 | |
Pavel Begunkov | fb27274 | 2021-10-23 12:14:02 +0100 | [diff] [blame] | 2925 | ioprio = READ_ONCE(sqe->ioprio); |
| 2926 | if (ioprio) { |
| 2927 | ret = ioprio_check_cap(ioprio); |
| 2928 | if (ret) |
| 2929 | return ret; |
| 2930 | |
| 2931 | kiocb->ki_ioprio = ioprio; |
| 2932 | } else { |
| 2933 | kiocb->ki_ioprio = get_current_ioprio(); |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 2934 | } |
| 2935 | |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 2936 | req->imu = NULL; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 2937 | req->rw.addr = READ_ONCE(sqe->addr); |
| 2938 | req->rw.len = READ_ONCE(sqe->len); |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 2939 | req->buf_index = READ_ONCE(sqe->buf_index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2940 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 2944 | { |
| 2945 | switch (ret) { |
| 2946 | case -EIOCBQUEUED: |
| 2947 | break; |
| 2948 | case -ERESTARTSYS: |
| 2949 | case -ERESTARTNOINTR: |
| 2950 | case -ERESTARTNOHAND: |
| 2951 | case -ERESTART_RESTARTBLOCK: |
| 2952 | /* |
| 2953 | * We can't just restart the syscall, since previously |
| 2954 | * submitted sqes may already be in progress. Just fail this |
| 2955 | * IO with EINTR. |
| 2956 | */ |
| 2957 | ret = -EINTR; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2958 | fallthrough; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2959 | default: |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 2960 | kiocb->ki_complete(kiocb, ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 2961 | } |
| 2962 | } |
| 2963 | |
Jens Axboe | a1d7c39 | 2020-06-22 11:09:46 -0600 | [diff] [blame] | 2964 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2965 | unsigned int issue_flags) |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2966 | { |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2967 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2968 | struct io_async_rw *io = req->async_data; |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2969 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2970 | /* add previously done IO, if any */ |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 2971 | if (req_has_async_data(req) && io->bytes_done > 0) { |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2972 | if (ret < 0) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2973 | ret = io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2974 | else |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 2975 | ret += io->bytes_done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 2976 | } |
| 2977 | |
Jens Axboe | ba04291 | 2019-12-25 16:33:42 -0700 | [diff] [blame] | 2978 | if (req->flags & REQ_F_CUR_POS) |
| 2979 | req->file->f_pos = kiocb->ki_pos; |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2980 | if (ret >= 0 && (kiocb->ki_complete == io_complete_rw)) |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 2981 | __io_complete_rw(req, ret, 0, issue_flags); |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 2982 | else |
| 2983 | io_rw_done(kiocb, ret); |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2984 | |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2985 | if (req->flags & REQ_F_REISSUE) { |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 2986 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | a7be7c2 | 2021-04-15 11:31:14 -0600 | [diff] [blame] | 2987 | if (io_resubmit_prep(req)) { |
Jens Axboe | 773af69 | 2021-07-27 10:25:55 -0600 | [diff] [blame] | 2988 | io_req_task_queue_reissue(req); |
Pavel Begunkov | 8c13082 | 2021-03-22 01:58:32 +0000 | [diff] [blame] | 2989 | } else { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2990 | unsigned int cflags = io_put_rw_kbuf(req); |
| 2991 | struct io_ring_ctx *ctx = req->ctx; |
| 2992 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 2993 | req_set_fail(req); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 2994 | if (issue_flags & IO_URING_F_UNLOCKED) { |
Pavel Begunkov | b66ceaf | 2021-09-15 11:00:05 +0100 | [diff] [blame] | 2995 | mutex_lock(&ctx->uring_lock); |
| 2996 | __io_req_complete(req, issue_flags, ret, cflags); |
| 2997 | mutex_unlock(&ctx->uring_lock); |
| 2998 | } else { |
| 2999 | __io_req_complete(req, issue_flags, ret, cflags); |
| 3000 | } |
Pavel Begunkov | 9728463 | 2021-04-08 19:28:03 +0100 | [diff] [blame] | 3001 | } |
| 3002 | } |
Jens Axboe | ba816ad | 2019-09-28 11:36:45 -0600 | [diff] [blame] | 3003 | } |
| 3004 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3005 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
| 3006 | struct io_mapped_ubuf *imu) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3007 | { |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3008 | size_t len = req->rw.len; |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 3009 | u64 buf_end, buf_addr = req->rw.addr; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3010 | size_t offset; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3011 | |
Pavel Begunkov | 75769e3 | 2021-04-01 15:43:54 +0100 | [diff] [blame] | 3012 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3013 | return -EFAULT; |
| 3014 | /* not inside the mapped region */ |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 3015 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3016 | return -EFAULT; |
| 3017 | |
| 3018 | /* |
| 3019 | * May not be a start of buffer, set size appropriately |
| 3020 | * and advance us to the beginning. |
| 3021 | */ |
| 3022 | offset = buf_addr - imu->ubuf; |
| 3023 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3024 | |
| 3025 | if (offset) { |
| 3026 | /* |
| 3027 | * Don't use iov_iter_advance() here, as it's really slow for |
| 3028 | * using the latter parts of a big fixed buffer - it iterates |
| 3029 | * over each segment manually. We can cheat a bit here, because |
| 3030 | * we know that: |
| 3031 | * |
| 3032 | * 1) it's a BVEC iter, we set it up |
| 3033 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 3034 | * first and last bvec |
| 3035 | * |
| 3036 | * So just find our index, and adjust the iterator afterwards. |
| 3037 | * If the offset is within the first bvec (or the whole first |
| 3038 | * bvec, just use iov_iter_advance(). This makes it easier |
| 3039 | * since we can just skip the first segment, which may not |
| 3040 | * be PAGE_SIZE aligned. |
| 3041 | */ |
| 3042 | const struct bio_vec *bvec = imu->bvec; |
| 3043 | |
| 3044 | if (offset <= bvec->bv_len) { |
| 3045 | iov_iter_advance(iter, offset); |
| 3046 | } else { |
| 3047 | unsigned long seg_skip; |
| 3048 | |
| 3049 | /* skip first vec */ |
| 3050 | offset -= bvec->bv_len; |
| 3051 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 3052 | |
| 3053 | iter->bvec = bvec + seg_skip; |
| 3054 | iter->nr_segs -= seg_skip; |
Aleix Roca Nonell | 99c79f6 | 2019-08-15 14:03:22 +0200 | [diff] [blame] | 3055 | iter->count -= bvec->bv_len + offset; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3056 | iter->iov_offset = offset & ~PAGE_MASK; |
Jens Axboe | bd11b3a | 2019-07-20 08:37:31 -0600 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3060 | return 0; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3063 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
| 3064 | { |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3065 | struct io_mapped_ubuf *imu = req->imu; |
| 3066 | u16 index, buf_index = req->buf_index; |
| 3067 | |
| 3068 | if (likely(!imu)) { |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 3069 | struct io_ring_ctx *ctx = req->ctx; |
| 3070 | |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3071 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 3072 | return -EFAULT; |
Pavel Begunkov | 578c0ee | 2021-10-15 17:09:15 +0100 | [diff] [blame] | 3073 | io_req_set_rsrc_node(req, ctx); |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 3074 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 3075 | imu = READ_ONCE(ctx->user_bufs[index]); |
| 3076 | req->imu = imu; |
| 3077 | } |
| 3078 | return __io_import_fixed(req, rw, iter, imu); |
| 3079 | } |
| 3080 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3081 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3082 | { |
| 3083 | if (needs_lock) |
| 3084 | mutex_unlock(&ctx->uring_lock); |
| 3085 | } |
| 3086 | |
| 3087 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| 3088 | { |
| 3089 | /* |
| 3090 | * "Normal" inline submissions always hold the uring_lock, since we |
| 3091 | * grab it from the system call. Same is true for the SQPOLL offload. |
| 3092 | * The only exception is when we've detached the request and issue it |
| 3093 | * from an async worker thread, grab the lock for that case. |
| 3094 | */ |
| 3095 | if (needs_lock) |
| 3096 | mutex_lock(&ctx->uring_lock); |
| 3097 | } |
| 3098 | |
| 3099 | 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] | 3100 | int bgid, unsigned int issue_flags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3101 | { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3102 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3103 | struct io_buffer *head; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 3104 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3105 | |
| 3106 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 3107 | return kbuf; |
| 3108 | |
| 3109 | io_ring_submit_lock(req->ctx, needs_lock); |
| 3110 | |
| 3111 | lockdep_assert_held(&req->ctx->uring_lock); |
| 3112 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3113 | head = xa_load(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3114 | if (head) { |
| 3115 | if (!list_empty(&head->list)) { |
| 3116 | kbuf = list_last_entry(&head->list, struct io_buffer, |
| 3117 | list); |
| 3118 | list_del(&kbuf->list); |
| 3119 | } else { |
| 3120 | kbuf = head; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 3121 | xa_erase(&req->ctx->io_buffers, bgid); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3122 | } |
| 3123 | if (*len > kbuf->len) |
| 3124 | *len = kbuf->len; |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3125 | req->flags |= REQ_F_BUFFER_SELECTED; |
| 3126 | req->kbuf = kbuf; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3127 | } else { |
| 3128 | kbuf = ERR_PTR(-ENOBUFS); |
| 3129 | } |
| 3130 | |
| 3131 | io_ring_submit_unlock(req->ctx, needs_lock); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3132 | return kbuf; |
| 3133 | } |
| 3134 | |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3135 | 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] | 3136 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3137 | { |
| 3138 | struct io_buffer *kbuf; |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3139 | u16 bgid; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3140 | |
Bijan Mottahedeh | 4f4eeba | 2020-05-19 14:52:49 -0700 | [diff] [blame] | 3141 | bgid = req->buf_index; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3142 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3143 | if (IS_ERR(kbuf)) |
| 3144 | return kbuf; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3145 | return u64_to_user_ptr(kbuf->addr); |
| 3146 | } |
| 3147 | |
| 3148 | #ifdef CONFIG_COMPAT |
| 3149 | 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] | 3150 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3151 | { |
| 3152 | struct compat_iovec __user *uiov; |
| 3153 | compat_ssize_t clen; |
| 3154 | void __user *buf; |
| 3155 | ssize_t len; |
| 3156 | |
| 3157 | uiov = u64_to_user_ptr(req->rw.addr); |
| 3158 | if (!access_ok(uiov, sizeof(*uiov))) |
| 3159 | return -EFAULT; |
| 3160 | if (__get_user(clen, &uiov->iov_len)) |
| 3161 | return -EFAULT; |
| 3162 | if (clen < 0) |
| 3163 | return -EINVAL; |
| 3164 | |
| 3165 | len = clen; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3166 | buf = io_rw_buffer_select(req, &len, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3167 | if (IS_ERR(buf)) |
| 3168 | return PTR_ERR(buf); |
| 3169 | iov[0].iov_base = buf; |
| 3170 | iov[0].iov_len = (compat_size_t) len; |
| 3171 | return 0; |
| 3172 | } |
| 3173 | #endif |
| 3174 | |
| 3175 | 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] | 3176 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3177 | { |
| 3178 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| 3179 | void __user *buf; |
| 3180 | ssize_t len; |
| 3181 | |
| 3182 | if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| 3183 | return -EFAULT; |
| 3184 | |
| 3185 | len = iov[0].iov_len; |
| 3186 | if (len < 0) |
| 3187 | return -EINVAL; |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3188 | buf = io_rw_buffer_select(req, &len, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3189 | if (IS_ERR(buf)) |
| 3190 | return PTR_ERR(buf); |
| 3191 | iov[0].iov_base = buf; |
| 3192 | iov[0].iov_len = len; |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
| 3196 | 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] | 3197 | unsigned int issue_flags) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3198 | { |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3199 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 3200 | struct io_buffer *kbuf = req->kbuf; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3201 | |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3202 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| 3203 | iov[0].iov_len = kbuf->len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3204 | return 0; |
Jens Axboe | dddb3e2 | 2020-06-04 11:27:01 -0600 | [diff] [blame] | 3205 | } |
Pavel Begunkov | dd20166 | 2020-12-19 03:15:43 +0000 | [diff] [blame] | 3206 | if (req->rw.len != 1) |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3207 | return -EINVAL; |
| 3208 | |
| 3209 | #ifdef CONFIG_COMPAT |
| 3210 | if (req->ctx->compat) |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3211 | return io_compat_import(req, iov, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3212 | #endif |
| 3213 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3214 | return __io_iov_buffer_select(req, iov, issue_flags); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3217 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
| 3218 | struct io_rw_state *s, |
| 3219 | unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3220 | { |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3221 | struct iov_iter *iter = &s->iter; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3222 | u8 opcode = req->opcode; |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3223 | struct iovec *iovec; |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3224 | void __user *buf; |
| 3225 | size_t sqe_len; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3226 | ssize_t ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 3227 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3228 | BUILD_BUG_ON(ERR_PTR(0) != NULL); |
| 3229 | |
| 3230 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) |
| 3231 | return ERR_PTR(io_import_fixed(req, rw, iter)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3232 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3233 | /* buffer index only valid with fixed read/write, or buffer select */ |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3234 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3235 | return ERR_PTR(-EINVAL); |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3236 | |
Pavel Begunkov | d1d681b | 2021-10-15 17:09:13 +0100 | [diff] [blame] | 3237 | buf = u64_to_user_ptr(req->rw.addr); |
| 3238 | sqe_len = req->rw.len; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3239 | |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3240 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3241 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3242 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
Pavel Begunkov | 867a23e | 2020-08-20 11:34:39 +0300 | [diff] [blame] | 3243 | if (IS_ERR(buf)) |
Changcheng Deng | 898df24 | 2021-10-20 08:49:48 +0000 | [diff] [blame] | 3244 | return ERR_CAST(buf); |
Jens Axboe | 3f9d644 | 2020-03-11 12:27:04 -0600 | [diff] [blame] | 3245 | req->rw.len = sqe_len; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 3246 | } |
| 3247 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3248 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3249 | return ERR_PTR(ret); |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 3250 | } |
| 3251 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3252 | iovec = s->fast_iov; |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3253 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3254 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3255 | if (!ret) |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3256 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); |
| 3257 | return ERR_PTR(ret); |
Jens Axboe | 4d954c2 | 2020-02-27 07:31:19 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3260 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 3261 | req->ctx->compat); |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3262 | if (unlikely(ret < 0)) |
| 3263 | return ERR_PTR(ret); |
| 3264 | return iovec; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3265 | } |
| 3266 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3267 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
| 3268 | struct iovec **iovec, struct io_rw_state *s, |
| 3269 | unsigned int issue_flags) |
| 3270 | { |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3271 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
| 3272 | if (unlikely(IS_ERR(*iovec))) |
| 3273 | return PTR_ERR(*iovec); |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3274 | |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3275 | iov_iter_save_state(&s->iter, &s->iter_state); |
Pavel Begunkov | caa8fe6 | 2021-10-15 17:09:14 +0100 | [diff] [blame] | 3276 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3279 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
| 3280 | { |
Pavel Begunkov | 5b09e37 | 2020-09-30 22:57:15 +0300 | [diff] [blame] | 3281 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3282 | } |
| 3283 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3284 | /* |
| 3285 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 3286 | * by looping over ->read() or ->write() manually. |
| 3287 | */ |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3288 | 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] | 3289 | { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3290 | struct kiocb *kiocb = &req->rw.kiocb; |
| 3291 | struct file *file = req->file; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3292 | ssize_t ret = 0; |
| 3293 | |
| 3294 | /* |
| 3295 | * Don't support polled IO through this interface, and we can't |
| 3296 | * support non-blocking either. For the latter, this just causes |
| 3297 | * the kiocb to be handled from an async context. |
| 3298 | */ |
| 3299 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 3300 | return -EOPNOTSUPP; |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3301 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
| 3302 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3303 | return -EAGAIN; |
| 3304 | |
| 3305 | while (iov_iter_count(iter)) { |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3306 | struct iovec iovec; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3307 | ssize_t nr; |
| 3308 | |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3309 | if (!iov_iter_is_bvec(iter)) { |
| 3310 | iovec = iov_iter_iovec(iter); |
| 3311 | } else { |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3312 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
| 3313 | iovec.iov_len = req->rw.len; |
Pavel Begunkov | 311ae9e | 2019-11-24 11:58:24 +0300 | [diff] [blame] | 3314 | } |
| 3315 | |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3316 | if (rw == READ) { |
| 3317 | nr = file->f_op->read(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3318 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3319 | } else { |
| 3320 | nr = file->f_op->write(file, iovec.iov_base, |
Jens Axboe | 0fef948 | 2020-08-26 10:36:20 -0600 | [diff] [blame] | 3321 | iovec.iov_len, io_kiocb_ppos(kiocb)); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | if (nr < 0) { |
| 3325 | if (!ret) |
| 3326 | ret = nr; |
| 3327 | break; |
| 3328 | } |
Jens Axboe | 16c8d2d | 2021-09-12 06:45:07 -0600 | [diff] [blame] | 3329 | if (!iov_iter_is_bvec(iter)) { |
| 3330 | iov_iter_advance(iter, nr); |
| 3331 | } else { |
| 3332 | req->rw.len -= nr; |
| 3333 | req->rw.addr += nr; |
| 3334 | } |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3335 | ret += nr; |
| 3336 | if (nr != iovec.iov_len) |
| 3337 | break; |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | return ret; |
| 3341 | } |
| 3342 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3343 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
| 3344 | const struct iovec *fast_iov, struct iov_iter *iter) |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3345 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3346 | struct io_async_rw *rw = req->async_data; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3347 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3348 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
Pavel Begunkov | afb8765 | 2020-09-06 00:45:46 +0300 | [diff] [blame] | 3349 | rw->free_iovec = iovec; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3350 | rw->bytes_done = 0; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3351 | /* can only be fixed buffers, no need to do anything */ |
Pavel Begunkov | 9c3a205 | 2020-11-23 23:20:27 +0000 | [diff] [blame] | 3352 | if (iov_iter_is_bvec(iter)) |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3353 | return; |
Pavel Begunkov | b64e344 | 2020-07-13 22:59:18 +0300 | [diff] [blame] | 3354 | if (!iovec) { |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3355 | unsigned iov_off = 0; |
| 3356 | |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3357 | rw->s.iter.iov = rw->s.fast_iov; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3358 | if (iter->iov != fast_iov) { |
| 3359 | iov_off = iter->iov - fast_iov; |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3360 | rw->s.iter.iov += iov_off; |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3361 | } |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3362 | if (rw->s.fast_iov != fast_iov) |
| 3363 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, |
Xiaoguang Wang | 45097da | 2020-04-08 22:29:58 +0800 | [diff] [blame] | 3364 | sizeof(struct iovec) * iter->nr_segs); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 3365 | } else { |
| 3366 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | |
Hao Xu | 8d4af68 | 2021-09-22 18:15:22 +0800 | [diff] [blame] | 3370 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3371 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3372 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
| 3373 | 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] | 3374 | if (req->async_data) { |
| 3375 | req->flags |= REQ_F_ASYNC_DATA; |
| 3376 | return false; |
| 3377 | } |
| 3378 | return true; |
Xiaoguang Wang | 3d9932a | 2020-03-27 15:36:52 +0800 | [diff] [blame] | 3379 | } |
| 3380 | |
Jens Axboe | ff6165b | 2020-08-13 09:47:43 -0600 | [diff] [blame] | 3381 | 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] | 3382 | struct io_rw_state *s, bool force) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3383 | { |
Pavel Begunkov | 26f0505 | 2021-02-28 22:35:18 +0000 | [diff] [blame] | 3384 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
Jens Axboe | 74566df | 2020-01-13 19:23:24 -0700 | [diff] [blame] | 3385 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3386 | if (!req_has_async_data(req)) { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3387 | struct io_async_rw *iorw; |
| 3388 | |
Pavel Begunkov | 6cb7868 | 2021-02-28 22:35:17 +0000 | [diff] [blame] | 3389 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3390 | kfree(iovec); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3391 | return -ENOMEM; |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3392 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3393 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3394 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3395 | iorw = req->async_data; |
| 3396 | /* we've copied and mapped the iter, ensure state is saved */ |
Pavel Begunkov | 538941e | 2021-10-14 16:10:15 +0100 | [diff] [blame] | 3397 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
Jens Axboe | 5d204bc | 2020-01-31 12:06:52 -0700 | [diff] [blame] | 3398 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 3399 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3402 | 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] | 3403 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3404 | struct io_async_rw *iorw = req->async_data; |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3405 | struct iovec *iov; |
Pavel Begunkov | 847595d | 2021-02-04 13:52:06 +0000 | [diff] [blame] | 3406 | int ret; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3407 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 3408 | /* submission path, ->uring_lock should already be taken */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 3409 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3410 | if (unlikely(ret < 0)) |
| 3411 | return ret; |
| 3412 | |
Pavel Begunkov | ab0b196 | 2020-09-06 00:45:47 +0300 | [diff] [blame] | 3413 | iorw->bytes_done = 0; |
| 3414 | iorw->free_iovec = iov; |
| 3415 | if (iov) |
| 3416 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | c3e330a | 2020-07-13 22:59:19 +0300 | [diff] [blame] | 3417 | return 0; |
| 3418 | } |
| 3419 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3420 | 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] | 3421 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3422 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
| 3423 | return -EBADF; |
Pavel Begunkov | b9a6b8f | 2021-10-23 12:14:01 +0100 | [diff] [blame] | 3424 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3425 | } |
| 3426 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3427 | /* |
Matthew Wilcox (Oracle) | ffdc8da | 2020-12-30 17:58:40 -0500 | [diff] [blame] | 3428 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3429 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
| 3430 | * This gets called when the page is unlocked, and we generally expect that to |
| 3431 | * happen when the page IO is completed and the page is now uptodate. This will |
| 3432 | * queue a task_work based retry of the operation, attempting to copy the data |
| 3433 | * again. If the latter fails because the page was NOT uptodate, then we will |
| 3434 | * do a thread based blocking retry of the operation. That's the unexpected |
| 3435 | * slow path. |
| 3436 | */ |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3437 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
| 3438 | int sync, void *arg) |
| 3439 | { |
| 3440 | struct wait_page_queue *wpq; |
| 3441 | struct io_kiocb *req = wait->private; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3442 | struct wait_page_key *key = arg; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3443 | |
| 3444 | wpq = container_of(wait, struct wait_page_queue, wait); |
| 3445 | |
Linus Torvalds | cdc8fcb | 2020-08-03 13:01:22 -0700 | [diff] [blame] | 3446 | if (!wake_page_match(wpq, key)) |
| 3447 | return 0; |
| 3448 | |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3449 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3450 | list_del_init(&wait->entry); |
Pavel Begunkov | 921b905 | 2021-02-12 03:23:53 +0000 | [diff] [blame] | 3451 | io_req_task_queue(req); |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3452 | return 1; |
| 3453 | } |
| 3454 | |
Jens Axboe | c1dd91d | 2020-08-03 16:43:59 -0600 | [diff] [blame] | 3455 | /* |
| 3456 | * This controls whether a given IO request should be armed for async page |
| 3457 | * based retry. If we return false here, the request is handed to the async |
| 3458 | * worker threads for retry. If we're doing buffered reads on a regular file, |
| 3459 | * we prepare a private wait_page_queue entry and retry the operation. This |
| 3460 | * will either succeed because the page is now uptodate and unlocked, or it |
| 3461 | * will register a callback when the page is unlocked at IO completion. Through |
| 3462 | * that callback, io_uring uses task_work to setup a retry of the operation. |
| 3463 | * That retry will attempt the buffered read again. The retry will generally |
| 3464 | * succeed, or in rare cases where it fails, we then fall back to using the |
| 3465 | * async worker threads for a blocking retry. |
| 3466 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3467 | static bool io_rw_should_retry(struct io_kiocb *req) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3468 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3469 | struct io_async_rw *rw = req->async_data; |
| 3470 | struct wait_page_queue *wait = &rw->wpq; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3471 | struct kiocb *kiocb = &req->rw.kiocb; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3472 | |
| 3473 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
| 3474 | if (req->flags & REQ_F_NOWAIT) |
| 3475 | return false; |
| 3476 | |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3477 | /* Only for buffered IO */ |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3478 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3479 | return false; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3480 | |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3481 | /* |
| 3482 | * just use poll if we can, and don't attempt if the fs doesn't |
| 3483 | * support callback based unlocks |
| 3484 | */ |
| 3485 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) |
| 3486 | return false; |
| 3487 | |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3488 | wait->wait.func = io_async_buf_func; |
| 3489 | wait->wait.private = req; |
| 3490 | wait->wait.flags = 0; |
| 3491 | INIT_LIST_HEAD(&wait->wait.entry); |
| 3492 | kiocb->ki_flags |= IOCB_WAITQ; |
Hao Xu | c8d317a | 2020-09-29 20:00:45 +0800 | [diff] [blame] | 3493 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3494 | kiocb->ki_waitq = wait; |
Jens Axboe | 3b2a443 | 2020-08-16 10:58:43 -0700 | [diff] [blame] | 3495 | return true; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3496 | } |
| 3497 | |
Pavel Begunkov | aeab950 | 2021-06-14 02:36:24 +0100 | [diff] [blame] | 3498 | 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] | 3499 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3500 | if (likely(req->file->f_op->read_iter)) |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3501 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3502 | else if (req->file->f_op->read) |
Jens Axboe | 4017eb9 | 2020-10-22 14:14:12 -0600 | [diff] [blame] | 3503 | return loop_rw_iter(READ, req, iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3504 | else |
| 3505 | return -EINVAL; |
Jens Axboe | bcf5a06 | 2020-05-22 09:24:42 -0600 | [diff] [blame] | 3506 | } |
| 3507 | |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3508 | static bool need_read_all(struct io_kiocb *req) |
| 3509 | { |
| 3510 | return req->flags & REQ_F_ISREG || |
| 3511 | S_ISBLK(file_inode(req->file)->i_mode); |
| 3512 | } |
| 3513 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3514 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3515 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3516 | struct io_rw_state __s, *s = &__s; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3517 | struct iovec *iovec; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3518 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3519 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3520 | struct io_async_rw *rw; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3521 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3522 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3523 | if (!req_has_async_data(req)) { |
| 3524 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); |
| 3525 | if (unlikely(ret < 0)) |
| 3526 | return ret; |
| 3527 | } else { |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 3528 | rw = req->async_data; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3529 | s = &rw->s; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3530 | /* |
| 3531 | * We come here from an earlier attempt, restore our state to |
| 3532 | * match in case it doesn't. It's cheap enough that we don't |
| 3533 | * need to make this conditional. |
| 3534 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3535 | iov_iter_restore(&s->iter, &s->iter_state); |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3536 | iovec = NULL; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3537 | } |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3538 | req->result = iov_iter_count(&s->iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3539 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3540 | if (force_nonblock) { |
| 3541 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3542 | if (unlikely(!io_file_supports_nowait(req))) { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3543 | ret = io_setup_async_rw(req, iovec, s, true); |
| 3544 | return ret ?: -EAGAIN; |
| 3545 | } |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3546 | kiocb->ki_flags |= IOCB_NOWAIT; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3547 | } else { |
| 3548 | /* Ensure we clear previously set non-block flag */ |
| 3549 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
Pavel Begunkov | 6713e7a | 2021-02-04 13:51:59 +0000 | [diff] [blame] | 3550 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3551 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3552 | 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] | 3553 | if (unlikely(ret)) { |
| 3554 | kfree(iovec); |
| 3555 | return ret; |
| 3556 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3557 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3558 | ret = io_iter_do_read(req, &s->iter); |
Jens Axboe | 3296061 | 2019-09-23 11:05:34 -0600 | [diff] [blame] | 3559 | |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3560 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3561 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3562 | /* IOPOLL retry should happen for io-wq threads */ |
| 3563 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | f91daf5 | 2020-08-15 15:58:42 -0700 | [diff] [blame] | 3564 | goto done; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3565 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3566 | if (req->flags & REQ_F_NOWAIT) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3567 | goto done; |
Jens Axboe | f38c7e3 | 2020-09-25 15:23:43 -0600 | [diff] [blame] | 3568 | ret = 0; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3569 | } else if (ret == -EIOCBQUEUED) { |
| 3570 | goto out_free; |
Pavel Begunkov | f80a50a | 2021-10-14 16:10:13 +0100 | [diff] [blame] | 3571 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
Ming Lei | 7db3043 | 2021-08-21 23:07:51 +0800 | [diff] [blame] | 3572 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
Pavel Begunkov | 7335e3b | 2021-02-04 13:52:02 +0000 | [diff] [blame] | 3573 | /* read all, failed, already did sync or don't want to retry */ |
Jens Axboe | 00d23d5 | 2020-08-25 12:59:22 -0600 | [diff] [blame] | 3574 | goto done; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3575 | } |
| 3576 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3577 | /* |
| 3578 | * Don't depend on the iter state matching what was consumed, or being |
| 3579 | * untouched in case of error. Restore it and we'll advance it |
| 3580 | * manually if we need to. |
| 3581 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3582 | iov_iter_restore(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3583 | |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3584 | ret2 = io_setup_async_rw(req, iovec, s, true); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3585 | if (ret2) |
| 3586 | return ret2; |
| 3587 | |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3588 | iovec = NULL; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 3589 | rw = req->async_data; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3590 | s = &rw->s; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3591 | /* |
| 3592 | * Now use our persistent iterator and state, if we aren't already. |
| 3593 | * We've restored and mapped the iter to match. |
| 3594 | */ |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3595 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3596 | do { |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3597 | /* |
| 3598 | * We end up here because of a partial read, either from |
| 3599 | * above or inside this loop. Advance the iter by the bytes |
| 3600 | * that were consumed. |
| 3601 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3602 | iov_iter_advance(&s->iter, ret); |
| 3603 | if (!iov_iter_count(&s->iter)) |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3604 | break; |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3605 | rw->bytes_done += ret; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3606 | iov_iter_save_state(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3607 | |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3608 | /* if we can retry, do so with the callbacks armed */ |
| 3609 | if (!io_rw_should_retry(req)) { |
| 3610 | kiocb->ki_flags &= ~IOCB_WAITQ; |
| 3611 | return -EAGAIN; |
| 3612 | } |
| 3613 | |
| 3614 | /* |
| 3615 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If |
| 3616 | * we get -EIOCBQUEUED, then we'll get a notification when the |
| 3617 | * desired page gets unlocked. We can also get a partial read |
| 3618 | * here, and if we do, then just retry at the new offset. |
| 3619 | */ |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3620 | ret = io_iter_do_read(req, &s->iter); |
Pavel Begunkov | b23df91 | 2021-02-04 13:52:04 +0000 | [diff] [blame] | 3621 | if (ret == -EIOCBQUEUED) |
| 3622 | return 0; |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3623 | /* we got some bytes, but not all. retry. */ |
Jens Axboe | b5b0ecb | 2021-03-04 21:02:58 -0700 | [diff] [blame] | 3624 | kiocb->ki_flags &= ~IOCB_WAITQ; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3625 | iov_iter_restore(&s->iter, &s->iter_state); |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3626 | } while (ret > 0); |
Jens Axboe | 227c0c9 | 2020-08-13 11:51:40 -0600 | [diff] [blame] | 3627 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3628 | kiocb_done(kiocb, ret, issue_flags); |
Pavel Begunkov | fe1cdd5 | 2021-02-17 21:02:36 +0000 | [diff] [blame] | 3629 | out_free: |
| 3630 | /* it's faster to check here then delegate to kfree */ |
| 3631 | if (iovec) |
| 3632 | kfree(iovec); |
Pavel Begunkov | 5ea5dd4 | 2021-02-04 13:52:03 +0000 | [diff] [blame] | 3633 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3634 | } |
| 3635 | |
Pavel Begunkov | 73debe6 | 2020-09-30 22:57:54 +0300 | [diff] [blame] | 3636 | 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] | 3637 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 3638 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
| 3639 | return -EBADF; |
Jens Axboe | 3884b83 | 2021-10-25 13:45:12 -0600 | [diff] [blame] | 3640 | 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] | 3641 | return io_prep_rw(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3644 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3645 | { |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3646 | struct io_rw_state __s, *s = &__s; |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3647 | struct iovec *iovec; |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 3648 | struct kiocb *kiocb = &req->rw.kiocb; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3649 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3650 | ssize_t ret, ret2; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3651 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3652 | if (!req_has_async_data(req)) { |
Pavel Begunkov | 5e49c97 | 2021-10-14 16:10:18 +0100 | [diff] [blame] | 3653 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
| 3654 | if (unlikely(ret < 0)) |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3655 | return ret; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3656 | } else { |
| 3657 | struct io_async_rw *rw = req->async_data; |
| 3658 | |
| 3659 | s = &rw->s; |
| 3660 | iov_iter_restore(&s->iter, &s->iter_state); |
| 3661 | iovec = NULL; |
Pavel Begunkov | 2846c48 | 2020-11-07 13:16:27 +0000 | [diff] [blame] | 3662 | } |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3663 | req->result = iov_iter_count(&s->iter); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3664 | |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3665 | if (force_nonblock) { |
| 3666 | /* If the file doesn't support async, just async punt */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3667 | if (unlikely(!io_file_supports_nowait(req))) |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3668 | goto copy_iov; |
| 3669 | |
| 3670 | /* file path doesn't support NOWAIT for non-direct_IO */ |
| 3671 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && |
| 3672 | (req->flags & REQ_F_ISREG)) |
| 3673 | goto copy_iov; |
| 3674 | |
Pavel Begunkov | a88fc40 | 2020-09-30 22:57:53 +0300 | [diff] [blame] | 3675 | kiocb->ki_flags |= IOCB_NOWAIT; |
Pavel Begunkov | 607b6fb | 2021-10-14 16:10:19 +0100 | [diff] [blame] | 3676 | } else { |
| 3677 | /* Ensure we clear previously set non-block flag */ |
| 3678 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
| 3679 | } |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 3680 | |
Jens Axboe | cd65869 | 2021-09-10 11:19:14 -0600 | [diff] [blame] | 3681 | 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] | 3682 | if (unlikely(ret)) |
| 3683 | goto out_free; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3684 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3685 | /* |
| 3686 | * Open-code file_start_write here to grab freeze protection, |
| 3687 | * which will be released by another thread in |
| 3688 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 3689 | * released so that it doesn't complain about the held lock when |
| 3690 | * we return to userspace. |
| 3691 | */ |
| 3692 | if (req->flags & REQ_F_ISREG) { |
Darrick J. Wong | 8a3c84b | 2020-11-10 16:50:21 -0800 | [diff] [blame] | 3693 | sb_start_write(file_inode(req->file)->i_sb); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3694 | __sb_writers_release(file_inode(req->file)->i_sb, |
| 3695 | SB_FREEZE_WRITE); |
| 3696 | } |
| 3697 | kiocb->ki_flags |= IOCB_WRITE; |
Roman Penyaev | 9bf7933 | 2019-03-25 20:09:24 +0100 | [diff] [blame] | 3698 | |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 3699 | if (likely(req->file->f_op->write_iter)) |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3700 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3701 | else if (req->file->f_op->write) |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3702 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
Guoyu Huang | 2dd2111 | 2020-08-05 03:53:50 -0700 | [diff] [blame] | 3703 | else |
| 3704 | ret2 = -EINVAL; |
Jens Axboe | 4ed734b | 2020-03-20 11:23:41 -0600 | [diff] [blame] | 3705 | |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3706 | if (req->flags & REQ_F_REISSUE) { |
| 3707 | req->flags &= ~REQ_F_REISSUE; |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3708 | ret2 = -EAGAIN; |
Pavel Begunkov | 6ad7f23 | 2021-04-08 01:54:39 +0100 | [diff] [blame] | 3709 | } |
Jens Axboe | 230d50d | 2021-04-01 20:41:15 -0600 | [diff] [blame] | 3710 | |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3711 | /* |
| 3712 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just |
| 3713 | * retry them without IOCB_NOWAIT. |
| 3714 | */ |
| 3715 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) |
| 3716 | ret2 = -EAGAIN; |
Pavel Begunkov | 75c668c | 2021-02-04 13:52:05 +0000 | [diff] [blame] | 3717 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
| 3718 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3719 | goto done; |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3720 | if (!force_nonblock || ret2 != -EAGAIN) { |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3721 | /* IOPOLL retry should happen for io-wq threads */ |
Noah Goldstein | b10841c | 2021-10-16 20:32:29 -0500 | [diff] [blame] | 3722 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | eefdf30 | 2020-08-27 16:40:19 -0600 | [diff] [blame] | 3723 | goto copy_iov; |
Jens Axboe | 355afae | 2020-09-02 09:30:31 -0600 | [diff] [blame] | 3724 | done: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 3725 | kiocb_done(kiocb, ret2, issue_flags); |
Pavel Begunkov | fa15baf | 2020-08-01 13:50:02 +0300 | [diff] [blame] | 3726 | } else { |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 3727 | copy_iov: |
Pavel Begunkov | c88598a | 2021-10-14 16:10:16 +0100 | [diff] [blame] | 3728 | iov_iter_restore(&s->iter, &s->iter_state); |
| 3729 | ret = io_setup_async_rw(req, iovec, s, false); |
Pavel Begunkov | 6bf985d | 2021-02-04 13:52:01 +0000 | [diff] [blame] | 3730 | return ret ?: -EAGAIN; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3731 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 3732 | out_free: |
Pavel Begunkov | f261c16 | 2020-08-20 11:34:10 +0300 | [diff] [blame] | 3733 | /* it's reportedly faster than delegating the null check to kfree() */ |
Pavel Begunkov | 252917c | 2020-07-13 22:59:20 +0300 | [diff] [blame] | 3734 | if (iovec) |
Xiaoguang Wang | 6f2cc16 | 2020-06-18 15:01:56 +0800 | [diff] [blame] | 3735 | kfree(iovec); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 3736 | return ret; |
| 3737 | } |
| 3738 | |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3739 | static int io_renameat_prep(struct io_kiocb *req, |
| 3740 | const struct io_uring_sqe *sqe) |
| 3741 | { |
| 3742 | struct io_rename *ren = &req->rename; |
| 3743 | const char __user *oldf, *newf; |
| 3744 | |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3745 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3746 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3747 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | ed7eb25 | 2021-06-23 09:04:13 -0600 | [diff] [blame] | 3748 | return -EINVAL; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3749 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3750 | return -EBADF; |
| 3751 | |
| 3752 | ren->old_dfd = READ_ONCE(sqe->fd); |
| 3753 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3754 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3755 | ren->new_dfd = READ_ONCE(sqe->len); |
| 3756 | ren->flags = READ_ONCE(sqe->rename_flags); |
| 3757 | |
| 3758 | ren->oldpath = getname(oldf); |
| 3759 | if (IS_ERR(ren->oldpath)) |
| 3760 | return PTR_ERR(ren->oldpath); |
| 3761 | |
| 3762 | ren->newpath = getname(newf); |
| 3763 | if (IS_ERR(ren->newpath)) { |
| 3764 | putname(ren->oldpath); |
| 3765 | return PTR_ERR(ren->newpath); |
| 3766 | } |
| 3767 | |
| 3768 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3769 | return 0; |
| 3770 | } |
| 3771 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3772 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3773 | { |
| 3774 | struct io_rename *ren = &req->rename; |
| 3775 | int ret; |
| 3776 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3777 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3778 | return -EAGAIN; |
| 3779 | |
| 3780 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, |
| 3781 | ren->newpath, ren->flags); |
| 3782 | |
| 3783 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3784 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3785 | req_set_fail(req); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 3786 | io_req_complete(req, ret); |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3790 | static int io_unlinkat_prep(struct io_kiocb *req, |
| 3791 | const struct io_uring_sqe *sqe) |
| 3792 | { |
| 3793 | struct io_unlink *un = &req->unlink; |
| 3794 | const char __user *fname; |
| 3795 | |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3796 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3797 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3798 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 3799 | sqe->splice_fd_in) |
Jens Axboe | 22634bc | 2021-06-23 09:07:45 -0600 | [diff] [blame] | 3800 | return -EINVAL; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3801 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3802 | return -EBADF; |
| 3803 | |
| 3804 | un->dfd = READ_ONCE(sqe->fd); |
| 3805 | |
| 3806 | un->flags = READ_ONCE(sqe->unlink_flags); |
| 3807 | if (un->flags & ~AT_REMOVEDIR) |
| 3808 | return -EINVAL; |
| 3809 | |
| 3810 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3811 | un->filename = getname(fname); |
| 3812 | if (IS_ERR(un->filename)) |
| 3813 | return PTR_ERR(un->filename); |
| 3814 | |
| 3815 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3816 | return 0; |
| 3817 | } |
| 3818 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3819 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3820 | { |
| 3821 | struct io_unlink *un = &req->unlink; |
| 3822 | int ret; |
| 3823 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3824 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3825 | return -EAGAIN; |
| 3826 | |
| 3827 | if (un->flags & AT_REMOVEDIR) |
| 3828 | ret = do_rmdir(un->dfd, un->filename); |
| 3829 | else |
| 3830 | ret = do_unlinkat(un->dfd, un->filename); |
| 3831 | |
| 3832 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3833 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 3834 | req_set_fail(req); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 3835 | io_req_complete(req, ret); |
| 3836 | return 0; |
| 3837 | } |
| 3838 | |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3839 | static int io_mkdirat_prep(struct io_kiocb *req, |
| 3840 | const struct io_uring_sqe *sqe) |
| 3841 | { |
| 3842 | struct io_mkdir *mkd = &req->mkdir; |
| 3843 | const char __user *fname; |
| 3844 | |
| 3845 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3846 | return -EINVAL; |
| 3847 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || |
| 3848 | sqe->splice_fd_in) |
| 3849 | return -EINVAL; |
| 3850 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3851 | return -EBADF; |
| 3852 | |
| 3853 | mkd->dfd = READ_ONCE(sqe->fd); |
| 3854 | mkd->mode = READ_ONCE(sqe->len); |
| 3855 | |
| 3856 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3857 | mkd->filename = getname(fname); |
| 3858 | if (IS_ERR(mkd->filename)) |
| 3859 | return PTR_ERR(mkd->filename); |
| 3860 | |
| 3861 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3862 | return 0; |
| 3863 | } |
| 3864 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 3865 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 3866 | { |
| 3867 | struct io_mkdir *mkd = &req->mkdir; |
| 3868 | int ret; |
| 3869 | |
| 3870 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3871 | return -EAGAIN; |
| 3872 | |
| 3873 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); |
| 3874 | |
| 3875 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3876 | if (ret < 0) |
| 3877 | req_set_fail(req); |
| 3878 | io_req_complete(req, ret); |
| 3879 | return 0; |
| 3880 | } |
| 3881 | |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3882 | static int io_symlinkat_prep(struct io_kiocb *req, |
| 3883 | const struct io_uring_sqe *sqe) |
| 3884 | { |
| 3885 | struct io_symlink *sl = &req->symlink; |
| 3886 | const char __user *oldpath, *newpath; |
| 3887 | |
| 3888 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3889 | return -EINVAL; |
| 3890 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || |
| 3891 | sqe->splice_fd_in) |
| 3892 | return -EINVAL; |
| 3893 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3894 | return -EBADF; |
| 3895 | |
| 3896 | sl->new_dfd = READ_ONCE(sqe->fd); |
| 3897 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3898 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3899 | |
| 3900 | sl->oldpath = getname(oldpath); |
| 3901 | if (IS_ERR(sl->oldpath)) |
| 3902 | return PTR_ERR(sl->oldpath); |
| 3903 | |
| 3904 | sl->newpath = getname(newpath); |
| 3905 | if (IS_ERR(sl->newpath)) { |
| 3906 | putname(sl->oldpath); |
| 3907 | return PTR_ERR(sl->newpath); |
| 3908 | } |
| 3909 | |
| 3910 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3911 | return 0; |
| 3912 | } |
| 3913 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 3914 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 3915 | { |
| 3916 | struct io_symlink *sl = &req->symlink; |
| 3917 | int ret; |
| 3918 | |
| 3919 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3920 | return -EAGAIN; |
| 3921 | |
| 3922 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); |
| 3923 | |
| 3924 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3925 | if (ret < 0) |
| 3926 | req_set_fail(req); |
| 3927 | io_req_complete(req, ret); |
| 3928 | return 0; |
| 3929 | } |
| 3930 | |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 3931 | static int io_linkat_prep(struct io_kiocb *req, |
| 3932 | const struct io_uring_sqe *sqe) |
| 3933 | { |
| 3934 | struct io_hardlink *lnk = &req->hardlink; |
| 3935 | const char __user *oldf, *newf; |
| 3936 | |
| 3937 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3938 | return -EINVAL; |
| 3939 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) |
| 3940 | return -EINVAL; |
| 3941 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
| 3942 | return -EBADF; |
| 3943 | |
| 3944 | lnk->old_dfd = READ_ONCE(sqe->fd); |
| 3945 | lnk->new_dfd = READ_ONCE(sqe->len); |
| 3946 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 3947 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 3948 | lnk->flags = READ_ONCE(sqe->hardlink_flags); |
| 3949 | |
| 3950 | lnk->oldpath = getname(oldf); |
| 3951 | if (IS_ERR(lnk->oldpath)) |
| 3952 | return PTR_ERR(lnk->oldpath); |
| 3953 | |
| 3954 | lnk->newpath = getname(newf); |
| 3955 | if (IS_ERR(lnk->newpath)) { |
| 3956 | putname(lnk->oldpath); |
| 3957 | return PTR_ERR(lnk->newpath); |
| 3958 | } |
| 3959 | |
| 3960 | req->flags |= REQ_F_NEED_CLEANUP; |
| 3961 | return 0; |
| 3962 | } |
| 3963 | |
Pavel Begunkov | 04f3408 | 2021-10-14 16:10:12 +0100 | [diff] [blame] | 3964 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 3965 | { |
| 3966 | struct io_hardlink *lnk = &req->hardlink; |
| 3967 | int ret; |
| 3968 | |
| 3969 | if (issue_flags & IO_URING_F_NONBLOCK) |
| 3970 | return -EAGAIN; |
| 3971 | |
| 3972 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, |
| 3973 | lnk->newpath, lnk->flags); |
| 3974 | |
| 3975 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 3976 | if (ret < 0) |
| 3977 | req_set_fail(req); |
| 3978 | io_req_complete(req, ret); |
| 3979 | return 0; |
| 3980 | } |
| 3981 | |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3982 | static int io_shutdown_prep(struct io_kiocb *req, |
| 3983 | const struct io_uring_sqe *sqe) |
| 3984 | { |
| 3985 | #if defined(CONFIG_NET) |
| 3986 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 3987 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 3988 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
| 3989 | sqe->buf_index || sqe->splice_fd_in)) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 3990 | return -EINVAL; |
| 3991 | |
| 3992 | req->shutdown.how = READ_ONCE(sqe->len); |
| 3993 | return 0; |
| 3994 | #else |
| 3995 | return -EOPNOTSUPP; |
| 3996 | #endif |
| 3997 | } |
| 3998 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 3999 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4000 | { |
| 4001 | #if defined(CONFIG_NET) |
| 4002 | struct socket *sock; |
| 4003 | int ret; |
| 4004 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4005 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4006 | return -EAGAIN; |
| 4007 | |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4008 | sock = sock_from_file(req->file); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4009 | if (unlikely(!sock)) |
Linus Torvalds | 48aba79 | 2020-12-16 12:44:05 -0800 | [diff] [blame] | 4010 | return -ENOTSOCK; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4011 | |
| 4012 | ret = __sys_shutdown_sock(sock, req->shutdown.how); |
Jens Axboe | a146468 | 2020-12-14 20:57:27 -0700 | [diff] [blame] | 4013 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4014 | req_set_fail(req); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 4015 | io_req_complete(req, ret); |
| 4016 | return 0; |
| 4017 | #else |
| 4018 | return -EOPNOTSUPP; |
| 4019 | #endif |
| 4020 | } |
| 4021 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4022 | static int __io_splice_prep(struct io_kiocb *req, |
| 4023 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4024 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4025 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4026 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4027 | |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4028 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4029 | return -EINVAL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4030 | |
| 4031 | sp->file_in = NULL; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4032 | sp->len = READ_ONCE(sqe->len); |
| 4033 | sp->flags = READ_ONCE(sqe->splice_flags); |
| 4034 | |
| 4035 | if (unlikely(sp->flags & ~valid_flags)) |
| 4036 | return -EINVAL; |
| 4037 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 4038 | 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] | 4039 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
| 4040 | if (!sp->file_in) |
| 4041 | return -EBADF; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4042 | req->flags |= REQ_F_NEED_CLEANUP; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4043 | return 0; |
| 4044 | } |
| 4045 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4046 | static int io_tee_prep(struct io_kiocb *req, |
| 4047 | const struct io_uring_sqe *sqe) |
| 4048 | { |
| 4049 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) |
| 4050 | return -EINVAL; |
| 4051 | return __io_splice_prep(req, sqe); |
| 4052 | } |
| 4053 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4054 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4055 | { |
| 4056 | struct io_splice *sp = &req->splice; |
| 4057 | struct file *in = sp->file_in; |
| 4058 | struct file *out = sp->file_out; |
| 4059 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4060 | long ret = 0; |
| 4061 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4062 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4063 | return -EAGAIN; |
| 4064 | if (sp->len) |
| 4065 | ret = do_tee(in, out, sp->len, flags); |
| 4066 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4067 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4068 | io_put_file(in); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4069 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4070 | |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4071 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4072 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4073 | io_req_complete(req, ret); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4074 | return 0; |
| 4075 | } |
| 4076 | |
| 4077 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4078 | { |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 4079 | struct io_splice *sp = &req->splice; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 4080 | |
| 4081 | sp->off_in = READ_ONCE(sqe->splice_off_in); |
| 4082 | sp->off_out = READ_ONCE(sqe->off); |
| 4083 | return __io_splice_prep(req, sqe); |
| 4084 | } |
| 4085 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4086 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4087 | { |
| 4088 | struct io_splice *sp = &req->splice; |
| 4089 | struct file *in = sp->file_in; |
| 4090 | struct file *out = sp->file_out; |
| 4091 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; |
| 4092 | loff_t *poff_in, *poff_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4093 | long ret = 0; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4094 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4095 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 2fb3e82 | 2020-05-01 17:09:38 +0300 | [diff] [blame] | 4096 | return -EAGAIN; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4097 | |
| 4098 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; |
| 4099 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4100 | |
Jens Axboe | 948a774 | 2020-05-17 14:21:38 -0600 | [diff] [blame] | 4101 | if (sp->len) |
Pavel Begunkov | c968742 | 2020-05-04 23:00:54 +0300 | [diff] [blame] | 4102 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4103 | |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 4104 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
| 4105 | io_put_file(in); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4106 | req->flags &= ~REQ_F_NEED_CLEANUP; |
| 4107 | |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4108 | if (ret != sp->len) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4109 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4110 | io_req_complete(req, ret); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 4111 | return 0; |
| 4112 | } |
| 4113 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4114 | /* |
| 4115 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 4116 | */ |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4117 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4118 | { |
| 4119 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4120 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4121 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4122 | return -EINVAL; |
| 4123 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4124 | __io_req_complete(req, issue_flags, 0, 0); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4125 | return 0; |
| 4126 | } |
| 4127 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4128 | 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] | 4129 | { |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4130 | struct io_ring_ctx *ctx = req->ctx; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4131 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 4132 | if (!req->file) |
| 4133 | return -EBADF; |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4134 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 4135 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 4136 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4137 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4138 | sqe->splice_fd_in)) |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4139 | return -EINVAL; |
| 4140 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4141 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
| 4142 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) |
| 4143 | return -EINVAL; |
| 4144 | |
| 4145 | req->sync.off = READ_ONCE(sqe->off); |
| 4146 | req->sync.len = READ_ONCE(sqe->len); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4147 | return 0; |
| 4148 | } |
| 4149 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4150 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 7891293 | 2020-01-14 22:09:06 -0700 | [diff] [blame] | 4151 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4152 | loff_t end = req->sync.off + req->sync.len; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4153 | int ret; |
| 4154 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4155 | /* fsync always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4156 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4157 | return -EAGAIN; |
| 4158 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4159 | ret = vfs_fsync_range(req->file, req->sync.off, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4160 | end > 0 ? end : LLONG_MAX, |
| 4161 | req->sync.flags & IORING_FSYNC_DATASYNC); |
| 4162 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4163 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4164 | io_req_complete(req, ret); |
Christoph Hellwig | c992fe2 | 2019-01-11 09:43:02 -0700 | [diff] [blame] | 4165 | return 0; |
| 4166 | } |
| 4167 | |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4168 | static int io_fallocate_prep(struct io_kiocb *req, |
| 4169 | const struct io_uring_sqe *sqe) |
| 4170 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4171 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
| 4172 | sqe->splice_fd_in) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4173 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4174 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4175 | return -EINVAL; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4176 | |
| 4177 | req->sync.off = READ_ONCE(sqe->off); |
| 4178 | req->sync.len = READ_ONCE(sqe->addr); |
| 4179 | req->sync.mode = READ_ONCE(sqe->len); |
| 4180 | return 0; |
| 4181 | } |
| 4182 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4183 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4184 | { |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4185 | int ret; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4186 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4187 | /* fallocate always requiring blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4188 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4189 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4190 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
| 4191 | req->sync.len); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4192 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4193 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4194 | io_req_complete(req, ret); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 4195 | return 0; |
| 4196 | } |
| 4197 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4198 | 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] | 4199 | { |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4200 | const char __user *fname; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4201 | int ret; |
| 4202 | |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4203 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4204 | return -EINVAL; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4205 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4206 | return -EINVAL; |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4207 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4208 | return -EBADF; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4209 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4210 | /* open.how should be already initialised */ |
| 4211 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
Jens Axboe | 08a1d26eb | 2020-04-08 09:20:54 -0600 | [diff] [blame] | 4212 | req->open.how.flags |= O_LARGEFILE; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4213 | |
Pavel Begunkov | 25e72d1 | 2020-06-03 18:03:23 +0300 | [diff] [blame] | 4214 | req->open.dfd = READ_ONCE(sqe->fd); |
| 4215 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | f874888 | 2020-01-08 17:47:02 -0700 | [diff] [blame] | 4216 | req->open.filename = getname(fname); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4217 | if (IS_ERR(req->open.filename)) { |
| 4218 | ret = PTR_ERR(req->open.filename); |
| 4219 | req->open.filename = NULL; |
| 4220 | return ret; |
| 4221 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4222 | |
| 4223 | req->open.file_slot = READ_ONCE(sqe->file_index); |
| 4224 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) |
| 4225 | return -EINVAL; |
| 4226 | |
Jens Axboe | 4022e7a | 2020-03-19 19:23:18 -0600 | [diff] [blame] | 4227 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4228 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4229 | return 0; |
| 4230 | } |
| 4231 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4232 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4233 | { |
Pavel Begunkov | d3fddf6 | 2021-08-09 13:04:16 +0100 | [diff] [blame] | 4234 | u64 mode = READ_ONCE(sqe->len); |
| 4235 | u64 flags = READ_ONCE(sqe->open_flags); |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4236 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4237 | req->open.how = build_open_how(flags, mode); |
| 4238 | return __io_openat_prep(req, sqe); |
| 4239 | } |
| 4240 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4241 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4242 | { |
| 4243 | struct open_how __user *how; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4244 | size_t len; |
| 4245 | int ret; |
| 4246 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4247 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4248 | len = READ_ONCE(sqe->len); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4249 | if (len < OPEN_HOW_SIZE_VER0) |
| 4250 | return -EINVAL; |
| 4251 | |
| 4252 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
| 4253 | len); |
| 4254 | if (ret) |
| 4255 | return ret; |
| 4256 | |
Pavel Begunkov | ec65fea | 2020-06-03 18:03:24 +0300 | [diff] [blame] | 4257 | return __io_openat_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4260 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4261 | { |
| 4262 | struct open_flags op; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4263 | struct file *file; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4264 | bool resolve_nonblock, nonblock_set; |
| 4265 | bool fixed = !!req->open.file_slot; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4266 | int ret; |
| 4267 | |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4268 | ret = build_open_flags(&req->open.how, &op); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4269 | if (ret) |
| 4270 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4271 | nonblock_set = op.open_flag & O_NONBLOCK; |
| 4272 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4273 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4274 | /* |
| 4275 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, |
| 4276 | * it'll always -EAGAIN |
| 4277 | */ |
| 4278 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) |
| 4279 | return -EAGAIN; |
| 4280 | op.lookup_flags |= LOOKUP_CACHED; |
| 4281 | op.open_flag |= O_NONBLOCK; |
| 4282 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4283 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4284 | if (!fixed) { |
| 4285 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); |
| 4286 | if (ret < 0) |
| 4287 | goto err; |
| 4288 | } |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4289 | |
| 4290 | file = do_filp_open(req->open.dfd, req->open.filename, &op); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4291 | if (IS_ERR(file)) { |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4292 | /* |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4293 | * We could hang on to this 'fd' on retrying, but seems like |
| 4294 | * marginal gain for something that is now known to be a slower |
| 4295 | * 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] | 4296 | */ |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4297 | if (!fixed) |
| 4298 | put_unused_fd(ret); |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4299 | |
| 4300 | ret = PTR_ERR(file); |
| 4301 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
| 4302 | if (ret == -EAGAIN && |
| 4303 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) |
| 4304 | return -EAGAIN; |
| 4305 | goto err; |
Jens Axboe | 3a81fd0 | 2020-12-10 12:25:36 -0700 | [diff] [blame] | 4306 | } |
| 4307 | |
Pavel Begunkov | 12dcb58a | 2021-06-24 15:10:00 +0100 | [diff] [blame] | 4308 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) |
| 4309 | file->f_flags &= ~O_NONBLOCK; |
| 4310 | fsnotify_open(file); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 4311 | |
| 4312 | if (!fixed) |
| 4313 | fd_install(ret, file); |
| 4314 | else |
| 4315 | ret = io_install_fixed_file(req, file, issue_flags, |
| 4316 | req->open.file_slot - 1); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4317 | err: |
| 4318 | putname(req->open.filename); |
Pavel Begunkov | 8fef80b | 2020-02-07 23:59:53 +0300 | [diff] [blame] | 4319 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4320 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4321 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4322 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 4323 | return 0; |
| 4324 | } |
| 4325 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4326 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4327 | { |
Pavel Begunkov | e45cff5 | 2021-02-28 22:35:14 +0000 | [diff] [blame] | 4328 | return io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 4329 | } |
| 4330 | |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4331 | static int io_remove_buffers_prep(struct io_kiocb *req, |
| 4332 | const struct io_uring_sqe *sqe) |
| 4333 | { |
| 4334 | struct io_provide_buf *p = &req->pbuf; |
| 4335 | u64 tmp; |
| 4336 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4337 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
| 4338 | sqe->splice_fd_in) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4339 | return -EINVAL; |
| 4340 | |
| 4341 | tmp = READ_ONCE(sqe->fd); |
| 4342 | if (!tmp || tmp > USHRT_MAX) |
| 4343 | return -EINVAL; |
| 4344 | |
| 4345 | memset(p, 0, sizeof(*p)); |
| 4346 | p->nbufs = tmp; |
| 4347 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4348 | return 0; |
| 4349 | } |
| 4350 | |
| 4351 | static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf, |
| 4352 | int bgid, unsigned nbufs) |
| 4353 | { |
| 4354 | unsigned i = 0; |
| 4355 | |
| 4356 | /* shouldn't happen */ |
| 4357 | if (!nbufs) |
| 4358 | return 0; |
| 4359 | |
| 4360 | /* the head kbuf is the list itself */ |
| 4361 | while (!list_empty(&buf->list)) { |
| 4362 | struct io_buffer *nxt; |
| 4363 | |
| 4364 | nxt = list_first_entry(&buf->list, struct io_buffer, list); |
| 4365 | list_del(&nxt->list); |
| 4366 | kfree(nxt); |
| 4367 | if (++i == nbufs) |
| 4368 | return i; |
Ye Bin | 1d0254e | 2021-11-22 10:47:37 +0800 | [diff] [blame] | 4369 | cond_resched(); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4370 | } |
| 4371 | i++; |
| 4372 | kfree(buf); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4373 | xa_erase(&ctx->io_buffers, bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4374 | |
| 4375 | return i; |
| 4376 | } |
| 4377 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4378 | 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] | 4379 | { |
| 4380 | struct io_provide_buf *p = &req->pbuf; |
| 4381 | struct io_ring_ctx *ctx = req->ctx; |
| 4382 | struct io_buffer *head; |
| 4383 | int ret = 0; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4384 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4385 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4386 | io_ring_submit_lock(ctx, needs_lock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4387 | |
| 4388 | lockdep_assert_held(&ctx->uring_lock); |
| 4389 | |
| 4390 | ret = -ENOENT; |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4391 | head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4392 | if (head) |
| 4393 | ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4394 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4395 | req_set_fail(req); |
Pavel Begunkov | 31bff9a | 2020-12-06 22:22:43 +0000 | [diff] [blame] | 4396 | |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4397 | /* complete before unlock, IOPOLL may need the lock */ |
| 4398 | __io_req_complete(req, issue_flags, ret, 0); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4399 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4400 | return 0; |
| 4401 | } |
| 4402 | |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4403 | static int io_provide_buffers_prep(struct io_kiocb *req, |
| 4404 | const struct io_uring_sqe *sqe) |
| 4405 | { |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4406 | unsigned long size, tmp_check; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4407 | struct io_provide_buf *p = &req->pbuf; |
| 4408 | u64 tmp; |
| 4409 | |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4410 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4411 | return -EINVAL; |
| 4412 | |
| 4413 | tmp = READ_ONCE(sqe->fd); |
| 4414 | if (!tmp || tmp > USHRT_MAX) |
| 4415 | return -E2BIG; |
| 4416 | p->nbufs = tmp; |
| 4417 | p->addr = READ_ONCE(sqe->addr); |
| 4418 | p->len = READ_ONCE(sqe->len); |
| 4419 | |
Pavel Begunkov | 38134ad | 2021-04-15 13:07:39 +0100 | [diff] [blame] | 4420 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
| 4421 | &size)) |
| 4422 | return -EOVERFLOW; |
| 4423 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) |
| 4424 | return -EOVERFLOW; |
| 4425 | |
Pavel Begunkov | d81269f | 2021-03-19 10:21:19 +0000 | [diff] [blame] | 4426 | size = (unsigned long)p->len * p->nbufs; |
| 4427 | if (!access_ok(u64_to_user_ptr(p->addr), size)) |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4428 | return -EFAULT; |
| 4429 | |
| 4430 | p->bgid = READ_ONCE(sqe->buf_group); |
| 4431 | tmp = READ_ONCE(sqe->off); |
| 4432 | if (tmp > USHRT_MAX) |
| 4433 | return -E2BIG; |
| 4434 | p->bid = tmp; |
| 4435 | return 0; |
| 4436 | } |
| 4437 | |
| 4438 | static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head) |
| 4439 | { |
| 4440 | struct io_buffer *buf; |
| 4441 | u64 addr = pbuf->addr; |
| 4442 | int i, bid = pbuf->bid; |
| 4443 | |
| 4444 | for (i = 0; i < pbuf->nbufs; i++) { |
Jens Axboe | 9990da9 | 2021-09-24 07:39:08 -0600 | [diff] [blame] | 4445 | buf = kmalloc(sizeof(*buf), GFP_KERNEL_ACCOUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4446 | if (!buf) |
| 4447 | break; |
| 4448 | |
| 4449 | buf->addr = addr; |
Thadeu Lima de Souza Cascardo | d1f8280 | 2021-05-05 09:47:06 -0300 | [diff] [blame] | 4450 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4451 | buf->bid = bid; |
| 4452 | addr += pbuf->len; |
| 4453 | bid++; |
| 4454 | if (!*head) { |
| 4455 | INIT_LIST_HEAD(&buf->list); |
| 4456 | *head = buf; |
| 4457 | } else { |
| 4458 | list_add_tail(&buf->list, &(*head)->list); |
| 4459 | } |
| 4460 | } |
| 4461 | |
| 4462 | return i ? i : -ENOMEM; |
| 4463 | } |
| 4464 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4465 | 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] | 4466 | { |
| 4467 | struct io_provide_buf *p = &req->pbuf; |
| 4468 | struct io_ring_ctx *ctx = req->ctx; |
| 4469 | struct io_buffer *head, *list; |
| 4470 | int ret = 0; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4471 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4472 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4473 | io_ring_submit_lock(ctx, needs_lock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4474 | |
| 4475 | lockdep_assert_held(&ctx->uring_lock); |
| 4476 | |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4477 | list = head = xa_load(&ctx->io_buffers, p->bgid); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4478 | |
| 4479 | ret = io_add_buffers(p, &head); |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 4480 | if (ret >= 0 && !list) { |
| 4481 | ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL); |
| 4482 | if (ret < 0) |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 4483 | __io_remove_buffers(ctx, head, p->bgid, -1U); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4484 | } |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4485 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4486 | req_set_fail(req); |
Pavel Begunkov | 9fb8cb4 | 2021-02-28 22:35:13 +0000 | [diff] [blame] | 4487 | /* complete before unlock, IOPOLL may need the lock */ |
| 4488 | __io_req_complete(req, issue_flags, ret, 0); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 4489 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 4490 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 4491 | } |
| 4492 | |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4493 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
| 4494 | const struct io_uring_sqe *sqe) |
| 4495 | { |
| 4496 | #if defined(CONFIG_EPOLL) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4497 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4498 | return -EINVAL; |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4499 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4500 | return -EINVAL; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4501 | |
| 4502 | req->epoll.epfd = READ_ONCE(sqe->fd); |
| 4503 | req->epoll.op = READ_ONCE(sqe->len); |
| 4504 | req->epoll.fd = READ_ONCE(sqe->off); |
| 4505 | |
| 4506 | if (ep_op_has_event(req->epoll.op)) { |
| 4507 | struct epoll_event __user *ev; |
| 4508 | |
| 4509 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 4510 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) |
| 4511 | return -EFAULT; |
| 4512 | } |
| 4513 | |
| 4514 | return 0; |
| 4515 | #else |
| 4516 | return -EOPNOTSUPP; |
| 4517 | #endif |
| 4518 | } |
| 4519 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4520 | 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] | 4521 | { |
| 4522 | #if defined(CONFIG_EPOLL) |
| 4523 | struct io_epoll *ie = &req->epoll; |
| 4524 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4525 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4526 | |
| 4527 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); |
| 4528 | if (force_nonblock && ret == -EAGAIN) |
| 4529 | return -EAGAIN; |
| 4530 | |
| 4531 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4532 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4533 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 4534 | return 0; |
| 4535 | #else |
| 4536 | return -EOPNOTSUPP; |
| 4537 | #endif |
| 4538 | } |
| 4539 | |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4540 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4541 | { |
| 4542 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4543 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4544 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4545 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4546 | return -EINVAL; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4547 | |
| 4548 | req->madvise.addr = READ_ONCE(sqe->addr); |
| 4549 | req->madvise.len = READ_ONCE(sqe->len); |
| 4550 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4551 | return 0; |
| 4552 | #else |
| 4553 | return -EOPNOTSUPP; |
| 4554 | #endif |
| 4555 | } |
| 4556 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4557 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4558 | { |
| 4559 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) |
| 4560 | struct io_madvise *ma = &req->madvise; |
| 4561 | int ret; |
| 4562 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4563 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4564 | return -EAGAIN; |
| 4565 | |
Minchan Kim | 0726b01 | 2020-10-17 16:14:50 -0700 | [diff] [blame] | 4566 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4567 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4568 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4569 | io_req_complete(req, ret); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 4570 | return 0; |
| 4571 | #else |
| 4572 | return -EOPNOTSUPP; |
| 4573 | #endif |
| 4574 | } |
| 4575 | |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4576 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4577 | { |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4578 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4579 | return -EINVAL; |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4580 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4581 | return -EINVAL; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4582 | |
| 4583 | req->fadvise.offset = READ_ONCE(sqe->off); |
| 4584 | req->fadvise.len = READ_ONCE(sqe->len); |
| 4585 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); |
| 4586 | return 0; |
| 4587 | } |
| 4588 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4589 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4590 | { |
| 4591 | struct io_fadvise *fa = &req->fadvise; |
| 4592 | int ret; |
| 4593 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4594 | if (issue_flags & IO_URING_F_NONBLOCK) { |
Jens Axboe | 3e69426 | 2020-02-01 09:22:49 -0700 | [diff] [blame] | 4595 | switch (fa->advice) { |
| 4596 | case POSIX_FADV_NORMAL: |
| 4597 | case POSIX_FADV_RANDOM: |
| 4598 | case POSIX_FADV_SEQUENTIAL: |
| 4599 | break; |
| 4600 | default: |
| 4601 | return -EAGAIN; |
| 4602 | } |
| 4603 | } |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4604 | |
| 4605 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); |
| 4606 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4607 | req_set_fail(req); |
Pavel Begunkov | 0bdf339 | 2021-04-11 01:46:29 +0100 | [diff] [blame] | 4608 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 4609 | return 0; |
| 4610 | } |
| 4611 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4612 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4613 | { |
Pavel Begunkov | 2d74d04 | 2021-05-14 12:05:46 +0100 | [diff] [blame] | 4614 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4615 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4616 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4617 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4618 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4619 | return -EBADF; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4620 | |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4621 | req->statx.dfd = READ_ONCE(sqe->fd); |
| 4622 | req->statx.mask = READ_ONCE(sqe->len); |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4623 | req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4624 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
| 4625 | req->statx.flags = READ_ONCE(sqe->statx_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4626 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4627 | return 0; |
| 4628 | } |
| 4629 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4630 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4631 | { |
Bijan Mottahedeh | 1d9e128 | 2020-05-22 21:31:16 -0700 | [diff] [blame] | 4632 | struct io_statx *ctx = &req->statx; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4633 | int ret; |
| 4634 | |
Pavel Begunkov | 59d7001 | 2021-03-22 01:58:30 +0000 | [diff] [blame] | 4635 | if (issue_flags & IO_URING_F_NONBLOCK) |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4636 | return -EAGAIN; |
| 4637 | |
Bijan Mottahedeh | e62753e | 2020-05-22 21:31:18 -0700 | [diff] [blame] | 4638 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
| 4639 | ctx->buffer); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4640 | |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4641 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4642 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4643 | io_req_complete(req, ret); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 4644 | return 0; |
| 4645 | } |
| 4646 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4647 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 4648 | { |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 4649 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Pavel Begunkov | 3232dd0 | 2020-06-03 18:03:22 +0300 | [diff] [blame] | 4650 | return -EINVAL; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4651 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4652 | sqe->rw_flags || sqe->buf_index) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4653 | return -EINVAL; |
Pavel Begunkov | 9c280f9 | 2020-04-08 08:58:46 +0300 | [diff] [blame] | 4654 | if (req->flags & REQ_F_FIXED_FILE) |
Jens Axboe | cf3040c | 2020-02-06 21:31:40 -0700 | [diff] [blame] | 4655 | return -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4656 | |
| 4657 | req->close.fd = READ_ONCE(sqe->fd); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4658 | req->close.file_slot = READ_ONCE(sqe->file_index); |
| 4659 | if (req->close.file_slot && req->close.fd) |
| 4660 | return -EINVAL; |
| 4661 | |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4662 | return 0; |
| 4663 | } |
| 4664 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4665 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4666 | { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4667 | struct files_struct *files = current->files; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4668 | struct io_close *close = &req->close; |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4669 | struct fdtable *fdt; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4670 | struct file *file = NULL; |
| 4671 | int ret = -EBADF; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4672 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 4673 | if (req->close.file_slot) { |
| 4674 | ret = io_close_fixed(req, issue_flags); |
| 4675 | goto err; |
| 4676 | } |
| 4677 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4678 | spin_lock(&files->file_lock); |
| 4679 | fdt = files_fdtable(files); |
| 4680 | if (close->fd >= fdt->max_fds) { |
| 4681 | spin_unlock(&files->file_lock); |
| 4682 | goto err; |
| 4683 | } |
| 4684 | file = fdt->fd[close->fd]; |
Pavel Begunkov | a1fde92 | 2021-04-11 01:46:28 +0100 | [diff] [blame] | 4685 | if (!file || file->f_op == &io_uring_fops) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4686 | spin_unlock(&files->file_lock); |
| 4687 | file = NULL; |
| 4688 | goto err; |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4689 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4690 | |
| 4691 | /* 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] | 4692 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4693 | spin_unlock(&files->file_lock); |
Pavel Begunkov | 0bf0eef | 2020-05-26 20:34:06 +0300 | [diff] [blame] | 4694 | return -EAGAIN; |
Pavel Begunkov | a210067 | 2020-03-02 23:45:16 +0300 | [diff] [blame] | 4695 | } |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4696 | |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4697 | ret = __close_fd_get_file(close->fd, &file); |
| 4698 | spin_unlock(&files->file_lock); |
| 4699 | if (ret < 0) { |
| 4700 | if (ret == -ENOENT) |
| 4701 | ret = -EBADF; |
| 4702 | goto err; |
| 4703 | } |
| 4704 | |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4705 | /* No ->flush() or already async, safely close from here */ |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4706 | ret = filp_close(file, current->files); |
| 4707 | err: |
Pavel Begunkov | 3af73b2 | 2020-06-08 21:08:17 +0300 | [diff] [blame] | 4708 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4709 | req_set_fail(req); |
Jens Axboe | 9eac190 | 2021-01-19 15:50:37 -0700 | [diff] [blame] | 4710 | if (file) |
| 4711 | fput(file); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4712 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 1a417f4 | 2020-01-31 17:16:48 -0700 | [diff] [blame] | 4713 | return 0; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 4714 | } |
| 4715 | |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 4716 | 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] | 4717 | { |
| 4718 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4719 | |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4720 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 4721 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 4722 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
| 4723 | sqe->splice_fd_in)) |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4724 | return -EINVAL; |
| 4725 | |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4726 | req->sync.off = READ_ONCE(sqe->off); |
| 4727 | req->sync.len = READ_ONCE(sqe->len); |
| 4728 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4729 | return 0; |
| 4730 | } |
| 4731 | |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4732 | 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] | 4733 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4734 | int ret; |
| 4735 | |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4736 | /* sync_file_range always requires a blocking context */ |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4737 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 4738 | return -EAGAIN; |
| 4739 | |
Jens Axboe | 9adbd45 | 2019-12-20 08:45:55 -0700 | [diff] [blame] | 4740 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 4741 | req->sync.flags); |
| 4742 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4743 | req_set_fail(req); |
Jens Axboe | e1e1609 | 2020-06-22 09:17:17 -0600 | [diff] [blame] | 4744 | io_req_complete(req, ret); |
Jens Axboe | 5d17b4a | 2019-04-09 14:56:44 -0600 | [diff] [blame] | 4745 | return 0; |
| 4746 | } |
| 4747 | |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 4748 | #if defined(CONFIG_NET) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4749 | static int io_setup_async_msg(struct io_kiocb *req, |
| 4750 | struct io_async_msghdr *kmsg) |
| 4751 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4752 | struct io_async_msghdr *async_msg = req->async_data; |
| 4753 | |
| 4754 | if (async_msg) |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4755 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4756 | if (io_alloc_async_data(req)) { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4757 | kfree(kmsg->free_iov); |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4758 | return -ENOMEM; |
| 4759 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4760 | async_msg = req->async_data; |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4761 | req->flags |= REQ_F_NEED_CLEANUP; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 4762 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
Pavel Begunkov | 2a78080 | 2021-02-05 00:57:58 +0000 | [diff] [blame] | 4763 | async_msg->msg.msg_name = &async_msg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4764 | /* if were using fast_iov, set it to the new one */ |
| 4765 | if (!async_msg->free_iov) |
| 4766 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; |
| 4767 | |
Pavel Begunkov | 02d27d8 | 2020-02-28 10:36:36 +0300 | [diff] [blame] | 4768 | return -EAGAIN; |
| 4769 | } |
| 4770 | |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4771 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
| 4772 | struct io_async_msghdr *iomsg) |
| 4773 | { |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4774 | iomsg->msg.msg_name = &iomsg->addr; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4775 | iomsg->free_iov = iomsg->fast_iov; |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4776 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4777 | req->sr_msg.msg_flags, &iomsg->free_iov); |
Pavel Begunkov | 2ae523e | 2020-07-12 20:41:06 +0300 | [diff] [blame] | 4778 | } |
| 4779 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4780 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
| 4781 | { |
| 4782 | int ret; |
| 4783 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4784 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
| 4785 | if (!ret) |
| 4786 | req->flags |= REQ_F_NEED_CLEANUP; |
| 4787 | return ret; |
| 4788 | } |
| 4789 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4790 | 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] | 4791 | { |
Jens Axboe | e47293f | 2019-12-20 08:58:21 -0700 | [diff] [blame] | 4792 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4793 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 4794 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 4795 | return -EINVAL; |
| 4796 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 4797 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4798 | sr->len = READ_ONCE(sqe->len); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4799 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 4800 | if (sr->msg_flags & MSG_DONTWAIT) |
| 4801 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 4802 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 4803 | #ifdef CONFIG_COMPAT |
| 4804 | if (req->ctx->compat) |
| 4805 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 4806 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4807 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4808 | } |
| 4809 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4810 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4811 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 4812 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4813 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4814 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4815 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4816 | int ret; |
| 4817 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4818 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4819 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4820 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4821 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 4822 | if (req_has_async_data(req)) { |
| 4823 | kmsg = req->async_data; |
| 4824 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4825 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4826 | if (ret) |
| 4827 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4828 | kmsg = &iomsg; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4829 | } |
| 4830 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4831 | flags = req->sr_msg.msg_flags; |
| 4832 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4833 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4834 | if (flags & MSG_WAITALL) |
| 4835 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 4836 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4837 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4838 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4839 | return io_setup_async_msg(req, kmsg); |
| 4840 | if (ret == -ERESTARTSYS) |
| 4841 | ret = -EINTR; |
| 4842 | |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4843 | /* fast path, check for non-NULL to avoid function call */ |
| 4844 | if (kmsg->free_iov) |
| 4845 | kfree(kmsg->free_iov); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4846 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4847 | if (ret < min_ret) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4848 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4849 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4850 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 4851 | } |
| 4852 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4853 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4854 | { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4855 | struct io_sr_msg *sr = &req->sr_msg; |
| 4856 | struct msghdr msg; |
| 4857 | struct iovec iov; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4858 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4859 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4860 | int min_ret = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4861 | int ret; |
| 4862 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4863 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4864 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 4865 | return -ENOTSOCK; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4866 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4867 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
| 4868 | if (unlikely(ret)) |
Zheng Bin | 14db841 | 2020-09-09 20:12:37 +0800 | [diff] [blame] | 4869 | return ret; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4870 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4871 | msg.msg_name = NULL; |
| 4872 | msg.msg_control = NULL; |
| 4873 | msg.msg_controllen = 0; |
| 4874 | msg.msg_namelen = 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4875 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 4876 | flags = req->sr_msg.msg_flags; |
| 4877 | if (issue_flags & IO_URING_F_NONBLOCK) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4878 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4879 | if (flags & MSG_WAITALL) |
| 4880 | min_ret = iov_iter_count(&msg.msg_iter); |
| 4881 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4882 | msg.msg_flags = flags; |
| 4883 | ret = sock_sendmsg(sock, &msg); |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 4884 | if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 4885 | return -EAGAIN; |
| 4886 | if (ret == -ERESTARTSYS) |
| 4887 | ret = -EINTR; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4888 | |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 4889 | if (ret < min_ret) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 4890 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 4891 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4892 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4893 | } |
| 4894 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4895 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4896 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4897 | { |
| 4898 | struct io_sr_msg *sr = &req->sr_msg; |
| 4899 | struct iovec __user *uiov; |
| 4900 | size_t iov_len; |
| 4901 | int ret; |
| 4902 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4903 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
| 4904 | &iomsg->uaddr, &uiov, &iov_len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4905 | if (ret) |
| 4906 | return ret; |
| 4907 | |
| 4908 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4909 | if (iov_len > 1) |
| 4910 | return -EINVAL; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4911 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4912 | return -EFAULT; |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 4913 | sr->len = iomsg->fast_iov[0].iov_len; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4914 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4915 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4916 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4917 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4918 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4919 | false); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4920 | if (ret > 0) |
| 4921 | ret = 0; |
| 4922 | } |
| 4923 | |
| 4924 | return ret; |
| 4925 | } |
| 4926 | |
| 4927 | #ifdef CONFIG_COMPAT |
| 4928 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4929 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4930 | { |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4931 | struct io_sr_msg *sr = &req->sr_msg; |
| 4932 | struct compat_iovec __user *uiov; |
| 4933 | compat_uptr_t ptr; |
| 4934 | compat_size_t len; |
| 4935 | int ret; |
| 4936 | |
Pavel Begunkov | 4af3417 | 2021-04-11 01:46:30 +0100 | [diff] [blame] | 4937 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
| 4938 | &ptr, &len); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4939 | if (ret) |
| 4940 | return ret; |
| 4941 | |
| 4942 | uiov = compat_ptr(ptr); |
| 4943 | if (req->flags & REQ_F_BUFFER_SELECT) { |
| 4944 | compat_ssize_t clen; |
| 4945 | |
| 4946 | if (len > 1) |
| 4947 | return -EINVAL; |
| 4948 | if (!access_ok(uiov, sizeof(*uiov))) |
| 4949 | return -EFAULT; |
| 4950 | if (__get_user(clen, &uiov->iov_len)) |
| 4951 | return -EFAULT; |
| 4952 | if (clen < 0) |
| 4953 | return -EINVAL; |
Pavel Begunkov | 2d280bc | 2020-11-29 18:33:32 +0000 | [diff] [blame] | 4954 | sr->len = clen; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4955 | iomsg->free_iov = NULL; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4956 | } else { |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4957 | iomsg->free_iov = iomsg->fast_iov; |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4958 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 4959 | UIO_FASTIOV, &iomsg->free_iov, |
Christoph Hellwig | 89cd35c | 2020-09-25 06:51:41 +0200 | [diff] [blame] | 4960 | &iomsg->msg.msg_iter, true); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4961 | if (ret < 0) |
| 4962 | return ret; |
| 4963 | } |
| 4964 | |
| 4965 | return 0; |
| 4966 | } |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4967 | #endif |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4968 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4969 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
| 4970 | struct io_async_msghdr *iomsg) |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4971 | { |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4972 | iomsg->msg.msg_name = &iomsg->addr; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4973 | |
| 4974 | #ifdef CONFIG_COMPAT |
| 4975 | if (req->ctx->compat) |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4976 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4977 | #endif |
| 4978 | |
Pavel Begunkov | 1400e69 | 2020-07-12 20:41:05 +0300 | [diff] [blame] | 4979 | return __io_recvmsg_copy_hdr(req, iomsg); |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 4980 | } |
| 4981 | |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4982 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 4983 | unsigned int issue_flags) |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4984 | { |
| 4985 | struct io_sr_msg *sr = &req->sr_msg; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 4986 | |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 4987 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4988 | } |
| 4989 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4990 | static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req) |
| 4991 | { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 4992 | return io_put_kbuf(req, req->kbuf); |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 4993 | } |
| 4994 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4995 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 4996 | { |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 4997 | int ret; |
Jens Axboe | 06b76d4 | 2019-12-19 14:44:26 -0700 | [diff] [blame] | 4998 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 4999 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
| 5000 | if (!ret) |
| 5001 | req->flags |= REQ_F_NEED_CLEANUP; |
| 5002 | return ret; |
| 5003 | } |
| 5004 | |
| 5005 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 5006 | { |
| 5007 | struct io_sr_msg *sr = &req->sr_msg; |
| 5008 | |
Pavel Begunkov | d2b6f48 | 2020-06-03 18:03:25 +0300 | [diff] [blame] | 5009 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5010 | return -EINVAL; |
| 5011 | |
Pavel Begunkov | 270a594 | 2020-07-12 20:41:04 +0300 | [diff] [blame] | 5012 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
Jens Axboe | 0b7b21e | 2020-01-31 08:34:59 -0700 | [diff] [blame] | 5013 | sr->len = READ_ONCE(sqe->len); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5014 | sr->bgid = READ_ONCE(sqe->buf_group); |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5015 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
| 5016 | if (sr->msg_flags & MSG_DONTWAIT) |
| 5017 | req->flags |= REQ_F_NOWAIT; |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5018 | |
Jens Axboe | d876836 | 2020-02-27 14:17:49 -0700 | [diff] [blame] | 5019 | #ifdef CONFIG_COMPAT |
| 5020 | if (req->ctx->compat) |
| 5021 | sr->msg_flags |= MSG_CMSG_COMPAT; |
| 5022 | #endif |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5023 | return 0; |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5024 | } |
| 5025 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5026 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 5027 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5028 | struct io_async_msghdr iomsg, *kmsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5029 | struct socket *sock; |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5030 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5031 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5032 | int min_ret = 0; |
Jens Axboe | 52de1fe | 2020-02-27 10:15:42 -0700 | [diff] [blame] | 5033 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5034 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5035 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5036 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5037 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5038 | return -ENOTSOCK; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5039 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5040 | if (req_has_async_data(req)) { |
| 5041 | kmsg = req->async_data; |
| 5042 | } else { |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5043 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
| 5044 | if (ret) |
Pavel Begunkov | 681fda8 | 2020-07-15 22:20:45 +0300 | [diff] [blame] | 5045 | return ret; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5046 | kmsg = &iomsg; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5047 | } |
| 5048 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5049 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5050 | kbuf = io_recv_buffer_select(req, issue_flags); |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5051 | if (IS_ERR(kbuf)) |
| 5052 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5053 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
Pavel Begunkov | 5476dfe | 2021-02-05 00:57:59 +0000 | [diff] [blame] | 5054 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
| 5055 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5056 | 1, req->sr_msg.len); |
| 5057 | } |
| 5058 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5059 | flags = req->sr_msg.msg_flags; |
| 5060 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5061 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5062 | if (flags & MSG_WAITALL) |
| 5063 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); |
| 5064 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5065 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
| 5066 | kmsg->uaddr, flags); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5067 | if (force_nonblock && ret == -EAGAIN) |
| 5068 | return io_setup_async_msg(req, kmsg); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5069 | if (ret == -ERESTARTSYS) |
| 5070 | ret = -EINTR; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 5071 | |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5072 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5073 | cflags = io_put_recv_kbuf(req); |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 5074 | /* fast path, check for non-NULL to avoid function call */ |
| 5075 | if (kmsg->free_iov) |
| 5076 | kfree(kmsg->free_iov); |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 5077 | req->flags &= ~REQ_F_NEED_CLEANUP; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5078 | if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5079 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5080 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5081 | return 0; |
Jens Axboe | 0fa03c6 | 2019-04-19 13:34:07 -0600 | [diff] [blame] | 5082 | } |
| 5083 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5084 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5085 | { |
Pavel Begunkov | 6b754c8 | 2020-07-16 23:28:00 +0300 | [diff] [blame] | 5086 | struct io_buffer *kbuf; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5087 | struct io_sr_msg *sr = &req->sr_msg; |
| 5088 | struct msghdr msg; |
| 5089 | void __user *buf = sr->buf; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5090 | struct socket *sock; |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5091 | struct iovec iov; |
| 5092 | unsigned flags; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5093 | int min_ret = 0; |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5094 | int ret, cflags = 0; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5095 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5096 | |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5097 | sock = sock_from_file(req->file); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5098 | if (unlikely(!sock)) |
Florent Revest | dba4a92 | 2020-12-04 12:36:04 +0100 | [diff] [blame] | 5099 | return -ENOTSOCK; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5100 | |
Pavel Begunkov | bc02ef3 | 2020-07-16 23:28:03 +0300 | [diff] [blame] | 5101 | if (req->flags & REQ_F_BUFFER_SELECT) { |
Pavel Begunkov | 51aac42 | 2021-10-14 16:10:17 +0100 | [diff] [blame] | 5102 | kbuf = io_recv_buffer_select(req, issue_flags); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 5103 | if (IS_ERR(kbuf)) |
| 5104 | return PTR_ERR(kbuf); |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5105 | buf = u64_to_user_ptr(kbuf->addr); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5106 | } |
| 5107 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5108 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5109 | if (unlikely(ret)) |
| 5110 | goto out_free; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5111 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5112 | msg.msg_name = NULL; |
| 5113 | msg.msg_control = NULL; |
| 5114 | msg.msg_controllen = 0; |
| 5115 | msg.msg_namelen = 0; |
| 5116 | msg.msg_iocb = NULL; |
| 5117 | msg.msg_flags = 0; |
| 5118 | |
Pavel Begunkov | 0441180 | 2021-04-01 15:44:00 +0100 | [diff] [blame] | 5119 | flags = req->sr_msg.msg_flags; |
| 5120 | if (force_nonblock) |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5121 | flags |= MSG_DONTWAIT; |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5122 | if (flags & MSG_WAITALL) |
| 5123 | min_ret = iov_iter_count(&msg.msg_iter); |
| 5124 | |
Pavel Begunkov | 7a7cacb | 2020-07-16 23:27:59 +0300 | [diff] [blame] | 5125 | ret = sock_recvmsg(sock, &msg, flags); |
| 5126 | if (force_nonblock && ret == -EAGAIN) |
| 5127 | return -EAGAIN; |
| 5128 | if (ret == -ERESTARTSYS) |
| 5129 | ret = -EINTR; |
Pavel Begunkov | 14c32ee | 2020-07-16 23:28:01 +0300 | [diff] [blame] | 5130 | out_free: |
Pavel Begunkov | 7fbb1b5 | 2020-07-16 23:28:05 +0300 | [diff] [blame] | 5131 | if (req->flags & REQ_F_BUFFER_SELECTED) |
| 5132 | cflags = io_put_recv_kbuf(req); |
Stefan Metzmacher | 0031275 | 2021-03-20 20:33:36 +0100 | [diff] [blame] | 5133 | if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)))) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5134 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5135 | __io_req_complete(req, issue_flags, ret, cflags); |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5136 | return 0; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 5137 | } |
| 5138 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5139 | 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] | 5140 | { |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5141 | struct io_accept *accept = &req->accept; |
| 5142 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5143 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5144 | return -EINVAL; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5145 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5146 | return -EINVAL; |
| 5147 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 5148 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5149 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5150 | accept->flags = READ_ONCE(sqe->accept_flags); |
Jens Axboe | 09952e3 | 2020-03-19 20:16:56 -0600 | [diff] [blame] | 5151 | accept->nofile = rlimit(RLIMIT_NOFILE); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5152 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5153 | accept->file_slot = READ_ONCE(sqe->file_index); |
| 5154 | if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) || |
| 5155 | (accept->flags & SOCK_CLOEXEC))) |
| 5156 | return -EINVAL; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5157 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
| 5158 | return -EINVAL; |
| 5159 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) |
| 5160 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5161 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5162 | } |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5163 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5164 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5165 | { |
| 5166 | struct io_accept *accept = &req->accept; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5167 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5168 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5169 | bool fixed = !!accept->file_slot; |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5170 | struct file *file; |
| 5171 | int ret, fd; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5172 | |
Jiufei Xue | e697dee | 2020-06-10 13:41:59 +0800 | [diff] [blame] | 5173 | if (req->file->f_flags & O_NONBLOCK) |
| 5174 | req->flags |= REQ_F_NOWAIT; |
| 5175 | |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5176 | if (!fixed) { |
| 5177 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); |
| 5178 | if (unlikely(fd < 0)) |
| 5179 | return fd; |
| 5180 | } |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5181 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
| 5182 | accept->flags); |
| 5183 | if (IS_ERR(file)) { |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5184 | if (!fixed) |
| 5185 | put_unused_fd(fd); |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5186 | ret = PTR_ERR(file); |
| 5187 | if (ret == -EAGAIN && force_nonblock) |
| 5188 | return -EAGAIN; |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5189 | if (ret == -ERESTARTSYS) |
| 5190 | ret = -EINTR; |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5191 | req_set_fail(req); |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5192 | } else if (!fixed) { |
Pavel Begunkov | a7083ad | 2021-08-25 12:25:46 +0100 | [diff] [blame] | 5193 | fd_install(fd, file); |
| 5194 | ret = fd; |
Pavel Begunkov | aaa4db1 | 2021-08-25 12:25:47 +0100 | [diff] [blame] | 5195 | } else { |
| 5196 | ret = io_install_fixed_file(req, file, issue_flags, |
| 5197 | accept->file_slot - 1); |
Pavel Begunkov | ac45abc | 2020-06-08 21:08:18 +0300 | [diff] [blame] | 5198 | } |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5199 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5200 | return 0; |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 5201 | } |
| 5202 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5203 | static int io_connect_prep_async(struct io_kiocb *req) |
| 5204 | { |
| 5205 | struct io_async_connect *io = req->async_data; |
| 5206 | struct io_connect *conn = &req->connect; |
| 5207 | |
| 5208 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); |
| 5209 | } |
| 5210 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5211 | 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] | 5212 | { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5213 | struct io_connect *conn = &req->connect; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5214 | |
Jens Axboe | 14587a46 | 2020-09-05 11:36:08 -0600 | [diff] [blame] | 5215 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5216 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5217 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
| 5218 | sqe->splice_fd_in) |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5219 | return -EINVAL; |
| 5220 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5221 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 5222 | conn->addr_len = READ_ONCE(sqe->addr2); |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 5223 | return 0; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5224 | } |
| 5225 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5226 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5227 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5228 | struct io_async_connect __io, *io; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5229 | unsigned file_flags; |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5230 | int ret; |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 5231 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5232 | |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5233 | if (req_has_async_data(req)) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5234 | io = req->async_data; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5235 | } else { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5236 | ret = move_addr_to_kernel(req->connect.addr, |
| 5237 | req->connect.addr_len, |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5238 | &__io.address); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5239 | if (ret) |
| 5240 | goto out; |
| 5241 | io = &__io; |
| 5242 | } |
| 5243 | |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5244 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
| 5245 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5246 | ret = __sys_connect_file(req->file, &io->address, |
Jens Axboe | 3fbb51c | 2019-12-20 08:51:52 -0700 | [diff] [blame] | 5247 | req->connect.addr_len, file_flags); |
Jens Axboe | 87f80d6 | 2019-12-03 11:23:54 -0700 | [diff] [blame] | 5248 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5249 | if (req_has_async_data(req)) |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 5250 | return -EAGAIN; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5251 | if (io_alloc_async_data(req)) { |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5252 | ret = -ENOMEM; |
| 5253 | goto out; |
| 5254 | } |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5255 | memcpy(req->async_data, &__io, sizeof(__io)); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5256 | return -EAGAIN; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5257 | } |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5258 | if (ret == -ERESTARTSYS) |
| 5259 | ret = -EINTR; |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 5260 | out: |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 5261 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5262 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 5263 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5264 | return 0; |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5265 | } |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5266 | #else /* !CONFIG_NET */ |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5267 | #define IO_NETOP_FN(op) \ |
| 5268 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ |
| 5269 | { \ |
| 5270 | return -EOPNOTSUPP; \ |
Jens Axboe | f8e85cf | 2019-11-23 14:24:24 -0700 | [diff] [blame] | 5271 | } |
| 5272 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5273 | #define IO_NETOP_PREP(op) \ |
| 5274 | IO_NETOP_FN(op) \ |
| 5275 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ |
| 5276 | { \ |
| 5277 | return -EOPNOTSUPP; \ |
| 5278 | } \ |
| 5279 | |
| 5280 | #define IO_NETOP_PREP_ASYNC(op) \ |
| 5281 | IO_NETOP_PREP(op) \ |
| 5282 | static int io_##op##_prep_async(struct io_kiocb *req) \ |
| 5283 | { \ |
| 5284 | return -EOPNOTSUPP; \ |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5285 | } |
| 5286 | |
Jens Axboe | 99a1008 | 2021-02-19 09:35:19 -0700 | [diff] [blame] | 5287 | IO_NETOP_PREP_ASYNC(sendmsg); |
| 5288 | IO_NETOP_PREP_ASYNC(recvmsg); |
| 5289 | IO_NETOP_PREP_ASYNC(connect); |
| 5290 | IO_NETOP_PREP(accept); |
| 5291 | IO_NETOP_FN(send); |
| 5292 | IO_NETOP_FN(recv); |
YueHaibing | 469956e | 2020-03-04 15:53:52 +0800 | [diff] [blame] | 5293 | #endif /* CONFIG_NET */ |
Jens Axboe | 17f2fe3 | 2019-10-17 14:42:58 -0600 | [diff] [blame] | 5294 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5295 | struct io_poll_table { |
| 5296 | struct poll_table_struct pt; |
| 5297 | struct io_kiocb *req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5298 | int nr_entries; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5299 | int error; |
| 5300 | }; |
| 5301 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5302 | static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll, |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 5303 | __poll_t mask, io_req_tw_func_t func) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5304 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5305 | /* for instances that support it check for an event match first: */ |
| 5306 | if (mask && !(mask & poll->events)) |
| 5307 | return 0; |
| 5308 | |
| 5309 | trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask); |
| 5310 | |
| 5311 | list_del_init(&poll->wait.entry); |
| 5312 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5313 | req->result = mask; |
Pavel Begunkov | 5b0a6ac | 2021-06-30 21:54:04 +0100 | [diff] [blame] | 5314 | req->io_task_work.func = func; |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5315 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5316 | /* |
Jens Axboe | e3aabf9 | 2020-05-18 11:04:17 -0600 | [diff] [blame] | 5317 | * If this fails, then the task is exiting. When a task exits, the |
| 5318 | * work gets canceled, so just cancel this request as well instead |
| 5319 | * of executing it. We can't safely execute it anyway, as we may not |
| 5320 | * have the needed state needed for it anyway. |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5321 | */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5322 | io_req_task_work_add(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5323 | return 1; |
| 5324 | } |
| 5325 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5326 | static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll) |
| 5327 | __acquires(&req->ctx->completion_lock) |
| 5328 | { |
| 5329 | struct io_ring_ctx *ctx = req->ctx; |
| 5330 | |
Jens Axboe | 316319e | 2021-08-19 09:41:42 -0600 | [diff] [blame] | 5331 | /* req->task == current here, checking PF_EXITING is safe */ |
Pavel Begunkov | e09ee51 | 2021-07-01 13:26:05 +0100 | [diff] [blame] | 5332 | if (unlikely(req->task->flags & PF_EXITING)) |
| 5333 | WRITE_ONCE(poll->canceled, true); |
| 5334 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5335 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5336 | struct poll_table_struct pt = { ._key = poll->events }; |
| 5337 | |
| 5338 | req->result = vfs_poll(req->file, &pt) & poll->events; |
| 5339 | } |
| 5340 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5341 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5342 | if (!req->result && !READ_ONCE(poll->canceled)) { |
| 5343 | add_wait_queue(poll->head, &poll->wait); |
| 5344 | return true; |
| 5345 | } |
| 5346 | |
| 5347 | return false; |
| 5348 | } |
| 5349 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5350 | 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] | 5351 | { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5352 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5353 | if (req->opcode == IORING_OP_POLL_ADD) |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5354 | return req->async_data; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5355 | return req->apoll->double_poll; |
| 5356 | } |
| 5357 | |
| 5358 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) |
| 5359 | { |
| 5360 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5361 | return &req->poll; |
| 5362 | return &req->apoll->poll; |
| 5363 | } |
| 5364 | |
| 5365 | static void io_poll_remove_double(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5366 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5367 | { |
| 5368 | struct io_poll_iocb *poll = io_poll_get_double(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5369 | |
| 5370 | lockdep_assert_held(&req->ctx->completion_lock); |
| 5371 | |
| 5372 | if (poll && poll->head) { |
| 5373 | struct wait_queue_head *head = poll->head; |
| 5374 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5375 | spin_lock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5376 | list_del_init(&poll->wait.entry); |
| 5377 | if (poll->wait.private) |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5378 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5379 | poll->head = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5380 | spin_unlock_irq(&head->lock); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5381 | } |
| 5382 | } |
| 5383 | |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5384 | static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5385 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5386 | { |
| 5387 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5388 | unsigned flags = IORING_CQE_F_MORE; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5389 | int error; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5390 | |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5391 | if (READ_ONCE(req->poll.canceled)) { |
Jens Axboe | 45ab03b | 2021-02-23 08:19:33 -0700 | [diff] [blame] | 5392 | error = -ECANCELED; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5393 | req->poll.events |= EPOLLONESHOT; |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5394 | } else { |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5395 | error = mangle_poll(mask); |
Pavel Begunkov | e27414b | 2021-04-09 09:13:20 +0100 | [diff] [blame] | 5396 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5397 | if (req->poll.events & EPOLLONESHOT) |
| 5398 | flags = 0; |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5399 | if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) { |
| 5400 | req->poll.events |= EPOLLONESHOT; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5401 | flags = 0; |
Hao Xu | a62682f | 2021-09-22 18:12:37 +0800 | [diff] [blame] | 5402 | } |
Hao Xu | 7b289c3 | 2021-04-13 15:20:39 +0800 | [diff] [blame] | 5403 | if (flags & IORING_CQE_F_MORE) |
| 5404 | ctx->cq_extra++; |
| 5405 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5406 | return !(flags & IORING_CQE_F_MORE); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5407 | } |
| 5408 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5409 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5410 | { |
Jens Axboe | 6d816e0 | 2020-08-11 08:04:14 -0600 | [diff] [blame] | 5411 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5412 | struct io_kiocb *nxt; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5413 | |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5414 | if (io_poll_rewait(req, &req->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5415 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5416 | } else { |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5417 | bool done; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5418 | |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5419 | if (req->poll.done) { |
| 5420 | spin_unlock(&ctx->completion_lock); |
| 5421 | return; |
| 5422 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5423 | done = __io_poll_complete(req, req->result); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5424 | if (done) { |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5425 | io_poll_remove_double(req); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5426 | hash_del(&req->hash_node); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5427 | req->poll.done = true; |
Pavel Begunkov | f40b964 | 2021-04-09 09:13:19 +0100 | [diff] [blame] | 5428 | } else { |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5429 | req->result = 0; |
| 5430 | add_wait_queue(req->poll.head, &req->poll.wait); |
| 5431 | } |
Xiaoguang Wang | 31efe48 | 2021-09-03 22:24:36 +0800 | [diff] [blame] | 5432 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5433 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5434 | io_cqring_ev_posted(ctx); |
Pavel Begunkov | dd221f46 | 2020-10-18 10:17:42 +0100 | [diff] [blame] | 5435 | |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5436 | if (done) { |
| 5437 | nxt = io_put_req_find_next(req); |
| 5438 | if (nxt) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5439 | io_req_task_submit(nxt, locked); |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5440 | } |
Pavel Begunkov | ea1164e | 2020-06-30 15:20:41 +0300 | [diff] [blame] | 5441 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode, |
| 5445 | int sync, void *key) |
| 5446 | { |
| 5447 | struct io_kiocb *req = wait->private; |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5448 | struct io_poll_iocb *poll = io_poll_get_single(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5449 | __poll_t mask = key_to_poll(key); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5450 | unsigned long flags; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5451 | |
| 5452 | /* for instances that support it check for an event match first: */ |
| 5453 | if (mask && !(mask & poll->events)) |
| 5454 | return 0; |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5455 | if (!(poll->events & EPOLLONESHOT)) |
| 5456 | return poll->wait.func(&poll->wait, mode, sync, key); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5457 | |
Jens Axboe | 8706e04 | 2020-09-28 08:38:54 -0600 | [diff] [blame] | 5458 | list_del_init(&wait->entry); |
| 5459 | |
Jens Axboe | 9ce85ef | 2021-07-09 08:20:28 -0600 | [diff] [blame] | 5460 | if (poll->head) { |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5461 | bool done; |
| 5462 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5463 | spin_lock_irqsave(&poll->head->lock, flags); |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5464 | done = list_empty(&poll->wait.entry); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5465 | if (!done) |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5466 | list_del_init(&poll->wait.entry); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5467 | /* make sure double remove sees this as being gone */ |
| 5468 | wait->private = NULL; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5469 | spin_unlock_irqrestore(&poll->head->lock, flags); |
Jens Axboe | c8b5e26 | 2020-10-25 13:53:26 -0600 | [diff] [blame] | 5470 | if (!done) { |
| 5471 | /* use wait func handler, so it matches the rq type */ |
| 5472 | poll->wait.func(&poll->wait, mode, sync, key); |
| 5473 | } |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5474 | } |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5475 | req_ref_put(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5476 | return 1; |
| 5477 | } |
| 5478 | |
| 5479 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
| 5480 | wait_queue_func_t wake_func) |
| 5481 | { |
| 5482 | poll->head = NULL; |
| 5483 | poll->done = false; |
| 5484 | poll->canceled = false; |
Jens Axboe | 464dca6 | 2021-03-19 14:06:24 -0600 | [diff] [blame] | 5485 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
| 5486 | /* mask in events that we always want/need */ |
| 5487 | poll->events = events | IO_POLL_UNMASK; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5488 | INIT_LIST_HEAD(&poll->wait.entry); |
| 5489 | init_waitqueue_func_entry(&poll->wait, wake_func); |
| 5490 | } |
| 5491 | |
| 5492 | 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] | 5493 | struct wait_queue_head *head, |
| 5494 | struct io_poll_iocb **poll_ptr) |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5495 | { |
| 5496 | struct io_kiocb *req = pt->req; |
| 5497 | |
| 5498 | /* |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5499 | * The file being polled uses multiple waitqueues for poll handling |
| 5500 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb |
| 5501 | * if this happens. |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5502 | */ |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5503 | if (unlikely(pt->nr_entries)) { |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5504 | struct io_poll_iocb *poll_one = poll; |
| 5505 | |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5506 | /* double add on the same waitqueue head, ignore */ |
| 5507 | if (poll_one->head == head) |
| 5508 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5509 | /* already have a 2nd entry, fail a third attempt */ |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5510 | if (*poll_ptr) { |
Pavel Begunkov | 23a65db | 2021-08-17 20:28:11 +0100 | [diff] [blame] | 5511 | if ((*poll_ptr)->head == head) |
| 5512 | return; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5513 | pt->error = -EINVAL; |
| 5514 | return; |
| 5515 | } |
Jens Axboe | ea6a693d | 2021-04-15 09:47:13 -0600 | [diff] [blame] | 5516 | /* |
| 5517 | * Can't handle multishot for double wait for now, turn it |
| 5518 | * into one-shot mode. |
| 5519 | */ |
Pavel Begunkov | 7a27472 | 2021-05-17 12:43:34 +0100 | [diff] [blame] | 5520 | if (!(poll_one->events & EPOLLONESHOT)) |
| 5521 | poll_one->events |= EPOLLONESHOT; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5522 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
| 5523 | if (!poll) { |
| 5524 | pt->error = -ENOMEM; |
| 5525 | return; |
| 5526 | } |
Pavel Begunkov | 58852d4 | 2020-10-16 20:55:56 +0100 | [diff] [blame] | 5527 | io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake); |
Jens Axboe | de9b4cc | 2021-02-24 13:28:27 -0700 | [diff] [blame] | 5528 | req_ref_get(req); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5529 | poll->wait.private = req; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5530 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5531 | *poll_ptr = poll; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 5532 | if (req->opcode == IORING_OP_POLL_ADD) |
| 5533 | req->flags |= REQ_F_ASYNC_DATA; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5534 | } |
| 5535 | |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5536 | pt->nr_entries++; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5537 | poll->head = head; |
Jiufei Xue | a31eb4a | 2020-06-17 17:53:56 +0800 | [diff] [blame] | 5538 | |
| 5539 | if (poll->events & EPOLLEXCLUSIVE) |
| 5540 | add_wait_queue_exclusive(head, &poll->wait); |
| 5541 | else |
| 5542 | add_wait_queue(head, &poll->wait); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5546 | struct poll_table_struct *p) |
| 5547 | { |
| 5548 | 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] | 5549 | struct async_poll *apoll = pt->req->apoll; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5550 | |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5551 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5552 | } |
| 5553 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5554 | static void io_async_task_func(struct io_kiocb *req, bool *locked) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5555 | { |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5556 | struct async_poll *apoll = req->apoll; |
| 5557 | struct io_ring_ctx *ctx = req->ctx; |
| 5558 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5559 | trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5560 | |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5561 | if (io_poll_rewait(req, &apoll->poll)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5562 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5563 | return; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5564 | } |
| 5565 | |
Pavel Begunkov | 0ea13b4 | 2021-04-09 09:13:21 +0100 | [diff] [blame] | 5566 | hash_del(&req->hash_node); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5567 | io_poll_remove_double(req); |
Hao Xu | bd99c71 | 2021-09-22 18:12:36 +0800 | [diff] [blame] | 5568 | apoll->poll.done = true; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5569 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 74ce6ce | 2020-04-13 11:09:12 -0600 | [diff] [blame] | 5570 | |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5571 | if (!READ_ONCE(apoll->poll.canceled)) |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5572 | io_req_task_submit(req, locked); |
Pavel Begunkov | 0be0b0e | 2020-06-30 15:20:42 +0300 | [diff] [blame] | 5573 | else |
Pavel Begunkov | 2593553 | 2021-03-19 17:22:40 +0000 | [diff] [blame] | 5574 | io_req_complete_failed(req, -ECANCELED); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5578 | void *key) |
| 5579 | { |
| 5580 | struct io_kiocb *req = wait->private; |
| 5581 | struct io_poll_iocb *poll = &req->apoll->poll; |
| 5582 | |
| 5583 | trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data, |
| 5584 | key_to_poll(key)); |
| 5585 | |
| 5586 | return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func); |
| 5587 | } |
| 5588 | |
| 5589 | static void io_poll_req_insert(struct io_kiocb *req) |
| 5590 | { |
| 5591 | struct io_ring_ctx *ctx = req->ctx; |
| 5592 | struct hlist_head *list; |
| 5593 | |
| 5594 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
| 5595 | hlist_add_head(&req->hash_node, list); |
| 5596 | } |
| 5597 | |
| 5598 | static __poll_t __io_arm_poll_handler(struct io_kiocb *req, |
| 5599 | struct io_poll_iocb *poll, |
| 5600 | struct io_poll_table *ipt, __poll_t mask, |
| 5601 | wait_queue_func_t wake_func) |
| 5602 | __acquires(&ctx->completion_lock) |
| 5603 | { |
| 5604 | struct io_ring_ctx *ctx = req->ctx; |
| 5605 | bool cancel = false; |
| 5606 | |
Pavel Begunkov | 4d52f33 | 2020-10-18 10:17:43 +0100 | [diff] [blame] | 5607 | INIT_HLIST_NODE(&req->hash_node); |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5608 | io_init_poll_iocb(poll, mask, wake_func); |
Pavel Begunkov | b90cd19 | 2020-06-21 13:09:52 +0300 | [diff] [blame] | 5609 | poll->file = req->file; |
Jens Axboe | 18bceab | 2020-05-15 11:56:54 -0600 | [diff] [blame] | 5610 | poll->wait.private = req; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5611 | |
| 5612 | ipt->pt._key = mask; |
| 5613 | ipt->req = req; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5614 | ipt->error = 0; |
| 5615 | ipt->nr_entries = 0; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5616 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5617 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
Pavel Begunkov | 68b11e8 | 2021-07-20 10:50:43 +0100 | [diff] [blame] | 5618 | if (unlikely(!ipt->nr_entries) && !ipt->error) |
| 5619 | ipt->error = -EINVAL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5620 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5621 | spin_lock(&ctx->completion_lock); |
Hao Xu | a890d01 | 2021-07-28 11:03:22 +0800 | [diff] [blame] | 5622 | if (ipt->error || (mask && (poll->events & EPOLLONESHOT))) |
Pavel Begunkov | 46fee9a | 2021-07-20 10:50:44 +0100 | [diff] [blame] | 5623 | io_poll_remove_double(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5624 | if (likely(poll->head)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5625 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5626 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 5627 | if (ipt->error) |
| 5628 | cancel = true; |
| 5629 | ipt->error = 0; |
| 5630 | mask = 0; |
| 5631 | } |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5632 | if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5633 | list_del_init(&poll->wait.entry); |
| 5634 | else if (cancel) |
| 5635 | WRITE_ONCE(poll->canceled, true); |
| 5636 | else if (!poll->done) /* actually waiting for an event */ |
| 5637 | io_poll_req_insert(req); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5638 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | return mask; |
| 5642 | } |
| 5643 | |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5644 | enum { |
| 5645 | IO_APOLL_OK, |
| 5646 | IO_APOLL_ABORTED, |
| 5647 | IO_APOLL_READY |
| 5648 | }; |
| 5649 | |
| 5650 | static int io_arm_poll_handler(struct io_kiocb *req) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5651 | { |
| 5652 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 5653 | struct io_ring_ctx *ctx = req->ctx; |
| 5654 | struct async_poll *apoll; |
| 5655 | struct io_poll_table ipt; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5656 | __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5657 | |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5658 | if (!def->pollin && !def->pollout) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5659 | return IO_APOLL_ABORTED; |
Pavel Begunkov | 658d0a4 | 2021-10-23 12:13:58 +0100 | [diff] [blame] | 5660 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
| 5661 | return IO_APOLL_ABORTED; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5662 | |
| 5663 | if (def->pollin) { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5664 | mask |= POLLIN | POLLRDNORM; |
| 5665 | |
| 5666 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ |
| 5667 | if ((req->opcode == IORING_OP_RECVMSG) && |
| 5668 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) |
| 5669 | mask &= ~POLLIN; |
| 5670 | } else { |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5671 | mask |= POLLOUT | POLLWRNORM; |
| 5672 | } |
| 5673 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5674 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); |
| 5675 | if (unlikely(!apoll)) |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5676 | return IO_APOLL_ABORTED; |
Jens Axboe | 807abcb | 2020-07-17 17:09:27 -0600 | [diff] [blame] | 5677 | apoll->double_poll = NULL; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5678 | req->apoll = apoll; |
Pavel Begunkov | b2d9c3d | 2021-06-26 21:40:44 +0100 | [diff] [blame] | 5679 | req->flags |= REQ_F_POLLED; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5680 | ipt.pt._qproc = io_async_queue_proc; |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5681 | io_req_set_refcount(req); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5682 | |
| 5683 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask, |
| 5684 | io_async_wake); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5685 | spin_unlock(&ctx->completion_lock); |
Hao Xu | 41a5169 | 2021-08-12 15:47:02 +0800 | [diff] [blame] | 5686 | if (ret || ipt.error) |
| 5687 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; |
| 5688 | |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 5689 | trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data, |
| 5690 | mask, apoll->poll.events); |
Olivier Langlois | 59b735a | 2021-06-22 05:17:39 -0700 | [diff] [blame] | 5691 | return IO_APOLL_OK; |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | static bool __io_poll_remove_one(struct io_kiocb *req, |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5695 | struct io_poll_iocb *poll, bool do_cancel) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5696 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5697 | { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5698 | bool do_complete = false; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5699 | |
Jens Axboe | 5082620 | 2021-02-23 09:02:26 -0700 | [diff] [blame] | 5700 | if (!poll->head) |
| 5701 | return false; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5702 | spin_lock_irq(&poll->head->lock); |
Jens Axboe | b2e720a | 2021-03-31 09:03:03 -0600 | [diff] [blame] | 5703 | if (do_cancel) |
| 5704 | WRITE_ONCE(poll->canceled, true); |
Jens Axboe | 392edb4 | 2019-12-09 17:52:20 -0700 | [diff] [blame] | 5705 | if (!list_empty(&poll->wait.entry)) { |
| 5706 | list_del_init(&poll->wait.entry); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5707 | do_complete = true; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5708 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5709 | spin_unlock_irq(&poll->head->lock); |
Jens Axboe | 3bfa5bc | 2020-05-17 13:54:12 -0600 | [diff] [blame] | 5710 | hash_del(&req->hash_node); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5711 | return do_complete; |
| 5712 | } |
| 5713 | |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5714 | static bool io_poll_remove_one(struct io_kiocb *req) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5715 | __must_hold(&req->ctx->completion_lock) |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5716 | { |
| 5717 | bool do_complete; |
| 5718 | |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5719 | io_poll_remove_double(req); |
Pavel Begunkov | e31001a | 2021-04-13 02:58:43 +0100 | [diff] [blame] | 5720 | do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true); |
Jens Axboe | d4e7cd3 | 2020-08-15 11:44:50 -0700 | [diff] [blame] | 5721 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5722 | if (do_complete) { |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 5723 | io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5724 | io_commit_cqring(req->ctx); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5725 | req_set_fail(req); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 5726 | io_put_req_deferred(req); |
Pavel Begunkov | 5d70904 | 2021-08-09 20:18:13 +0100 | [diff] [blame] | 5727 | } |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5728 | return do_complete; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5729 | } |
| 5730 | |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5731 | /* |
| 5732 | * Returns true if we found and killed one or more poll requests |
| 5733 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 5734 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
| 5735 | struct task_struct *tsk, bool cancel_all) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5736 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5737 | struct hlist_node *tmp; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5738 | struct io_kiocb *req; |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5739 | int posted = 0, i; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5740 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5741 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5742 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 5743 | struct hlist_head *list; |
| 5744 | |
| 5745 | list = &ctx->cancel_hash[i]; |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5746 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 5747 | if (io_match_task_safe(req, tsk, cancel_all)) |
Jens Axboe | f3606e3 | 2020-09-22 08:18:24 -0600 | [diff] [blame] | 5748 | posted += io_poll_remove_one(req); |
| 5749 | } |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5750 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5751 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5752 | |
Jens Axboe | 8e2e1fa | 2020-04-13 17:05:14 -0600 | [diff] [blame] | 5753 | if (posted) |
| 5754 | io_cqring_ev_posted(ctx); |
Jens Axboe | 76e1b64 | 2020-09-26 15:05:03 -0600 | [diff] [blame] | 5755 | |
| 5756 | return posted != 0; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5757 | } |
| 5758 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5759 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5760 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5761 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5762 | { |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5763 | struct hlist_head *list; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5764 | struct io_kiocb *req; |
| 5765 | |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 5766 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
| 5767 | hlist_for_each_entry(req, list, hash_node) { |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 5768 | if (sqe_addr != req->user_data) |
| 5769 | continue; |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5770 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
| 5771 | continue; |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5772 | return req; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5773 | } |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5774 | return NULL; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5775 | } |
| 5776 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5777 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
| 5778 | bool poll_only) |
Pavel Begunkov | e07785b | 2021-04-01 15:43:57 +0100 | [diff] [blame] | 5779 | __must_hold(&ctx->completion_lock) |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5780 | { |
| 5781 | struct io_kiocb *req; |
| 5782 | |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5783 | req = io_poll_find(ctx, sqe_addr, poll_only); |
Jens Axboe | b2cb805 | 2021-03-17 08:17:19 -0600 | [diff] [blame] | 5784 | if (!req) |
| 5785 | return -ENOENT; |
| 5786 | if (io_poll_remove_one(req)) |
| 5787 | return 0; |
| 5788 | |
| 5789 | return -EALREADY; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5790 | } |
| 5791 | |
Pavel Begunkov | 9096af3 | 2021-04-14 13:38:36 +0100 | [diff] [blame] | 5792 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
| 5793 | unsigned int flags) |
| 5794 | { |
| 5795 | u32 events; |
| 5796 | |
| 5797 | events = READ_ONCE(sqe->poll32_events); |
| 5798 | #ifdef __BIG_ENDIAN |
| 5799 | events = swahw32(events); |
| 5800 | #endif |
| 5801 | if (!(flags & IORING_POLL_ADD_MULTI)) |
| 5802 | events |= EPOLLONESHOT; |
| 5803 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); |
| 5804 | } |
| 5805 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5806 | static int io_poll_update_prep(struct io_kiocb *req, |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5807 | const struct io_uring_sqe *sqe) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5808 | { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5809 | struct io_poll_update *upd = &req->poll_update; |
| 5810 | u32 flags; |
| 5811 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5812 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5813 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 5814 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5815 | return -EINVAL; |
| 5816 | flags = READ_ONCE(sqe->len); |
| 5817 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | |
| 5818 | IORING_POLL_ADD_MULTI)) |
| 5819 | return -EINVAL; |
| 5820 | /* meaningless without update */ |
| 5821 | if (flags == IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5822 | return -EINVAL; |
| 5823 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5824 | upd->old_user_data = READ_ONCE(sqe->addr); |
| 5825 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; |
| 5826 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5827 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5828 | upd->new_user_data = READ_ONCE(sqe->off); |
| 5829 | if (!upd->update_user_data && upd->new_user_data) |
| 5830 | return -EINVAL; |
| 5831 | if (upd->update_events) |
| 5832 | upd->events = io_poll_parse_events(sqe, flags); |
| 5833 | else if (sqe->poll32_events) |
| 5834 | return -EINVAL; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5835 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5836 | return 0; |
| 5837 | } |
| 5838 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5839 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 5840 | void *key) |
| 5841 | { |
Jens Axboe | c2f2eb7 | 2020-02-10 09:07:05 -0700 | [diff] [blame] | 5842 | struct io_kiocb *req = wait->private; |
| 5843 | struct io_poll_iocb *poll = &req->poll; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5844 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5845 | return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5846 | } |
| 5847 | |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5848 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 5849 | struct poll_table_struct *p) |
| 5850 | { |
| 5851 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 5852 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 5853 | __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data); |
Jens Axboe | eac406c | 2019-11-14 12:09:58 -0700 | [diff] [blame] | 5854 | } |
| 5855 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 5856 | 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] | 5857 | { |
| 5858 | struct io_poll_iocb *poll = &req->poll; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5859 | u32 flags; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5860 | |
| 5861 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 5862 | return -EINVAL; |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5863 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5864 | return -EINVAL; |
| 5865 | flags = READ_ONCE(sqe->len); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5866 | if (flags & ~IORING_POLL_ADD_MULTI) |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5867 | return -EINVAL; |
| 5868 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 5869 | io_req_set_refcount(req); |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5870 | poll->events = io_poll_parse_events(sqe, flags); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5871 | return 0; |
| 5872 | } |
| 5873 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 5874 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5875 | { |
| 5876 | struct io_poll_iocb *poll = &req->poll; |
| 5877 | struct io_ring_ctx *ctx = req->ctx; |
| 5878 | struct io_poll_table ipt; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5879 | __poll_t mask; |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5880 | bool done; |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 5881 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5882 | ipt.pt._qproc = io_poll_queue_proc; |
Jens Axboe | 3670324 | 2019-07-25 10:20:18 -0600 | [diff] [blame] | 5883 | |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 5884 | mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events, |
| 5885 | io_poll_wake); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5886 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5887 | if (mask) { /* no async, we'd stolen it */ |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5888 | ipt.error = 0; |
Pavel Begunkov | eb6e6f0 | 2021-10-04 20:02:59 +0100 | [diff] [blame] | 5889 | done = __io_poll_complete(req, mask); |
| 5890 | io_commit_cqring(req->ctx); |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5891 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5892 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5893 | |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5894 | if (mask) { |
| 5895 | io_cqring_ev_posted(ctx); |
Hao Xu | 5b7aa38 | 2021-09-22 18:12:38 +0800 | [diff] [blame] | 5896 | if (done) |
Jens Axboe | 88e41cf | 2021-02-22 22:08:01 -0700 | [diff] [blame] | 5897 | io_put_req(req); |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5898 | } |
Jens Axboe | 8c83878 | 2019-03-12 15:48:16 -0600 | [diff] [blame] | 5899 | return ipt.error; |
Jens Axboe | 221c5eb | 2019-01-17 09:41:58 -0700 | [diff] [blame] | 5900 | } |
| 5901 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5902 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5903 | { |
| 5904 | struct io_ring_ctx *ctx = req->ctx; |
| 5905 | struct io_kiocb *preq; |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5906 | bool completing; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5907 | int ret; |
| 5908 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5909 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9ba5fac | 2021-04-14 13:38:35 +0100 | [diff] [blame] | 5910 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5911 | if (!preq) { |
| 5912 | ret = -ENOENT; |
| 5913 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5914 | } |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5915 | |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5916 | if (!req->poll_update.update_events && !req->poll_update.update_user_data) { |
| 5917 | completing = true; |
| 5918 | ret = io_poll_remove_one(preq) ? 0 : -EALREADY; |
| 5919 | goto err; |
| 5920 | } |
| 5921 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5922 | /* |
| 5923 | * Don't allow racy completion with singleshot, as we cannot safely |
| 5924 | * update those. For multishot, if we're racing with completion, just |
| 5925 | * let completion re-add it. |
| 5926 | */ |
| 5927 | completing = !__io_poll_remove_one(preq, &preq->poll, false); |
| 5928 | if (completing && (preq->poll.events & EPOLLONESHOT)) { |
| 5929 | ret = -EALREADY; |
| 5930 | goto err; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5931 | } |
| 5932 | /* we now have a detached poll request. reissue. */ |
| 5933 | ret = 0; |
| 5934 | err: |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5935 | if (ret < 0) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5936 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5937 | req_set_fail(req); |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5938 | io_req_complete(req, ret); |
| 5939 | return 0; |
| 5940 | } |
| 5941 | /* only mask one event flags, keep behavior flags */ |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5942 | if (req->poll_update.update_events) { |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5943 | preq->poll.events &= ~0xffff; |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5944 | preq->poll.events |= req->poll_update.events & 0xffff; |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5945 | preq->poll.events |= IO_POLL_UNMASK; |
| 5946 | } |
Pavel Begunkov | 9d80589 | 2021-04-13 02:58:40 +0100 | [diff] [blame] | 5947 | if (req->poll_update.update_user_data) |
| 5948 | preq->user_data = req->poll_update.new_user_data; |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 5949 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5950 | |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5951 | /* complete update request, we're done with it */ |
| 5952 | io_req_complete(req, ret); |
| 5953 | |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5954 | if (!completing) { |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 5955 | ret = io_poll_add(preq, issue_flags); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5956 | if (ret < 0) { |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 5957 | req_set_fail(preq); |
Jens Axboe | cb3b200e | 2021-04-06 09:49:31 -0600 | [diff] [blame] | 5958 | io_req_complete(preq, ret); |
| 5959 | } |
Jens Axboe | b69de28 | 2021-03-17 08:37:41 -0600 | [diff] [blame] | 5960 | } |
| 5961 | return 0; |
| 5962 | } |
| 5963 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 5964 | static void io_req_task_timeout(struct io_kiocb *req, bool *locked) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5965 | { |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 5966 | struct io_timeout_data *data = req->async_data; |
| 5967 | |
| 5968 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
| 5969 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 5970 | io_req_complete_post(req, -ETIME, 0); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5971 | } |
| 5972 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5973 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 5974 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 5975 | struct io_timeout_data *data = container_of(timer, |
| 5976 | struct io_timeout_data, timer); |
| 5977 | struct io_kiocb *req = data->req; |
| 5978 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5979 | unsigned long flags; |
| 5980 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5981 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | a71976f | 2020-10-10 18:34:11 +0100 | [diff] [blame] | 5982 | list_del_init(&req->timeout.list); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5983 | atomic_set(&req->ctx->cq_timeouts, |
| 5984 | atomic_read(&req->ctx->cq_timeouts) + 1); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5985 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Pavel Begunkov | 01cec8c | 2020-07-30 18:43:50 +0300 | [diff] [blame] | 5986 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5987 | req->io_task_work.func = io_req_task_timeout; |
| 5988 | io_req_task_work_add(req); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 5989 | return HRTIMER_NORESTART; |
| 5990 | } |
| 5991 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5992 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
| 5993 | __u64 user_data) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 5994 | __must_hold(&ctx->timeout_lock) |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 5995 | { |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 5996 | struct io_timeout_data *io; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5997 | struct io_kiocb *req; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 5998 | bool found = false; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 5999 | |
| 6000 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6001 | found = user_data == req->user_data; |
| 6002 | if (found) |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6003 | break; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6004 | } |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6005 | if (!found) |
| 6006 | return ERR_PTR(-ENOENT); |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6007 | |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6008 | io = req->async_data; |
Pavel Begunkov | fd9c7bc | 2021-04-13 02:58:42 +0100 | [diff] [blame] | 6009 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6010 | return ERR_PTR(-EALREADY); |
| 6011 | list_del_init(&req->timeout.list); |
| 6012 | return req; |
| 6013 | } |
| 6014 | |
| 6015 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6016 | __must_hold(&ctx->completion_lock) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6017 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6018 | { |
| 6019 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6020 | |
| 6021 | if (IS_ERR(req)) |
| 6022 | return PTR_ERR(req); |
| 6023 | |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6024 | req_set_fail(req); |
Pavel Begunkov | d4d19c1 | 2021-04-25 14:32:17 +0100 | [diff] [blame] | 6025 | io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0); |
Pavel Begunkov | 91c2f69 | 2021-08-11 19:28:28 +0100 | [diff] [blame] | 6026 | io_put_req_deferred(req); |
Pavel Begunkov | fbd1584 | 2020-11-30 19:11:15 +0000 | [diff] [blame] | 6027 | return 0; |
Jens Axboe | f254ac0 | 2020-08-12 17:33:30 -0600 | [diff] [blame] | 6028 | } |
| 6029 | |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6030 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
| 6031 | { |
| 6032 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { |
| 6033 | case IORING_TIMEOUT_BOOTTIME: |
| 6034 | return CLOCK_BOOTTIME; |
| 6035 | case IORING_TIMEOUT_REALTIME: |
| 6036 | return CLOCK_REALTIME; |
| 6037 | default: |
| 6038 | /* can't happen, vetted at prep time */ |
| 6039 | WARN_ON_ONCE(1); |
| 6040 | fallthrough; |
| 6041 | case 0: |
| 6042 | return CLOCK_MONOTONIC; |
| 6043 | } |
| 6044 | } |
| 6045 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6046 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6047 | struct timespec64 *ts, enum hrtimer_mode mode) |
| 6048 | __must_hold(&ctx->timeout_lock) |
| 6049 | { |
| 6050 | struct io_timeout_data *io; |
| 6051 | struct io_kiocb *req; |
| 6052 | bool found = false; |
| 6053 | |
| 6054 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { |
| 6055 | found = user_data == req->user_data; |
| 6056 | if (found) |
| 6057 | break; |
| 6058 | } |
| 6059 | if (!found) |
| 6060 | return -ENOENT; |
| 6061 | |
| 6062 | io = req->async_data; |
| 6063 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
| 6064 | return -EALREADY; |
| 6065 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); |
| 6066 | io->timer.function = io_link_timeout_fn; |
| 6067 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); |
| 6068 | return 0; |
| 6069 | } |
| 6070 | |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6071 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
| 6072 | struct timespec64 *ts, enum hrtimer_mode mode) |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6073 | __must_hold(&ctx->timeout_lock) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6074 | { |
| 6075 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
| 6076 | struct io_timeout_data *data; |
| 6077 | |
| 6078 | if (IS_ERR(req)) |
| 6079 | return PTR_ERR(req); |
| 6080 | |
| 6081 | req->timeout.off = 0; /* noseq */ |
| 6082 | data = req->async_data; |
| 6083 | list_add_tail(&req->timeout.list, &ctx->timeout_list); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6084 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6085 | data->timer.function = io_timeout_fn; |
| 6086 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); |
| 6087 | return 0; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6088 | } |
| 6089 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6090 | static int io_timeout_remove_prep(struct io_kiocb *req, |
| 6091 | const struct io_uring_sqe *sqe) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6092 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6093 | struct io_timeout_rem *tr = &req->timeout_rem; |
| 6094 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6095 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 6096 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6097 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6098 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6099 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6100 | return -EINVAL; |
| 6101 | |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6102 | tr->ltimeout = false; |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6103 | tr->addr = READ_ONCE(sqe->addr); |
| 6104 | tr->flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6105 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
| 6106 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
| 6107 | return -EINVAL; |
| 6108 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) |
| 6109 | tr->ltimeout = true; |
| 6110 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6111 | return -EINVAL; |
| 6112 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) |
| 6113 | return -EFAULT; |
| 6114 | } else if (tr->flags) { |
| 6115 | /* timeout removal doesn't support flags */ |
| 6116 | return -EINVAL; |
| 6117 | } |
| 6118 | |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6119 | return 0; |
| 6120 | } |
| 6121 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6122 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
| 6123 | { |
| 6124 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS |
| 6125 | : HRTIMER_MODE_REL; |
| 6126 | } |
| 6127 | |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6128 | /* |
| 6129 | * Remove or update an existing timeout command |
| 6130 | */ |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6131 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6132 | { |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6133 | struct io_timeout_rem *tr = &req->timeout_rem; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6134 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6135 | int ret; |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6136 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6137 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
| 6138 | spin_lock(&ctx->completion_lock); |
| 6139 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 9c8e11b | 2020-11-30 19:11:16 +0000 | [diff] [blame] | 6140 | ret = io_timeout_cancel(ctx, tr->addr); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6141 | spin_unlock_irq(&ctx->timeout_lock); |
| 6142 | spin_unlock(&ctx->completion_lock); |
| 6143 | } else { |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6144 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
| 6145 | |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6146 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | f1042b6 | 2021-08-28 19:54:39 -0600 | [diff] [blame] | 6147 | if (tr->ltimeout) |
| 6148 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); |
| 6149 | else |
| 6150 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); |
Pavel Begunkov | ec3c3d0 | 2021-08-18 10:50:52 +0100 | [diff] [blame] | 6151 | spin_unlock_irq(&ctx->timeout_lock); |
| 6152 | } |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6153 | |
Jens Axboe | 4e88d6e | 2019-12-07 20:59:47 -0700 | [diff] [blame] | 6154 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6155 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6156 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 1136504 | 2019-10-16 09:08:32 -0600 | [diff] [blame] | 6157 | return 0; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6158 | } |
| 6159 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6160 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6161 | bool is_timeout_link) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6162 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6163 | struct io_timeout_data *data; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6164 | unsigned flags; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6165 | u32 off = READ_ONCE(sqe->off); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6166 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6167 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6168 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6169 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
| 6170 | sqe->splice_fd_in) |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6171 | return -EINVAL; |
Pavel Begunkov | 56080b0 | 2020-05-26 20:34:04 +0300 | [diff] [blame] | 6172 | if (off && is_timeout_link) |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6173 | return -EINVAL; |
Jens Axboe | a41525a | 2019-10-15 16:48:15 -0600 | [diff] [blame] | 6174 | flags = READ_ONCE(sqe->timeout_flags); |
Pavel Begunkov | 6224590 | 2021-10-02 19:36:14 +0100 | [diff] [blame] | 6175 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
| 6176 | IORING_TIMEOUT_ETIME_SUCCESS)) |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6177 | return -EINVAL; |
| 6178 | /* more than one clock specified is invalid, obviously */ |
| 6179 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6180 | return -EINVAL; |
Arnd Bergmann | bdf2007 | 2019-10-01 09:53:29 -0600 | [diff] [blame] | 6181 | |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6182 | INIT_LIST_HEAD(&req->timeout.list); |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6183 | req->timeout.off = off; |
Pavel Begunkov | f18ee4c | 2021-06-14 23:37:25 +0100 | [diff] [blame] | 6184 | if (unlikely(off && !req->ctx->off_timeout_used)) |
| 6185 | req->ctx->off_timeout_used = true; |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6186 | |
Pavel Begunkov | d6a644a | 2021-10-23 12:14:00 +0100 | [diff] [blame] | 6187 | if (WARN_ON_ONCE(req_has_async_data(req))) |
| 6188 | return -EFAULT; |
| 6189 | if (io_alloc_async_data(req)) |
Jens Axboe | 26a6167 | 2019-12-20 09:02:01 -0700 | [diff] [blame] | 6190 | return -ENOMEM; |
| 6191 | |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6192 | data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6193 | data->req = req; |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6194 | data->flags = flags; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6195 | |
| 6196 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6197 | return -EFAULT; |
| 6198 | |
Ye Bin | f6223ff | 2021-11-18 09:59:07 +0800 | [diff] [blame] | 6199 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
| 6200 | return -EINVAL; |
| 6201 | |
Pavel Begunkov | 8662dae | 2021-01-19 13:32:44 +0000 | [diff] [blame] | 6202 | data->mode = io_translate_timeout_mode(flags); |
Jens Axboe | 50c1df2 | 2021-08-27 17:11:06 -0600 | [diff] [blame] | 6203 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6204 | |
| 6205 | if (is_timeout_link) { |
| 6206 | struct io_submit_link *link = &req->ctx->submit_state.link; |
| 6207 | |
| 6208 | if (!link->head) |
| 6209 | return -EINVAL; |
| 6210 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) |
| 6211 | return -EINVAL; |
Pavel Begunkov | 4d13d1a | 2021-08-15 10:40:24 +0100 | [diff] [blame] | 6212 | req->timeout.head = link->last; |
| 6213 | link->last->flags |= REQ_F_ARM_LTIMEOUT; |
Pavel Begunkov | b97e736 | 2021-08-15 10:40:23 +0100 | [diff] [blame] | 6214 | } |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6215 | return 0; |
| 6216 | } |
| 6217 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6218 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6219 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6220 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6221 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6222 | struct list_head *entry; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6223 | u32 tail, off = req->timeout.off; |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6224 | |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6225 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6226 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6227 | /* |
| 6228 | * sqe->off holds how many events that need to occur for this |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6229 | * timeout event to be satisfied. If it isn't set, then this is |
| 6230 | * a pure timeout request, sequence isn't used. |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6231 | */ |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6232 | if (io_is_timeout_noseq(req)) { |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6233 | entry = ctx->timeout_list.prev; |
| 6234 | goto add; |
| 6235 | } |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6236 | |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6237 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| 6238 | req->timeout.target_seq = tail + off; |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6239 | |
Marcelo Diop-Gonzalez | f010505 | 2021-01-15 11:54:40 -0500 | [diff] [blame] | 6240 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
| 6241 | * This is safe because ->completion_lock is held, and submissions |
| 6242 | * and completions are never mixed in the same ->completion_lock section. |
| 6243 | */ |
| 6244 | ctx->cq_last_tm_flush = tail; |
| 6245 | |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6246 | /* |
| 6247 | * Insertion sort, ensuring the first entry in the list is always |
| 6248 | * the one we need first. |
| 6249 | */ |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6250 | list_for_each_prev(entry, &ctx->timeout_list) { |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6251 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
| 6252 | timeout.list); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6253 | |
Pavel Begunkov | 8eb7e2d | 2020-06-29 13:13:02 +0300 | [diff] [blame] | 6254 | if (io_is_timeout_noseq(nxt)) |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6255 | continue; |
Pavel Begunkov | bfe68a2 | 2020-05-30 14:54:18 +0300 | [diff] [blame] | 6256 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
| 6257 | if (off >= nxt->timeout.target_seq - tail) |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6258 | break; |
| 6259 | } |
Jens Axboe | 93bd25b | 2019-11-11 23:34:31 -0700 | [diff] [blame] | 6260 | add: |
Pavel Begunkov | 135fcde | 2020-07-13 23:37:12 +0300 | [diff] [blame] | 6261 | list_add(&req->timeout.list, entry); |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6262 | data->timer.function = io_timeout_fn; |
| 6263 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); |
Jens Axboe | 89850fc | 2021-08-10 15:11:51 -0600 | [diff] [blame] | 6264 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 5262f56 | 2019-09-17 12:26:57 -0600 | [diff] [blame] | 6265 | return 0; |
| 6266 | } |
| 6267 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6268 | struct io_cancel_data { |
| 6269 | struct io_ring_ctx *ctx; |
| 6270 | u64 user_data; |
| 6271 | }; |
| 6272 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6273 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6274 | { |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6275 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6276 | struct io_cancel_data *cd = data; |
Jens Axboe | de0617e | 2019-04-06 21:51:27 -0600 | [diff] [blame] | 6277 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6278 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6279 | } |
| 6280 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6281 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
| 6282 | struct io_ring_ctx *ctx) |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6283 | { |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6284 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6285 | enum io_wq_cancel cancel_ret; |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6286 | int ret = 0; |
| 6287 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6288 | if (!tctx || !tctx->io_wq) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 6289 | return -ENOENT; |
| 6290 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6291 | 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] | 6292 | switch (cancel_ret) { |
| 6293 | case IO_WQ_CANCEL_OK: |
| 6294 | ret = 0; |
| 6295 | break; |
| 6296 | case IO_WQ_CANCEL_RUNNING: |
| 6297 | ret = -EALREADY; |
| 6298 | break; |
| 6299 | case IO_WQ_CANCEL_NOTFOUND: |
| 6300 | ret = -ENOENT; |
| 6301 | break; |
| 6302 | } |
| 6303 | |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6304 | return ret; |
| 6305 | } |
| 6306 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6307 | 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] | 6308 | { |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6309 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6310 | int ret; |
| 6311 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6312 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6313 | |
Pavel Begunkov | f458dd84 | 2021-03-08 12:14:14 +0000 | [diff] [blame] | 6314 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
Pavel Begunkov | df9727a | 2021-04-01 15:43:59 +0100 | [diff] [blame] | 6315 | if (ret != -ENOENT) |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6316 | return ret; |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6317 | |
| 6318 | spin_lock(&ctx->completion_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6319 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6320 | ret = io_timeout_cancel(ctx, sqe_addr); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6321 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6322 | if (ret != -ENOENT) |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6323 | goto out; |
| 6324 | ret = io_poll_cancel(ctx, sqe_addr, false); |
| 6325 | out: |
| 6326 | spin_unlock(&ctx->completion_lock); |
| 6327 | return ret; |
Jens Axboe | 47f4676 | 2019-11-09 17:43:02 -0700 | [diff] [blame] | 6328 | } |
| 6329 | |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6330 | static int io_async_cancel_prep(struct io_kiocb *req, |
| 6331 | const struct io_uring_sqe *sqe) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6332 | { |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6333 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6334 | return -EINVAL; |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6335 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6336 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6337 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
| 6338 | sqe->splice_fd_in) |
Jens Axboe | e977d6d | 2019-11-05 12:39:45 -0700 | [diff] [blame] | 6339 | return -EINVAL; |
| 6340 | |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6341 | req->cancel.addr = READ_ONCE(sqe->addr); |
| 6342 | return 0; |
| 6343 | } |
| 6344 | |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6345 | 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] | 6346 | { |
| 6347 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6348 | u64 sqe_addr = req->cancel.addr; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6349 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6350 | struct io_tctx_node *node; |
| 6351 | int ret; |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6352 | |
Pavel Begunkov | 8cb01fa | 2021-08-15 10:40:22 +0100 | [diff] [blame] | 6353 | ret = io_try_cancel_userdata(req, sqe_addr); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6354 | if (ret != -ENOENT) |
| 6355 | goto done; |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6356 | |
| 6357 | /* slow path, try all io-wq's */ |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6358 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6359 | ret = -ENOENT; |
| 6360 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 6361 | struct io_uring_task *tctx = node->task->io_uring; |
| 6362 | |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6363 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
| 6364 | if (ret != -ENOENT) |
| 6365 | break; |
| 6366 | } |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6367 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6368 | done: |
Pavel Begunkov | 58f9937 | 2021-03-12 16:25:55 +0000 | [diff] [blame] | 6369 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6370 | req_set_fail(req); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6371 | io_req_complete_post(req, ret, 0); |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 6372 | return 0; |
| 6373 | } |
| 6374 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6375 | static int io_rsrc_update_prep(struct io_kiocb *req, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6376 | const struct io_uring_sqe *sqe) |
| 6377 | { |
Daniele Albano | 61710e4 | 2020-07-18 14:15:16 -0600 | [diff] [blame] | 6378 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
| 6379 | return -EINVAL; |
Pavel Begunkov | 26578cd | 2021-08-20 10:36:37 +0100 | [diff] [blame] | 6380 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6381 | return -EINVAL; |
| 6382 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6383 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
| 6384 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); |
| 6385 | if (!req->rsrc_update.nr_args) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6386 | return -EINVAL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6387 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6388 | return 0; |
| 6389 | } |
| 6390 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6391 | 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] | 6392 | { |
| 6393 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6394 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6395 | struct io_uring_rsrc_update2 up; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6396 | int ret; |
| 6397 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6398 | up.offset = req->rsrc_update.offset; |
| 6399 | up.data = req->rsrc_update.arg; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 6400 | up.nr = 0; |
| 6401 | up.tags = 0; |
Colin Ian King | 615cee4 | 2021-04-26 10:47:35 +0100 | [diff] [blame] | 6402 | up.resv = 0; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6403 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6404 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 6405 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 6406 | &up, req->rsrc_update.nr_args); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 6407 | io_ring_submit_unlock(ctx, needs_lock); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6408 | |
| 6409 | if (ret < 0) |
Pavel Begunkov | 93d2bcd | 2021-05-16 22:58:05 +0100 | [diff] [blame] | 6410 | req_set_fail(req); |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6411 | __io_req_complete(req, issue_flags, ret, 0); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6412 | return 0; |
| 6413 | } |
| 6414 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6415 | 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] | 6416 | { |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6417 | switch (req->opcode) { |
Jens Axboe | e781573 | 2019-12-17 19:45:06 -0700 | [diff] [blame] | 6418 | case IORING_OP_NOP: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6419 | return 0; |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6420 | case IORING_OP_READV: |
| 6421 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6422 | case IORING_OP_READ: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6423 | return io_read_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6424 | case IORING_OP_WRITEV: |
| 6425 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6426 | case IORING_OP_WRITE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6427 | return io_write_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6428 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6429 | return io_poll_add_prep(req, sqe); |
Jens Axboe | 0969e78 | 2019-12-17 18:40:57 -0700 | [diff] [blame] | 6430 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6431 | return io_poll_update_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6432 | case IORING_OP_FSYNC: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6433 | return io_fsync_prep(req, sqe); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6434 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 1155c76 | 2021-02-18 18:29:38 +0000 | [diff] [blame] | 6435 | return io_sfr_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6436 | case IORING_OP_SENDMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6437 | case IORING_OP_SEND: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6438 | return io_sendmsg_prep(req, sqe); |
Jens Axboe | 03b1230 | 2019-12-02 18:50:25 -0700 | [diff] [blame] | 6439 | case IORING_OP_RECVMSG: |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6440 | case IORING_OP_RECV: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6441 | return io_recvmsg_prep(req, sqe); |
Jens Axboe | f499a02 | 2019-12-02 16:28:46 -0700 | [diff] [blame] | 6442 | case IORING_OP_CONNECT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6443 | return io_connect_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6444 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6445 | return io_timeout_prep(req, sqe, false); |
Jens Axboe | b29472e | 2019-12-17 18:50:29 -0700 | [diff] [blame] | 6446 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6447 | return io_timeout_remove_prep(req, sqe); |
Jens Axboe | fbf2384 | 2019-12-17 18:45:56 -0700 | [diff] [blame] | 6448 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6449 | return io_async_cancel_prep(req, sqe); |
Jens Axboe | 2d28390 | 2019-12-04 11:08:05 -0700 | [diff] [blame] | 6450 | case IORING_OP_LINK_TIMEOUT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6451 | return io_timeout_prep(req, sqe, true); |
Jens Axboe | 8ed8d3c | 2019-12-16 11:55:28 -0700 | [diff] [blame] | 6452 | case IORING_OP_ACCEPT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6453 | return io_accept_prep(req, sqe); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6454 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6455 | return io_fallocate_prep(req, sqe); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6456 | case IORING_OP_OPENAT: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6457 | return io_openat_prep(req, sqe); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6458 | case IORING_OP_CLOSE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6459 | return io_close_prep(req, sqe); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6460 | case IORING_OP_FILES_UPDATE: |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 6461 | return io_rsrc_update_prep(req, sqe); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6462 | case IORING_OP_STATX: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6463 | return io_statx_prep(req, sqe); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6464 | case IORING_OP_FADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6465 | return io_fadvise_prep(req, sqe); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6466 | case IORING_OP_MADVISE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6467 | return io_madvise_prep(req, sqe); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6468 | case IORING_OP_OPENAT2: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6469 | return io_openat2_prep(req, sqe); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6470 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6471 | return io_epoll_ctl_prep(req, sqe); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6472 | case IORING_OP_SPLICE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6473 | return io_splice_prep(req, sqe); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6474 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6475 | return io_provide_buffers_prep(req, sqe); |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6476 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6477 | return io_remove_buffers_prep(req, sqe); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6478 | case IORING_OP_TEE: |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6479 | return io_tee_prep(req, sqe); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6480 | case IORING_OP_SHUTDOWN: |
| 6481 | return io_shutdown_prep(req, sqe); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6482 | case IORING_OP_RENAMEAT: |
| 6483 | return io_renameat_prep(req, sqe); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6484 | case IORING_OP_UNLINKAT: |
| 6485 | return io_unlinkat_prep(req, sqe); |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6486 | case IORING_OP_MKDIRAT: |
| 6487 | return io_mkdirat_prep(req, sqe); |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6488 | case IORING_OP_SYMLINKAT: |
| 6489 | return io_symlinkat_prep(req, sqe); |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6490 | case IORING_OP_LINKAT: |
| 6491 | return io_linkat_prep(req, sqe); |
Jens Axboe | f67676d | 2019-12-02 11:03:47 -0700 | [diff] [blame] | 6492 | } |
| 6493 | |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6494 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
| 6495 | req->opcode); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 6496 | return -EINVAL; |
Pavel Begunkov | bfe7655 | 2020-09-30 22:57:55 +0300 | [diff] [blame] | 6497 | } |
| 6498 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6499 | static int io_req_prep_async(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6500 | { |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6501 | if (!io_op_defs[req->opcode].needs_async_setup) |
| 6502 | return 0; |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6503 | if (WARN_ON_ONCE(req_has_async_data(req))) |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6504 | return -EFAULT; |
| 6505 | if (io_alloc_async_data(req)) |
| 6506 | return -EAGAIN; |
| 6507 | |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6508 | switch (req->opcode) { |
| 6509 | case IORING_OP_READV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6510 | return io_rw_prep_async(req, READ); |
| 6511 | case IORING_OP_WRITEV: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6512 | return io_rw_prep_async(req, WRITE); |
| 6513 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6514 | return io_sendmsg_prep_async(req); |
| 6515 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 93642ef | 2021-02-18 18:29:44 +0000 | [diff] [blame] | 6516 | return io_recvmsg_prep_async(req); |
| 6517 | case IORING_OP_CONNECT: |
| 6518 | return io_connect_prep_async(req); |
| 6519 | } |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6520 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
| 6521 | req->opcode); |
| 6522 | return -EFAULT; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6523 | } |
| 6524 | |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6525 | static u32 io_get_sequence(struct io_kiocb *req) |
| 6526 | { |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6527 | u32 seq = req->ctx->cached_sq_head; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6528 | |
Pavel Begunkov | a3dbdf5 | 2021-06-17 18:14:05 +0100 | [diff] [blame] | 6529 | /* need original cached_sq_head, but it was increased for each req */ |
| 6530 | io_for_each_link(req, req) |
| 6531 | seq--; |
| 6532 | return seq; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6533 | } |
| 6534 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 6535 | static __cold void io_drain_req(struct io_kiocb *req) |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6536 | { |
| 6537 | struct io_ring_ctx *ctx = req->ctx; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6538 | struct io_defer_entry *de; |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6539 | int ret; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6540 | u32 seq = io_get_sequence(req); |
Pavel Begunkov | 3c19966 | 2021-06-15 16:47:57 +0100 | [diff] [blame] | 6541 | |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 6542 | /* Still need defer if there is pending req in defer list. */ |
Pavel Begunkov | 5e37126 | 2021-09-24 22:00:04 +0100 | [diff] [blame] | 6543 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6544 | queue: |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6545 | ctx->drain_active = false; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6546 | io_req_task_queue(req); |
| 6547 | return; |
Pavel Begunkov | 10c6690 | 2021-06-15 16:47:56 +0100 | [diff] [blame] | 6548 | } |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6549 | |
Pavel Begunkov | b7e298d | 2021-02-28 22:35:19 +0000 | [diff] [blame] | 6550 | ret = io_req_prep_async(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6551 | if (ret) { |
| 6552 | fail: |
| 6553 | io_req_complete_failed(req, ret); |
| 6554 | return; |
| 6555 | } |
Pavel Begunkov | cbdcb43 | 2020-06-29 19:18:43 +0300 | [diff] [blame] | 6556 | io_prep_async_link(req); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6557 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6558 | if (!de) { |
Pavel Begunkov | 1b48773 | 2021-07-11 22:41:13 +0100 | [diff] [blame] | 6559 | ret = -ENOMEM; |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6560 | goto fail; |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 6561 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6562 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6563 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6564 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6565 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6566 | kfree(de); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 6567 | goto queue; |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | trace_io_uring_defer(ctx, req, req->user_data); |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6571 | de->req = req; |
Pavel Begunkov | 9cf7c10 | 2020-07-13 23:37:15 +0300 | [diff] [blame] | 6572 | de->seq = seq; |
Pavel Begunkov | 27dc833 | 2020-07-13 23:37:14 +0300 | [diff] [blame] | 6573 | list_add_tail(&de->list, &ctx->defer_list); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 6574 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6575 | } |
| 6576 | |
Pavel Begunkov | 68fb897 | 2021-03-19 17:22:41 +0000 | [diff] [blame] | 6577 | static void io_clean_op(struct io_kiocb *req) |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6578 | { |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6579 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
Pavel Begunkov | 30d51dd | 2021-10-01 18:07:03 +0100 | [diff] [blame] | 6580 | kfree(req->kbuf); |
| 6581 | req->kbuf = NULL; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6582 | } |
| 6583 | |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6584 | if (req->flags & REQ_F_NEED_CLEANUP) { |
| 6585 | switch (req->opcode) { |
| 6586 | case IORING_OP_READV: |
| 6587 | case IORING_OP_READ_FIXED: |
| 6588 | case IORING_OP_READ: |
| 6589 | case IORING_OP_WRITEV: |
| 6590 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6591 | case IORING_OP_WRITE: { |
| 6592 | struct io_async_rw *io = req->async_data; |
Pavel Begunkov | 1dacb4d | 2021-06-17 18:14:03 +0100 | [diff] [blame] | 6593 | |
| 6594 | kfree(io->free_iovec); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6595 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6596 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6597 | case IORING_OP_RECVMSG: |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6598 | case IORING_OP_SENDMSG: { |
| 6599 | struct io_async_msghdr *io = req->async_data; |
Pavel Begunkov | 257e84a | 2021-02-05 00:58:00 +0000 | [diff] [blame] | 6600 | |
| 6601 | kfree(io->free_iov); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6602 | break; |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6603 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6604 | case IORING_OP_SPLICE: |
| 6605 | case IORING_OP_TEE: |
Pavel Begunkov | e1d767f | 2021-03-19 17:22:43 +0000 | [diff] [blame] | 6606 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
| 6607 | io_put_file(req->splice.file_in); |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6608 | break; |
Jens Axboe | f3cd4850 | 2020-09-24 14:55:54 -0600 | [diff] [blame] | 6609 | case IORING_OP_OPENAT: |
| 6610 | case IORING_OP_OPENAT2: |
| 6611 | if (req->open.filename) |
| 6612 | putname(req->open.filename); |
| 6613 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6614 | case IORING_OP_RENAMEAT: |
| 6615 | putname(req->rename.oldpath); |
| 6616 | putname(req->rename.newpath); |
| 6617 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6618 | case IORING_OP_UNLINKAT: |
| 6619 | putname(req->unlink.filename); |
| 6620 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6621 | case IORING_OP_MKDIRAT: |
| 6622 | putname(req->mkdir.filename); |
| 6623 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6624 | case IORING_OP_SYMLINKAT: |
| 6625 | putname(req->symlink.oldpath); |
| 6626 | putname(req->symlink.newpath); |
| 6627 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6628 | case IORING_OP_LINKAT: |
| 6629 | putname(req->hardlink.oldpath); |
| 6630 | putname(req->hardlink.newpath); |
| 6631 | break; |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6632 | } |
Pavel Begunkov | 0e1b6fe3 | 2020-07-16 23:28:02 +0300 | [diff] [blame] | 6633 | } |
Jens Axboe | 75652a30 | 2021-04-15 09:52:40 -0600 | [diff] [blame] | 6634 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
| 6635 | kfree(req->apoll->double_poll); |
| 6636 | kfree(req->apoll); |
| 6637 | req->apoll = NULL; |
| 6638 | } |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6639 | if (req->flags & REQ_F_INFLIGHT) { |
| 6640 | struct io_uring_task *tctx = req->task->io_uring; |
| 6641 | |
| 6642 | atomic_dec(&tctx->inflight_tracked); |
Pavel Begunkov | 3a0a690 | 2021-04-20 12:03:31 +0100 | [diff] [blame] | 6643 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6644 | if (req->flags & REQ_F_CREDS) |
Pavel Begunkov | b8e64b5 | 2021-06-17 18:14:02 +0100 | [diff] [blame] | 6645 | put_cred(req->creds); |
Pavel Begunkov | d886e18 | 2021-10-04 20:02:56 +0100 | [diff] [blame] | 6646 | if (req->flags & REQ_F_ASYNC_DATA) { |
| 6647 | kfree(req->async_data); |
| 6648 | req->async_data = NULL; |
| 6649 | } |
Pavel Begunkov | c854357 | 2021-06-17 18:14:04 +0100 | [diff] [blame] | 6650 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
Pavel Begunkov | 99bc4c3 | 2020-02-07 22:04:45 +0300 | [diff] [blame] | 6651 | } |
| 6652 | |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6653 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6654 | { |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6655 | const struct cred *creds = NULL; |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6656 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 6657 | |
Pavel Begunkov | 6878b40 | 2021-09-24 21:59:41 +0100 | [diff] [blame] | 6658 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
Pavel Begunkov | c10d1f9 | 2021-06-17 18:14:01 +0100 | [diff] [blame] | 6659 | creds = override_creds(req->creds); |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6660 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 6661 | if (!io_op_defs[req->opcode].audit_skip) |
| 6662 | audit_uring_entry(req->opcode); |
| 6663 | |
Jens Axboe | d625c6e | 2019-12-17 19:53:05 -0700 | [diff] [blame] | 6664 | switch (req->opcode) { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6665 | case IORING_OP_NOP: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6666 | ret = io_nop(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6667 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6668 | case IORING_OP_READV: |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 6669 | case IORING_OP_READ_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6670 | case IORING_OP_READ: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6671 | ret = io_read(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6672 | break; |
| 6673 | case IORING_OP_WRITEV: |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6674 | case IORING_OP_WRITE_FIXED: |
Jens Axboe | 3a6820f | 2019-12-22 15:19:35 -0700 | [diff] [blame] | 6675 | case IORING_OP_WRITE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6676 | ret = io_write(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6677 | break; |
| 6678 | case IORING_OP_FSYNC: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6679 | ret = io_fsync(req, issue_flags); |
Jackie Liu | ba5290c | 2019-10-09 09:19:59 +0800 | [diff] [blame] | 6680 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6681 | case IORING_OP_POLL_ADD: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6682 | ret = io_poll_add(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6683 | break; |
| 6684 | case IORING_OP_POLL_REMOVE: |
Pavel Begunkov | c5de003 | 2021-04-14 13:38:37 +0100 | [diff] [blame] | 6685 | ret = io_poll_update(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6686 | break; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6687 | case IORING_OP_SYNC_FILE_RANGE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6688 | ret = io_sync_file_range(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6689 | break; |
| 6690 | case IORING_OP_SENDMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6691 | ret = io_sendmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6692 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6693 | case IORING_OP_SEND: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6694 | ret = io_send(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6695 | break; |
| 6696 | case IORING_OP_RECVMSG: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6697 | ret = io_recvmsg(req, issue_flags); |
Pavel Begunkov | 062d04d | 2020-10-10 18:34:12 +0100 | [diff] [blame] | 6698 | break; |
Jens Axboe | fddafac | 2020-01-04 20:19:44 -0700 | [diff] [blame] | 6699 | case IORING_OP_RECV: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6700 | ret = io_recv(req, issue_flags); |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 6701 | break; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6702 | case IORING_OP_TIMEOUT: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6703 | ret = io_timeout(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6704 | break; |
| 6705 | case IORING_OP_TIMEOUT_REMOVE: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6706 | ret = io_timeout_remove(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6707 | break; |
| 6708 | case IORING_OP_ACCEPT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6709 | ret = io_accept(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6710 | break; |
| 6711 | case IORING_OP_CONNECT: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6712 | ret = io_connect(req, issue_flags); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6713 | break; |
| 6714 | case IORING_OP_ASYNC_CANCEL: |
Pavel Begunkov | 61e9820 | 2021-02-10 00:03:08 +0000 | [diff] [blame] | 6715 | ret = io_async_cancel(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6716 | break; |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6717 | case IORING_OP_FALLOCATE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6718 | ret = io_fallocate(req, issue_flags); |
Jens Axboe | d63d1b5 | 2019-12-10 10:38:56 -0700 | [diff] [blame] | 6719 | break; |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6720 | case IORING_OP_OPENAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6721 | ret = io_openat(req, issue_flags); |
Jens Axboe | 15b71ab | 2019-12-11 11:20:36 -0700 | [diff] [blame] | 6722 | break; |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6723 | case IORING_OP_CLOSE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6724 | ret = io_close(req, issue_flags); |
Jens Axboe | b5dba59 | 2019-12-11 14:02:38 -0700 | [diff] [blame] | 6725 | break; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6726 | case IORING_OP_FILES_UPDATE: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6727 | ret = io_files_update(req, issue_flags); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 6728 | break; |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6729 | case IORING_OP_STATX: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6730 | ret = io_statx(req, issue_flags); |
Jens Axboe | eddc7ef | 2019-12-13 21:18:10 -0700 | [diff] [blame] | 6731 | break; |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6732 | case IORING_OP_FADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6733 | ret = io_fadvise(req, issue_flags); |
Jens Axboe | 4840e41 | 2019-12-25 22:03:45 -0700 | [diff] [blame] | 6734 | break; |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6735 | case IORING_OP_MADVISE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6736 | ret = io_madvise(req, issue_flags); |
Jens Axboe | c1ca757 | 2019-12-25 22:18:28 -0700 | [diff] [blame] | 6737 | break; |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6738 | case IORING_OP_OPENAT2: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6739 | ret = io_openat2(req, issue_flags); |
Jens Axboe | cebdb98 | 2020-01-08 17:59:24 -0700 | [diff] [blame] | 6740 | break; |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6741 | case IORING_OP_EPOLL_CTL: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6742 | ret = io_epoll_ctl(req, issue_flags); |
Jens Axboe | 3e4827b | 2020-01-08 15:18:09 -0700 | [diff] [blame] | 6743 | break; |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6744 | case IORING_OP_SPLICE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6745 | ret = io_splice(req, issue_flags); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 6746 | break; |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6747 | case IORING_OP_PROVIDE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6748 | ret = io_provide_buffers(req, issue_flags); |
Jens Axboe | ddf0322d | 2020-02-23 16:41:33 -0700 | [diff] [blame] | 6749 | break; |
Jens Axboe | 067524e | 2020-03-02 16:32:28 -0700 | [diff] [blame] | 6750 | case IORING_OP_REMOVE_BUFFERS: |
Pavel Begunkov | 889fca7 | 2021-02-10 00:03:09 +0000 | [diff] [blame] | 6751 | ret = io_remove_buffers(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6752 | break; |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6753 | case IORING_OP_TEE: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6754 | ret = io_tee(req, issue_flags); |
Pavel Begunkov | f2a8d5c | 2020-05-17 14:18:06 +0300 | [diff] [blame] | 6755 | break; |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6756 | case IORING_OP_SHUTDOWN: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6757 | ret = io_shutdown(req, issue_flags); |
Jens Axboe | 36f4fa6 | 2020-09-05 11:14:22 -0600 | [diff] [blame] | 6758 | break; |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6759 | case IORING_OP_RENAMEAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6760 | ret = io_renameat(req, issue_flags); |
Jens Axboe | 80a261f | 2020-09-28 14:23:58 -0600 | [diff] [blame] | 6761 | break; |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6762 | case IORING_OP_UNLINKAT: |
Pavel Begunkov | 45d189c | 2021-02-10 00:03:07 +0000 | [diff] [blame] | 6763 | ret = io_unlinkat(req, issue_flags); |
Jens Axboe | 14a1143 | 2020-09-28 14:27:37 -0600 | [diff] [blame] | 6764 | break; |
Dmitry Kadashev | e34a02d | 2021-07-08 13:34:45 +0700 | [diff] [blame] | 6765 | case IORING_OP_MKDIRAT: |
| 6766 | ret = io_mkdirat(req, issue_flags); |
| 6767 | break; |
Dmitry Kadashev | 7a8721f | 2021-07-08 13:34:46 +0700 | [diff] [blame] | 6768 | case IORING_OP_SYMLINKAT: |
| 6769 | ret = io_symlinkat(req, issue_flags); |
| 6770 | break; |
Dmitry Kadashev | cf30da9 | 2021-07-08 13:34:47 +0700 | [diff] [blame] | 6771 | case IORING_OP_LINKAT: |
| 6772 | ret = io_linkat(req, issue_flags); |
| 6773 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6774 | default: |
| 6775 | ret = -EINVAL; |
| 6776 | break; |
| 6777 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6778 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 6779 | if (!io_op_defs[req->opcode].audit_skip) |
| 6780 | audit_uring_exit(!ret, ret); |
| 6781 | |
Jens Axboe | 5730b27 | 2021-02-27 15:57:30 -0700 | [diff] [blame] | 6782 | if (creds) |
| 6783 | revert_creds(creds); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6784 | if (ret) |
| 6785 | return ret; |
Jens Axboe | b532576 | 2020-05-19 21:20:27 -0600 | [diff] [blame] | 6786 | /* If the op doesn't have a file, we're not polling for it */ |
Pavel Begunkov | 9983028 | 2021-10-15 17:09:11 +0100 | [diff] [blame] | 6787 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
Pavel Begunkov | 9882131 | 2021-10-15 17:09:12 +0100 | [diff] [blame] | 6788 | io_iopoll_req_issued(req, issue_flags); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6789 | |
| 6790 | return 0; |
| 6791 | } |
| 6792 | |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 6793 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
| 6794 | { |
| 6795 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 6796 | |
| 6797 | req = io_put_req_find_next(req); |
| 6798 | return req ? &req->work : NULL; |
| 6799 | } |
| 6800 | |
Pavel Begunkov | 5280f7e | 2021-02-04 13:52:08 +0000 | [diff] [blame] | 6801 | static void io_wq_submit_work(struct io_wq_work *work) |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6802 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6803 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6804 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
| 6805 | bool needs_poll = false; |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6806 | struct io_kiocb *timeout; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6807 | int ret = 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6808 | |
Pavel Begunkov | 48dcd38 | 2021-08-15 10:40:18 +0100 | [diff] [blame] | 6809 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
| 6810 | if (!(req->flags & REQ_F_REFCOUNT)) |
| 6811 | __io_req_set_refcount(req, 2); |
| 6812 | else |
| 6813 | req_ref_get(req); |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6814 | |
Pavel Begunkov | 6df1db6 | 2020-07-03 22:15:06 +0300 | [diff] [blame] | 6815 | timeout = io_prep_linked_timeout(req); |
| 6816 | if (timeout) |
| 6817 | io_queue_linked_timeout(timeout); |
Pavel Begunkov | d4c81f3 | 2020-06-08 21:08:19 +0300 | [diff] [blame] | 6818 | |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 6819 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6820 | if (work->flags & IO_WQ_WORK_CANCEL) { |
| 6821 | io_req_task_queue_fail(req, -ECANCELED); |
| 6822 | return; |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 6823 | } |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6824 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6825 | if (req->flags & REQ_F_FORCE_ASYNC) { |
Pavel Begunkov | afb7f56f | 2021-10-23 12:13:59 +0100 | [diff] [blame] | 6826 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
| 6827 | bool opcode_poll = def->pollin || def->pollout; |
| 6828 | |
| 6829 | if (opcode_poll && file_can_poll(req->file)) { |
| 6830 | needs_poll = true; |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6831 | issue_flags |= IO_URING_F_NONBLOCK; |
Pavel Begunkov | afb7f56f | 2021-10-23 12:13:59 +0100 | [diff] [blame] | 6832 | } |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6833 | } |
Hao Xu | 90fa028 | 2021-10-18 21:34:45 +0800 | [diff] [blame] | 6834 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6835 | do { |
| 6836 | ret = io_issue_sqe(req, issue_flags); |
| 6837 | if (ret != -EAGAIN) |
| 6838 | break; |
| 6839 | /* |
| 6840 | * We can get EAGAIN for iopolled IO even though we're |
| 6841 | * forcing a sync submission from here, since we can't |
| 6842 | * wait for request slots on the block side. |
| 6843 | */ |
| 6844 | if (!needs_poll) { |
| 6845 | cond_resched(); |
| 6846 | continue; |
Hao Xu | 90fa028 | 2021-10-18 21:34:45 +0800 | [diff] [blame] | 6847 | } |
| 6848 | |
Pavel Begunkov | d01905d | 2021-10-23 12:13:57 +0100 | [diff] [blame] | 6849 | if (io_arm_poll_handler(req) == IO_APOLL_OK) |
| 6850 | return; |
| 6851 | /* aborted or ready, in either case retry blocking */ |
| 6852 | needs_poll = false; |
| 6853 | issue_flags &= ~IO_URING_F_NONBLOCK; |
| 6854 | } while (1); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6855 | |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6856 | /* avoid locking problems by failing it from a clean context */ |
Pavel Begunkov | 5d5901a | 2021-08-11 19:28:29 +0100 | [diff] [blame] | 6857 | if (ret) |
Pavel Begunkov | a3df7698 | 2021-02-18 22:32:52 +0000 | [diff] [blame] | 6858 | io_req_task_queue_fail(req, ret); |
Jens Axboe | 31b5151 | 2019-01-18 22:56:34 -0700 | [diff] [blame] | 6859 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 6860 | |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6861 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 6862 | unsigned i) |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6863 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 6864 | return &table->files[i]; |
Pavel Begunkov | dafecf1 | 2021-02-28 22:35:11 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6867 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
| 6868 | int index) |
| 6869 | { |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 6870 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6871 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6872 | return (struct file *) (slot->file_ptr & FFS_MASK); |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 6873 | } |
| 6874 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6875 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 6876 | { |
| 6877 | unsigned long file_ptr = (unsigned long) file; |
| 6878 | |
Pavel Begunkov | 88459b5 | 2021-10-17 00:07:10 +0100 | [diff] [blame] | 6879 | file_ptr |= io_file_get_flags(file); |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 6880 | file_slot->file_ptr = file_ptr; |
Jens Axboe | 09bb839 | 2019-03-13 12:39:28 -0600 | [diff] [blame] | 6881 | } |
| 6882 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6883 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
| 6884 | struct io_kiocb *req, int fd) |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6885 | { |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6886 | struct file *file; |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6887 | unsigned long file_ptr; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6888 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6889 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
| 6890 | return NULL; |
| 6891 | fd = array_index_nospec(fd, ctx->nr_user_files); |
| 6892 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; |
| 6893 | file = (struct file *) (file_ptr & FFS_MASK); |
| 6894 | file_ptr &= ~FFS_MASK; |
| 6895 | /* mask in overlapping REQ_F and FFS bits */ |
Pavel Begunkov | 35645ac | 2021-10-17 00:07:09 +0100 | [diff] [blame] | 6896 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
Pavel Begunkov | a46be97 | 2021-10-09 23:14:40 +0100 | [diff] [blame] | 6897 | io_req_set_rsrc_node(req, ctx); |
Pavel Begunkov | 8371adf | 2020-10-10 18:34:08 +0100 | [diff] [blame] | 6898 | return file; |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 6899 | } |
| 6900 | |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6901 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6902 | struct io_kiocb *req, int fd) |
| 6903 | { |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6904 | struct file *file = fget(fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6905 | |
| 6906 | trace_io_uring_file_get(ctx, fd); |
| 6907 | |
| 6908 | /* we don't allow fixed io_uring files */ |
| 6909 | if (file && unlikely(file->f_op == &io_uring_fops)) |
| 6910 | io_req_track_inflight(req); |
| 6911 | return file; |
| 6912 | } |
| 6913 | |
| 6914 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6915 | struct io_kiocb *req, int fd, bool fixed) |
| 6916 | { |
| 6917 | if (fixed) |
| 6918 | return io_file_get_fixed(ctx, req, fd); |
| 6919 | else |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 6920 | return io_file_get_normal(ctx, req, fd); |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 6921 | } |
| 6922 | |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 6923 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6924 | { |
| 6925 | struct io_kiocb *prev = req->timeout.prev; |
Pavel Begunkov | 617a894 | 2021-11-26 14:38:14 +0000 | [diff] [blame] | 6926 | int ret = -ENOENT; |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6927 | |
| 6928 | if (prev) { |
Pavel Begunkov | 617a894 | 2021-11-26 14:38:14 +0000 | [diff] [blame] | 6929 | if (!(req->task->flags & PF_EXITING)) |
| 6930 | ret = io_try_cancel_userdata(req, prev->user_data); |
Pavel Begunkov | 505657b | 2021-08-17 20:28:09 +0100 | [diff] [blame] | 6931 | io_req_complete_post(req, ret ?: -ETIME, 0); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6932 | io_put_req(prev); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6933 | } else { |
| 6934 | io_req_complete_post(req, -ETIME, 0); |
| 6935 | } |
| 6936 | } |
| 6937 | |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6938 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
| 6939 | { |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6940 | struct io_timeout_data *data = container_of(timer, |
| 6941 | struct io_timeout_data, timer); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6942 | struct io_kiocb *prev, *req = data->req; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6943 | struct io_ring_ctx *ctx = req->ctx; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6944 | unsigned long flags; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6945 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6946 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6947 | prev = req->timeout.head; |
| 6948 | req->timeout.head = NULL; |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6949 | |
| 6950 | /* |
| 6951 | * We don't expect the list to be empty, that will only happen if we |
| 6952 | * race with the completion of the linked work. |
| 6953 | */ |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6954 | if (prev) { |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6955 | io_remove_next_linked(prev); |
Pavel Begunkov | 447c19f | 2021-05-14 12:02:50 +0100 | [diff] [blame] | 6956 | if (!req_ref_inc_not_zero(prev)) |
| 6957 | prev = NULL; |
| 6958 | } |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6959 | list_del(&req->timeout.list); |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6960 | req->timeout.prev = prev; |
| 6961 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6962 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6963 | req->io_task_work.func = io_req_task_link_timeout; |
| 6964 | io_req_task_work_add(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6965 | return HRTIMER_NORESTART; |
| 6966 | } |
| 6967 | |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6968 | static void io_queue_linked_timeout(struct io_kiocb *req) |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6969 | { |
Pavel Begunkov | de968c1 | 2021-03-19 17:22:33 +0000 | [diff] [blame] | 6970 | struct io_ring_ctx *ctx = req->ctx; |
| 6971 | |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6972 | spin_lock_irq(&ctx->timeout_lock); |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6973 | /* |
Pavel Begunkov | f2f8737 | 2020-10-27 23:25:37 +0000 | [diff] [blame] | 6974 | * If the back reference is NULL, then our linked request finished |
| 6975 | * before we got a chance to setup the timer |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6976 | */ |
Pavel Begunkov | 90cd7e4 | 2020-10-27 23:25:36 +0000 | [diff] [blame] | 6977 | if (req->timeout.head) { |
Jens Axboe | e8c2bc1 | 2020-08-15 18:44:09 -0700 | [diff] [blame] | 6978 | struct io_timeout_data *data = req->async_data; |
Jens Axboe | 94ae5e7 | 2019-11-14 19:39:52 -0700 | [diff] [blame] | 6979 | |
Jens Axboe | ad8a48a | 2019-11-15 08:49:11 -0700 | [diff] [blame] | 6980 | data->timer.function = io_link_timeout_fn; |
| 6981 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), |
| 6982 | data->mode); |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 6983 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6984 | } |
Jens Axboe | 89b263f | 2021-08-10 15:14:18 -0600 | [diff] [blame] | 6985 | spin_unlock_irq(&ctx->timeout_lock); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6986 | /* drop submission reference */ |
Jens Axboe | 76a46e0 | 2019-11-10 23:34:16 -0700 | [diff] [blame] | 6987 | io_put_req(req); |
Jens Axboe | 2665abf | 2019-11-05 12:40:47 -0700 | [diff] [blame] | 6988 | } |
| 6989 | |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 6990 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
| 6991 | __must_hold(&req->ctx->uring_lock) |
| 6992 | { |
| 6993 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); |
| 6994 | |
| 6995 | switch (io_arm_poll_handler(req)) { |
| 6996 | case IO_APOLL_READY: |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 6997 | io_req_task_queue(req); |
| 6998 | break; |
| 6999 | case IO_APOLL_ABORTED: |
| 7000 | /* |
| 7001 | * Queued up for async execution, worker will release |
| 7002 | * submit reference when the iocb is actually submitted. |
| 7003 | */ |
| 7004 | io_queue_async_work(req, NULL); |
| 7005 | break; |
| 7006 | } |
| 7007 | |
| 7008 | if (linked_timeout) |
| 7009 | io_queue_linked_timeout(linked_timeout); |
| 7010 | } |
| 7011 | |
| 7012 | static inline void __io_queue_sqe(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7013 | __must_hold(&req->ctx->uring_lock) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7014 | { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7015 | struct io_kiocb *linked_timeout; |
Jens Axboe | e0c5c57 | 2019-03-12 10:18:47 -0600 | [diff] [blame] | 7016 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7017 | |
Pavel Begunkov | c5eef2b | 2021-02-10 00:03:22 +0000 | [diff] [blame] | 7018 | 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] | 7019 | |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 7020 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
| 7021 | io_req_add_compl_list(req); |
Pavel Begunkov | d9f9d28 | 2021-09-24 22:00:00 +0100 | [diff] [blame] | 7022 | return; |
Pavel Begunkov | fff4e40 | 2021-10-04 20:02:48 +0100 | [diff] [blame] | 7023 | } |
Jens Axboe | 491381ce | 2019-10-17 09:20:46 -0600 | [diff] [blame] | 7024 | /* |
| 7025 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 7026 | * doesn't support non-blocking read/write attempts |
| 7027 | */ |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7028 | if (likely(!ret)) { |
Pavel Begunkov | 906c6ca | 2021-08-15 10:40:26 +0100 | [diff] [blame] | 7029 | linked_timeout = io_prep_linked_timeout(req); |
| 7030 | if (linked_timeout) |
| 7031 | io_queue_linked_timeout(linked_timeout); |
Pavel Begunkov | 1840038 | 2021-03-19 17:22:34 +0000 | [diff] [blame] | 7032 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
Pavel Begunkov | d475a9a | 2021-09-24 21:59:59 +0100 | [diff] [blame] | 7033 | io_queue_sqe_arm_apoll(req); |
Pavel Begunkov | 0d63c14 | 2020-10-22 16:47:18 +0100 | [diff] [blame] | 7034 | } else { |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 7035 | io_req_complete_failed(req, ret); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7036 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7037 | } |
| 7038 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7039 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7040 | __must_hold(&req->ctx->uring_lock) |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7041 | { |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7042 | if (req->flags & REQ_F_FAIL) { |
Pavel Begunkov | c6d3d9c | 2021-08-31 14:13:10 +0100 | [diff] [blame] | 7043 | io_req_complete_fail_submit(req); |
Pavel Begunkov | e0eb71d | 2021-10-01 18:07:01 +0100 | [diff] [blame] | 7044 | } else if (unlikely(req->ctx->drain_active)) { |
| 7045 | io_drain_req(req); |
Pavel Begunkov | 76cc33d | 2021-06-14 23:37:30 +0100 | [diff] [blame] | 7046 | } else { |
| 7047 | int ret = io_req_prep_async(req); |
| 7048 | |
| 7049 | if (unlikely(ret)) |
| 7050 | io_req_complete_failed(req, ret); |
| 7051 | else |
Pavel Begunkov | f237c30 | 2021-08-18 12:42:46 +0100 | [diff] [blame] | 7052 | io_queue_async_work(req, NULL); |
Jens Axboe | ce35a47 | 2019-12-17 08:04:44 -0700 | [diff] [blame] | 7053 | } |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7054 | } |
| 7055 | |
Pavel Begunkov | 4652fe3 | 2021-09-24 21:59:58 +0100 | [diff] [blame] | 7056 | static inline void io_queue_sqe(struct io_kiocb *req) |
| 7057 | __must_hold(&req->ctx->uring_lock) |
| 7058 | { |
| 7059 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) |
| 7060 | __io_queue_sqe(req); |
| 7061 | else |
| 7062 | io_queue_sqe_fallback(req); |
| 7063 | } |
| 7064 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7065 | /* |
| 7066 | * Check SQE restrictions (opcode and flags). |
| 7067 | * |
| 7068 | * Returns 'true' if SQE is allowed, 'false' otherwise. |
| 7069 | */ |
| 7070 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, |
| 7071 | struct io_kiocb *req, |
| 7072 | unsigned int sqe_flags) |
| 7073 | { |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7074 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
| 7075 | return false; |
| 7076 | |
| 7077 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != |
| 7078 | ctx->restrictions.sqe_flags_required) |
| 7079 | return false; |
| 7080 | |
| 7081 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | |
| 7082 | ctx->restrictions.sqe_flags_required)) |
| 7083 | return false; |
| 7084 | |
| 7085 | return true; |
| 7086 | } |
| 7087 | |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7088 | static void io_init_req_drain(struct io_kiocb *req) |
| 7089 | { |
| 7090 | struct io_ring_ctx *ctx = req->ctx; |
| 7091 | struct io_kiocb *head = ctx->submit_state.link.head; |
| 7092 | |
| 7093 | ctx->drain_active = true; |
| 7094 | if (head) { |
| 7095 | /* |
| 7096 | * If we need to drain a request in the middle of a link, drain |
| 7097 | * the head request and the next request/link after the current |
| 7098 | * link. Considering sequential execution of links, |
| 7099 | * IOSQE_IO_DRAIN will be maintained for every request of our |
| 7100 | * link. |
| 7101 | */ |
| 7102 | head->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
| 7103 | ctx->drain_next = true; |
| 7104 | } |
| 7105 | } |
| 7106 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7107 | 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] | 7108 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7109 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7110 | { |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7111 | unsigned int sqe_flags; |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7112 | int personality; |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7113 | u8 opcode; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7114 | |
Pavel Begunkov | 864ea92 | 2021-08-09 13:04:08 +0100 | [diff] [blame] | 7115 | /* req is partially pre-initialised, see io_preinit_req() */ |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7116 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7117 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
| 7118 | req->flags = sqe_flags = READ_ONCE(sqe->flags); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7119 | req->user_data = READ_ONCE(sqe->user_data); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7120 | req->file = NULL; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 7121 | req->fixed_rsrc_refs = NULL; |
Pavel Begunkov | 4dd2824 | 2020-06-15 10:33:13 +0300 | [diff] [blame] | 7122 | req->task = current; |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7123 | |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7124 | if (unlikely(opcode >= IORING_OP_LAST)) { |
| 7125 | req->opcode = 0; |
Pavel Begunkov | 5be9ad1 | 2021-02-12 18:41:17 +0000 | [diff] [blame] | 7126 | return -EINVAL; |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7127 | } |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7128 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
| 7129 | /* enforce forwards compatibility on users */ |
| 7130 | if (sqe_flags & ~SQE_VALID_FLAGS) |
| 7131 | return -EINVAL; |
| 7132 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7133 | !io_op_defs[opcode].buffer_select) |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7134 | return -EOPNOTSUPP; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7135 | if (sqe_flags & IOSQE_IO_DRAIN) |
| 7136 | io_init_req_drain(req); |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 7137 | } |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7138 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { |
| 7139 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) |
| 7140 | return -EACCES; |
| 7141 | /* knock it to the slow queue path, will be drained there */ |
| 7142 | if (ctx->drain_active) |
| 7143 | req->flags |= REQ_F_FORCE_ASYNC; |
| 7144 | /* if there is no link, we're at "next" request and need to drain */ |
| 7145 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { |
| 7146 | ctx->drain_next = false; |
| 7147 | ctx->drain_active = true; |
Pavel Begunkov | 22b2ca3 | 2021-10-01 18:07:00 +0100 | [diff] [blame] | 7148 | req->flags |= IOSQE_IO_DRAIN | REQ_F_FORCE_ASYNC; |
Pavel Begunkov | 2a56a9b | 2021-09-24 21:59:57 +0100 | [diff] [blame] | 7149 | } |
| 7150 | } |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 7151 | |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7152 | if (io_op_defs[opcode].needs_file) { |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7153 | struct io_submit_state *state = &ctx->submit_state; |
| 7154 | |
| 7155 | /* |
| 7156 | * Plug now if we have more than 2 IO left after this, and the |
| 7157 | * target is potentially a read/write to block based storage. |
| 7158 | */ |
Pavel Begunkov | 4a04d1d | 2021-10-06 16:06:49 +0100 | [diff] [blame] | 7159 | if (state->need_plug && io_op_defs[opcode].plug) { |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7160 | state->plug_started = true; |
| 7161 | state->need_plug = false; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 7162 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
Pavel Begunkov | 6d63416 | 2021-10-06 16:06:46 +0100 | [diff] [blame] | 7163 | } |
| 7164 | |
Pavel Begunkov | 62906e8 | 2021-08-10 14:52:47 +0100 | [diff] [blame] | 7165 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
Pavel Begunkov | ac17705 | 2021-08-09 13:04:02 +0100 | [diff] [blame] | 7166 | (sqe_flags & IOSQE_FIXED_FILE)); |
Pavel Begunkov | ba13e23 | 2021-02-01 18:59:52 +0000 | [diff] [blame] | 7167 | if (unlikely(!req->file)) |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7168 | return -EBADF; |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7169 | } |
Pavel Begunkov | c11368a5 | 2020-05-17 14:13:42 +0300 | [diff] [blame] | 7170 | |
Pavel Begunkov | ef4ff58 | 2020-04-12 02:05:05 +0300 | [diff] [blame] | 7171 | personality = READ_ONCE(sqe->personality); |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7172 | if (personality) { |
Linus Torvalds | cdab10b | 2021-11-01 21:06:18 -0700 | [diff] [blame] | 7173 | int ret; |
| 7174 | |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7175 | req->creds = xa_load(&ctx->personalities, personality); |
| 7176 | if (!req->creds) |
Jens Axboe | 27926b6 | 2020-10-28 09:33:23 -0600 | [diff] [blame] | 7177 | return -EINVAL; |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7178 | get_cred(req->creds); |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 7179 | ret = security_uring_override_creds(req->creds); |
| 7180 | if (ret) { |
| 7181 | put_cred(req->creds); |
| 7182 | return ret; |
| 7183 | } |
Jens Axboe | 63ff822 | 2020-05-07 14:56:15 -0600 | [diff] [blame] | 7184 | req->flags |= REQ_F_CREDS; |
| 7185 | } |
Pavel Begunkov | bd5bbda | 2020-11-20 15:50:51 +0000 | [diff] [blame] | 7186 | |
Pavel Begunkov | fc0ae02 | 2021-10-01 18:07:02 +0100 | [diff] [blame] | 7187 | return io_req_prep(req, sqe); |
Pavel Begunkov | 0553b8b | 2020-04-08 08:58:45 +0300 | [diff] [blame] | 7188 | } |
| 7189 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7190 | 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] | 7191 | const struct io_uring_sqe *sqe) |
Pavel Begunkov | 282cdc8 | 2021-08-09 13:04:10 +0100 | [diff] [blame] | 7192 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7193 | { |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7194 | struct io_submit_link *link = &ctx->submit_state.link; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7195 | int ret; |
| 7196 | |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7197 | ret = io_init_req(ctx, req, sqe); |
| 7198 | if (unlikely(ret)) { |
Jens Axboe | a87acfd | 2021-09-11 16:04:50 -0600 | [diff] [blame] | 7199 | trace_io_uring_req_failed(sqe, ret); |
| 7200 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7201 | /* fail even hard links since we don't submit */ |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7202 | if (link->head) { |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7203 | /* |
| 7204 | * we can judge a link req is failed or cancelled by if |
| 7205 | * REQ_F_FAIL is set, but the head is an exception since |
| 7206 | * it may be set REQ_F_FAIL because of other req's failure |
| 7207 | * so let's leverage req->result to distinguish if a head |
| 7208 | * is set REQ_F_FAIL because of its failure or other req's |
| 7209 | * failure so that we can set the correct ret code for it. |
| 7210 | * init result here to avoid affecting the normal path. |
| 7211 | */ |
| 7212 | if (!(link->head->flags & REQ_F_FAIL)) |
| 7213 | req_fail_link_node(link->head, -ECANCELED); |
| 7214 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { |
| 7215 | /* |
| 7216 | * the current req is a normal req, we should return |
| 7217 | * error and thus break the submittion loop. |
| 7218 | */ |
| 7219 | io_req_complete_failed(req, ret); |
| 7220 | return ret; |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7221 | } |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7222 | req_fail_link_node(req, ret); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7223 | } |
Pavel Begunkov | 441b8a7 | 2021-06-14 23:37:31 +0100 | [diff] [blame] | 7224 | |
Pavel Begunkov | be7053b | 2021-02-18 18:29:45 +0000 | [diff] [blame] | 7225 | /* don't need @sqe from now on */ |
Olivier Langlois | 236daeae | 2021-05-31 02:36:37 -0400 | [diff] [blame] | 7226 | trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data, |
| 7227 | req->flags, true, |
| 7228 | ctx->flags & IORING_SETUP_SQPOLL); |
Pavel Begunkov | a6b8cadc | 2021-02-18 18:29:41 +0000 | [diff] [blame] | 7229 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7230 | /* |
| 7231 | * If we already have a head request, queue this one for async |
| 7232 | * submittal once the head completes. If we don't have a head but |
| 7233 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 7234 | * submitted sync once the chain is complete. If none of those |
| 7235 | * conditions are true (normal request), then just queue it. |
| 7236 | */ |
| 7237 | if (link->head) { |
| 7238 | struct io_kiocb *head = link->head; |
| 7239 | |
Hao Xu | a8295b9 | 2021-08-27 17:46:09 +0800 | [diff] [blame] | 7240 | if (!(req->flags & REQ_F_FAIL)) { |
| 7241 | ret = io_req_prep_async(req); |
| 7242 | if (unlikely(ret)) { |
| 7243 | req_fail_link_node(req, ret); |
| 7244 | if (!(head->flags & REQ_F_FAIL)) |
| 7245 | req_fail_link_node(head, -ECANCELED); |
| 7246 | } |
| 7247 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7248 | trace_io_uring_link(ctx, req, head); |
| 7249 | link->last->link = req; |
| 7250 | link->last = req; |
| 7251 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7252 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
| 7253 | return 0; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7254 | /* last request of a link, enqueue the link */ |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7255 | link->head = NULL; |
| 7256 | req = head; |
| 7257 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
| 7258 | link->head = req; |
| 7259 | link->last = req; |
| 7260 | return 0; |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7261 | } |
| 7262 | |
Pavel Begunkov | f15a343 | 2021-09-24 21:59:56 +0100 | [diff] [blame] | 7263 | io_queue_sqe(req); |
Jackie Liu | 4fe2c96 | 2019-09-09 20:50:40 +0800 | [diff] [blame] | 7264 | return 0; |
| 7265 | } |
| 7266 | |
| 7267 | /* |
| 7268 | * Batched submission is done, ensure local IO is flushed out. |
| 7269 | */ |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7270 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7271 | { |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7272 | struct io_submit_state *state = &ctx->submit_state; |
| 7273 | |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7274 | if (state->link.head) |
Pavel Begunkov | de59bc1 | 2021-02-18 18:29:47 +0000 | [diff] [blame] | 7275 | io_queue_sqe(state->link.head); |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7276 | /* flush only after queuing links as they can generate completions */ |
Pavel Begunkov | c450178 | 2021-09-08 16:40:52 +0100 | [diff] [blame] | 7277 | io_submit_flush_completions(ctx); |
Jackie Liu | a197f66 | 2019-11-08 08:09:12 -0700 | [diff] [blame] | 7278 | if (state->plug_started) |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7279 | blk_finish_plug(&state->plug); |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7280 | } |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7281 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7282 | /* |
| 7283 | * Start submission side cache. |
Pavel Begunkov | 32fe525 | 2019-12-17 22:26:58 +0300 | [diff] [blame] | 7284 | */ |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7285 | static void io_submit_state_start(struct io_submit_state *state, |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7286 | unsigned int max_ios) |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7287 | { |
| 7288 | state->plug_started = false; |
Pavel Begunkov | 4b628ae | 2021-09-08 16:40:49 +0100 | [diff] [blame] | 7289 | state->need_plug = max_ios > 2; |
Jens Axboe | 5ca7a8b | 2021-10-06 11:01:42 -0600 | [diff] [blame] | 7290 | state->submit_nr = max_ios; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7291 | /* set only head, no need to init link_last in advance */ |
| 7292 | state->link.head = NULL; |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7293 | } |
| 7294 | |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7295 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 7296 | { |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7297 | struct io_rings *rings = ctx->rings; |
| 7298 | |
| 7299 | /* |
Jens Axboe | 193155c | 2020-02-22 23:22:19 -0700 | [diff] [blame] | 7300 | * Ensure any loads from the SQEs are done at this point, |
Jens Axboe | 75c6a03 | 2020-01-28 10:15:23 -0700 | [diff] [blame] | 7301 | * since once we write the new head, the application could |
| 7302 | * write new data to them. |
Pavel Begunkov | 6b47ee6 | 2020-01-18 20:22:41 +0300 | [diff] [blame] | 7303 | */ |
Pavel Begunkov | 8da11c1 | 2020-02-24 11:32:44 +0300 | [diff] [blame] | 7304 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
Jens Axboe | bcda7ba | 2020-02-23 16:42:51 -0700 | [diff] [blame] | 7305 | } |
| 7306 | |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7307 | /* |
Fam Zheng | dd9ae8a | 2021-06-04 17:42:56 +0100 | [diff] [blame] | 7308 | * 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] | 7309 | * that is mapped by userspace. This means that care needs to be taken to |
| 7310 | * ensure that reads are stable, as we cannot rely on userspace always |
Jens Axboe | 78e19bb | 2019-11-06 15:21:34 -0700 | [diff] [blame] | 7311 | * being a good citizen. If members of the sqe are validated and then later |
| 7312 | * 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] | 7313 | * prevent a re-load down the line. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7314 | */ |
| 7315 | 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] | 7316 | { |
Pavel Begunkov | ea5ab3b | 2021-05-16 22:58:09 +0100 | [diff] [blame] | 7317 | unsigned head, mask = ctx->sq_entries - 1; |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7318 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7319 | |
| 7320 | /* |
| 7321 | * The cached sq head (or cq tail) serves two purposes: |
| 7322 | * |
| 7323 | * 1) allows us to batch the cost of updating the user visible |
Pavel Begunkov | 9d76377 | 2019-12-17 02:22:07 +0300 | [diff] [blame] | 7324 | * head updates. |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7325 | * 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] | 7326 | * though the application is the one updating it. |
| 7327 | */ |
Pavel Begunkov | 17d3aeb | 2021-06-14 23:37:23 +0100 | [diff] [blame] | 7328 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
Pavel Begunkov | 8cdf219 | 2020-01-25 00:40:24 +0300 | [diff] [blame] | 7329 | if (likely(head < ctx->sq_entries)) |
| 7330 | return &ctx->sq_sqes[head]; |
| 7331 | |
| 7332 | /* drop invalid entries */ |
Pavel Begunkov | 15641e4 | 2021-06-14 23:37:24 +0100 | [diff] [blame] | 7333 | ctx->cq_extra--; |
| 7334 | WRITE_ONCE(ctx->rings->sq_dropped, |
| 7335 | READ_ONCE(ctx->rings->sq_dropped) + 1); |
Pavel Begunkov | 711be03 | 2020-01-17 03:57:59 +0300 | [diff] [blame] | 7336 | return NULL; |
| 7337 | } |
Jens Axboe | b7bb4f7 | 2019-12-15 22:13:43 -0700 | [diff] [blame] | 7338 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 7339 | 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] | 7340 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7341 | { |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7342 | unsigned int entries = io_sqring_entries(ctx); |
Pavel Begunkov | 46c4e16 | 2021-02-18 18:29:37 +0000 | [diff] [blame] | 7343 | int submitted = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7344 | |
Pavel Begunkov | 51d48da | 2021-10-04 20:02:47 +0100 | [diff] [blame] | 7345 | if (unlikely(!entries)) |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7346 | return 0; |
Pavel Begunkov | ee7d46d | 2019-12-30 21:24:45 +0300 | [diff] [blame] | 7347 | /* make sure SQ entry isn't read before tail */ |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7348 | nr = min3(nr, ctx->sq_entries, entries); |
Pavel Begunkov | 9a10867 | 2021-08-27 11:55:01 +0100 | [diff] [blame] | 7349 | io_get_task_refs(nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7350 | |
Pavel Begunkov | ba88ff1 | 2021-02-10 00:03:11 +0000 | [diff] [blame] | 7351 | io_submit_state_start(&ctx->submit_state, nr); |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7352 | do { |
Jens Axboe | 3529d8c | 2019-12-19 18:24:38 -0700 | [diff] [blame] | 7353 | const struct io_uring_sqe *sqe; |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7354 | struct io_kiocb *req; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7355 | |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7356 | if (unlikely(!io_alloc_req_refill(ctx))) { |
Pavel Begunkov | 196be95 | 2019-11-07 01:41:06 +0300 | [diff] [blame] | 7357 | if (!submitted) |
| 7358 | submitted = -EAGAIN; |
Pavel Begunkov | fb5ccc9 | 2019-10-25 12:31:30 +0300 | [diff] [blame] | 7359 | break; |
Jens Axboe | 9e645e11 | 2019-05-10 16:07:28 -0600 | [diff] [blame] | 7360 | } |
Pavel Begunkov | a33ae9c | 2021-10-04 20:02:49 +0100 | [diff] [blame] | 7361 | req = io_alloc_req(ctx); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7362 | sqe = io_get_sqe(ctx); |
| 7363 | if (unlikely(!sqe)) { |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 7364 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
Pavel Begunkov | 4fccfcb | 2021-02-12 11:55:17 +0000 | [diff] [blame] | 7365 | break; |
| 7366 | } |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7367 | /* will complete beyond this point, count as submitted */ |
| 7368 | submitted++; |
Pavel Begunkov | a1ab7b3 | 2021-02-18 18:29:42 +0000 | [diff] [blame] | 7369 | if (io_submit_sqe(ctx, req, sqe)) |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 7370 | break; |
Pavel Begunkov | 6962980 | 2021-09-24 22:00:01 +0100 | [diff] [blame] | 7371 | } while (submitted < nr); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7372 | |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7373 | if (unlikely(submitted != nr)) { |
| 7374 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 7375 | int unused = nr - ref_used; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7376 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 7377 | current->io_uring->cached_refs += unused; |
Pavel Begunkov | 9466f43 | 2020-01-25 22:34:01 +0300 | [diff] [blame] | 7378 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7379 | |
Pavel Begunkov | 553deff | 2021-09-24 21:59:55 +0100 | [diff] [blame] | 7380 | io_submit_state_end(ctx); |
Pavel Begunkov | ae9428c | 2019-11-06 00:22:14 +0300 | [diff] [blame] | 7381 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
| 7382 | io_commit_sqring(ctx); |
| 7383 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7384 | return submitted; |
| 7385 | } |
| 7386 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7387 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
| 7388 | { |
| 7389 | return READ_ONCE(sqd->state); |
| 7390 | } |
| 7391 | |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7392 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
| 7393 | { |
| 7394 | /* Tell userspace we may need a wakeup call */ |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7395 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7396 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7397 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7398 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) |
| 7402 | { |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7403 | spin_lock(&ctx->completion_lock); |
Nadav Amit | 20c0b38 | 2021-08-07 17:13:42 -0700 | [diff] [blame] | 7404 | WRITE_ONCE(ctx->rings->sq_flags, |
| 7405 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 7406 | spin_unlock(&ctx->completion_lock); |
Xiaoguang Wang | 23b3628 | 2020-07-23 20:57:24 +0800 | [diff] [blame] | 7407 | } |
| 7408 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7409 | 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] | 7410 | { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7411 | unsigned int to_submit; |
Xiaoguang Wang | bdcd3ea | 2020-02-25 22:12:08 +0800 | [diff] [blame] | 7412 | int ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7413 | |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7414 | to_submit = io_sqring_entries(ctx); |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7415 | /* if we're handling multiple rings, cap submit size for fairness */ |
Olivier Langlois | 4ce8ad9 | 2021-06-23 11:50:18 -0700 | [diff] [blame] | 7416 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
| 7417 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7418 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7419 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7420 | const struct cred *creds = NULL; |
| 7421 | |
| 7422 | if (ctx->sq_creds != current_cred()) |
| 7423 | creds = override_creds(ctx->sq_creds); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7424 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7425 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7426 | if (!wq_list_empty(&ctx->iopoll_list)) |
Pavel Begunkov | 5ba3c87 | 2021-09-24 21:59:43 +0100 | [diff] [blame] | 7427 | io_do_iopoll(ctx, true); |
Xiaoguang Wang | 906a3c6 | 2020-11-12 14:56:00 +0800 | [diff] [blame] | 7428 | |
Pavel Begunkov | 3b763ba | 2021-04-18 14:52:08 +0100 | [diff] [blame] | 7429 | /* |
| 7430 | * Don't submit if refs are dying, good for io_uring_register(), |
| 7431 | * but also it is relied upon by io_ring_exit_work() |
| 7432 | */ |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 7433 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
| 7434 | !(ctx->flags & IORING_SETUP_R_DISABLED)) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7435 | ret = io_submit_sqes(ctx, to_submit); |
| 7436 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7437 | |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7438 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
| 7439 | wake_up(&ctx->sqo_sq_wait); |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7440 | if (creds) |
| 7441 | revert_creds(creds); |
Pavel Begunkov | acfb381 | 2021-05-16 22:58:03 +0100 | [diff] [blame] | 7442 | } |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 7443 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7444 | return ret; |
| 7445 | } |
| 7446 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7447 | 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] | 7448 | { |
| 7449 | struct io_ring_ctx *ctx; |
| 7450 | unsigned sq_thread_idle = 0; |
| 7451 | |
Pavel Begunkov | c9dca27 | 2021-03-10 13:13:55 +0000 | [diff] [blame] | 7452 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7453 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7454 | sqd->sq_thread_idle = sq_thread_idle; |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7455 | } |
| 7456 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7457 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
| 7458 | { |
| 7459 | bool did_sig = false; |
| 7460 | struct ksignal ksig; |
| 7461 | |
| 7462 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || |
| 7463 | signal_pending(current)) { |
| 7464 | mutex_unlock(&sqd->lock); |
| 7465 | if (signal_pending(current)) |
| 7466 | did_sig = get_signal(&ksig); |
| 7467 | cond_resched(); |
| 7468 | mutex_lock(&sqd->lock); |
| 7469 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7470 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
| 7471 | } |
| 7472 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7473 | static int io_sq_thread(void *data) |
| 7474 | { |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7475 | struct io_sq_data *sqd = data; |
| 7476 | struct io_ring_ctx *ctx; |
Xiaoguang Wang | a0d9205 | 2020-11-12 14:55:59 +0800 | [diff] [blame] | 7477 | unsigned long timeout = 0; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7478 | char buf[TASK_COMM_LEN]; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7479 | DEFINE_WAIT(wait); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7480 | |
Pavel Begunkov | 696ee88 | 2021-04-01 09:55:04 +0100 | [diff] [blame] | 7481 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7482 | set_task_comm(current, buf); |
Jens Axboe | 28cea78a | 2020-09-14 10:51:17 -0600 | [diff] [blame] | 7483 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7484 | if (sqd->sq_cpu != -1) |
| 7485 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); |
| 7486 | else |
| 7487 | set_cpus_allowed_ptr(current, cpu_online_mask); |
| 7488 | current->flags |= PF_NO_SETAFFINITY; |
| 7489 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 7490 | audit_alloc_kernel(current); |
| 7491 | |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7492 | mutex_lock(&sqd->lock); |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7493 | while (1) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7494 | bool cap_entries, sqt_spin = false; |
Jens Axboe | c1edbf5 | 2019-11-10 16:56:04 -0700 | [diff] [blame] | 7495 | |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7496 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
| 7497 | if (io_sqd_handle_event(sqd)) |
Pavel Begunkov | c7d9561 | 2021-04-13 11:43:00 +0100 | [diff] [blame] | 7498 | break; |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7499 | timeout = jiffies + sqd->sq_thread_idle; |
| 7500 | } |
Pavel Begunkov | e4b6d90 | 2021-05-16 22:58:00 +0100 | [diff] [blame] | 7501 | |
Jens Axboe | e95eee2 | 2020-09-08 09:11:32 -0600 | [diff] [blame] | 7502 | cap_entries = !list_is_singular(&sqd->ctx_list); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7503 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | 948e194 | 2021-06-24 15:09:55 +0100 | [diff] [blame] | 7504 | int ret = __io_sq_thread(ctx, cap_entries); |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 7505 | |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7506 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7507 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7508 | } |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7509 | if (io_run_task_work()) |
| 7510 | sqt_spin = true; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7511 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7512 | if (sqt_spin || !time_after(jiffies, timeout)) { |
Jens Axboe | c8d1ba5 | 2020-09-14 11:07:26 -0600 | [diff] [blame] | 7513 | cond_resched(); |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7514 | if (sqt_spin) |
| 7515 | timeout = jiffies + sqd->sq_thread_idle; |
| 7516 | continue; |
| 7517 | } |
| 7518 | |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7519 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
Pavel Begunkov | dd432ea5 | 2021-06-26 21:40:45 +0100 | [diff] [blame] | 7520 | if (!io_sqd_events_pending(sqd) && !current->task_works) { |
Pavel Begunkov | 1a924a8 | 2021-06-24 15:09:56 +0100 | [diff] [blame] | 7521 | bool needs_sched = true; |
| 7522 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7523 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
Pavel Begunkov | aaa9f0f | 2021-05-16 22:58:01 +0100 | [diff] [blame] | 7524 | io_ring_set_wakeup_flag(ctx); |
| 7525 | |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7526 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 7527 | !wq_list_empty(&ctx->iopoll_list)) { |
Hao Xu | 724cb4f | 2021-04-21 23:19:11 +0800 | [diff] [blame] | 7528 | needs_sched = false; |
| 7529 | break; |
| 7530 | } |
| 7531 | if (io_sqring_entries(ctx)) { |
| 7532 | needs_sched = false; |
| 7533 | break; |
| 7534 | } |
| 7535 | } |
| 7536 | |
| 7537 | if (needs_sched) { |
| 7538 | mutex_unlock(&sqd->lock); |
| 7539 | schedule(); |
| 7540 | mutex_lock(&sqd->lock); |
| 7541 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 7542 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 7543 | io_ring_clear_wakeup_flag(ctx); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7544 | } |
Xiaoguang Wang | 0836924 | 2020-11-03 14:15:59 +0800 | [diff] [blame] | 7545 | |
| 7546 | finish_wait(&sqd->wait, &wait); |
| 7547 | timeout = jiffies + sqd->sq_thread_idle; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7548 | } |
| 7549 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 7550 | io_uring_cancel_generic(true, sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7551 | sqd->thread = NULL; |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7552 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 7553 | io_ring_set_wakeup_flag(ctx); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7554 | io_run_task_work(); |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 7555 | mutex_unlock(&sqd->lock); |
| 7556 | |
Paul Moore | 5bd2182 | 2021-02-16 19:46:48 -0500 | [diff] [blame] | 7557 | audit_free(current); |
| 7558 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7559 | complete(&sqd->exited); |
| 7560 | do_exit(0); |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 7561 | } |
| 7562 | |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7563 | struct io_wait_queue { |
| 7564 | struct wait_queue_entry wq; |
| 7565 | struct io_ring_ctx *ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7566 | unsigned cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7567 | unsigned nr_timeouts; |
| 7568 | }; |
| 7569 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7570 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7571 | { |
| 7572 | struct io_ring_ctx *ctx = iowq->ctx; |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7573 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7574 | |
| 7575 | /* |
Brian Gianforcaro | d195a66 | 2019-12-13 03:09:50 -0800 | [diff] [blame] | 7576 | * 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] | 7577 | * started waiting. For timeouts, we always want to return to userspace, |
| 7578 | * regardless of event count. |
| 7579 | */ |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7580 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7581 | } |
| 7582 | |
| 7583 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 7584 | int wake_flags, void *key) |
| 7585 | { |
| 7586 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 7587 | wq); |
| 7588 | |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7589 | /* |
| 7590 | * Cannot safely flush overflowed CQEs from here, ensure we wake up |
| 7591 | * the task, and the next invocation will do it. |
| 7592 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7593 | 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] | 7594 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 7595 | return -1; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7596 | } |
| 7597 | |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7598 | static int io_run_task_work_sig(void) |
| 7599 | { |
| 7600 | if (io_run_task_work()) |
| 7601 | return 1; |
| 7602 | if (!signal_pending(current)) |
| 7603 | return 0; |
Jens Axboe | 0b8cfa9 | 2021-03-21 14:16:08 -0600 | [diff] [blame] | 7604 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
Jens Axboe | 792ee0f6 | 2020-10-22 20:17:18 -0600 | [diff] [blame] | 7605 | return -ERESTARTSYS; |
Jens Axboe | af9c1a4 | 2020-09-24 13:32:18 -0600 | [diff] [blame] | 7606 | return -EINTR; |
| 7607 | } |
| 7608 | |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7609 | /* when returns >0, the caller should retry */ |
| 7610 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, |
| 7611 | struct io_wait_queue *iowq, |
| 7612 | signed long *timeout) |
| 7613 | { |
| 7614 | int ret; |
| 7615 | |
| 7616 | /* make sure we run task_work before checking for signals */ |
| 7617 | ret = io_run_task_work_sig(); |
| 7618 | if (ret || io_should_wake(iowq)) |
| 7619 | return ret; |
| 7620 | /* let the caller flush overflows, retry */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 7621 | if (test_bit(0, &ctx->check_cq_overflow)) |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7622 | return 1; |
| 7623 | |
| 7624 | *timeout = schedule_timeout(*timeout); |
| 7625 | return !*timeout ? -ETIME : 1; |
| 7626 | } |
| 7627 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7628 | /* |
| 7629 | * Wait until events become available, if we don't already have some. The |
| 7630 | * application must reap them itself, as they reside on the shared cq ring. |
| 7631 | */ |
| 7632 | 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] | 7633 | const sigset_t __user *sig, size_t sigsz, |
| 7634 | struct __kernel_timespec __user *uts) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7635 | { |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7636 | struct io_wait_queue iowq; |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7637 | struct io_rings *rings = ctx->rings; |
Pavel Begunkov | c1d5a22 | 2021-02-04 13:51:57 +0000 | [diff] [blame] | 7638 | signed long timeout = MAX_SCHEDULE_TIMEOUT; |
| 7639 | int ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7640 | |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7641 | do { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7642 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 6c50315 | 2021-01-04 20:36:36 +0000 | [diff] [blame] | 7643 | if (io_cqring_events(ctx) >= min_events) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7644 | return 0; |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 7645 | if (!io_run_task_work()) |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7646 | break; |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 7647 | } while (1); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7648 | |
Xiaoguang Wang | 44df58d | 2021-09-14 22:38:52 +0800 | [diff] [blame] | 7649 | if (uts) { |
| 7650 | struct timespec64 ts; |
| 7651 | |
| 7652 | if (get_timespec64(&ts, uts)) |
| 7653 | return -EFAULT; |
| 7654 | timeout = timespec64_to_jiffies(&ts); |
| 7655 | } |
| 7656 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7657 | if (sig) { |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7658 | #ifdef CONFIG_COMPAT |
| 7659 | if (in_compat_syscall()) |
| 7660 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7661 | sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7662 | else |
| 7663 | #endif |
Oleg Nesterov | b772434 | 2019-07-16 16:29:53 -0700 | [diff] [blame] | 7664 | ret = set_user_sigmask(sig, sigsz); |
Arnd Bergmann | 9e75ad5 | 2019-03-25 15:34:53 +0100 | [diff] [blame] | 7665 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7666 | if (ret) |
| 7667 | return ret; |
| 7668 | } |
| 7669 | |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7670 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
| 7671 | iowq.wq.private = current; |
| 7672 | INIT_LIST_HEAD(&iowq.wq.entry); |
| 7673 | iowq.ctx = ctx; |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7674 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
Jens Axboe | 5fd4617 | 2021-08-06 14:04:31 -0600 | [diff] [blame] | 7675 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
Pavel Begunkov | 90291099 | 2021-08-09 09:07:32 -0600 | [diff] [blame] | 7676 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 7677 | trace_io_uring_cqring_wait(ctx, min_events); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7678 | do { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7679 | /* if we can't even flush overflow, don't wait for more */ |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 7680 | if (!io_cqring_overflow_flush(ctx)) { |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7681 | ret = -EBUSY; |
| 7682 | break; |
| 7683 | } |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7684 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7685 | TASK_INTERRUPTIBLE); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7686 | ret = io_cqring_wait_schedule(ctx, &iowq, &timeout); |
Pavel Begunkov | 311997b | 2021-06-14 23:37:28 +0100 | [diff] [blame] | 7687 | finish_wait(&ctx->cq_wait, &iowq.wq); |
Jens Axboe | ca0a265 | 2021-03-04 17:15:48 -0700 | [diff] [blame] | 7688 | cond_resched(); |
Pavel Begunkov | eeb60b9 | 2021-02-04 13:51:58 +0000 | [diff] [blame] | 7689 | } while (ret > 0); |
Jens Axboe | bda5216 | 2019-09-24 13:47:15 -0600 | [diff] [blame] | 7690 | |
Jens Axboe | b7db41c | 2020-07-04 08:55:50 -0600 | [diff] [blame] | 7691 | restore_saved_sigmask_unless(ret == -EINTR); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7692 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 7693 | 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] | 7694 | } |
| 7695 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7696 | static void io_free_page_table(void **table, size_t size) |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7697 | { |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7698 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7699 | |
| 7700 | for (i = 0; i < nr_tables; i++) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7701 | kfree(table[i]); |
| 7702 | kfree(table); |
| 7703 | } |
| 7704 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7705 | static __cold void **io_alloc_page_table(size_t size) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7706 | { |
| 7707 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
| 7708 | size_t init_size = size; |
| 7709 | void **table; |
| 7710 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7711 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7712 | if (!table) |
| 7713 | return NULL; |
| 7714 | |
| 7715 | for (i = 0; i < nr_tables; i++) { |
Pavel Begunkov | 27f6b31 | 2021-06-15 13:20:13 +0100 | [diff] [blame] | 7716 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7717 | |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7718 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7719 | if (!table[i]) { |
| 7720 | io_free_page_table(table, init_size); |
| 7721 | return NULL; |
| 7722 | } |
| 7723 | size -= this_size; |
| 7724 | } |
| 7725 | return table; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 7726 | } |
| 7727 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 7728 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
| 7729 | { |
| 7730 | percpu_ref_exit(&ref_node->refs); |
| 7731 | kfree(ref_node); |
| 7732 | } |
| 7733 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7734 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
Pavel Begunkov | b9bd2be | 2021-08-09 09:09:47 -0600 | [diff] [blame] | 7735 | { |
| 7736 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); |
| 7737 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; |
| 7738 | unsigned long flags; |
| 7739 | bool first_add = false; |
| 7740 | |
| 7741 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); |
| 7742 | node->done = true; |
| 7743 | |
| 7744 | while (!list_empty(&ctx->rsrc_ref_list)) { |
| 7745 | node = list_first_entry(&ctx->rsrc_ref_list, |
| 7746 | struct io_rsrc_node, node); |
| 7747 | /* recycle ref nodes in order */ |
| 7748 | if (!node->done) |
| 7749 | break; |
| 7750 | list_del(&node->node); |
| 7751 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); |
| 7752 | } |
| 7753 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); |
| 7754 | |
| 7755 | if (first_add) |
| 7756 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ); |
| 7757 | } |
| 7758 | |
| 7759 | static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx) |
| 7760 | { |
| 7761 | struct io_rsrc_node *ref_node; |
| 7762 | |
| 7763 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); |
| 7764 | if (!ref_node) |
| 7765 | return NULL; |
| 7766 | |
| 7767 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, |
| 7768 | 0, GFP_KERNEL)) { |
| 7769 | kfree(ref_node); |
| 7770 | return NULL; |
| 7771 | } |
| 7772 | INIT_LIST_HEAD(&ref_node->node); |
| 7773 | INIT_LIST_HEAD(&ref_node->rsrc_list); |
| 7774 | ref_node->done = false; |
| 7775 | return ref_node; |
| 7776 | } |
| 7777 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7778 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
| 7779 | struct io_rsrc_data *data_to_kill) |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 7780 | __must_hold(&ctx->uring_lock) |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7781 | { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7782 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
| 7783 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7784 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 7785 | io_rsrc_refs_drop(ctx); |
| 7786 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7787 | if (data_to_kill) { |
| 7788 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7789 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7790 | rsrc_node->rsrc_data = data_to_kill; |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7791 | spin_lock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7792 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
Jens Axboe | 4956b9e | 2021-08-09 07:49:41 -0600 | [diff] [blame] | 7793 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
Pavel Begunkov | 82fbcfa | 2021-04-01 15:43:43 +0100 | [diff] [blame] | 7794 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7795 | atomic_inc(&data_to_kill->refs); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7796 | percpu_ref_kill(&rsrc_node->refs); |
| 7797 | ctx->rsrc_node = NULL; |
| 7798 | } |
| 7799 | |
| 7800 | if (!ctx->rsrc_node) { |
| 7801 | ctx->rsrc_node = ctx->rsrc_backup_node; |
| 7802 | ctx->rsrc_backup_node = NULL; |
| 7803 | } |
Pavel Begunkov | 1642b44 | 2020-12-30 21:34:14 +0000 | [diff] [blame] | 7804 | } |
| 7805 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7806 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7807 | { |
| 7808 | if (ctx->rsrc_backup_node) |
| 7809 | return 0; |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7810 | ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7811 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
| 7812 | } |
| 7813 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7814 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
| 7815 | struct io_ring_ctx *ctx) |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7816 | { |
| 7817 | int ret; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 7818 | |
Pavel Begunkov | 215c390 | 2021-04-01 15:43:48 +0100 | [diff] [blame] | 7819 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7820 | if (data->quiesce) |
| 7821 | return -ENXIO; |
| 7822 | |
| 7823 | data->quiesce = true; |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7824 | do { |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7825 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7826 | if (ret) |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7827 | break; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 7828 | io_rsrc_node_switch(ctx, data); |
| 7829 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7830 | /* kill initial ref, already quiesced if zero */ |
| 7831 | if (atomic_dec_and_test(&data->refs)) |
| 7832 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7833 | mutex_unlock(&ctx->uring_lock); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7834 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7835 | ret = wait_for_completion_interruptible(&data->done); |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7836 | if (!ret) { |
| 7837 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 1ffc542 | 2020-12-30 21:34:15 +0000 | [diff] [blame] | 7838 | break; |
Jens Axboe | c018db4 | 2021-08-09 08:15:50 -0600 | [diff] [blame] | 7839 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 7840 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7841 | atomic_inc(&data->refs); |
| 7842 | /* wait for all works potentially completing data->done */ |
| 7843 | flush_delayed_work(&ctx->rsrc_put_work); |
Jens Axboe | cb5e1b8 | 2021-02-25 07:37:35 -0700 | [diff] [blame] | 7844 | reinit_completion(&data->done); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 7845 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7846 | ret = io_run_task_work_sig(); |
| 7847 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | f2303b1 | 2021-02-20 18:03:49 +0000 | [diff] [blame] | 7848 | } while (ret >= 0); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7849 | data->quiesce = false; |
| 7850 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 7851 | return ret; |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7852 | } |
| 7853 | |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7854 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
| 7855 | { |
| 7856 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; |
| 7857 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; |
| 7858 | |
| 7859 | return &data->tags[table_idx][off]; |
| 7860 | } |
| 7861 | |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7862 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
| 7863 | { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7864 | size_t size = data->nr * sizeof(data->tags[0][0]); |
| 7865 | |
| 7866 | if (data->tags) |
| 7867 | io_free_page_table((void **)data->tags, size); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7868 | kfree(data); |
| 7869 | } |
| 7870 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 7871 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
| 7872 | u64 __user *utags, unsigned nr, |
| 7873 | struct io_rsrc_data **pdata) |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7874 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 7875 | struct io_rsrc_data *data; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7876 | int ret = -ENOMEM; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7877 | unsigned i; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7878 | |
| 7879 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 7880 | if (!data) |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7881 | return -ENOMEM; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7882 | 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] | 7883 | if (!data->tags) { |
| 7884 | kfree(data); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7885 | return -ENOMEM; |
| 7886 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7887 | |
| 7888 | data->nr = nr; |
| 7889 | data->ctx = ctx; |
| 7890 | data->do_put = do_put; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7891 | if (utags) { |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7892 | ret = -EFAULT; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7893 | for (i = 0; i < nr; i++) { |
Colin Ian King | fdd1dc3 | 2021-06-15 14:00:11 +0100 | [diff] [blame] | 7894 | u64 *tag_slot = io_get_tag_slot(data, i); |
| 7895 | |
| 7896 | if (copy_from_user(tag_slot, &utags[i], |
| 7897 | sizeof(*tag_slot))) |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7898 | goto fail; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7899 | } |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 7900 | } |
| 7901 | |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 7902 | atomic_set(&data->refs, 1); |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7903 | init_completion(&data->done); |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 7904 | *pdata = data; |
| 7905 | return 0; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 7906 | fail: |
| 7907 | io_rsrc_data_free(data); |
| 7908 | return ret; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7909 | } |
| 7910 | |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7911 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
| 7912 | { |
Pavel Begunkov | 0bea96f | 2021-08-20 10:36:36 +0100 | [diff] [blame] | 7913 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
| 7914 | GFP_KERNEL_ACCOUNT); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7915 | return !!table->files; |
| 7916 | } |
| 7917 | |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7918 | static void io_free_file_tables(struct io_file_table *table) |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7919 | { |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7920 | kvfree(table->files); |
Pavel Begunkov | 9123c8f | 2021-06-14 02:36:20 +0100 | [diff] [blame] | 7921 | table->files = NULL; |
| 7922 | } |
| 7923 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 7924 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7925 | { |
| 7926 | #if defined(CONFIG_UNIX) |
| 7927 | if (ctx->ring_sock) { |
| 7928 | struct sock *sock = ctx->ring_sock->sk; |
| 7929 | struct sk_buff *skb; |
| 7930 | |
| 7931 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 7932 | kfree_skb(skb); |
| 7933 | } |
| 7934 | #else |
| 7935 | int i; |
| 7936 | |
| 7937 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 7938 | struct file *file; |
| 7939 | |
| 7940 | file = io_file_from_index(ctx, i); |
| 7941 | if (file) |
| 7942 | fput(file); |
| 7943 | } |
| 7944 | #endif |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 7945 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 7946 | io_rsrc_data_free(ctx->file_data); |
Pavel Begunkov | fff4db7 | 2021-04-25 14:32:15 +0100 | [diff] [blame] | 7947 | ctx->file_data = NULL; |
| 7948 | ctx->nr_user_files = 0; |
Bijan Mottahedeh | 1ad555c | 2021-01-15 17:37:51 +0000 | [diff] [blame] | 7949 | } |
| 7950 | |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7951 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 7952 | { |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7953 | int ret; |
| 7954 | |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7955 | if (!ctx->file_data) |
Bijan Mottahedeh | d7954b2 | 2021-01-15 17:37:50 +0000 | [diff] [blame] | 7956 | return -ENXIO; |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 7957 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
| 7958 | if (!ret) |
| 7959 | __io_sqe_files_unregister(ctx); |
| 7960 | return ret; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 7961 | } |
| 7962 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7963 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7964 | __releases(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7965 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7966 | WARN_ON_ONCE(sqd->thread == current); |
| 7967 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7968 | /* |
| 7969 | * Do the dance but not conditional clear_bit() because it'd race with |
| 7970 | * other threads incrementing park_pending and setting the bit. |
| 7971 | */ |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7972 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7973 | if (atomic_dec_return(&sqd->park_pending)) |
| 7974 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7975 | mutex_unlock(&sqd->lock); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7976 | } |
| 7977 | |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7978 | static void io_sq_thread_park(struct io_sq_data *sqd) |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7979 | __acquires(&sqd->lock) |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7980 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7981 | WARN_ON_ONCE(sqd->thread == current); |
| 7982 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 7983 | atomic_inc(&sqd->park_pending); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7984 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7985 | mutex_lock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7986 | if (sqd->thread) |
Jens Axboe | 86e0d67 | 2021-03-05 08:44:39 -0700 | [diff] [blame] | 7987 | wake_up_process(sqd->thread); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 7988 | } |
| 7989 | |
| 7990 | static void io_sq_thread_stop(struct io_sq_data *sqd) |
| 7991 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7992 | WARN_ON_ONCE(sqd->thread == current); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7993 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 7994 | |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 7995 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
Pavel Begunkov | 88885f6 | 2021-04-11 01:46:38 +0100 | [diff] [blame] | 7996 | mutex_lock(&sqd->lock); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 7997 | if (sqd->thread) |
| 7998 | wake_up_process(sqd->thread); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 7999 | mutex_unlock(&sqd->lock); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8000 | wait_for_completion(&sqd->exited); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8001 | } |
| 8002 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8003 | static void io_put_sq_data(struct io_sq_data *sqd) |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8004 | { |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8005 | if (refcount_dec_and_test(&sqd->refs)) { |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8006 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
| 8007 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8008 | io_sq_thread_stop(sqd); |
| 8009 | kfree(sqd); |
| 8010 | } |
| 8011 | } |
| 8012 | |
| 8013 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) |
| 8014 | { |
| 8015 | struct io_sq_data *sqd = ctx->sq_data; |
| 8016 | |
| 8017 | if (sqd) { |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8018 | io_sq_thread_park(sqd); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 8019 | list_del_init(&ctx->sqd_list); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8020 | io_sqd_update_thread_idle(sqd); |
Jens Axboe | 05962f9 | 2021-03-06 13:58:48 -0700 | [diff] [blame] | 8021 | io_sq_thread_unpark(sqd); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8022 | |
| 8023 | io_put_sq_data(sqd); |
| 8024 | ctx->sq_data = NULL; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8025 | } |
| 8026 | } |
| 8027 | |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8028 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
| 8029 | { |
| 8030 | struct io_ring_ctx *ctx_attach; |
| 8031 | struct io_sq_data *sqd; |
| 8032 | struct fd f; |
| 8033 | |
| 8034 | f = fdget(p->wq_fd); |
| 8035 | if (!f.file) |
| 8036 | return ERR_PTR(-ENXIO); |
| 8037 | if (f.file->f_op != &io_uring_fops) { |
| 8038 | fdput(f); |
| 8039 | return ERR_PTR(-EINVAL); |
| 8040 | } |
| 8041 | |
| 8042 | ctx_attach = f.file->private_data; |
| 8043 | sqd = ctx_attach->sq_data; |
| 8044 | if (!sqd) { |
| 8045 | fdput(f); |
| 8046 | return ERR_PTR(-EINVAL); |
| 8047 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8048 | if (sqd->task_tgid != current->tgid) { |
| 8049 | fdput(f); |
| 8050 | return ERR_PTR(-EPERM); |
| 8051 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8052 | |
| 8053 | refcount_inc(&sqd->refs); |
| 8054 | fdput(f); |
| 8055 | return sqd; |
| 8056 | } |
| 8057 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8058 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
| 8059 | bool *attached) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8060 | { |
| 8061 | struct io_sq_data *sqd; |
| 8062 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8063 | *attached = false; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8064 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
| 8065 | sqd = io_attach_sq_data(p); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8066 | if (!IS_ERR(sqd)) { |
| 8067 | *attached = true; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8068 | return sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8069 | } |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8070 | /* fall through for EPERM case, setup new sqd/task */ |
| 8071 | if (PTR_ERR(sqd) != -EPERM) |
| 8072 | return sqd; |
| 8073 | } |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8074 | |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8075 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
| 8076 | if (!sqd) |
| 8077 | return ERR_PTR(-ENOMEM); |
| 8078 | |
Pavel Begunkov | 9e138a4 | 2021-03-14 20:57:12 +0000 | [diff] [blame] | 8079 | atomic_set(&sqd->park_pending, 0); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8080 | refcount_set(&sqd->refs, 1); |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8081 | INIT_LIST_HEAD(&sqd->ctx_list); |
Pavel Begunkov | 09a6f4e | 2021-03-14 20:57:10 +0000 | [diff] [blame] | 8082 | mutex_init(&sqd->lock); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8083 | init_waitqueue_head(&sqd->wait); |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8084 | init_completion(&sqd->exited); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8085 | return sqd; |
| 8086 | } |
| 8087 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8088 | #if defined(CONFIG_UNIX) |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8089 | /* |
| 8090 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 8091 | * the io_uring can be safely unregistered on process exit, even if we have |
| 8092 | * loops in the file referencing. |
| 8093 | */ |
| 8094 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 8095 | { |
| 8096 | struct sock *sk = ctx->ring_sock->sk; |
| 8097 | struct scm_fp_list *fpl; |
| 8098 | struct sk_buff *skb; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8099 | int i, nr_files; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8100 | |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8101 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 8102 | if (!fpl) |
| 8103 | return -ENOMEM; |
| 8104 | |
| 8105 | skb = alloc_skb(0, GFP_KERNEL); |
| 8106 | if (!skb) { |
| 8107 | kfree(fpl); |
| 8108 | return -ENOMEM; |
| 8109 | } |
| 8110 | |
| 8111 | skb->sk = sk; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8112 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8113 | nr_files = 0; |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8114 | fpl->user = get_uid(current_user()); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8115 | for (i = 0; i < nr; i++) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8116 | struct file *file = io_file_from_index(ctx, i + offset); |
| 8117 | |
| 8118 | if (!file) |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8119 | continue; |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8120 | fpl->fp[nr_files] = get_file(file); |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8121 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
| 8122 | nr_files++; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8123 | } |
| 8124 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8125 | if (nr_files) { |
| 8126 | fpl->max = SCM_MAX_FD; |
| 8127 | fpl->count = nr_files; |
| 8128 | UNIXCB(skb).fp = fpl; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8129 | skb->destructor = unix_destruct_scm; |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8130 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 8131 | skb_queue_head(&sk->sk_receive_queue, skb); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8132 | |
Jens Axboe | 08a4517 | 2019-10-03 08:11:03 -0600 | [diff] [blame] | 8133 | for (i = 0; i < nr_files; i++) |
| 8134 | fput(fpl->fp[i]); |
| 8135 | } else { |
| 8136 | kfree_skb(skb); |
| 8137 | kfree(fpl); |
| 8138 | } |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8139 | |
| 8140 | return 0; |
| 8141 | } |
| 8142 | |
| 8143 | /* |
| 8144 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 8145 | * causes regular reference counting to break down. We rely on the UNIX |
| 8146 | * garbage collection to take care of this problem for us. |
| 8147 | */ |
| 8148 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8149 | { |
| 8150 | unsigned left, total; |
| 8151 | int ret = 0; |
| 8152 | |
| 8153 | total = 0; |
| 8154 | left = ctx->nr_user_files; |
| 8155 | while (left) { |
| 8156 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8157 | |
| 8158 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 8159 | if (ret) |
| 8160 | break; |
| 8161 | left -= this_files; |
| 8162 | total += this_files; |
| 8163 | } |
| 8164 | |
| 8165 | if (!ret) |
| 8166 | return 0; |
| 8167 | |
| 8168 | while (total < ctx->nr_user_files) { |
Jens Axboe | 65e19f5 | 2019-10-26 07:20:21 -0600 | [diff] [blame] | 8169 | struct file *file = io_file_from_index(ctx, total); |
| 8170 | |
| 8171 | if (file) |
| 8172 | fput(file); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 8173 | total++; |
| 8174 | } |
| 8175 | |
| 8176 | return ret; |
| 8177 | } |
| 8178 | #else |
| 8179 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 8180 | { |
| 8181 | return 0; |
| 8182 | } |
| 8183 | #endif |
| 8184 | |
Pavel Begunkov | 47e9039 | 2021-04-01 15:43:56 +0100 | [diff] [blame] | 8185 | 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] | 8186 | { |
Bijan Mottahedeh | 5023853 | 2021-01-15 17:37:45 +0000 | [diff] [blame] | 8187 | struct file *file = prsrc->file; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8188 | #if defined(CONFIG_UNIX) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8189 | struct sock *sock = ctx->ring_sock->sk; |
| 8190 | struct sk_buff_head list, *head = &sock->sk_receive_queue; |
| 8191 | struct sk_buff *skb; |
| 8192 | int i; |
| 8193 | |
| 8194 | __skb_queue_head_init(&list); |
| 8195 | |
| 8196 | /* |
| 8197 | * Find the skb that holds this file in its SCM_RIGHTS. When found, |
| 8198 | * remove this entry and rearrange the file array. |
| 8199 | */ |
| 8200 | skb = skb_dequeue(head); |
| 8201 | while (skb) { |
| 8202 | struct scm_fp_list *fp; |
| 8203 | |
| 8204 | fp = UNIXCB(skb).fp; |
| 8205 | for (i = 0; i < fp->count; i++) { |
| 8206 | int left; |
| 8207 | |
| 8208 | if (fp->fp[i] != file) |
| 8209 | continue; |
| 8210 | |
| 8211 | unix_notinflight(fp->user, fp->fp[i]); |
| 8212 | left = fp->count - 1 - i; |
| 8213 | if (left) { |
| 8214 | memmove(&fp->fp[i], &fp->fp[i + 1], |
| 8215 | left * sizeof(struct file *)); |
| 8216 | } |
| 8217 | fp->count--; |
| 8218 | if (!fp->count) { |
| 8219 | kfree_skb(skb); |
| 8220 | skb = NULL; |
| 8221 | } else { |
| 8222 | __skb_queue_tail(&list, skb); |
| 8223 | } |
| 8224 | fput(file); |
| 8225 | file = NULL; |
| 8226 | break; |
| 8227 | } |
| 8228 | |
| 8229 | if (!file) |
| 8230 | break; |
| 8231 | |
| 8232 | __skb_queue_tail(&list, skb); |
| 8233 | |
| 8234 | skb = skb_dequeue(head); |
| 8235 | } |
| 8236 | |
| 8237 | if (skb_peek(&list)) { |
| 8238 | spin_lock_irq(&head->lock); |
| 8239 | while ((skb = __skb_dequeue(&list)) != NULL) |
| 8240 | __skb_queue_tail(head, skb); |
| 8241 | spin_unlock_irq(&head->lock); |
| 8242 | } |
| 8243 | #else |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8244 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8245 | #endif |
| 8246 | } |
| 8247 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8248 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8249 | { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8250 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8251 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
| 8252 | struct io_rsrc_put *prsrc, *tmp; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8253 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8254 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
| 8255 | list_del(&prsrc->list); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8256 | |
| 8257 | if (prsrc->tag) { |
| 8258 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8259 | |
| 8260 | io_ring_submit_lock(ctx, lock_ring); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8261 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8262 | io_cqring_fill_event(ctx, prsrc->tag, 0, 0); |
Pavel Begunkov | 2840f71 | 2021-04-27 16:13:51 +0100 | [diff] [blame] | 8263 | ctx->cq_extra++; |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8264 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 8265 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8266 | io_cqring_ev_posted(ctx); |
| 8267 | io_ring_submit_unlock(ctx, lock_ring); |
| 8268 | } |
| 8269 | |
Pavel Begunkov | 40ae0ff | 2021-04-01 15:43:44 +0100 | [diff] [blame] | 8270 | rsrc_data->do_put(ctx, prsrc); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8271 | kfree(prsrc); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8272 | } |
| 8273 | |
Pavel Begunkov | 28a9fe2 | 2021-04-01 15:43:47 +0100 | [diff] [blame] | 8274 | io_rsrc_node_destroy(ref_node); |
Pavel Begunkov | 3e94249 | 2021-04-11 01:46:34 +0100 | [diff] [blame] | 8275 | if (atomic_dec_and_test(&rsrc_data->refs)) |
| 8276 | complete(&rsrc_data->done); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8277 | } |
| 8278 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8279 | static void io_rsrc_put_work(struct work_struct *work) |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8280 | { |
| 8281 | struct io_ring_ctx *ctx; |
| 8282 | struct llist_node *node; |
| 8283 | |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8284 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
| 8285 | node = llist_del_all(&ctx->rsrc_put_llist); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8286 | |
| 8287 | while (node) { |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8288 | struct io_rsrc_node *ref_node; |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8289 | struct llist_node *next = node->next; |
| 8290 | |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8291 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
Bijan Mottahedeh | 269bbe5 | 2021-01-15 17:37:44 +0000 | [diff] [blame] | 8292 | __io_rsrc_put_work(ref_node); |
Jens Axboe | 4a38aed2 | 2020-05-14 17:21:15 -0600 | [diff] [blame] | 8293 | node = next; |
| 8294 | } |
| 8295 | } |
| 8296 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8297 | 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] | 8298 | unsigned nr_args, u64 __user *tags) |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8299 | { |
| 8300 | __s32 __user *fds = (__s32 __user *) arg; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8301 | struct file *file; |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8302 | int fd, ret; |
Pavel Begunkov | 846a4ef | 2021-04-01 15:44:03 +0100 | [diff] [blame] | 8303 | unsigned i; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8304 | |
| 8305 | if (ctx->file_data) |
| 8306 | return -EBUSY; |
| 8307 | if (!nr_args) |
| 8308 | return -EINVAL; |
| 8309 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 8310 | return -EMFILE; |
Pavel Begunkov | 3a1b8a4 | 2021-08-20 10:36:35 +0100 | [diff] [blame] | 8311 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
| 8312 | return -EMFILE; |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8313 | ret = io_rsrc_node_switch_start(ctx); |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8314 | if (ret) |
| 8315 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8316 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
| 8317 | &ctx->file_data); |
| 8318 | if (ret) |
| 8319 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8320 | |
Pavel Begunkov | f3baed3 | 2021-04-01 15:43:42 +0100 | [diff] [blame] | 8321 | ret = -ENOMEM; |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8322 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8323 | goto out_free; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8324 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8325 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 8326 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8327 | ret = -EFAULT; |
| 8328 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8329 | } |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8330 | /* allow sparse sets */ |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8331 | if (fd == -1) { |
| 8332 | ret = -EINVAL; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8333 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8334 | goto out_fput; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8335 | continue; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8336 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8337 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8338 | file = fget(fd); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8339 | ret = -EBADF; |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 8340 | if (unlikely(!file)) |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8341 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8342 | |
| 8343 | /* |
| 8344 | * Don't allow io_uring instances to be registered. If UNIX |
| 8345 | * isn't enabled, then this causes a reference cycle and this |
| 8346 | * instance can never get freed. If UNIX is enabled we'll |
| 8347 | * handle it just fine, but there's still no point in allowing |
| 8348 | * a ring fd as it doesn't support regular read/write anyway. |
| 8349 | */ |
| 8350 | if (file->f_op == &io_uring_fops) { |
| 8351 | fput(file); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8352 | goto out_fput; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8353 | } |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8354 | 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] | 8355 | } |
| 8356 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8357 | ret = io_sqe_files_scm(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8358 | if (ret) { |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 8359 | __io_sqe_files_unregister(ctx); |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8360 | return ret; |
| 8361 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8362 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8363 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8364 | return ret; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8365 | out_fput: |
| 8366 | for (i = 0; i < ctx->nr_user_files; i++) { |
| 8367 | file = io_file_from_index(ctx, i); |
| 8368 | if (file) |
| 8369 | fput(file); |
| 8370 | } |
Pavel Begunkov | 042b0d8 | 2021-08-09 13:04:01 +0100 | [diff] [blame] | 8371 | io_free_file_tables(&ctx->file_table); |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8372 | ctx->nr_user_files = 0; |
Pavel Begunkov | 600cf3f | 2020-10-10 18:34:15 +0100 | [diff] [blame] | 8373 | out_free: |
Pavel Begunkov | 44b31f2 | 2021-04-25 14:32:16 +0100 | [diff] [blame] | 8374 | io_rsrc_data_free(ctx->file_data); |
Jens Axboe | 55cbc25 | 2020-10-14 07:35:57 -0600 | [diff] [blame] | 8375 | ctx->file_data = NULL; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8376 | return ret; |
| 8377 | } |
| 8378 | |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8379 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
| 8380 | int index) |
| 8381 | { |
| 8382 | #if defined(CONFIG_UNIX) |
| 8383 | struct sock *sock = ctx->ring_sock->sk; |
| 8384 | struct sk_buff_head *head = &sock->sk_receive_queue; |
| 8385 | struct sk_buff *skb; |
| 8386 | |
| 8387 | /* |
| 8388 | * See if we can merge this file into an existing skb SCM_RIGHTS |
| 8389 | * file set. If there's no room, fall back to allocating a new skb |
| 8390 | * and filling it in. |
| 8391 | */ |
| 8392 | spin_lock_irq(&head->lock); |
| 8393 | skb = skb_peek(head); |
| 8394 | if (skb) { |
| 8395 | struct scm_fp_list *fpl = UNIXCB(skb).fp; |
| 8396 | |
| 8397 | if (fpl->count < SCM_MAX_FD) { |
| 8398 | __skb_unlink(skb, head); |
| 8399 | spin_unlock_irq(&head->lock); |
| 8400 | fpl->fp[fpl->count] = get_file(file); |
| 8401 | unix_inflight(fpl->user, fpl->fp[fpl->count]); |
| 8402 | fpl->count++; |
| 8403 | spin_lock_irq(&head->lock); |
| 8404 | __skb_queue_head(head, skb); |
| 8405 | } else { |
| 8406 | skb = NULL; |
| 8407 | } |
| 8408 | } |
| 8409 | spin_unlock_irq(&head->lock); |
| 8410 | |
| 8411 | if (skb) { |
| 8412 | fput(file); |
| 8413 | return 0; |
| 8414 | } |
| 8415 | |
| 8416 | return __io_sqe_files_scm(ctx, 1, index); |
| 8417 | #else |
| 8418 | return 0; |
| 8419 | #endif |
| 8420 | } |
| 8421 | |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8422 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
| 8423 | struct io_rsrc_node *node, void *rsrc) |
| 8424 | { |
| 8425 | struct io_rsrc_put *prsrc; |
| 8426 | |
| 8427 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); |
| 8428 | if (!prsrc) |
| 8429 | return -ENOMEM; |
| 8430 | |
| 8431 | prsrc->tag = *io_get_tag_slot(data, idx); |
| 8432 | prsrc->rsrc = rsrc; |
| 8433 | list_add(&prsrc->list, &node->rsrc_list); |
| 8434 | return 0; |
| 8435 | } |
| 8436 | |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8437 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
| 8438 | unsigned int issue_flags, u32 slot_index) |
| 8439 | { |
| 8440 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8441 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8442 | bool needs_switch = false; |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8443 | struct io_fixed_file *file_slot; |
| 8444 | int ret = -EBADF; |
| 8445 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8446 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8447 | if (file->f_op == &io_uring_fops) |
| 8448 | goto err; |
| 8449 | ret = -ENXIO; |
| 8450 | if (!ctx->file_data) |
| 8451 | goto err; |
| 8452 | ret = -EINVAL; |
| 8453 | if (slot_index >= ctx->nr_user_files) |
| 8454 | goto err; |
| 8455 | |
| 8456 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); |
| 8457 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8458 | |
| 8459 | if (file_slot->file_ptr) { |
| 8460 | struct file *old_file; |
| 8461 | |
| 8462 | ret = io_rsrc_node_switch_start(ctx); |
| 8463 | if (ret) |
| 8464 | goto err; |
| 8465 | |
| 8466 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8467 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, |
| 8468 | ctx->rsrc_node, old_file); |
| 8469 | if (ret) |
| 8470 | goto err; |
| 8471 | file_slot->file_ptr = 0; |
| 8472 | needs_switch = true; |
| 8473 | } |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8474 | |
| 8475 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; |
| 8476 | io_fixed_file_set(file_slot, file); |
| 8477 | ret = io_sqe_file_register(ctx, file, slot_index); |
| 8478 | if (ret) { |
| 8479 | file_slot->file_ptr = 0; |
| 8480 | goto err; |
| 8481 | } |
| 8482 | |
| 8483 | ret = 0; |
| 8484 | err: |
Pavel Begunkov | 9c7b0ba | 2021-09-14 16:12:52 +0100 | [diff] [blame] | 8485 | if (needs_switch) |
| 8486 | io_rsrc_node_switch(ctx, ctx->file_data); |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8487 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 8488 | if (ret) |
| 8489 | fput(file); |
| 8490 | return ret; |
| 8491 | } |
| 8492 | |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8493 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
| 8494 | { |
| 8495 | unsigned int offset = req->close.file_slot - 1; |
| 8496 | struct io_ring_ctx *ctx = req->ctx; |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8497 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8498 | struct io_fixed_file *file_slot; |
| 8499 | struct file *file; |
| 8500 | int ret, i; |
| 8501 | |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8502 | io_ring_submit_lock(ctx, needs_lock); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8503 | ret = -ENXIO; |
| 8504 | if (unlikely(!ctx->file_data)) |
| 8505 | goto out; |
| 8506 | ret = -EINVAL; |
| 8507 | if (offset >= ctx->nr_user_files) |
| 8508 | goto out; |
| 8509 | ret = io_rsrc_node_switch_start(ctx); |
| 8510 | if (ret) |
| 8511 | goto out; |
| 8512 | |
| 8513 | i = array_index_nospec(offset, ctx->nr_user_files); |
| 8514 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
| 8515 | ret = -EBADF; |
| 8516 | if (!file_slot->file_ptr) |
| 8517 | goto out; |
| 8518 | |
| 8519 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
| 8520 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); |
| 8521 | if (ret) |
| 8522 | goto out; |
| 8523 | |
| 8524 | file_slot->file_ptr = 0; |
| 8525 | io_rsrc_node_switch(ctx, ctx->file_data); |
| 8526 | ret = 0; |
| 8527 | out: |
Hao Xu | 3b44b37 | 2021-10-18 21:34:31 +0800 | [diff] [blame] | 8528 | io_ring_submit_unlock(ctx, needs_lock); |
Pavel Begunkov | 7df778b | 2021-09-24 20:04:29 +0100 | [diff] [blame] | 8529 | return ret; |
| 8530 | } |
| 8531 | |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8532 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8533 | struct io_uring_rsrc_update2 *up, |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8534 | unsigned nr_args) |
| 8535 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8536 | u64 __user *tags = u64_to_user_ptr(up->tags); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8537 | __s32 __user *fds = u64_to_user_ptr(up->data); |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 8538 | struct io_rsrc_data *data = ctx->file_data; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8539 | struct io_fixed_file *file_slot; |
| 8540 | struct file *file; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8541 | int fd, i, err = 0; |
| 8542 | unsigned int done; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8543 | bool needs_switch = false; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8544 | |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 8545 | if (!ctx->file_data) |
| 8546 | return -ENXIO; |
| 8547 | if (up->offset + nr_args > ctx->nr_user_files) |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8548 | return -EINVAL; |
| 8549 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8550 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8551 | u64 tag = 0; |
| 8552 | |
| 8553 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || |
| 8554 | copy_from_user(&fd, &fds[done], sizeof(fd))) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8555 | err = -EFAULT; |
| 8556 | break; |
| 8557 | } |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 8558 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
| 8559 | err = -EINVAL; |
| 8560 | break; |
| 8561 | } |
noah | 4e0377a | 2021-01-26 15:23:28 -0500 | [diff] [blame] | 8562 | if (fd == IORING_REGISTER_FILES_SKIP) |
| 8563 | continue; |
| 8564 | |
Pavel Begunkov | 67973b9 | 2021-01-26 13:51:09 +0000 | [diff] [blame] | 8565 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
Pavel Begunkov | aeca241 | 2021-04-11 01:46:37 +0100 | [diff] [blame] | 8566 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
Pavel Begunkov | ea64ec02 | 2021-02-04 13:52:07 +0000 | [diff] [blame] | 8567 | |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8568 | if (file_slot->file_ptr) { |
| 8569 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); |
Pavel Begunkov | b60c8dc | 2021-04-25 14:32:18 +0100 | [diff] [blame] | 8570 | err = io_queue_rsrc_removal(data, up->offset + done, |
| 8571 | ctx->rsrc_node, file); |
Hillf Danton | a5318d3 | 2020-03-23 17:47:15 +0800 | [diff] [blame] | 8572 | if (err) |
| 8573 | break; |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8574 | file_slot->file_ptr = 0; |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8575 | needs_switch = true; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8576 | } |
| 8577 | if (fd != -1) { |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8578 | file = fget(fd); |
| 8579 | if (!file) { |
| 8580 | err = -EBADF; |
| 8581 | break; |
| 8582 | } |
| 8583 | /* |
| 8584 | * Don't allow io_uring instances to be registered. If |
| 8585 | * UNIX isn't enabled, then this causes a reference |
| 8586 | * cycle and this instance can never get freed. If UNIX |
| 8587 | * is enabled we'll handle it just fine, but there's |
| 8588 | * still no point in allowing a ring fd as it doesn't |
| 8589 | * support regular read/write anyway. |
| 8590 | */ |
| 8591 | if (file->f_op == &io_uring_fops) { |
| 8592 | fput(file); |
| 8593 | err = -EBADF; |
| 8594 | break; |
| 8595 | } |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 8596 | *io_get_tag_slot(data, up->offset + done) = tag; |
Pavel Begunkov | 9a321c9 | 2021-04-01 15:44:01 +0100 | [diff] [blame] | 8597 | io_fixed_file_set(file_slot, file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8598 | err = io_sqe_file_register(ctx, file, i); |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8599 | if (err) { |
Pavel Begunkov | a04b0ac | 2021-04-01 15:44:04 +0100 | [diff] [blame] | 8600 | file_slot->file_ptr = 0; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8601 | fput(file); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8602 | break; |
Yang Yingliang | f3bd9da | 2020-07-09 10:11:41 +0000 | [diff] [blame] | 8603 | } |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8604 | } |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 8605 | } |
| 8606 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 8607 | if (needs_switch) |
| 8608 | io_rsrc_node_switch(ctx, data); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 8609 | return done ? done : err; |
| 8610 | } |
Xiaoguang Wang | 0558955 | 2020-03-31 14:05:18 +0800 | [diff] [blame] | 8611 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8612 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
| 8613 | struct task_struct *task) |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8614 | { |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8615 | struct io_wq_hash *hash; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8616 | struct io_wq_data data; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8617 | unsigned int concurrency; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8618 | |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8619 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8620 | hash = ctx->hash_map; |
| 8621 | if (!hash) { |
| 8622 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8623 | if (!hash) { |
| 8624 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8625 | return ERR_PTR(-ENOMEM); |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8626 | } |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8627 | refcount_set(&hash->refs, 1); |
| 8628 | init_waitqueue_head(&hash->wait); |
| 8629 | ctx->hash_map = hash; |
| 8630 | } |
Yang Yingliang | 362a9e6 | 2021-07-20 16:38:05 +0800 | [diff] [blame] | 8631 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 8632 | |
| 8633 | data.hash = hash; |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8634 | data.task = task; |
Pavel Begunkov | ebc11b6 | 2021-08-09 13:04:05 +0100 | [diff] [blame] | 8635 | data.free_work = io_wq_free_work; |
Pavel Begunkov | f5fa38c | 2020-06-08 21:08:20 +0300 | [diff] [blame] | 8636 | data.do_work = io_wq_submit_work; |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8637 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8638 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
| 8639 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8640 | |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8641 | return io_wq_create(concurrency, &data); |
Pavel Begunkov | 24369c2 | 2020-01-28 03:15:48 +0300 | [diff] [blame] | 8642 | } |
| 8643 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8644 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
| 8645 | struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8646 | { |
| 8647 | struct io_uring_task *tctx; |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8648 | int ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8649 | |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8650 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8651 | if (unlikely(!tctx)) |
| 8652 | return -ENOMEM; |
| 8653 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8654 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
| 8655 | if (unlikely(ret)) { |
| 8656 | kfree(tctx); |
| 8657 | return ret; |
| 8658 | } |
| 8659 | |
Jens Axboe | 685fe7f | 2021-03-08 09:37:51 -0700 | [diff] [blame] | 8660 | tctx->io_wq = io_init_wq_offload(ctx, task); |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8661 | if (IS_ERR(tctx->io_wq)) { |
| 8662 | ret = PTR_ERR(tctx->io_wq); |
| 8663 | percpu_counter_destroy(&tctx->inflight); |
| 8664 | kfree(tctx); |
| 8665 | return ret; |
| 8666 | } |
| 8667 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8668 | xa_init(&tctx->xa); |
| 8669 | init_waitqueue_head(&tctx->wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 8670 | atomic_set(&tctx->in_idle, 0); |
Pavel Begunkov | b303fe2 | 2021-04-11 01:46:26 +0100 | [diff] [blame] | 8671 | atomic_set(&tctx->inflight_tracked, 0); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8672 | task->io_uring = tctx; |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8673 | spin_lock_init(&tctx->task_lock); |
| 8674 | INIT_WQ_LIST(&tctx->task_list); |
Jens Axboe | 7cbf172 | 2021-02-10 00:03:20 +0000 | [diff] [blame] | 8675 | init_task_work(&tctx->task_work, tctx_task_work); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8676 | return 0; |
| 8677 | } |
| 8678 | |
| 8679 | void __io_uring_free(struct task_struct *tsk) |
| 8680 | { |
| 8681 | struct io_uring_task *tctx = tsk->io_uring; |
| 8682 | |
| 8683 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8684 | WARN_ON_ONCE(tctx->io_wq); |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 8685 | WARN_ON_ONCE(tctx->cached_refs); |
Pavel Begunkov | ef8eaa4 | 2021-02-27 11:16:45 +0000 | [diff] [blame] | 8686 | |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 8687 | percpu_counter_destroy(&tctx->inflight); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8688 | kfree(tctx); |
| 8689 | tsk->io_uring = NULL; |
| 8690 | } |
| 8691 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 8692 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
| 8693 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8694 | { |
| 8695 | int ret; |
| 8696 | |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8697 | /* Retain compatibility with failing for an invalid attach attempt */ |
| 8698 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == |
| 8699 | IORING_SETUP_ATTACH_WQ) { |
| 8700 | struct fd f; |
| 8701 | |
| 8702 | f = fdget(p->wq_fd); |
| 8703 | if (!f.file) |
| 8704 | return -ENXIO; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8705 | if (f.file->f_op != &io_uring_fops) { |
| 8706 | fdput(f); |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8707 | return -EINVAL; |
Jens Axboe | 0cc936f | 2021-07-22 17:08:07 -0600 | [diff] [blame] | 8708 | } |
| 8709 | fdput(f); |
Jens Axboe | d25e3a3 | 2021-02-16 11:41:41 -0700 | [diff] [blame] | 8710 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8711 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8712 | struct task_struct *tsk; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8713 | struct io_sq_data *sqd; |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8714 | bool attached; |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8715 | |
Paul Moore | cdc1404 | 2021-02-01 19:56:49 -0500 | [diff] [blame] | 8716 | ret = security_uring_sqpoll(); |
| 8717 | if (ret) |
| 8718 | return ret; |
| 8719 | |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8720 | sqd = io_get_sq_data(p, &attached); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8721 | if (IS_ERR(sqd)) { |
| 8722 | ret = PTR_ERR(sqd); |
| 8723 | goto err; |
| 8724 | } |
Jens Axboe | 69fb213 | 2020-09-14 11:16:23 -0600 | [diff] [blame] | 8725 | |
Stefan Metzmacher | 7c30f36a | 2021-03-07 11:54:28 +0100 | [diff] [blame] | 8726 | ctx->sq_creds = get_current_cred(); |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 8727 | ctx->sq_data = sqd; |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8728 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 8729 | if (!ctx->sq_thread_idle) |
| 8730 | ctx->sq_thread_idle = HZ; |
| 8731 | |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8732 | io_sq_thread_park(sqd); |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8733 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
| 8734 | io_sqd_update_thread_idle(sqd); |
Pavel Begunkov | 26984fb | 2021-03-11 23:29:37 +0000 | [diff] [blame] | 8735 | /* don't attach to a dying SQPOLL thread, would be racy */ |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8736 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
Pavel Begunkov | 78d7f6b | 2021-03-10 13:13:53 +0000 | [diff] [blame] | 8737 | io_sq_thread_unpark(sqd); |
| 8738 | |
Pavel Begunkov | de75a3d | 2021-03-18 11:54:35 +0000 | [diff] [blame] | 8739 | if (ret < 0) |
| 8740 | goto err; |
| 8741 | if (attached) |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 8742 | return 0; |
Jens Axboe | aa06165 | 2020-09-02 14:50:27 -0600 | [diff] [blame] | 8743 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8744 | if (p->flags & IORING_SETUP_SQ_AFF) { |
Jens Axboe | 44a9bd1 | 2019-05-14 20:00:30 -0600 | [diff] [blame] | 8745 | int cpu = p->sq_thread_cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8746 | |
Jens Axboe | 917257d | 2019-04-13 09:28:55 -0600 | [diff] [blame] | 8747 | ret = -EINVAL; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8748 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8749 | goto err_sqpoll; |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8750 | sqd->sq_cpu = cpu; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8751 | } else { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8752 | sqd->sq_cpu = -1; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8753 | } |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8754 | |
| 8755 | sqd->task_pid = current->pid; |
Jens Axboe | 5c2469e | 2021-03-11 10:17:56 -0700 | [diff] [blame] | 8756 | sqd->task_tgid = current->tgid; |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8757 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
| 8758 | if (IS_ERR(tsk)) { |
| 8759 | ret = PTR_ERR(tsk); |
Jens Axboe | e8f98f24 | 2021-03-09 16:32:13 -0700 | [diff] [blame] | 8760 | goto err_sqpoll; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8761 | } |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8762 | |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8763 | sqd->thread = tsk; |
Pavel Begunkov | 97a73a0 | 2021-03-08 17:30:54 +0000 | [diff] [blame] | 8764 | ret = io_uring_alloc_task_context(tsk, ctx); |
Jens Axboe | 46fe18b | 2021-03-04 12:39:36 -0700 | [diff] [blame] | 8765 | wake_up_new_task(tsk); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 8766 | if (ret) |
| 8767 | goto err; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 8768 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 8769 | /* Can't have SQ_AFF without SQPOLL */ |
| 8770 | ret = -EINVAL; |
| 8771 | goto err; |
| 8772 | } |
| 8773 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8774 | return 0; |
Pavel Begunkov | f2a48dd | 2021-04-20 12:03:33 +0100 | [diff] [blame] | 8775 | err_sqpoll: |
| 8776 | complete(&ctx->sq_data->exited); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8777 | err: |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 8778 | io_sq_thread_finish(ctx); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8779 | return ret; |
| 8780 | } |
| 8781 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8782 | static inline void __io_unaccount_mem(struct user_struct *user, |
| 8783 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8784 | { |
| 8785 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 8786 | } |
| 8787 | |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8788 | static inline int __io_account_mem(struct user_struct *user, |
| 8789 | unsigned long nr_pages) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8790 | { |
| 8791 | unsigned long page_limit, cur_pages, new_pages; |
| 8792 | |
| 8793 | /* Don't allow more pages than we can safely lock */ |
| 8794 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 8795 | |
| 8796 | do { |
| 8797 | cur_pages = atomic_long_read(&user->locked_vm); |
| 8798 | new_pages = cur_pages + nr_pages; |
| 8799 | if (new_pages > page_limit) |
| 8800 | return -ENOMEM; |
| 8801 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 8802 | new_pages) != cur_pages); |
| 8803 | |
| 8804 | return 0; |
| 8805 | } |
| 8806 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8807 | 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] | 8808 | { |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8809 | if (ctx->user) |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8810 | __io_unaccount_mem(ctx->user, nr_pages); |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8811 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8812 | if (ctx->mm_account) |
| 8813 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8814 | } |
| 8815 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8816 | 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] | 8817 | { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8818 | int ret; |
| 8819 | |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 8820 | if (ctx->user) { |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 8821 | ret = __io_account_mem(ctx->user, nr_pages); |
| 8822 | if (ret) |
| 8823 | return ret; |
| 8824 | } |
| 8825 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8826 | if (ctx->mm_account) |
| 8827 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); |
Bijan Mottahedeh | a087e2b | 2020-06-16 16:36:07 -0700 | [diff] [blame] | 8828 | |
| 8829 | return 0; |
| 8830 | } |
| 8831 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8832 | static void io_mem_free(void *ptr) |
| 8833 | { |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8834 | struct page *page; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8835 | |
Mark Rutland | 52e04ef | 2019-04-30 17:30:21 +0100 | [diff] [blame] | 8836 | if (!ptr) |
| 8837 | return; |
| 8838 | |
| 8839 | page = virt_to_head_page(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8840 | if (put_page_testzero(page)) |
| 8841 | free_compound_page(page); |
| 8842 | } |
| 8843 | |
| 8844 | static void *io_mem_alloc(size_t size) |
| 8845 | { |
| 8846 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 8847 | __GFP_NORETRY | __GFP_ACCOUNT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 8848 | |
| 8849 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 8850 | } |
| 8851 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8852 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 8853 | size_t *sq_offset) |
| 8854 | { |
| 8855 | struct io_rings *rings; |
| 8856 | size_t off, sq_array_size; |
| 8857 | |
| 8858 | off = struct_size(rings, cqes, cq_entries); |
| 8859 | if (off == SIZE_MAX) |
| 8860 | return SIZE_MAX; |
| 8861 | |
| 8862 | #ifdef CONFIG_SMP |
| 8863 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 8864 | if (off == 0) |
| 8865 | return SIZE_MAX; |
| 8866 | #endif |
| 8867 | |
Dmitry Vyukov | b36200f | 2020-07-11 11:31:11 +0200 | [diff] [blame] | 8868 | if (sq_offset) |
| 8869 | *sq_offset = off; |
| 8870 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8871 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 8872 | if (sq_array_size == SIZE_MAX) |
| 8873 | return SIZE_MAX; |
| 8874 | |
| 8875 | if (check_add_overflow(off, sq_array_size, &off)) |
| 8876 | return SIZE_MAX; |
| 8877 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 8878 | return off; |
| 8879 | } |
| 8880 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8881 | 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] | 8882 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8883 | struct io_mapped_ubuf *imu = *slot; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8884 | unsigned int i; |
| 8885 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 8886 | if (imu != ctx->dummy_ubuf) { |
| 8887 | for (i = 0; i < imu->nr_bvecs; i++) |
| 8888 | unpin_user_page(imu->bvec[i].bv_page); |
| 8889 | if (imu->acct_pages) |
| 8890 | io_unaccount_mem(ctx, imu->acct_pages); |
| 8891 | kvfree(imu); |
| 8892 | } |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8893 | *slot = NULL; |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8894 | } |
| 8895 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8896 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
| 8897 | { |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 8898 | io_buffer_unmap(ctx, &prsrc->buf); |
| 8899 | prsrc->buf = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8900 | } |
| 8901 | |
| 8902 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8903 | { |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8904 | unsigned int i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8905 | |
Pavel Begunkov | 7f61a1e | 2021-04-11 01:46:35 +0100 | [diff] [blame] | 8906 | for (i = 0; i < ctx->nr_user_bufs; i++) |
| 8907 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8908 | kfree(ctx->user_bufs); |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 8909 | io_rsrc_data_free(ctx->buf_data); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8910 | ctx->user_bufs = NULL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8911 | ctx->buf_data = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8912 | ctx->nr_user_bufs = 0; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8913 | } |
| 8914 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8915 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
| 8916 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8917 | int ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8918 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8919 | if (!ctx->buf_data) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8920 | return -ENXIO; |
| 8921 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 8922 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
| 8923 | if (!ret) |
| 8924 | __io_sqe_buffers_unregister(ctx); |
| 8925 | return ret; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8926 | } |
| 8927 | |
| 8928 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 8929 | void __user *arg, unsigned index) |
| 8930 | { |
| 8931 | struct iovec __user *src; |
| 8932 | |
| 8933 | #ifdef CONFIG_COMPAT |
| 8934 | if (ctx->compat) { |
| 8935 | struct compat_iovec __user *ciovs; |
| 8936 | struct compat_iovec ciov; |
| 8937 | |
| 8938 | ciovs = (struct compat_iovec __user *) arg; |
| 8939 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 8940 | return -EFAULT; |
| 8941 | |
Jens Axboe | d55e5f5 | 2019-12-11 16:12:15 -0700 | [diff] [blame] | 8942 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 8943 | dst->iov_len = ciov.iov_len; |
| 8944 | return 0; |
| 8945 | } |
| 8946 | #endif |
| 8947 | src = (struct iovec __user *) arg; |
| 8948 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 8949 | return -EFAULT; |
| 8950 | return 0; |
| 8951 | } |
| 8952 | |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8953 | /* |
| 8954 | * Not super efficient, but this is just a registration time. And we do cache |
| 8955 | * the last compound head, so generally we'll only do a full search if we don't |
| 8956 | * match that one. |
| 8957 | * |
| 8958 | * We check if the given compound head page has already been accounted, to |
| 8959 | * avoid double accounting it. This allows us to account the full size of the |
| 8960 | * page, not just the constituent pages of a huge page. |
| 8961 | */ |
| 8962 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, |
| 8963 | int nr_pages, struct page *hpage) |
| 8964 | { |
| 8965 | int i, j; |
| 8966 | |
| 8967 | /* check current page array */ |
| 8968 | for (i = 0; i < nr_pages; i++) { |
| 8969 | if (!PageCompound(pages[i])) |
| 8970 | continue; |
| 8971 | if (compound_head(pages[i]) == hpage) |
| 8972 | return true; |
| 8973 | } |
| 8974 | |
| 8975 | /* check previously registered pages */ |
| 8976 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 8977 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8978 | |
| 8979 | for (j = 0; j < imu->nr_bvecs; j++) { |
| 8980 | if (!PageCompound(imu->bvec[j].bv_page)) |
| 8981 | continue; |
| 8982 | if (compound_head(imu->bvec[j].bv_page) == hpage) |
| 8983 | return true; |
| 8984 | } |
| 8985 | } |
| 8986 | |
| 8987 | return false; |
| 8988 | } |
| 8989 | |
| 8990 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, |
| 8991 | int nr_pages, struct io_mapped_ubuf *imu, |
| 8992 | struct page **last_hpage) |
| 8993 | { |
| 8994 | int i, ret; |
| 8995 | |
Pavel Begunkov | 216e583 | 2021-05-29 12:01:02 +0100 | [diff] [blame] | 8996 | imu->acct_pages = 0; |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 8997 | for (i = 0; i < nr_pages; i++) { |
| 8998 | if (!PageCompound(pages[i])) { |
| 8999 | imu->acct_pages++; |
| 9000 | } else { |
| 9001 | struct page *hpage; |
| 9002 | |
| 9003 | hpage = compound_head(pages[i]); |
| 9004 | if (hpage == *last_hpage) |
| 9005 | continue; |
| 9006 | *last_hpage = hpage; |
| 9007 | if (headpage_already_acct(ctx, pages, i, hpage)) |
| 9008 | continue; |
| 9009 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; |
| 9010 | } |
| 9011 | } |
| 9012 | |
| 9013 | if (!imu->acct_pages) |
| 9014 | return 0; |
| 9015 | |
Jens Axboe | 26bfa89e | 2021-02-09 20:14:12 -0700 | [diff] [blame] | 9016 | ret = io_account_mem(ctx, imu->acct_pages); |
Jens Axboe | de29393 | 2020-09-17 16:19:16 -0600 | [diff] [blame] | 9017 | if (ret) |
| 9018 | imu->acct_pages = 0; |
| 9019 | return ret; |
| 9020 | } |
| 9021 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9022 | 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] | 9023 | struct io_mapped_ubuf **pimu, |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9024 | struct page **last_hpage) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9025 | { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9026 | struct io_mapped_ubuf *imu = NULL; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9027 | struct vm_area_struct **vmas = NULL; |
| 9028 | struct page **pages = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9029 | unsigned long off, start, end, ubuf; |
| 9030 | size_t size; |
| 9031 | int ret, pret, nr_pages, i; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9032 | |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9033 | if (!iov->iov_base) { |
| 9034 | *pimu = ctx->dummy_ubuf; |
| 9035 | return 0; |
| 9036 | } |
| 9037 | |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9038 | ubuf = (unsigned long) iov->iov_base; |
| 9039 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 9040 | start = ubuf >> PAGE_SHIFT; |
| 9041 | nr_pages = end - start; |
| 9042 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9043 | *pimu = NULL; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9044 | ret = -ENOMEM; |
| 9045 | |
| 9046 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 9047 | if (!pages) |
| 9048 | goto done; |
| 9049 | |
| 9050 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), |
| 9051 | GFP_KERNEL); |
| 9052 | if (!vmas) |
| 9053 | goto done; |
| 9054 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9055 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
Pavel Begunkov | a2b4198 | 2021-04-26 00:16:31 +0100 | [diff] [blame] | 9056 | if (!imu) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9057 | goto done; |
| 9058 | |
| 9059 | ret = 0; |
| 9060 | mmap_read_lock(current->mm); |
| 9061 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, |
| 9062 | pages, vmas); |
| 9063 | if (pret == nr_pages) { |
| 9064 | /* don't support file backed memory */ |
| 9065 | for (i = 0; i < nr_pages; i++) { |
| 9066 | struct vm_area_struct *vma = vmas[i]; |
| 9067 | |
Pavel Begunkov | 40dad76 | 2021-06-09 15:26:54 +0100 | [diff] [blame] | 9068 | if (vma_is_shmem(vma)) |
| 9069 | continue; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9070 | if (vma->vm_file && |
| 9071 | !is_file_hugepages(vma->vm_file)) { |
| 9072 | ret = -EOPNOTSUPP; |
| 9073 | break; |
| 9074 | } |
| 9075 | } |
| 9076 | } else { |
| 9077 | ret = pret < 0 ? pret : -EFAULT; |
| 9078 | } |
| 9079 | mmap_read_unlock(current->mm); |
| 9080 | if (ret) { |
| 9081 | /* |
| 9082 | * if we did partial map, or found file backed vmas, |
| 9083 | * release any pages we did get |
| 9084 | */ |
| 9085 | if (pret > 0) |
| 9086 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9087 | goto done; |
| 9088 | } |
| 9089 | |
| 9090 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); |
| 9091 | if (ret) { |
| 9092 | unpin_user_pages(pages, pret); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9093 | goto done; |
| 9094 | } |
| 9095 | |
| 9096 | off = ubuf & ~PAGE_MASK; |
| 9097 | size = iov->iov_len; |
| 9098 | for (i = 0; i < nr_pages; i++) { |
| 9099 | size_t vec_len; |
| 9100 | |
| 9101 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 9102 | imu->bvec[i].bv_page = pages[i]; |
| 9103 | imu->bvec[i].bv_len = vec_len; |
| 9104 | imu->bvec[i].bv_offset = off; |
| 9105 | off = 0; |
| 9106 | size -= vec_len; |
| 9107 | } |
| 9108 | /* store original address for later verification */ |
| 9109 | imu->ubuf = ubuf; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 9110 | imu->ubuf_end = ubuf + iov->iov_len; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9111 | imu->nr_bvecs = nr_pages; |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9112 | *pimu = imu; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9113 | ret = 0; |
| 9114 | done: |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9115 | if (ret) |
| 9116 | kvfree(imu); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9117 | kvfree(pages); |
| 9118 | kvfree(vmas); |
| 9119 | return ret; |
| 9120 | } |
| 9121 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9122 | 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] | 9123 | { |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9124 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
| 9125 | return ctx->user_bufs ? 0 : -ENOMEM; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9126 | } |
| 9127 | |
| 9128 | static int io_buffer_validate(struct iovec *iov) |
| 9129 | { |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9130 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
| 9131 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9132 | /* |
| 9133 | * Don't impose further limits on the size and buffer |
| 9134 | * constraints here, we'll -EINVAL later when IO is |
| 9135 | * submitted if they are wrong. |
| 9136 | */ |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9137 | if (!iov->iov_base) |
| 9138 | return iov->iov_len ? -EFAULT : 0; |
| 9139 | if (!iov->iov_len) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9140 | return -EFAULT; |
| 9141 | |
| 9142 | /* arbitrary limit, but we need something */ |
| 9143 | if (iov->iov_len > SZ_1G) |
| 9144 | return -EFAULT; |
| 9145 | |
Pavel Begunkov | 50e9698 | 2021-03-24 22:59:01 +0000 | [diff] [blame] | 9146 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
| 9147 | return -EOVERFLOW; |
| 9148 | |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9149 | return 0; |
| 9150 | } |
| 9151 | |
| 9152 | 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] | 9153 | unsigned int nr_args, u64 __user *tags) |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9154 | { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9155 | struct page *last_hpage = NULL; |
| 9156 | struct io_rsrc_data *data; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9157 | int i, ret; |
| 9158 | struct iovec iov; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9159 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9160 | if (ctx->user_bufs) |
| 9161 | return -EBUSY; |
Pavel Begunkov | 489809e | 2021-05-14 12:06:44 +0100 | [diff] [blame] | 9162 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9163 | return -EINVAL; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9164 | ret = io_rsrc_node_switch_start(ctx); |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9165 | if (ret) |
| 9166 | return ret; |
Pavel Begunkov | d878c81 | 2021-06-14 02:36:18 +0100 | [diff] [blame] | 9167 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
| 9168 | if (ret) |
| 9169 | return ret; |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9170 | ret = io_buffers_map_alloc(ctx, nr_args); |
| 9171 | if (ret) { |
Zqiang | bb6659c | 2021-04-30 16:25:15 +0800 | [diff] [blame] | 9172 | io_rsrc_data_free(data); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9173 | return ret; |
| 9174 | } |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9175 | |
Pavel Begunkov | 8709446 | 2021-04-11 01:46:36 +0100 | [diff] [blame] | 9176 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9177 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 9178 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9179 | break; |
Bijan Mottahedeh | 2b35860 | 2021-01-06 12:39:11 -0800 | [diff] [blame] | 9180 | ret = io_buffer_validate(&iov); |
| 9181 | if (ret) |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9182 | break; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9183 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9184 | ret = -EINVAL; |
| 9185 | break; |
| 9186 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9187 | |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 9188 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
| 9189 | &last_hpage); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9190 | if (ret) |
| 9191 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9192 | } |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9193 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9194 | WARN_ON_ONCE(ctx->buf_data); |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 9195 | |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9196 | ctx->buf_data = data; |
| 9197 | if (ret) |
| 9198 | __io_sqe_buffers_unregister(ctx); |
| 9199 | else |
| 9200 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 9201 | return ret; |
| 9202 | } |
| 9203 | |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9204 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
| 9205 | struct io_uring_rsrc_update2 *up, |
| 9206 | unsigned int nr_args) |
| 9207 | { |
| 9208 | u64 __user *tags = u64_to_user_ptr(up->tags); |
| 9209 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9210 | struct page *last_hpage = NULL; |
| 9211 | bool needs_switch = false; |
| 9212 | __u32 done; |
| 9213 | int i, err; |
| 9214 | |
| 9215 | if (!ctx->buf_data) |
| 9216 | return -ENXIO; |
| 9217 | if (up->offset + nr_args > ctx->nr_user_bufs) |
| 9218 | return -EINVAL; |
| 9219 | |
| 9220 | for (done = 0; done < nr_args; done++) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9221 | struct io_mapped_ubuf *imu; |
| 9222 | int offset = up->offset + done; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9223 | u64 tag = 0; |
| 9224 | |
| 9225 | err = io_copy_iov(ctx, &iov, iovs, done); |
| 9226 | if (err) |
| 9227 | break; |
| 9228 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { |
| 9229 | err = -EFAULT; |
| 9230 | break; |
| 9231 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9232 | err = io_buffer_validate(&iov); |
| 9233 | if (err) |
| 9234 | break; |
Colin Ian King | cf3770e | 2021-04-29 11:46:02 +0100 | [diff] [blame] | 9235 | if (!iov.iov_base && tag) { |
| 9236 | err = -EINVAL; |
| 9237 | break; |
| 9238 | } |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9239 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
| 9240 | if (err) |
| 9241 | break; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9242 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9243 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9244 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9245 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
| 9246 | ctx->rsrc_node, ctx->user_bufs[i]); |
| 9247 | if (unlikely(err)) { |
| 9248 | io_buffer_unmap(ctx, &imu); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9249 | break; |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9250 | } |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9251 | ctx->user_bufs[i] = NULL; |
| 9252 | needs_switch = true; |
| 9253 | } |
| 9254 | |
Pavel Begunkov | 0b8c0e7 | 2021-04-26 15:17:38 +0100 | [diff] [blame] | 9255 | ctx->user_bufs[i] = imu; |
Pavel Begunkov | 2d091d6 | 2021-06-14 02:36:21 +0100 | [diff] [blame] | 9256 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 9257 | } |
| 9258 | |
| 9259 | if (needs_switch) |
| 9260 | io_rsrc_node_switch(ctx, ctx->buf_data); |
| 9261 | return done ? done : err; |
| 9262 | } |
| 9263 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9264 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 9265 | { |
| 9266 | __s32 __user *fds = arg; |
| 9267 | int fd; |
| 9268 | |
| 9269 | if (ctx->cq_ev_fd) |
| 9270 | return -EBUSY; |
| 9271 | |
| 9272 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 9273 | return -EFAULT; |
| 9274 | |
| 9275 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 9276 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 9277 | int ret = PTR_ERR(ctx->cq_ev_fd); |
Pavel Begunkov | fe7e325 | 2021-06-24 15:09:57 +0100 | [diff] [blame] | 9278 | |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9279 | ctx->cq_ev_fd = NULL; |
| 9280 | return ret; |
| 9281 | } |
| 9282 | |
| 9283 | return 0; |
| 9284 | } |
| 9285 | |
| 9286 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 9287 | { |
| 9288 | if (ctx->cq_ev_fd) { |
| 9289 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 9290 | ctx->cq_ev_fd = NULL; |
| 9291 | return 0; |
| 9292 | } |
| 9293 | |
| 9294 | return -ENXIO; |
| 9295 | } |
| 9296 | |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9297 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
| 9298 | { |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9299 | struct io_buffer *buf; |
| 9300 | unsigned long index; |
| 9301 | |
Ye Bin | 1d0254e | 2021-11-22 10:47:37 +0800 | [diff] [blame] | 9302 | xa_for_each(&ctx->io_buffers, index, buf) |
Jens Axboe | 9e15c3a | 2021-03-13 12:29:43 -0700 | [diff] [blame] | 9303 | __io_remove_buffers(ctx, buf, index, -1U); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9304 | } |
| 9305 | |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9306 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9307 | { |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9308 | struct io_submit_state *state = &ctx->submit_state; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9309 | int nr = 0; |
Pavel Begunkov | bf019da | 2021-02-10 00:03:17 +0000 | [diff] [blame] | 9310 | |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9311 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | cd0ca2e | 2021-08-09 20:18:11 +0100 | [diff] [blame] | 9312 | io_flush_cached_locked_reqs(ctx, state); |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9313 | |
| 9314 | while (state->free_list.next) { |
| 9315 | struct io_wq_work_node *node; |
| 9316 | struct io_kiocb *req; |
| 9317 | |
| 9318 | node = wq_stack_extract(&state->free_list); |
| 9319 | req = container_of(node, struct io_kiocb, comp_list); |
| 9320 | kmem_cache_free(req_cachep, req); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9321 | nr++; |
Pavel Begunkov | c2b6c6b | 2021-09-24 21:59:47 +0100 | [diff] [blame] | 9322 | } |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9323 | if (nr) |
| 9324 | percpu_ref_put_many(&ctx->refs, nr); |
Jens Axboe | 9a4fdbd | 2021-02-13 09:09:44 -0700 | [diff] [blame] | 9325 | mutex_unlock(&ctx->uring_lock); |
| 9326 | } |
| 9327 | |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9328 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9329 | { |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9330 | if (data && !atomic_dec_and_test(&data->refs)) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9331 | wait_for_completion(&data->done); |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9332 | } |
| 9333 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9334 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9335 | { |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9336 | io_sq_thread_finish(ctx); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9337 | |
Jens Axboe | 37d1e2e | 2021-02-17 21:03:43 -0700 | [diff] [blame] | 9338 | if (ctx->mm_account) { |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 9339 | mmdrop(ctx->mm_account); |
| 9340 | ctx->mm_account = NULL; |
Bijan Mottahedeh | 3097582 | 2020-06-16 16:36:09 -0700 | [diff] [blame] | 9341 | } |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9342 | |
Pavel Begunkov | ab40940 | 2021-10-09 23:14:41 +0100 | [diff] [blame] | 9343 | io_rsrc_refs_drop(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9344 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
| 9345 | io_wait_rsrc_data(ctx->buf_data); |
| 9346 | io_wait_rsrc_data(ctx->file_data); |
| 9347 | |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9348 | mutex_lock(&ctx->uring_lock); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9349 | if (ctx->buf_data) |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 9350 | __io_sqe_buffers_unregister(ctx); |
Pavel Begunkov | 43597aa | 2021-08-10 02:44:23 +0100 | [diff] [blame] | 9351 | if (ctx->file_data) |
Pavel Begunkov | 0848040 | 2021-04-13 02:58:38 +0100 | [diff] [blame] | 9352 | __io_sqe_files_unregister(ctx); |
Pavel Begunkov | c4ea060 | 2021-04-01 15:43:58 +0100 | [diff] [blame] | 9353 | if (ctx->rings) |
| 9354 | __io_cqring_overflow_flush(ctx, true); |
Hao Xu | 8bad28d | 2021-02-19 17:19:36 +0800 | [diff] [blame] | 9355 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 9356 | io_eventfd_unregister(ctx); |
Jens Axboe | 5a2e745 | 2020-02-23 16:23:11 -0700 | [diff] [blame] | 9357 | io_destroy_buffers(ctx); |
Pavel Begunkov | 07db298 | 2021-04-20 12:03:32 +0100 | [diff] [blame] | 9358 | if (ctx->sq_creds) |
| 9359 | put_cred(ctx->sq_creds); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 9360 | |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9361 | /* there are no registered resources left, nobody uses it */ |
| 9362 | if (ctx->rsrc_node) |
| 9363 | io_rsrc_node_destroy(ctx->rsrc_node); |
Pavel Begunkov | 8dd03af | 2021-03-19 17:22:36 +0000 | [diff] [blame] | 9364 | if (ctx->rsrc_backup_node) |
Pavel Begunkov | b895c9a | 2021-04-01 15:43:40 +0100 | [diff] [blame] | 9365 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9366 | flush_delayed_work(&ctx->rsrc_put_work); |
Pavel Begunkov | 756ab7c | 2021-10-06 16:06:47 +0100 | [diff] [blame] | 9367 | flush_delayed_work(&ctx->fallback_work); |
Pavel Begunkov | a7f0ed5 | 2021-04-01 15:43:46 +0100 | [diff] [blame] | 9368 | |
| 9369 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); |
| 9370 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9371 | |
| 9372 | #if defined(CONFIG_UNIX) |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9373 | if (ctx->ring_sock) { |
| 9374 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9375 | sock_release(ctx->ring_sock); |
Eric Biggers | 355e8d2 | 2019-06-12 14:58:43 -0700 | [diff] [blame] | 9376 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9377 | #endif |
Pavel Begunkov | ef9dd63 | 2021-08-28 19:54:38 -0600 | [diff] [blame] | 9378 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9379 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9380 | io_mem_free(ctx->rings); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9381 | io_mem_free(ctx->sq_sqes); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9382 | |
| 9383 | percpu_ref_exit(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9384 | free_uid(ctx->user); |
Jens Axboe | 4010fec | 2021-02-27 15:04:18 -0700 | [diff] [blame] | 9385 | io_req_caches_free(ctx); |
Jens Axboe | e941894 | 2021-02-19 12:33:30 -0700 | [diff] [blame] | 9386 | if (ctx->hash_map) |
| 9387 | io_wq_put_hash(ctx->hash_map); |
Jens Axboe | 78076bb | 2019-12-04 19:56:40 -0700 | [diff] [blame] | 9388 | kfree(ctx->cancel_hash); |
Pavel Begunkov | 6224843 | 2021-04-28 13:11:29 +0100 | [diff] [blame] | 9389 | kfree(ctx->dummy_ubuf); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9390 | kfree(ctx); |
| 9391 | } |
| 9392 | |
| 9393 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 9394 | { |
| 9395 | struct io_ring_ctx *ctx = file->private_data; |
| 9396 | __poll_t mask = 0; |
| 9397 | |
Pavel Begunkov | d60aa65 | 2021-10-04 20:02:52 +0100 | [diff] [blame] | 9398 | poll_wait(file, &ctx->cq_wait, wait); |
Stefan Bühler | 4f7067c | 2019-04-24 23:54:17 +0200 | [diff] [blame] | 9399 | /* |
| 9400 | * synchronizes with barrier from wq_has_sleeper call in |
| 9401 | * io_commit_cqring |
| 9402 | */ |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9403 | smp_rmb(); |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9404 | if (!io_sqring_full(ctx)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9405 | mask |= EPOLLOUT | EPOLLWRNORM; |
Hao Xu | ed670c3 | 2021-02-05 16:34:21 +0800 | [diff] [blame] | 9406 | |
| 9407 | /* |
| 9408 | * Don't flush cqring overflow list here, just do a simple check. |
| 9409 | * Otherwise there could possible be ABBA deadlock: |
| 9410 | * CPU0 CPU1 |
| 9411 | * ---- ---- |
| 9412 | * lock(&ctx->uring_lock); |
| 9413 | * lock(&ep->mtx); |
| 9414 | * lock(&ctx->uring_lock); |
| 9415 | * lock(&ep->mtx); |
| 9416 | * |
| 9417 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this |
| 9418 | * pushs them to do the flush. |
| 9419 | */ |
Pavel Begunkov | 5ed7a37 | 2021-06-14 23:37:27 +0100 | [diff] [blame] | 9420 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9421 | mask |= EPOLLIN | EPOLLRDNORM; |
| 9422 | |
| 9423 | return mask; |
| 9424 | } |
| 9425 | |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9426 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9427 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9428 | const struct cred *creds; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 9429 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9430 | creds = xa_erase(&ctx->personalities, id); |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 9431 | if (creds) { |
| 9432 | put_cred(creds); |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9433 | return 0; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 9434 | } |
Yejune Deng | 0bead8c | 2020-12-24 11:02:20 +0800 | [diff] [blame] | 9435 | |
| 9436 | return -EINVAL; |
| 9437 | } |
| 9438 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9439 | struct io_tctx_exit { |
| 9440 | struct callback_head task_work; |
| 9441 | struct completion completion; |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9442 | struct io_ring_ctx *ctx; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9443 | }; |
| 9444 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9445 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9446 | { |
| 9447 | struct io_uring_task *tctx = current->io_uring; |
| 9448 | struct io_tctx_exit *work; |
| 9449 | |
| 9450 | work = container_of(cb, struct io_tctx_exit, task_work); |
| 9451 | /* |
| 9452 | * When @in_idle, we're in cancellation and it's racy to remove the |
| 9453 | * node. It'll be removed by the end of cancellation, just ignore it. |
| 9454 | */ |
| 9455 | if (!atomic_read(&tctx->in_idle)) |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9456 | io_uring_del_tctx_node((unsigned long)work->ctx); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9457 | complete(&work->completion); |
| 9458 | } |
| 9459 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9460 | 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] | 9461 | { |
| 9462 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 9463 | |
| 9464 | return req->ctx == data; |
| 9465 | } |
| 9466 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9467 | static __cold void io_ring_exit_work(struct work_struct *work) |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9468 | { |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9469 | 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] | 9470 | unsigned long timeout = jiffies + HZ * 60 * 5; |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9471 | unsigned long interval = HZ / 20; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9472 | struct io_tctx_exit exit; |
| 9473 | struct io_tctx_node *node; |
| 9474 | int ret; |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9475 | |
Jens Axboe | 56952e9 | 2020-06-17 15:00:04 -0600 | [diff] [blame] | 9476 | /* |
| 9477 | * If we're doing polled IO and end up having requests being |
| 9478 | * submitted async (out-of-line), then completions can come in while |
| 9479 | * we're waiting for refs to drop. We need to reap these manually, |
| 9480 | * as nobody else will be looking for them. |
| 9481 | */ |
Pavel Begunkov | b2edc0a | 2020-07-07 16:36:22 +0300 | [diff] [blame] | 9482 | do { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9483 | io_uring_try_cancel_requests(ctx, NULL, true); |
Pavel Begunkov | 28090c1 | 2021-04-25 23:34:45 +0100 | [diff] [blame] | 9484 | if (ctx->sq_data) { |
| 9485 | struct io_sq_data *sqd = ctx->sq_data; |
| 9486 | struct task_struct *tsk; |
| 9487 | |
| 9488 | io_sq_thread_park(sqd); |
| 9489 | tsk = sqd->thread; |
| 9490 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) |
| 9491 | io_wq_cancel_cb(tsk->io_uring->io_wq, |
| 9492 | io_cancel_ctx_cb, ctx, true); |
| 9493 | io_sq_thread_unpark(sqd); |
| 9494 | } |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9495 | |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 9496 | io_req_caches_free(ctx); |
| 9497 | |
Pavel Begunkov | 58d3be2 | 2021-08-09 13:04:17 +0100 | [diff] [blame] | 9498 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
| 9499 | /* there is little hope left, don't run it too often */ |
| 9500 | interval = HZ * 60; |
| 9501 | } |
| 9502 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9503 | |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9504 | init_completion(&exit.completion); |
| 9505 | init_task_work(&exit.task_work, io_tctx_exit_cb); |
| 9506 | exit.ctx = ctx; |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9507 | /* |
| 9508 | * Some may use context even when all refs and requests have been put, |
| 9509 | * 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] | 9510 | * completion_lock, see io_req_task_submit(). Apart from other work, |
Pavel Begunkov | 89b5066 | 2021-04-01 15:43:50 +0100 | [diff] [blame] | 9511 | * this lock/unlock section also waits them to finish. |
| 9512 | */ |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9513 | mutex_lock(&ctx->uring_lock); |
| 9514 | while (!list_empty(&ctx->tctx_list)) { |
Pavel Begunkov | b5bb3a2 | 2021-03-06 11:02:16 +0000 | [diff] [blame] | 9515 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
| 9516 | |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9517 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
| 9518 | ctx_node); |
Pavel Begunkov | 7f00651 | 2021-04-14 13:38:34 +0100 | [diff] [blame] | 9519 | /* don't spin on a single task if cancellation failed */ |
| 9520 | list_rotate_left(&ctx->tctx_list); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9521 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
| 9522 | if (WARN_ON_ONCE(ret)) |
| 9523 | continue; |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9524 | |
| 9525 | mutex_unlock(&ctx->uring_lock); |
| 9526 | wait_for_completion(&exit.completion); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9527 | mutex_lock(&ctx->uring_lock); |
| 9528 | } |
| 9529 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9530 | spin_lock(&ctx->completion_lock); |
| 9531 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | d56d938 | 2021-03-06 11:02:13 +0000 | [diff] [blame] | 9532 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9533 | io_ring_ctx_free(ctx); |
| 9534 | } |
| 9535 | |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9536 | /* Returns true if we found and killed one or more timeouts */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9537 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
| 9538 | struct task_struct *tsk, bool cancel_all) |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9539 | { |
| 9540 | struct io_kiocb *req, *tmp; |
| 9541 | int canceled = 0; |
| 9542 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9543 | spin_lock(&ctx->completion_lock); |
| 9544 | spin_lock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9545 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9546 | if (io_match_task(req, tsk, cancel_all)) { |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9547 | io_kill_timeout(req, -ECANCELED); |
| 9548 | canceled++; |
| 9549 | } |
| 9550 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9551 | spin_unlock_irq(&ctx->timeout_lock); |
Pavel Begunkov | 5152042 | 2021-03-29 11:39:29 +0100 | [diff] [blame] | 9552 | if (canceled != 0) |
| 9553 | io_commit_cqring(ctx); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9554 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | 80c4cbd | 2021-03-25 18:32:43 +0000 | [diff] [blame] | 9555 | if (canceled != 0) |
| 9556 | io_cqring_ev_posted(ctx); |
| 9557 | return canceled != 0; |
| 9558 | } |
| 9559 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9560 | 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] | 9561 | { |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9562 | unsigned long index; |
| 9563 | struct creds *creds; |
| 9564 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9565 | mutex_lock(&ctx->uring_lock); |
| 9566 | percpu_ref_kill(&ctx->refs); |
Pavel Begunkov | 634578f | 2020-12-06 22:22:44 +0000 | [diff] [blame] | 9567 | if (ctx->rings) |
Pavel Begunkov | 6c2450a | 2021-02-23 12:40:22 +0000 | [diff] [blame] | 9568 | __io_cqring_overflow_flush(ctx, true); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 9569 | xa_for_each(&ctx->personalities, index, creds) |
| 9570 | io_unregister_personality(ctx, index); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9571 | mutex_unlock(&ctx->uring_lock); |
| 9572 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9573 | io_kill_timeouts(ctx, NULL, true); |
| 9574 | io_poll_remove_all(ctx, NULL, true); |
Jens Axboe | 561fb04 | 2019-10-24 07:25:42 -0600 | [diff] [blame] | 9575 | |
Jens Axboe | 15dff28 | 2019-11-13 09:09:23 -0700 | [diff] [blame] | 9576 | /* 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] | 9577 | io_iopoll_try_reap_events(ctx); |
Jens Axboe | 309fc03 | 2020-07-10 09:13:34 -0600 | [diff] [blame] | 9578 | |
Jens Axboe | 85faa7b | 2020-04-09 18:14:00 -0600 | [diff] [blame] | 9579 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
Jens Axboe | fc66677 | 2020-08-19 11:10:51 -0600 | [diff] [blame] | 9580 | /* |
| 9581 | * Use system_unbound_wq to avoid spawning tons of event kworkers |
| 9582 | * if we're exiting a ton of rings at the same time. It just adds |
| 9583 | * noise and overhead, there's no discernable change in runtime |
| 9584 | * over using system_wq. |
| 9585 | */ |
| 9586 | queue_work(system_unbound_wq, &ctx->exit_work); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9587 | } |
| 9588 | |
| 9589 | static int io_uring_release(struct inode *inode, struct file *file) |
| 9590 | { |
| 9591 | struct io_ring_ctx *ctx = file->private_data; |
| 9592 | |
| 9593 | file->private_data = NULL; |
| 9594 | io_ring_ctx_wait_and_kill(ctx); |
| 9595 | return 0; |
| 9596 | } |
| 9597 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9598 | struct io_task_cancel { |
| 9599 | struct task_struct *task; |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9600 | bool all; |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9601 | }; |
Pavel Begunkov | 67c4d9e | 2020-06-15 10:24:05 +0300 | [diff] [blame] | 9602 | |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9603 | 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] | 9604 | { |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9605 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
Pavel Begunkov | f6edbab | 2020-11-06 13:00:26 +0000 | [diff] [blame] | 9606 | struct io_task_cancel *cancel = data; |
Pavel Begunkov | 9a472ef | 2020-11-05 22:31:37 +0000 | [diff] [blame] | 9607 | |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 9608 | return io_match_task_safe(req, cancel->task, cancel->all); |
Jens Axboe | b711d4e | 2020-08-16 08:23:05 -0700 | [diff] [blame] | 9609 | } |
| 9610 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9611 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
| 9612 | struct task_struct *task, |
| 9613 | bool cancel_all) |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9614 | { |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9615 | struct io_defer_entry *de; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9616 | LIST_HEAD(list); |
| 9617 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9618 | spin_lock(&ctx->completion_lock); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9619 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
Pavel Begunkov | 6af3f48 | 2021-11-26 14:38:15 +0000 | [diff] [blame] | 9620 | if (io_match_task_safe(de->req, task, cancel_all)) { |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9621 | list_cut_position(&list, &ctx->defer_list, &de->list); |
| 9622 | break; |
| 9623 | } |
| 9624 | } |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 9625 | spin_unlock(&ctx->completion_lock); |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9626 | if (list_empty(&list)) |
| 9627 | return false; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9628 | |
| 9629 | while (!list_empty(&list)) { |
| 9630 | de = list_first_entry(&list, struct io_defer_entry, list); |
| 9631 | list_del_init(&de->list); |
Pavel Begunkov | f41db273 | 2021-02-28 22:35:12 +0000 | [diff] [blame] | 9632 | io_req_complete_failed(de->req, -ECANCELED); |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9633 | kfree(de); |
| 9634 | } |
Pavel Begunkov | e1915f7 | 2021-03-11 23:29:35 +0000 | [diff] [blame] | 9635 | return true; |
Pavel Begunkov | b7ddce3 | 2020-09-06 00:45:14 +0300 | [diff] [blame] | 9636 | } |
| 9637 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9638 | 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] | 9639 | { |
| 9640 | struct io_tctx_node *node; |
| 9641 | enum io_wq_cancel cret; |
| 9642 | bool ret = false; |
| 9643 | |
| 9644 | mutex_lock(&ctx->uring_lock); |
| 9645 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 9646 | struct io_uring_task *tctx = node->task->io_uring; |
| 9647 | |
| 9648 | /* |
| 9649 | * io_wq will stay alive while we hold uring_lock, because it's |
| 9650 | * killed after ctx nodes, which requires to take the lock. |
| 9651 | */ |
| 9652 | if (!tctx || !tctx->io_wq) |
| 9653 | continue; |
| 9654 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); |
| 9655 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9656 | } |
| 9657 | mutex_unlock(&ctx->uring_lock); |
| 9658 | |
| 9659 | return ret; |
| 9660 | } |
| 9661 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9662 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
| 9663 | struct task_struct *task, |
| 9664 | bool cancel_all) |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9665 | { |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9666 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9667 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9668 | |
| 9669 | while (1) { |
| 9670 | enum io_wq_cancel cret; |
| 9671 | bool ret = false; |
| 9672 | |
Pavel Begunkov | 1b00764 | 2021-03-06 11:02:17 +0000 | [diff] [blame] | 9673 | if (!task) { |
| 9674 | ret |= io_uring_try_cancel_iowq(ctx); |
| 9675 | } else if (tctx && tctx->io_wq) { |
| 9676 | /* |
| 9677 | * Cancels requests of all rings, not only @ctx, but |
| 9678 | * it's fine as the task is in exit/exec. |
| 9679 | */ |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9680 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9681 | &cancel, true); |
| 9682 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); |
| 9683 | } |
| 9684 | |
| 9685 | /* SQPOLL thread does its own polling */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9686 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
Jens Axboe | d052d1d | 2021-03-11 10:49:20 -0700 | [diff] [blame] | 9687 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
Pavel Begunkov | 5eef4e8 | 2021-09-24 21:59:49 +0100 | [diff] [blame] | 9688 | while (!wq_list_empty(&ctx->iopoll_list)) { |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9689 | io_iopoll_try_reap_events(ctx); |
| 9690 | ret = true; |
| 9691 | } |
| 9692 | } |
| 9693 | |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9694 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
| 9695 | ret |= io_poll_remove_all(ctx, task, cancel_all); |
| 9696 | ret |= io_kill_timeouts(ctx, task, cancel_all); |
Pavel Begunkov | e5dc480 | 2021-06-26 21:40:46 +0100 | [diff] [blame] | 9697 | if (task) |
| 9698 | ret |= io_run_task_work(); |
Pavel Begunkov | 9936c7c | 2021-02-04 13:51:56 +0000 | [diff] [blame] | 9699 | if (!ret) |
| 9700 | break; |
| 9701 | cond_resched(); |
| 9702 | } |
| 9703 | } |
| 9704 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9705 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9706 | { |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9707 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9708 | struct io_tctx_node *node; |
Pavel Begunkov | a528b04 | 2020-12-21 18:34:04 +0000 | [diff] [blame] | 9709 | int ret; |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9710 | |
| 9711 | if (unlikely(!tctx)) { |
Jens Axboe | 5aa75ed | 2021-02-16 12:56:50 -0700 | [diff] [blame] | 9712 | ret = io_uring_alloc_task_context(current, ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9713 | if (unlikely(ret)) |
| 9714 | return ret; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9715 | |
Matthew Wilcox (Oracle) | 236434c | 2020-10-09 13:49:52 +0100 | [diff] [blame] | 9716 | tctx = current->io_uring; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 9717 | if (ctx->iowq_limits_set) { |
| 9718 | unsigned int limits[2] = { ctx->iowq_limits[0], |
| 9719 | ctx->iowq_limits[1], }; |
| 9720 | |
| 9721 | ret = io_wq_max_workers(tctx->io_wq, limits); |
| 9722 | if (ret) |
| 9723 | return ret; |
| 9724 | } |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9725 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9726 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
| 9727 | node = kmalloc(sizeof(*node), GFP_KERNEL); |
| 9728 | if (!node) |
| 9729 | return -ENOMEM; |
| 9730 | node->ctx = ctx; |
| 9731 | node->task = current; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9732 | |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9733 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
| 9734 | node, GFP_KERNEL)); |
| 9735 | if (ret) { |
| 9736 | kfree(node); |
| 9737 | return ret; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9738 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9739 | |
| 9740 | mutex_lock(&ctx->uring_lock); |
| 9741 | list_add(&node->ctx_node, &ctx->tctx_list); |
| 9742 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9743 | } |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9744 | tctx->last = ctx; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9745 | return 0; |
| 9746 | } |
| 9747 | |
| 9748 | /* |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9749 | * Note that this task has used io_uring. We use it for cancelation purposes. |
| 9750 | */ |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9751 | 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] | 9752 | { |
| 9753 | struct io_uring_task *tctx = current->io_uring; |
| 9754 | |
| 9755 | if (likely(tctx && tctx->last == ctx)) |
| 9756 | return 0; |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9757 | return __io_uring_add_tctx_node(ctx); |
Pavel Begunkov | cf27f3b | 2021-03-19 17:22:31 +0000 | [diff] [blame] | 9758 | } |
| 9759 | |
| 9760 | /* |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9761 | * Remove this io_uring_file -> task mapping. |
| 9762 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9763 | static __cold void io_uring_del_tctx_node(unsigned long index) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9764 | { |
| 9765 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9766 | struct io_tctx_node *node; |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9767 | |
Pavel Begunkov | eebd2e3 | 2021-03-06 11:02:14 +0000 | [diff] [blame] | 9768 | if (!tctx) |
| 9769 | return; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9770 | node = xa_erase(&tctx->xa, index); |
| 9771 | if (!node) |
Pavel Begunkov | 2941267 | 2021-03-06 11:02:11 +0000 | [diff] [blame] | 9772 | return; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9773 | |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9774 | WARN_ON_ONCE(current != node->task); |
| 9775 | WARN_ON_ONCE(list_empty(&node->ctx_node)); |
| 9776 | |
| 9777 | mutex_lock(&node->ctx->uring_lock); |
| 9778 | list_del(&node->ctx_node); |
| 9779 | mutex_unlock(&node->ctx->uring_lock); |
| 9780 | |
Pavel Begunkov | baf186c | 2021-03-06 11:02:15 +0000 | [diff] [blame] | 9781 | if (tctx->last == node->ctx) |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9782 | tctx->last = NULL; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9783 | kfree(node); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9784 | } |
| 9785 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9786 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9787 | { |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9788 | struct io_wq *wq = tctx->io_wq; |
Pavel Begunkov | 13bf43f | 2021-03-06 11:02:12 +0000 | [diff] [blame] | 9789 | struct io_tctx_node *node; |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9790 | unsigned long index; |
| 9791 | |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9792 | xa_for_each(&tctx->xa, index, node) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 9793 | io_uring_del_tctx_node(index); |
Jens Axboe | 8bab4c0 | 2021-09-24 07:12:27 -0600 | [diff] [blame] | 9794 | cond_resched(); |
| 9795 | } |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9796 | if (wq) { |
| 9797 | /* |
Kamal Mostafa | f6f9b27 | 2021-11-16 09:55:30 -0800 | [diff] [blame] | 9798 | * Must be after io_uring_del_tctx_node() (removes nodes under |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9799 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
| 9800 | */ |
Pavel Begunkov | ba5ef6d | 2021-05-20 13:21:20 +0100 | [diff] [blame] | 9801 | io_wq_put_and_exit(wq); |
Pavel Begunkov | dadebc3 | 2021-08-23 13:30:44 +0100 | [diff] [blame] | 9802 | tctx->io_wq = NULL; |
Marco Elver | b16ef42 | 2021-05-27 11:25:48 +0200 | [diff] [blame] | 9803 | } |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9804 | } |
| 9805 | |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9806 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9807 | { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9808 | if (tracked) |
| 9809 | return atomic_read(&tctx->inflight_tracked); |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9810 | return percpu_counter_sum(&tctx->inflight); |
| 9811 | } |
| 9812 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9813 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9814 | { |
| 9815 | struct io_uring_task *tctx = task->io_uring; |
| 9816 | unsigned int refs = tctx->cached_refs; |
| 9817 | |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9818 | if (refs) { |
| 9819 | tctx->cached_refs = 0; |
| 9820 | percpu_counter_sub(&tctx->inflight, refs); |
| 9821 | put_task_struct_many(task, refs); |
| 9822 | } |
Pavel Begunkov | 09899b1 | 2021-06-14 02:36:22 +0100 | [diff] [blame] | 9823 | } |
| 9824 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9825 | /* |
| 9826 | * 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^] | 9827 | * 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] | 9828 | */ |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9829 | static __cold void io_uring_cancel_generic(bool cancel_all, |
| 9830 | struct io_sq_data *sqd) |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9831 | { |
Pavel Begunkov | 521d6a7 | 2021-03-11 23:29:38 +0000 | [diff] [blame] | 9832 | struct io_uring_task *tctx = current->io_uring; |
Pavel Begunkov | 734551d | 2021-04-18 14:52:09 +0100 | [diff] [blame] | 9833 | struct io_ring_ctx *ctx; |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9834 | s64 inflight; |
Pavel Begunkov | 0e9ddb3 | 2021-02-07 22:34:26 +0000 | [diff] [blame] | 9835 | DEFINE_WAIT(wait); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9836 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9837 | WARN_ON_ONCE(sqd && sqd->thread != current); |
| 9838 | |
Palash Oswal | 6d042ff | 2021-04-27 18:21:49 +0530 | [diff] [blame] | 9839 | if (!current->io_uring) |
| 9840 | return; |
Pavel Begunkov | 17a9105 | 2021-05-23 15:48:39 +0100 | [diff] [blame] | 9841 | if (tctx->io_wq) |
| 9842 | io_wq_exit_start(tctx->io_wq); |
| 9843 | |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9844 | atomic_inc(&tctx->in_idle); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9845 | do { |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9846 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9847 | /* read completions before cancelations */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9848 | inflight = tctx_inflight(tctx, !cancel_all); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9849 | if (!inflight) |
| 9850 | break; |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9851 | |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9852 | if (!sqd) { |
| 9853 | struct io_tctx_node *node; |
| 9854 | unsigned long index; |
| 9855 | |
| 9856 | xa_for_each(&tctx->xa, index, node) { |
| 9857 | /* sqpoll task will cancel all its requests */ |
| 9858 | if (node->ctx->sq_data) |
| 9859 | continue; |
| 9860 | io_uring_try_cancel_requests(node->ctx, current, |
| 9861 | cancel_all); |
| 9862 | } |
| 9863 | } else { |
| 9864 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
| 9865 | io_uring_try_cancel_requests(ctx, current, |
| 9866 | cancel_all); |
| 9867 | } |
| 9868 | |
Jens Axboe | 78a7806 | 2021-12-09 08:54:29 -0700 | [diff] [blame^] | 9869 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
| 9870 | io_run_task_work(); |
Pavel Begunkov | e9dbe22 | 2021-08-09 13:04:20 +0100 | [diff] [blame] | 9871 | io_uring_drop_tctx_refs(current); |
Jens Axboe | 78a7806 | 2021-12-09 08:54:29 -0700 | [diff] [blame^] | 9872 | |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9873 | /* |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9874 | * If we've seen completions, retry without waiting. This |
| 9875 | * avoids a race where a completion comes in before we did |
| 9876 | * prepare_to_wait(). |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 9877 | */ |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9878 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
Pavel Begunkov | a1bb3cd | 2021-01-26 15:28:26 +0000 | [diff] [blame] | 9879 | schedule(); |
Pavel Begunkov | f57555e | 2020-12-20 13:21:44 +0000 | [diff] [blame] | 9880 | finish_wait(&tctx->wait, &wait); |
Jens Axboe | d8a6df1 | 2020-10-15 16:24:45 -0600 | [diff] [blame] | 9881 | } while (1); |
Jens Axboe | fdaf083 | 2020-10-30 09:37:30 -0600 | [diff] [blame] | 9882 | atomic_dec(&tctx->in_idle); |
Pavel Begunkov | de7f1d9 | 2021-01-04 20:43:29 +0000 | [diff] [blame] | 9883 | |
Pavel Begunkov | 8452d4a | 2021-02-27 11:16:46 +0000 | [diff] [blame] | 9884 | io_uring_clean_tctx(tctx); |
Pavel Begunkov | 3dd0c97 | 2021-05-16 22:58:04 +0100 | [diff] [blame] | 9885 | if (cancel_all) { |
Pavel Begunkov | 3f48cf1 | 2021-04-11 01:46:27 +0100 | [diff] [blame] | 9886 | /* for exec all current's requests should be gone, kill tctx */ |
| 9887 | __io_uring_free(current); |
| 9888 | } |
Pavel Begunkov | 44e728b | 2020-06-15 10:24:04 +0300 | [diff] [blame] | 9889 | } |
| 9890 | |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9891 | void __io_uring_cancel(bool cancel_all) |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9892 | { |
Hao Xu | f552a27 | 2021-08-12 12:14:35 +0800 | [diff] [blame] | 9893 | io_uring_cancel_generic(cancel_all, NULL); |
Pavel Begunkov | 78cc687 | 2021-06-14 02:36:23 +0100 | [diff] [blame] | 9894 | } |
| 9895 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9896 | static void *io_uring_validate_mmap_request(struct file *file, |
| 9897 | loff_t pgoff, size_t sz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9898 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9899 | struct io_ring_ctx *ctx = file->private_data; |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9900 | loff_t offset = pgoff << PAGE_SHIFT; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9901 | struct page *page; |
| 9902 | void *ptr; |
| 9903 | |
| 9904 | switch (offset) { |
| 9905 | case IORING_OFF_SQ_RING: |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 9906 | case IORING_OFF_CQ_RING: |
| 9907 | ptr = ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9908 | break; |
| 9909 | case IORING_OFF_SQES: |
| 9910 | ptr = ctx->sq_sqes; |
| 9911 | break; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9912 | default: |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9913 | return ERR_PTR(-EINVAL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9914 | } |
| 9915 | |
| 9916 | page = virt_to_head_page(ptr); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 9917 | if (sz > page_size(page)) |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9918 | return ERR_PTR(-EINVAL); |
| 9919 | |
| 9920 | return ptr; |
| 9921 | } |
| 9922 | |
| 9923 | #ifdef CONFIG_MMU |
| 9924 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 9925 | 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] | 9926 | { |
| 9927 | size_t sz = vma->vm_end - vma->vm_start; |
| 9928 | unsigned long pfn; |
| 9929 | void *ptr; |
| 9930 | |
| 9931 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); |
| 9932 | if (IS_ERR(ptr)) |
| 9933 | return PTR_ERR(ptr); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 9934 | |
| 9935 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 9936 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 9937 | } |
| 9938 | |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 9939 | #else /* !CONFIG_MMU */ |
| 9940 | |
| 9941 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 9942 | { |
| 9943 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; |
| 9944 | } |
| 9945 | |
| 9946 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) |
| 9947 | { |
| 9948 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; |
| 9949 | } |
| 9950 | |
| 9951 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, |
| 9952 | unsigned long addr, unsigned long len, |
| 9953 | unsigned long pgoff, unsigned long flags) |
| 9954 | { |
| 9955 | void *ptr; |
| 9956 | |
| 9957 | ptr = io_uring_validate_mmap_request(file, pgoff, len); |
| 9958 | if (IS_ERR(ptr)) |
| 9959 | return PTR_ERR(ptr); |
| 9960 | |
| 9961 | return (unsigned long) ptr; |
| 9962 | } |
| 9963 | |
| 9964 | #endif /* !CONFIG_MMU */ |
| 9965 | |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 9966 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9967 | { |
| 9968 | DEFINE_WAIT(wait); |
| 9969 | |
| 9970 | do { |
| 9971 | if (!io_sqring_full(ctx)) |
| 9972 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9973 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
| 9974 | |
| 9975 | if (!io_sqring_full(ctx)) |
| 9976 | break; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9977 | schedule(); |
| 9978 | } while (!signal_pending(current)); |
| 9979 | |
| 9980 | finish_wait(&ctx->sqo_sq_wait, &wait); |
Yang Li | 5199328 | 2021-03-09 14:30:41 +0800 | [diff] [blame] | 9981 | return 0; |
Jens Axboe | 9055420 | 2020-09-03 12:12:41 -0600 | [diff] [blame] | 9982 | } |
| 9983 | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 9984 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
| 9985 | struct __kernel_timespec __user **ts, |
| 9986 | const sigset_t __user **sig) |
| 9987 | { |
| 9988 | struct io_uring_getevents_arg arg; |
| 9989 | |
| 9990 | /* |
| 9991 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer |
| 9992 | * is just a pointer to the sigset_t. |
| 9993 | */ |
| 9994 | if (!(flags & IORING_ENTER_EXT_ARG)) { |
| 9995 | *sig = (const sigset_t __user *) argp; |
| 9996 | *ts = NULL; |
| 9997 | return 0; |
| 9998 | } |
| 9999 | |
| 10000 | /* |
| 10001 | * EXT_ARG is set - ensure we agree on the size of it and copy in our |
| 10002 | * timespec and sigset_t pointers if good. |
| 10003 | */ |
| 10004 | if (*argsz != sizeof(arg)) |
| 10005 | return -EINVAL; |
| 10006 | if (copy_from_user(&arg, argp, sizeof(arg))) |
| 10007 | return -EFAULT; |
| 10008 | *sig = u64_to_user_ptr(arg.sigmask); |
| 10009 | *argsz = arg.sigmask_sz; |
| 10010 | *ts = u64_to_user_ptr(arg.ts); |
| 10011 | return 0; |
| 10012 | } |
| 10013 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10014 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10015 | u32, min_complete, u32, flags, const void __user *, argp, |
| 10016 | size_t, argsz) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10017 | { |
| 10018 | struct io_ring_ctx *ctx; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10019 | int submitted = 0; |
| 10020 | struct fd f; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10021 | long ret; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10022 | |
Jens Axboe | 4c6e277 | 2020-07-01 11:29:10 -0600 | [diff] [blame] | 10023 | io_run_task_work(); |
Jens Axboe | b41e985 | 2020-02-17 09:52:41 -0700 | [diff] [blame] | 10024 | |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10025 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
| 10026 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10027 | return -EINVAL; |
| 10028 | |
| 10029 | f = fdget(fd); |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10030 | if (unlikely(!f.file)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10031 | return -EBADF; |
| 10032 | |
| 10033 | ret = -EOPNOTSUPP; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10034 | if (unlikely(f.file->f_op != &io_uring_fops)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10035 | goto out_fput; |
| 10036 | |
| 10037 | ret = -ENXIO; |
| 10038 | ctx = f.file->private_data; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10039 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10040 | goto out_fput; |
| 10041 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10042 | ret = -EBADFD; |
Pavel Begunkov | 33f993d | 2021-03-19 17:22:30 +0000 | [diff] [blame] | 10043 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10044 | goto out; |
| 10045 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10046 | /* |
| 10047 | * For SQ polling, the thread will do all submissions and completions. |
| 10048 | * Just return the requested submit count, and wake the thread if |
| 10049 | * we were asked to. |
| 10050 | */ |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10051 | ret = 0; |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10052 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
Pavel Begunkov | 90f6736 | 2021-08-09 20:18:12 +0100 | [diff] [blame] | 10053 | io_cqring_overflow_flush(ctx); |
Pavel Begunkov | 89448c4 | 2020-12-17 00:24:39 +0000 | [diff] [blame] | 10054 | |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10055 | if (unlikely(ctx->sq_data->thread == NULL)) { |
| 10056 | ret = -EOWNERDEAD; |
Stefan Metzmacher | 0414748 | 2021-03-07 11:54:29 +0100 | [diff] [blame] | 10057 | goto out; |
Jens Axboe | 21f9652 | 2021-08-14 09:04:40 -0600 | [diff] [blame] | 10058 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10059 | if (flags & IORING_ENTER_SQ_WAKEUP) |
Jens Axboe | 534ca6d | 2020-09-02 13:52:19 -0600 | [diff] [blame] | 10060 | wake_up(&ctx->sq_data->wait); |
Pavel Begunkov | d9d0521 | 2021-01-08 20:57:25 +0000 | [diff] [blame] | 10061 | if (flags & IORING_ENTER_SQ_WAIT) { |
| 10062 | ret = io_sqpoll_wait_sq(ctx); |
| 10063 | if (ret) |
| 10064 | goto out; |
| 10065 | } |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10066 | submitted = to_submit; |
Jens Axboe | b2a9ead | 2019-09-12 14:19:16 -0600 | [diff] [blame] | 10067 | } else if (to_submit) { |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10068 | ret = io_uring_add_tctx_node(ctx); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10069 | if (unlikely(ret)) |
| 10070 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10071 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 0f21220 | 2020-09-13 13:09:39 -0600 | [diff] [blame] | 10072 | submitted = io_submit_sqes(ctx, to_submit); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10073 | mutex_unlock(&ctx->uring_lock); |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10074 | |
| 10075 | if (submitted != to_submit) |
| 10076 | goto out; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10077 | } |
| 10078 | if (flags & IORING_ENTER_GETEVENTS) { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10079 | const sigset_t __user *sig; |
| 10080 | struct __kernel_timespec __user *ts; |
| 10081 | |
| 10082 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); |
| 10083 | if (unlikely(ret)) |
| 10084 | goto out; |
| 10085 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10086 | min_complete = min(min_complete, ctx->cq_entries); |
| 10087 | |
Xiaoguang Wang | 32b2244 | 2020-03-11 09:26:09 +0800 | [diff] [blame] | 10088 | /* |
| 10089 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user |
| 10090 | * space applications don't need to do io completion events |
| 10091 | * polling again, they can rely on io_sq_thread to do polling |
| 10092 | * work, which can reduce cpu usage and uring_lock contention. |
| 10093 | */ |
| 10094 | if (ctx->flags & IORING_SETUP_IOPOLL && |
| 10095 | !(ctx->flags & IORING_SETUP_SQPOLL)) { |
Pavel Begunkov | 7668b92 | 2020-07-07 16:36:21 +0300 | [diff] [blame] | 10096 | ret = io_iopoll_check(ctx, min_complete); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10097 | } else { |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10098 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
Jens Axboe | def596e | 2019-01-09 08:59:42 -0700 | [diff] [blame] | 10099 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10100 | } |
| 10101 | |
Pavel Begunkov | 7c504e65 | 2019-12-18 19:53:45 +0300 | [diff] [blame] | 10102 | out: |
Pavel Begunkov | 6805b32 | 2019-10-08 02:18:42 +0300 | [diff] [blame] | 10103 | percpu_ref_put(&ctx->refs); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10104 | out_fput: |
| 10105 | fdput(f); |
| 10106 | return submitted ? submitted : ret; |
| 10107 | } |
| 10108 | |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10109 | #ifdef CONFIG_PROC_FS |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10110 | 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] | 10111 | const struct cred *cred) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10112 | { |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10113 | struct user_namespace *uns = seq_user_ns(m); |
| 10114 | struct group_info *gi; |
| 10115 | kernel_cap_t cap; |
| 10116 | unsigned __capi; |
| 10117 | int g; |
| 10118 | |
| 10119 | seq_printf(m, "%5d\n", id); |
| 10120 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); |
| 10121 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); |
| 10122 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); |
| 10123 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); |
| 10124 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); |
| 10125 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); |
| 10126 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); |
| 10127 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); |
| 10128 | seq_puts(m, "\n\tGroups:\t"); |
| 10129 | gi = cred->group_info; |
| 10130 | for (g = 0; g < gi->ngroups; g++) { |
| 10131 | seq_put_decimal_ull(m, g ? " " : "", |
| 10132 | from_kgid_munged(uns, gi->gid[g])); |
| 10133 | } |
| 10134 | seq_puts(m, "\n\tCapEff:\t"); |
| 10135 | cap = cred->cap_effective; |
| 10136 | CAP_FOR_EACH_U32(__capi) |
| 10137 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); |
| 10138 | seq_putc(m, '\n'); |
| 10139 | return 0; |
| 10140 | } |
| 10141 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10142 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
| 10143 | struct seq_file *m) |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10144 | { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10145 | struct io_sq_data *sq = NULL; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10146 | struct io_overflow_cqe *ocqe; |
| 10147 | struct io_rings *r = ctx->rings; |
| 10148 | 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] | 10149 | unsigned int sq_head = READ_ONCE(r->sq.head); |
| 10150 | unsigned int sq_tail = READ_ONCE(r->sq.tail); |
| 10151 | unsigned int cq_head = READ_ONCE(r->cq.head); |
| 10152 | unsigned int cq_tail = READ_ONCE(r->cq.tail); |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10153 | unsigned int sq_entries, cq_entries; |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10154 | bool has_lock; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10155 | unsigned int i; |
| 10156 | |
| 10157 | /* |
| 10158 | * we may get imprecise sqe and cqe info if uring is actively running |
| 10159 | * since we get cached_sq_head and cached_cq_tail without uring_lock |
| 10160 | * and sq_tail and cq_head are changed by userspace. But it's ok since |
| 10161 | * we usually use these info when it is stuck. |
| 10162 | */ |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10163 | seq_printf(m, "SqMask:\t\t0x%x\n", sq_mask); |
| 10164 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
| 10165 | seq_printf(m, "SqTail:\t%u\n", sq_tail); |
| 10166 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); |
| 10167 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); |
| 10168 | seq_printf(m, "CqHead:\t%u\n", cq_head); |
| 10169 | seq_printf(m, "CqTail:\t%u\n", cq_tail); |
| 10170 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); |
| 10171 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); |
| 10172 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); |
| 10173 | for (i = 0; i < sq_entries; i++) { |
| 10174 | unsigned int entry = i + sq_head; |
| 10175 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); |
Jens Axboe | a195778 | 2021-11-05 09:31:05 -0600 | [diff] [blame] | 10176 | struct io_uring_sqe *sqe; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10177 | |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10178 | if (sq_idx > sq_mask) |
| 10179 | continue; |
| 10180 | sqe = &ctx->sq_sqes[sq_idx]; |
| 10181 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", |
| 10182 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, |
| 10183 | sqe->user_data); |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10184 | } |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10185 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
| 10186 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); |
| 10187 | for (i = 0; i < cq_entries; i++) { |
| 10188 | unsigned int entry = i + cq_head; |
| 10189 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10190 | |
| 10191 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", |
Jens Axboe | f75d118 | 2021-10-29 06:36:45 -0600 | [diff] [blame] | 10192 | entry & cq_mask, cqe->user_data, cqe->res, |
| 10193 | cqe->flags); |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10194 | } |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10195 | |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10196 | /* |
| 10197 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, |
| 10198 | * since fdinfo case grabs it in the opposite direction of normal use |
| 10199 | * cases. If we fail to get the lock, we just don't iterate any |
| 10200 | * structures that could be going away outside the io_uring mutex. |
| 10201 | */ |
| 10202 | has_lock = mutex_trylock(&ctx->uring_lock); |
| 10203 | |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10204 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10205 | sq = ctx->sq_data; |
Jens Axboe | 5f3f26f | 2021-02-25 10:17:46 -0700 | [diff] [blame] | 10206 | if (!sq->thread) |
| 10207 | sq = NULL; |
| 10208 | } |
Joseph Qi | dbbe9c6 | 2020-09-29 09:01:22 -0600 | [diff] [blame] | 10209 | |
| 10210 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); |
| 10211 | 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] | 10212 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10213 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
Jens Axboe | 7b29f92 | 2021-03-12 08:30:14 -0700 | [diff] [blame] | 10214 | struct file *f = io_file_from_index(ctx, i); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10215 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10216 | if (f) |
| 10217 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); |
| 10218 | else |
| 10219 | seq_printf(m, "%5u: <none>\n", i); |
| 10220 | } |
| 10221 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); |
Jens Axboe | fad8e0d | 2020-09-28 08:57:48 -0600 | [diff] [blame] | 10222 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
Pavel Begunkov | 41edf1a | 2021-04-25 14:32:23 +0100 | [diff] [blame] | 10223 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10224 | unsigned int len = buf->ubuf_end - buf->ubuf; |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10225 | |
Pavel Begunkov | 4751f53 | 2021-04-01 15:43:55 +0100 | [diff] [blame] | 10226 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10227 | } |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10228 | if (has_lock && !xa_empty(&ctx->personalities)) { |
| 10229 | unsigned long index; |
| 10230 | const struct cred *cred; |
| 10231 | |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10232 | seq_printf(m, "Personalities:\n"); |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10233 | xa_for_each(&ctx->personalities, index, cred) |
| 10234 | io_uring_show_cred(m, index, cred); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10235 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10236 | if (has_lock) |
| 10237 | mutex_unlock(&ctx->uring_lock); |
| 10238 | |
| 10239 | seq_puts(m, "PollList:\n"); |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10240 | spin_lock(&ctx->completion_lock); |
Jens Axboe | d7718a9 | 2020-02-14 22:23:12 -0700 | [diff] [blame] | 10241 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
| 10242 | struct hlist_head *list = &ctx->cancel_hash[i]; |
| 10243 | struct io_kiocb *req; |
| 10244 | |
| 10245 | hlist_for_each_entry(req, list, hash_node) |
| 10246 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, |
| 10247 | req->task->task_works != NULL); |
| 10248 | } |
Hao Xu | 83f8435 | 2021-09-13 21:08:54 +0800 | [diff] [blame] | 10249 | |
| 10250 | seq_puts(m, "CqOverflowList:\n"); |
| 10251 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { |
| 10252 | struct io_uring_cqe *cqe = &ocqe->cqe; |
| 10253 | |
| 10254 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", |
| 10255 | cqe->user_data, cqe->res, cqe->flags); |
| 10256 | |
| 10257 | } |
| 10258 | |
Jens Axboe | 79ebeae | 2021-08-10 15:18:27 -0600 | [diff] [blame] | 10259 | spin_unlock(&ctx->completion_lock); |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10260 | } |
| 10261 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10262 | 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] | 10263 | { |
| 10264 | struct io_ring_ctx *ctx = f->private_data; |
| 10265 | |
| 10266 | if (percpu_ref_tryget(&ctx->refs)) { |
| 10267 | __io_uring_show_fdinfo(ctx, m); |
| 10268 | percpu_ref_put(&ctx->refs); |
| 10269 | } |
| 10270 | } |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10271 | #endif |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10272 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10273 | static const struct file_operations io_uring_fops = { |
| 10274 | .release = io_uring_release, |
| 10275 | .mmap = io_uring_mmap, |
Roman Penyaev | 6c5c240 | 2019-11-28 12:53:22 +0100 | [diff] [blame] | 10276 | #ifndef CONFIG_MMU |
| 10277 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, |
| 10278 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, |
| 10279 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10280 | .poll = io_uring_poll, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10281 | #ifdef CONFIG_PROC_FS |
Jens Axboe | 87ce955 | 2020-01-30 08:25:34 -0700 | [diff] [blame] | 10282 | .show_fdinfo = io_uring_show_fdinfo, |
Tobias Klauser | bebdb65 | 2020-02-26 18:38:32 +0100 | [diff] [blame] | 10283 | #endif |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10284 | }; |
| 10285 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10286 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 10287 | struct io_uring_params *p) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10288 | { |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10289 | struct io_rings *rings; |
| 10290 | size_t size, sq_array_offset; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10291 | |
Jens Axboe | bd74048 | 2020-08-05 12:58:23 -0600 | [diff] [blame] | 10292 | /* make sure these are sane, as we already accounted them */ |
| 10293 | ctx->sq_entries = p->sq_entries; |
| 10294 | ctx->cq_entries = p->cq_entries; |
| 10295 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10296 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 10297 | if (size == SIZE_MAX) |
| 10298 | return -EOVERFLOW; |
| 10299 | |
| 10300 | rings = io_mem_alloc(size); |
| 10301 | if (!rings) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10302 | return -ENOMEM; |
| 10303 | |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10304 | ctx->rings = rings; |
| 10305 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 10306 | rings->sq_ring_mask = p->sq_entries - 1; |
| 10307 | rings->cq_ring_mask = p->cq_entries - 1; |
| 10308 | rings->sq_ring_entries = p->sq_entries; |
| 10309 | rings->cq_ring_entries = p->cq_entries; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10310 | |
| 10311 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10312 | if (size == SIZE_MAX) { |
| 10313 | io_mem_free(ctx->rings); |
| 10314 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10315 | return -EOVERFLOW; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10316 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10317 | |
| 10318 | ctx->sq_sqes = io_mem_alloc(size); |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10319 | if (!ctx->sq_sqes) { |
| 10320 | io_mem_free(ctx->rings); |
| 10321 | ctx->rings = NULL; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10322 | return -ENOMEM; |
Jens Axboe | eb065d3 | 2019-11-20 09:26:29 -0700 | [diff] [blame] | 10323 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10324 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10325 | return 0; |
| 10326 | } |
| 10327 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10328 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
| 10329 | { |
| 10330 | int ret, fd; |
| 10331 | |
| 10332 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 10333 | if (fd < 0) |
| 10334 | return fd; |
| 10335 | |
Pavel Begunkov | eef51da | 2021-06-14 02:36:15 +0100 | [diff] [blame] | 10336 | ret = io_uring_add_tctx_node(ctx); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10337 | if (ret) { |
| 10338 | put_unused_fd(fd); |
| 10339 | return ret; |
| 10340 | } |
| 10341 | fd_install(fd, file); |
| 10342 | return fd; |
| 10343 | } |
| 10344 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10345 | /* |
| 10346 | * Allocate an anonymous fd, this is what constitutes the application |
| 10347 | * visible backing of an io_uring instance. The application mmaps this |
| 10348 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 10349 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 10350 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10351 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10352 | { |
| 10353 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10354 | #if defined(CONFIG_UNIX) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10355 | int ret; |
| 10356 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10357 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 10358 | &ctx->ring_sock); |
| 10359 | if (ret) |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10360 | return ERR_PTR(ret); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10361 | #endif |
| 10362 | |
Paul Moore | 91a9ab7 | 2021-02-01 19:33:52 -0500 | [diff] [blame] | 10363 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
| 10364 | O_RDWR | O_CLOEXEC, NULL); |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10365 | #if defined(CONFIG_UNIX) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10366 | if (IS_ERR(file)) { |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10367 | sock_release(ctx->ring_sock); |
| 10368 | ctx->ring_sock = NULL; |
| 10369 | } else { |
| 10370 | ctx->ring_sock->file = file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10371 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10372 | #endif |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10373 | return file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10374 | } |
| 10375 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10376 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
| 10377 | struct io_uring_params __user *params) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10378 | { |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10379 | struct io_ring_ctx *ctx; |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10380 | struct file *file; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10381 | int ret; |
| 10382 | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10383 | if (!entries) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10384 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10385 | if (entries > IORING_MAX_ENTRIES) { |
| 10386 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10387 | return -EINVAL; |
| 10388 | entries = IORING_MAX_ENTRIES; |
| 10389 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10390 | |
| 10391 | /* |
| 10392 | * Use twice as many entries for the CQ ring. It's possible for the |
| 10393 | * application to drive a higher depth than the size of the SQ ring, |
| 10394 | * since the sqes are only used at submission time. This allows for |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10395 | * some flexibility in overcommitting a bit. If the application has |
| 10396 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number |
| 10397 | * of CQ ring entries manually. |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10398 | */ |
| 10399 | p->sq_entries = roundup_pow_of_two(entries); |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10400 | if (p->flags & IORING_SETUP_CQSIZE) { |
| 10401 | /* |
| 10402 | * If IORING_SETUP_CQSIZE is set, we do the same roundup |
| 10403 | * to a power-of-two, if it isn't already. We do NOT impose |
| 10404 | * any cq vs sq ring sizing. |
| 10405 | */ |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10406 | if (!p->cq_entries) |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10407 | return -EINVAL; |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10408 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
| 10409 | if (!(p->flags & IORING_SETUP_CLAMP)) |
| 10410 | return -EINVAL; |
| 10411 | p->cq_entries = IORING_MAX_CQ_ENTRIES; |
| 10412 | } |
Joseph Qi | eb2667b3 | 2020-11-24 15:03:03 +0800 | [diff] [blame] | 10413 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
| 10414 | if (p->cq_entries < p->sq_entries) |
| 10415 | return -EINVAL; |
Jens Axboe | 33a107f | 2019-10-04 12:10:03 -0600 | [diff] [blame] | 10416 | } else { |
| 10417 | p->cq_entries = 2 * p->sq_entries; |
| 10418 | } |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10419 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10420 | ctx = io_ring_ctx_alloc(p); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10421 | if (!ctx) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10422 | return -ENOMEM; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10423 | ctx->compat = in_compat_syscall(); |
Jens Axboe | 62e398b | 2021-02-21 16:19:37 -0700 | [diff] [blame] | 10424 | if (!capable(CAP_IPC_LOCK)) |
| 10425 | ctx->user = get_uid(current_user()); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10426 | |
| 10427 | /* |
| 10428 | * This is just grabbed for accounting purposes. When a process exits, |
| 10429 | * the mm is exited and dropped before the files, hence we need to hang |
| 10430 | * on to this mm purely for the purposes of being able to unaccount |
| 10431 | * memory (locked/pinned vm). It's not used for anything else. |
| 10432 | */ |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10433 | mmgrab(current->mm); |
Jens Axboe | 2aede0e | 2020-09-14 10:45:53 -0600 | [diff] [blame] | 10434 | ctx->mm_account = current->mm; |
Jens Axboe | 6b7898e | 2020-08-25 07:58:00 -0600 | [diff] [blame] | 10435 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10436 | ret = io_allocate_scq_urings(ctx, p); |
| 10437 | if (ret) |
| 10438 | goto err; |
| 10439 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10440 | ret = io_sq_offload_create(ctx, p); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10441 | if (ret) |
| 10442 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10443 | /* always set a rsrc node */ |
Pavel Begunkov | 47b228c | 2021-04-29 11:46:48 +0100 | [diff] [blame] | 10444 | ret = io_rsrc_node_switch_start(ctx); |
| 10445 | if (ret) |
| 10446 | goto err; |
Pavel Begunkov | eae071c | 2021-04-25 14:32:24 +0100 | [diff] [blame] | 10447 | io_rsrc_node_switch(ctx, NULL); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10448 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10449 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10450 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 10451 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 10452 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 10453 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 10454 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 10455 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 10456 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10457 | |
| 10458 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
Hristo Venev | 75b28af | 2019-08-26 17:23:46 +0000 | [diff] [blame] | 10459 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 10460 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 10461 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 10462 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 10463 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 10464 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
Stefano Garzarella | 0d9b5b3 | 2020-05-15 18:38:04 +0200 | [diff] [blame] | 10465 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
Jens Axboe | ac90f24 | 2019-09-06 10:26:21 -0600 | [diff] [blame] | 10466 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10467 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
| 10468 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 10469 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
Hao Xu | c73ebb6 | 2020-11-03 10:54:37 +0800 | [diff] [blame] | 10470 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
Pavel Begunkov | 9690557 | 2021-06-10 16:37:38 +0100 | [diff] [blame] | 10471 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
| 10472 | IORING_FEAT_RSRC_TAGS; |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10473 | |
| 10474 | if (copy_to_user(params, p, sizeof(*p))) { |
| 10475 | ret = -EFAULT; |
| 10476 | goto err; |
| 10477 | } |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10478 | |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10479 | file = io_uring_get_file(ctx); |
| 10480 | if (IS_ERR(file)) { |
| 10481 | ret = PTR_ERR(file); |
| 10482 | goto err; |
| 10483 | } |
| 10484 | |
Jens Axboe | d1719f7 | 2020-07-30 13:43:53 -0600 | [diff] [blame] | 10485 | /* |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10486 | * Install ring fd as the very last thing, so we don't risk someone |
| 10487 | * having closed it before we finish setup |
| 10488 | */ |
Pavel Begunkov | 9faadcc | 2020-12-21 18:34:05 +0000 | [diff] [blame] | 10489 | ret = io_uring_install_fd(ctx, file); |
| 10490 | if (ret < 0) { |
| 10491 | /* fput will clean it up */ |
| 10492 | fput(file); |
| 10493 | return ret; |
| 10494 | } |
Jens Axboe | 044c1ab | 2019-10-28 09:15:33 -0600 | [diff] [blame] | 10495 | |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 10496 | 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] | 10497 | return ret; |
| 10498 | err: |
| 10499 | io_ring_ctx_wait_and_kill(ctx); |
| 10500 | return ret; |
| 10501 | } |
| 10502 | |
| 10503 | /* |
| 10504 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 10505 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 10506 | * params structure passed in. |
| 10507 | */ |
| 10508 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 10509 | { |
| 10510 | struct io_uring_params p; |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10511 | int i; |
| 10512 | |
| 10513 | if (copy_from_user(&p, params, sizeof(p))) |
| 10514 | return -EFAULT; |
| 10515 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 10516 | if (p.resv[i]) |
| 10517 | return -EINVAL; |
| 10518 | } |
| 10519 | |
Jens Axboe | 6c271ce | 2019-01-10 11:22:30 -0700 | [diff] [blame] | 10520 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
Jens Axboe | 8110c1a | 2019-12-28 15:39:54 -0700 | [diff] [blame] | 10521 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10522 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
| 10523 | IORING_SETUP_R_DISABLED)) |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10524 | return -EINVAL; |
| 10525 | |
Xiaoguang Wang | 7f13657 | 2020-05-05 16:28:53 +0800 | [diff] [blame] | 10526 | return io_uring_create(entries, &p, params); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 10527 | } |
| 10528 | |
| 10529 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 10530 | struct io_uring_params __user *, params) |
| 10531 | { |
| 10532 | return io_uring_setup(entries, params); |
| 10533 | } |
| 10534 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10535 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
| 10536 | unsigned nr_args) |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10537 | { |
| 10538 | struct io_uring_probe *p; |
| 10539 | size_t size; |
| 10540 | int i, ret; |
| 10541 | |
| 10542 | size = struct_size(p, ops, nr_args); |
| 10543 | if (size == SIZE_MAX) |
| 10544 | return -EOVERFLOW; |
| 10545 | p = kzalloc(size, GFP_KERNEL); |
| 10546 | if (!p) |
| 10547 | return -ENOMEM; |
| 10548 | |
| 10549 | ret = -EFAULT; |
| 10550 | if (copy_from_user(p, arg, size)) |
| 10551 | goto out; |
| 10552 | ret = -EINVAL; |
| 10553 | if (memchr_inv(p, 0, size)) |
| 10554 | goto out; |
| 10555 | |
| 10556 | p->last_op = IORING_OP_LAST - 1; |
| 10557 | if (nr_args > IORING_OP_LAST) |
| 10558 | nr_args = IORING_OP_LAST; |
| 10559 | |
| 10560 | for (i = 0; i < nr_args; i++) { |
| 10561 | p->ops[i].op = i; |
| 10562 | if (!io_op_defs[i].not_supported) |
| 10563 | p->ops[i].flags = IO_URING_OP_SUPPORTED; |
| 10564 | } |
| 10565 | p->ops_len = i; |
| 10566 | |
| 10567 | ret = 0; |
| 10568 | if (copy_to_user(arg, p, size)) |
| 10569 | ret = -EFAULT; |
| 10570 | out: |
| 10571 | kfree(p); |
| 10572 | return ret; |
| 10573 | } |
| 10574 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10575 | static int io_register_personality(struct io_ring_ctx *ctx) |
| 10576 | { |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10577 | const struct cred *creds; |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10578 | u32 id; |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10579 | int ret; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10580 | |
Jens Axboe | 4379bf8 | 2021-02-15 13:40:22 -0700 | [diff] [blame] | 10581 | creds = get_current_cred(); |
Jens Axboe | 1e6fa52 | 2020-10-15 08:46:24 -0600 | [diff] [blame] | 10582 | |
Matthew Wilcox (Oracle) | 61cf937 | 2021-03-08 14:16:16 +0000 | [diff] [blame] | 10583 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
| 10584 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); |
Jens Axboe | a30f895 | 2021-08-20 14:53:59 -0600 | [diff] [blame] | 10585 | if (ret < 0) { |
| 10586 | put_cred(creds); |
| 10587 | return ret; |
| 10588 | } |
| 10589 | return id; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10590 | } |
| 10591 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10592 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
| 10593 | void __user *arg, unsigned int nr_args) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10594 | { |
| 10595 | struct io_uring_restriction *res; |
| 10596 | size_t size; |
| 10597 | int i, ret; |
| 10598 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10599 | /* Restrictions allowed only if rings started disabled */ |
| 10600 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10601 | return -EBADFD; |
| 10602 | |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10603 | /* We allow only a single restrictions registration */ |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10604 | if (ctx->restrictions.registered) |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10605 | return -EBUSY; |
| 10606 | |
| 10607 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) |
| 10608 | return -EINVAL; |
| 10609 | |
| 10610 | size = array_size(nr_args, sizeof(*res)); |
| 10611 | if (size == SIZE_MAX) |
| 10612 | return -EOVERFLOW; |
| 10613 | |
| 10614 | res = memdup_user(arg, size); |
| 10615 | if (IS_ERR(res)) |
| 10616 | return PTR_ERR(res); |
| 10617 | |
| 10618 | ret = 0; |
| 10619 | |
| 10620 | for (i = 0; i < nr_args; i++) { |
| 10621 | switch (res[i].opcode) { |
| 10622 | case IORING_RESTRICTION_REGISTER_OP: |
| 10623 | if (res[i].register_op >= IORING_REGISTER_LAST) { |
| 10624 | ret = -EINVAL; |
| 10625 | goto out; |
| 10626 | } |
| 10627 | |
| 10628 | __set_bit(res[i].register_op, |
| 10629 | ctx->restrictions.register_op); |
| 10630 | break; |
| 10631 | case IORING_RESTRICTION_SQE_OP: |
| 10632 | if (res[i].sqe_op >= IORING_OP_LAST) { |
| 10633 | ret = -EINVAL; |
| 10634 | goto out; |
| 10635 | } |
| 10636 | |
| 10637 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); |
| 10638 | break; |
| 10639 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: |
| 10640 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; |
| 10641 | break; |
| 10642 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: |
| 10643 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; |
| 10644 | break; |
| 10645 | default: |
| 10646 | ret = -EINVAL; |
| 10647 | goto out; |
| 10648 | } |
| 10649 | } |
| 10650 | |
| 10651 | out: |
| 10652 | /* Reset all restrictions if an error happened */ |
| 10653 | if (ret != 0) |
| 10654 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); |
| 10655 | else |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10656 | ctx->restrictions.registered = true; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 10657 | |
| 10658 | kfree(res); |
| 10659 | return ret; |
| 10660 | } |
| 10661 | |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10662 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
| 10663 | { |
| 10664 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) |
| 10665 | return -EBADFD; |
| 10666 | |
| 10667 | if (ctx->restrictions.registered) |
| 10668 | ctx->restricted = 1; |
| 10669 | |
Pavel Begunkov | 0298ef9 | 2021-03-08 13:20:57 +0000 | [diff] [blame] | 10670 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
| 10671 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) |
| 10672 | wake_up(&ctx->sq_data->wait); |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 10673 | return 0; |
| 10674 | } |
| 10675 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10676 | 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] | 10677 | struct io_uring_rsrc_update2 *up, |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10678 | unsigned nr_args) |
| 10679 | { |
| 10680 | __u32 tmp; |
| 10681 | int err; |
| 10682 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10683 | if (up->resv) |
| 10684 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10685 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
| 10686 | return -EOVERFLOW; |
| 10687 | err = io_rsrc_node_switch_start(ctx); |
| 10688 | if (err) |
| 10689 | return err; |
| 10690 | |
Pavel Begunkov | fdecb66 | 2021-04-25 14:32:20 +0100 | [diff] [blame] | 10691 | switch (type) { |
| 10692 | case IORING_RSRC_FILE: |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10693 | return __io_sqe_files_update(ctx, up, nr_args); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10694 | case IORING_RSRC_BUFFER: |
| 10695 | return __io_sqe_buffers_update(ctx, up, nr_args); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10696 | } |
| 10697 | return -EINVAL; |
| 10698 | } |
| 10699 | |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10700 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
| 10701 | unsigned nr_args) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10702 | { |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10703 | struct io_uring_rsrc_update2 up; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10704 | |
| 10705 | if (!nr_args) |
| 10706 | return -EINVAL; |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10707 | memset(&up, 0, sizeof(up)); |
| 10708 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) |
| 10709 | return -EFAULT; |
| 10710 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); |
| 10711 | } |
| 10712 | |
| 10713 | 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] | 10714 | unsigned size, unsigned type) |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10715 | { |
| 10716 | struct io_uring_rsrc_update2 up; |
| 10717 | |
| 10718 | if (size != sizeof(up)) |
| 10719 | return -EINVAL; |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10720 | if (copy_from_user(&up, arg, sizeof(up))) |
| 10721 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10722 | if (!up.nr || up.resv) |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10723 | return -EINVAL; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10724 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
Pavel Begunkov | 98f0b3b | 2021-04-25 14:32:19 +0100 | [diff] [blame] | 10725 | } |
| 10726 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10727 | 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] | 10728 | unsigned int size, unsigned int type) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10729 | { |
| 10730 | struct io_uring_rsrc_register rr; |
| 10731 | |
| 10732 | /* keep it extendible */ |
| 10733 | if (size != sizeof(rr)) |
| 10734 | return -EINVAL; |
| 10735 | |
| 10736 | memset(&rr, 0, sizeof(rr)); |
| 10737 | if (copy_from_user(&rr, arg, size)) |
| 10738 | return -EFAULT; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10739 | if (!rr.nr || rr.resv || rr.resv2) |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10740 | return -EINVAL; |
| 10741 | |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10742 | switch (type) { |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10743 | case IORING_RSRC_FILE: |
| 10744 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), |
| 10745 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10746 | case IORING_RSRC_BUFFER: |
| 10747 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), |
| 10748 | rr.nr, u64_to_user_ptr(rr.tags)); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10749 | } |
| 10750 | return -EINVAL; |
| 10751 | } |
| 10752 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10753 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
| 10754 | void __user *arg, unsigned len) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10755 | { |
| 10756 | struct io_uring_task *tctx = current->io_uring; |
| 10757 | cpumask_var_t new_mask; |
| 10758 | int ret; |
| 10759 | |
| 10760 | if (!tctx || !tctx->io_wq) |
| 10761 | return -EINVAL; |
| 10762 | |
| 10763 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
| 10764 | return -ENOMEM; |
| 10765 | |
| 10766 | cpumask_clear(new_mask); |
| 10767 | if (len > cpumask_size()) |
| 10768 | len = cpumask_size(); |
| 10769 | |
| 10770 | if (copy_from_user(new_mask, arg, len)) { |
| 10771 | free_cpumask_var(new_mask); |
| 10772 | return -EFAULT; |
| 10773 | } |
| 10774 | |
| 10775 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); |
| 10776 | free_cpumask_var(new_mask); |
| 10777 | return ret; |
| 10778 | } |
| 10779 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10780 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10781 | { |
| 10782 | struct io_uring_task *tctx = current->io_uring; |
| 10783 | |
| 10784 | if (!tctx || !tctx->io_wq) |
| 10785 | return -EINVAL; |
| 10786 | |
| 10787 | return io_wq_cpu_affinity(tctx->io_wq, NULL); |
| 10788 | } |
| 10789 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10790 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
| 10791 | void __user *arg) |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10792 | __must_hold(&ctx->uring_lock) |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10793 | { |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10794 | struct io_tctx_node *node; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10795 | struct io_uring_task *tctx = NULL; |
| 10796 | struct io_sq_data *sqd = NULL; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10797 | __u32 new_count[2]; |
| 10798 | int i, ret; |
| 10799 | |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10800 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
| 10801 | return -EFAULT; |
| 10802 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10803 | if (new_count[i] > INT_MAX) |
| 10804 | return -EINVAL; |
| 10805 | |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10806 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 10807 | sqd = ctx->sq_data; |
| 10808 | if (sqd) { |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10809 | /* |
| 10810 | * Observe the correct sqd->lock -> ctx->uring_lock |
| 10811 | * ordering. Fine to drop uring_lock here, we hold |
| 10812 | * a ref to the ctx. |
| 10813 | */ |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10814 | refcount_inc(&sqd->refs); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10815 | mutex_unlock(&ctx->uring_lock); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10816 | mutex_lock(&sqd->lock); |
Jens Axboe | 009ad9f | 2021-09-08 19:07:26 -0600 | [diff] [blame] | 10817 | mutex_lock(&ctx->uring_lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10818 | if (sqd->thread) |
| 10819 | tctx = sqd->thread->io_uring; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10820 | } |
| 10821 | } else { |
| 10822 | tctx = current->io_uring; |
| 10823 | } |
| 10824 | |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10825 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10826 | |
Pavel Begunkov | bad119b | 2021-11-08 15:10:03 +0000 | [diff] [blame] | 10827 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10828 | if (new_count[i]) |
| 10829 | ctx->iowq_limits[i] = new_count[i]; |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10830 | ctx->iowq_limits_set = true; |
| 10831 | |
Pavel Begunkov | e139a1e | 2021-10-19 23:43:46 +0100 | [diff] [blame] | 10832 | if (tctx && tctx->io_wq) { |
| 10833 | ret = io_wq_max_workers(tctx->io_wq, new_count); |
| 10834 | if (ret) |
| 10835 | goto err; |
| 10836 | } else { |
| 10837 | memset(new_count, 0, sizeof(new_count)); |
| 10838 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10839 | |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10840 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10841 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10842 | io_put_sq_data(sqd); |
| 10843 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10844 | |
| 10845 | if (copy_to_user(arg, new_count, sizeof(new_count))) |
| 10846 | return -EFAULT; |
| 10847 | |
Pavel Begunkov | b22fa62 | 2021-10-21 13:20:29 +0100 | [diff] [blame] | 10848 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
| 10849 | if (sqd) |
| 10850 | return 0; |
| 10851 | |
| 10852 | /* now propagate the restriction to all registered users */ |
| 10853 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { |
| 10854 | struct io_uring_task *tctx = node->task->io_uring; |
| 10855 | |
| 10856 | if (WARN_ON_ONCE(!tctx->io_wq)) |
| 10857 | continue; |
| 10858 | |
| 10859 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
| 10860 | new_count[i] = ctx->iowq_limits[i]; |
| 10861 | /* ignore errors, it always returns zero anyway */ |
| 10862 | (void)io_wq_max_workers(tctx->io_wq, new_count); |
| 10863 | } |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10864 | return 0; |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10865 | err: |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10866 | if (sqd) { |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10867 | mutex_unlock(&sqd->lock); |
Jens Axboe | 41d3a6b | 2021-09-13 13:08:51 -0600 | [diff] [blame] | 10868 | io_put_sq_data(sqd); |
| 10869 | } |
Jens Axboe | fa84693 | 2021-09-01 14:15:59 -0600 | [diff] [blame] | 10870 | return ret; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10871 | } |
| 10872 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10873 | static bool io_register_op_must_quiesce(int op) |
| 10874 | { |
| 10875 | switch (op) { |
Bijan Mottahedeh | bd54b6f | 2021-04-25 14:32:25 +0100 | [diff] [blame] | 10876 | case IORING_REGISTER_BUFFERS: |
| 10877 | case IORING_UNREGISTER_BUFFERS: |
Pavel Begunkov | f4f7d21 | 2021-04-01 15:44:02 +0100 | [diff] [blame] | 10878 | case IORING_REGISTER_FILES: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10879 | case IORING_UNREGISTER_FILES: |
| 10880 | case IORING_REGISTER_FILES_UPDATE: |
| 10881 | case IORING_REGISTER_PROBE: |
| 10882 | case IORING_REGISTER_PERSONALITY: |
| 10883 | case IORING_UNREGISTER_PERSONALITY: |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 10884 | case IORING_REGISTER_FILES2: |
| 10885 | case IORING_REGISTER_FILES_UPDATE2: |
| 10886 | case IORING_REGISTER_BUFFERS2: |
| 10887 | case IORING_REGISTER_BUFFERS_UPDATE: |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 10888 | case IORING_REGISTER_IOWQ_AFF: |
| 10889 | case IORING_UNREGISTER_IOWQ_AFF: |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 10890 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10891 | return false; |
| 10892 | default: |
| 10893 | return true; |
| 10894 | } |
| 10895 | } |
| 10896 | |
Pavel Begunkov | c072481 | 2021-10-04 20:02:54 +0100 | [diff] [blame] | 10897 | static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10898 | { |
| 10899 | long ret; |
| 10900 | |
| 10901 | percpu_ref_kill(&ctx->refs); |
| 10902 | |
| 10903 | /* |
| 10904 | * Drop uring mutex before waiting for references to exit. If another |
| 10905 | * thread is currently inside io_uring_enter() it might need to grab the |
| 10906 | * uring_lock to make progress. If we hold it here across the drain |
| 10907 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 10908 | * no new references will come in after we've killed the percpu ref. |
| 10909 | */ |
| 10910 | mutex_unlock(&ctx->uring_lock); |
| 10911 | do { |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10912 | ret = wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); |
| 10913 | if (ret) { |
| 10914 | ret = min(0L, ret); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10915 | break; |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10916 | } |
| 10917 | |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10918 | ret = io_run_task_work_sig(); |
Pavel Begunkov | 37f0e76 | 2021-10-04 20:02:53 +0100 | [diff] [blame] | 10919 | io_req_caches_free(ctx); |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10920 | } while (ret >= 0); |
| 10921 | mutex_lock(&ctx->uring_lock); |
| 10922 | |
| 10923 | if (ret) |
| 10924 | io_refs_resurrect(&ctx->refs, &ctx->ref_comp); |
| 10925 | return ret; |
| 10926 | } |
| 10927 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10928 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 10929 | void __user *arg, unsigned nr_args) |
Jens Axboe | b19062a | 2019-04-15 10:49:38 -0600 | [diff] [blame] | 10930 | __releases(ctx->uring_lock) |
| 10931 | __acquires(ctx->uring_lock) |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10932 | { |
| 10933 | int ret; |
| 10934 | |
Jens Axboe | 35fa71a | 2019-04-22 10:23:23 -0600 | [diff] [blame] | 10935 | /* |
| 10936 | * We're inside the ring mutex, if the ref is already dying, then |
| 10937 | * someone else killed the ctx or is already going through |
| 10938 | * io_uring_register(). |
| 10939 | */ |
| 10940 | if (percpu_ref_is_dying(&ctx->refs)) |
| 10941 | return -ENXIO; |
| 10942 | |
Pavel Begunkov | 75c4021 | 2021-04-15 13:07:40 +0100 | [diff] [blame] | 10943 | if (ctx->restricted) { |
| 10944 | if (opcode >= IORING_REGISTER_LAST) |
| 10945 | return -EINVAL; |
| 10946 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); |
| 10947 | if (!test_bit(opcode, ctx->restrictions.register_op)) |
| 10948 | return -EACCES; |
| 10949 | } |
| 10950 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 10951 | if (io_register_op_must_quiesce(opcode)) { |
Pavel Begunkov | e73c5c7 | 2021-08-09 13:04:12 +0100 | [diff] [blame] | 10952 | ret = io_ctx_quiesce(ctx); |
| 10953 | if (ret) |
Pavel Begunkov | f70865d | 2021-04-11 01:46:40 +0100 | [diff] [blame] | 10954 | return ret; |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 10955 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10956 | |
| 10957 | switch (opcode) { |
| 10958 | case IORING_REGISTER_BUFFERS: |
Pavel Begunkov | 634d00d | 2021-04-25 14:32:26 +0100 | [diff] [blame] | 10959 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10960 | break; |
| 10961 | case IORING_UNREGISTER_BUFFERS: |
| 10962 | ret = -EINVAL; |
| 10963 | if (arg || nr_args) |
| 10964 | break; |
Bijan Mottahedeh | 0a96bbe | 2021-01-06 12:39:10 -0800 | [diff] [blame] | 10965 | ret = io_sqe_buffers_unregister(ctx); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 10966 | break; |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10967 | case IORING_REGISTER_FILES: |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 10968 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
Jens Axboe | 6b06314 | 2019-01-10 22:13:58 -0700 | [diff] [blame] | 10969 | break; |
| 10970 | case IORING_UNREGISTER_FILES: |
| 10971 | ret = -EINVAL; |
| 10972 | if (arg || nr_args) |
| 10973 | break; |
| 10974 | ret = io_sqe_files_unregister(ctx); |
| 10975 | break; |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10976 | case IORING_REGISTER_FILES_UPDATE: |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 10977 | ret = io_register_files_update(ctx, arg, nr_args); |
Jens Axboe | c3a31e6 | 2019-10-03 13:59:56 -0600 | [diff] [blame] | 10978 | break; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10979 | case IORING_REGISTER_EVENTFD: |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10980 | case IORING_REGISTER_EVENTFD_ASYNC: |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10981 | ret = -EINVAL; |
| 10982 | if (nr_args != 1) |
| 10983 | break; |
| 10984 | ret = io_eventfd_register(ctx, arg); |
Jens Axboe | f2842ab | 2020-01-08 11:04:00 -0700 | [diff] [blame] | 10985 | if (ret) |
| 10986 | break; |
| 10987 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) |
| 10988 | ctx->eventfd_async = 1; |
| 10989 | else |
| 10990 | ctx->eventfd_async = 0; |
Jens Axboe | 9b40284 | 2019-04-11 11:45:41 -0600 | [diff] [blame] | 10991 | break; |
| 10992 | case IORING_UNREGISTER_EVENTFD: |
| 10993 | ret = -EINVAL; |
| 10994 | if (arg || nr_args) |
| 10995 | break; |
| 10996 | ret = io_eventfd_unregister(ctx); |
| 10997 | break; |
Jens Axboe | 66f4af9 | 2020-01-16 15:36:52 -0700 | [diff] [blame] | 10998 | case IORING_REGISTER_PROBE: |
| 10999 | ret = -EINVAL; |
| 11000 | if (!arg || nr_args > 256) |
| 11001 | break; |
| 11002 | ret = io_probe(ctx, arg, nr_args); |
| 11003 | break; |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11004 | case IORING_REGISTER_PERSONALITY: |
| 11005 | ret = -EINVAL; |
| 11006 | if (arg || nr_args) |
| 11007 | break; |
| 11008 | ret = io_register_personality(ctx); |
| 11009 | break; |
| 11010 | case IORING_UNREGISTER_PERSONALITY: |
| 11011 | ret = -EINVAL; |
| 11012 | if (arg) |
| 11013 | break; |
| 11014 | ret = io_unregister_personality(ctx, nr_args); |
| 11015 | break; |
Stefano Garzarella | 7e84e1c | 2020-08-27 16:58:31 +0200 | [diff] [blame] | 11016 | case IORING_REGISTER_ENABLE_RINGS: |
| 11017 | ret = -EINVAL; |
| 11018 | if (arg || nr_args) |
| 11019 | break; |
| 11020 | ret = io_register_enable_rings(ctx); |
| 11021 | break; |
Stefano Garzarella | 21b55db | 2020-08-27 16:58:30 +0200 | [diff] [blame] | 11022 | case IORING_REGISTER_RESTRICTIONS: |
| 11023 | ret = io_register_restrictions(ctx, arg, nr_args); |
| 11024 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11025 | case IORING_REGISTER_FILES2: |
| 11026 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); |
Pavel Begunkov | 792e358 | 2021-04-25 14:32:21 +0100 | [diff] [blame] | 11027 | break; |
Pavel Begunkov | 992da01 | 2021-06-10 16:37:37 +0100 | [diff] [blame] | 11028 | case IORING_REGISTER_FILES_UPDATE2: |
| 11029 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11030 | IORING_RSRC_FILE); |
| 11031 | break; |
| 11032 | case IORING_REGISTER_BUFFERS2: |
| 11033 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); |
| 11034 | break; |
| 11035 | case IORING_REGISTER_BUFFERS_UPDATE: |
| 11036 | ret = io_register_rsrc_update(ctx, arg, nr_args, |
| 11037 | IORING_RSRC_BUFFER); |
Pavel Begunkov | c3bdad0 | 2021-04-25 14:32:22 +0100 | [diff] [blame] | 11038 | break; |
Jens Axboe | fe76421 | 2021-06-17 10:19:54 -0600 | [diff] [blame] | 11039 | case IORING_REGISTER_IOWQ_AFF: |
| 11040 | ret = -EINVAL; |
| 11041 | if (!arg || !nr_args) |
| 11042 | break; |
| 11043 | ret = io_register_iowq_aff(ctx, arg, nr_args); |
| 11044 | break; |
| 11045 | case IORING_UNREGISTER_IOWQ_AFF: |
| 11046 | ret = -EINVAL; |
| 11047 | if (arg || nr_args) |
| 11048 | break; |
| 11049 | ret = io_unregister_iowq_aff(ctx); |
| 11050 | break; |
Jens Axboe | 2e48005 | 2021-08-27 11:33:19 -0600 | [diff] [blame] | 11051 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
| 11052 | ret = -EINVAL; |
| 11053 | if (!arg || nr_args != 2) |
| 11054 | break; |
| 11055 | ret = io_register_iowq_max_workers(ctx, arg); |
| 11056 | break; |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11057 | default: |
| 11058 | ret = -EINVAL; |
| 11059 | break; |
| 11060 | } |
| 11061 | |
Jens Axboe | 071698e | 2020-01-28 10:04:42 -0700 | [diff] [blame] | 11062 | if (io_register_op_must_quiesce(opcode)) { |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11063 | /* bring the ctx back to life */ |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11064 | percpu_ref_reinit(&ctx->refs); |
Jens Axboe | 0f158b4 | 2020-05-14 17:18:39 -0600 | [diff] [blame] | 11065 | reinit_completion(&ctx->ref_comp); |
Jens Axboe | 05f3fb3 | 2019-12-09 11:22:50 -0700 | [diff] [blame] | 11066 | } |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11067 | return ret; |
| 11068 | } |
| 11069 | |
| 11070 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 11071 | void __user *, arg, unsigned int, nr_args) |
| 11072 | { |
| 11073 | struct io_ring_ctx *ctx; |
| 11074 | long ret = -EBADF; |
| 11075 | struct fd f; |
| 11076 | |
| 11077 | f = fdget(fd); |
| 11078 | if (!f.file) |
| 11079 | return -EBADF; |
| 11080 | |
| 11081 | ret = -EOPNOTSUPP; |
| 11082 | if (f.file->f_op != &io_uring_fops) |
| 11083 | goto out_fput; |
| 11084 | |
| 11085 | ctx = f.file->private_data; |
| 11086 | |
Pavel Begunkov | b6c23dd | 2021-02-20 15:17:18 +0000 | [diff] [blame] | 11087 | io_run_task_work(); |
| 11088 | |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11089 | mutex_lock(&ctx->uring_lock); |
| 11090 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 11091 | mutex_unlock(&ctx->uring_lock); |
Dmitrii Dolgov | c826bd7 | 2019-10-15 19:02:01 +0200 | [diff] [blame] | 11092 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
| 11093 | ctx->cq_ev_fd != NULL, ret); |
Jens Axboe | edafcce | 2019-01-09 09:16:05 -0700 | [diff] [blame] | 11094 | out_fput: |
| 11095 | fdput(f); |
| 11096 | return ret; |
| 11097 | } |
| 11098 | |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11099 | static int __init io_uring_init(void) |
| 11100 | { |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11101 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
| 11102 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ |
| 11103 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ |
| 11104 | } while (0) |
| 11105 | |
| 11106 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ |
| 11107 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) |
| 11108 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); |
| 11109 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); |
| 11110 | BUILD_BUG_SQE_ELEM(1, __u8, flags); |
| 11111 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); |
| 11112 | BUILD_BUG_SQE_ELEM(4, __s32, fd); |
| 11113 | BUILD_BUG_SQE_ELEM(8, __u64, off); |
| 11114 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); |
| 11115 | BUILD_BUG_SQE_ELEM(16, __u64, addr); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11116 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11117 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
| 11118 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); |
| 11119 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); |
| 11120 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); |
| 11121 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); |
Jiufei Xue | 5769a35 | 2020-06-17 17:53:55 +0800 | [diff] [blame] | 11122 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
| 11123 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11124 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
| 11125 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); |
| 11126 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); |
| 11127 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); |
| 11128 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); |
| 11129 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); |
| 11130 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); |
| 11131 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11132 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11133 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
| 11134 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11135 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11136 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
Pavel Begunkov | 7d67af2 | 2020-02-24 11:32:45 +0300 | [diff] [blame] | 11137 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
Pavel Begunkov | b944559 | 2021-08-25 12:25:45 +0100 | [diff] [blame] | 11138 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
Stefan Metzmacher | d7f62e8 | 2020-01-29 14:39:41 +0100 | [diff] [blame] | 11139 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11140 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
| 11141 | sizeof(struct io_uring_rsrc_update)); |
| 11142 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > |
| 11143 | sizeof(struct io_uring_rsrc_update2)); |
Pavel Begunkov | 90499ad | 2021-08-25 20:51:40 +0100 | [diff] [blame] | 11144 | |
| 11145 | /* ->buf_index is u16 */ |
| 11146 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); |
| 11147 | |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11148 | /* should fit into one byte */ |
| 11149 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); |
Pavel Begunkov | 68fe256 | 2021-09-15 12:03:38 +0100 | [diff] [blame] | 11150 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
| 11151 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); |
Pavel Begunkov | b0d658ec | 2021-04-27 16:13:53 +0100 | [diff] [blame] | 11152 | |
Jens Axboe | d365634 | 2019-12-18 09:50:26 -0700 | [diff] [blame] | 11153 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
Hao Xu | 32c2d33 | 2021-09-07 11:22:43 +0800 | [diff] [blame] | 11154 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
Pavel Begunkov | 16340ea | 2021-06-24 15:09:58 +0100 | [diff] [blame] | 11155 | |
Jens Axboe | 91f245d | 2021-02-09 13:48:50 -0700 | [diff] [blame] | 11156 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
| 11157 | SLAB_ACCOUNT); |
Jens Axboe | 2b188cc | 2019-01-07 10:46:33 -0700 | [diff] [blame] | 11158 | return 0; |
| 11159 | }; |
| 11160 | __initcall(io_uring_init); |