blob: a864a94364c6cd74603d565bacd1bc021ca12d83 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// 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ühler1e84b972019-04-24 23:54:16 +02007 * 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 Begunkovd068b502021-05-16 22:58:11 +010014 * through a control-dependency in io_get_cqe (smp_store_release to
Stefan Bühler1e84b972019-04-24 23:54:16 +020015 * 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 Axboe2b188cc2019-01-07 10:46:33 -070029 *
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 Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
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 Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
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 Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070076#include <linux/eventpoll.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030077#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070078#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060079#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060080#include <linux/io_uring.h>
Nadav Amitef98eb02021-08-07 17:13:41 -070081#include <linux/tracehook.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Olivier Langlois4ce8ad92021-06-23 11:50:18 -070093#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
Jens Axboe65e19f52019-10-26 07:20:21 -060094
wangyangbo187f08c2021-08-19 13:56:57 +080095/* only define max */
Pavel Begunkov042b0d82021-08-09 13:04:01 +010096#define IORING_MAX_FIXED_FILES (1U << 15)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020097#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
98 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -070099
wangyangbo187f08c2021-08-19 13:56:57 +0800100#define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100101#define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
102#define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
103
Pavel Begunkov489809e2021-05-14 12:06:44 +0100104#define IORING_MAX_REG_BUFFERS (1U << 14)
105
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000106#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
107 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
108 IOSQE_BUFFER_SELECT)
Pavel Begunkovc8543572021-06-17 18:14:04 +0100109#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
110 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000111
Pavel Begunkov09899b12021-06-14 02:36:22 +0100112#define IO_TCTX_REFS_CACHE_NR (1U << 10)
113
Jens Axboe2b188cc2019-01-07 10:46:33 -0700114struct io_uring {
115 u32 head ____cacheline_aligned_in_smp;
116 u32 tail ____cacheline_aligned_in_smp;
117};
118
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 * This data is shared with the application through the mmap at offsets
121 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 *
123 * The offsets to the member fields are published through struct
124 * io_sqring_offsets when calling io_uring_setup.
125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Head and tail offsets into the ring; the offsets need to be
129 * masked to get valid indices.
130 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 * The kernel controls head of the sq ring and the tail of the cq ring,
132 * and the application controls tail of the sq ring and the head of the
133 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000135 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 * ring_entries - 1)
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_ring_mask, cq_ring_mask;
141 /* Ring sizes (constant, power of 2) */
142 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Number of invalid entries dropped by the kernel due to
145 * invalid index stored in array
146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application (i.e. get number of "new events" by comparing to
149 * cached value).
150 *
151 * After a new SQ head value was read by the application this
152 * counter includes all submissions that were dropped reaching
153 * the new SQ head (and possibly more).
154 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000155 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200156 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200157 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application.
161 *
162 * The application needs a full memory barrier before checking
163 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200167 * Runtime CQ flags
168 *
169 * Written by the application, shouldn't be modified by the
170 * kernel.
171 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100172 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200173 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200174 * Number of completion events lost because the queue was full;
175 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800176 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200177 * the completion queue.
178 *
179 * Written by the kernel, shouldn't be modified by the
180 * application (i.e. get number of "new events" by comparing to
181 * cached value).
182 *
183 * As completion events come in out of order this counter is not
184 * ordered with any other data.
185 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000186 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200187 /*
188 * Ring buffer of completion events.
189 *
190 * The kernel writes completion events fresh every time they are
191 * produced, so the application is allowed to modify pending
192 * entries.
193 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000194 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700195};
196
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000197enum io_uring_cmd_flags {
198 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000199 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000200};
201
Jens Axboeedafcce2019-01-09 09:16:05 -0700202struct io_mapped_ubuf {
203 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100204 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700205 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600206 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100207 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700208};
209
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000210struct io_ring_ctx;
211
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000212struct io_overflow_cqe {
213 struct io_uring_cqe cqe;
214 struct list_head list;
215};
216
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100217struct io_fixed_file {
218 /* file * with additional FFS_* flags */
219 unsigned long file_ptr;
220};
221
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000222struct io_rsrc_put {
223 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100224 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000225 union {
226 void *rsrc;
227 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100228 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000229 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000230};
231
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100232struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100233 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700234};
235
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100236struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800237 struct percpu_ref refs;
238 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000239 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100240 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600241 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000242 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800243};
244
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100245typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
246
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100247struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700248 struct io_ring_ctx *ctx;
249
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100250 u64 **tags;
251 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100252 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100253 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700254 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800255 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700256};
257
Jens Axboe5a2e7452020-02-23 16:23:11 -0700258struct io_buffer {
259 struct list_head list;
260 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300261 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700262 __u16 bid;
263};
264
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200265struct io_restriction {
266 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
267 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
268 u8 sqe_flags_allowed;
269 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200270 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200271};
272
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700273enum {
274 IO_SQ_THREAD_SHOULD_STOP = 0,
275 IO_SQ_THREAD_SHOULD_PARK,
276};
277
Jens Axboe534ca6d2020-09-02 13:52:19 -0600278struct io_sq_data {
279 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000280 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000281 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600282
283 /* ctx's that are using this sqd */
284 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600285
Jens Axboe534ca6d2020-09-02 13:52:19 -0600286 struct task_struct *thread;
287 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800288
289 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700290 int sq_cpu;
291 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700292 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700293
294 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700295 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600296};
297
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000298#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000299#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000300#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000301
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000302struct io_submit_link {
303 struct io_kiocb *head;
304 struct io_kiocb *last;
305};
306
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000307struct io_submit_state {
308 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000309 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000310
311 /*
312 * io_kiocb alloc cache
313 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000314 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000315 unsigned int free_reqs;
316
317 bool plug_started;
318
319 /*
320 * Batch completion logic
321 */
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +0100322 struct io_kiocb *compl_reqs[IO_COMPL_BATCH];
323 unsigned int compl_nr;
324 /* inline/task_work completion list, under ->uring_lock */
325 struct list_head free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000326
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000327 unsigned int ios_left;
328};
329
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100331 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332 struct {
333 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100335 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800337 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800338 unsigned int drain_next: 1;
339 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200340 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100341 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100342 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100343 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100345 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100346 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100347 struct mutex uring_lock;
348
Hristo Venev75b28af2019-08-26 17:23:46 +0000349 /*
350 * Ring buffer of indices into array of io_uring_sqe, which is
351 * mmapped by the application using the IORING_OFF_SQES offset.
352 *
353 * This indirection could e.g. be used to assign fixed
354 * io_uring_sqe entries to operations and only submit them to
355 * the queue when needed.
356 *
357 * The kernel modifies neither the indices array nor the entries
358 * array.
359 */
360 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100361 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362 unsigned cached_sq_head;
363 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600364 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100365
366 /*
367 * Fixed resources fast path, should be accessed only under
368 * uring_lock, and updated through io_uring_register(2)
369 */
370 struct io_rsrc_node *rsrc_node;
371 struct io_file_table file_table;
372 unsigned nr_user_files;
373 unsigned nr_user_bufs;
374 struct io_mapped_ubuf **user_bufs;
375
376 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600377 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600378 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700379 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100380 struct xarray io_buffers;
381 struct xarray personalities;
382 u32 pers_next;
383 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700384 } ____cacheline_aligned_in_smp;
385
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100386 /* IRQ completion list, under ->completion_lock */
387 struct list_head locked_free_list;
388 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700389
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100390 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600391 struct io_sq_data *sq_data; /* if using sq thread polling */
392
Jens Axboe90554202020-09-03 12:12:41 -0600393 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600394 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000395
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100396 unsigned long check_cq_overflow;
397
Jens Axboe206aefd2019-11-07 18:27:42 -0700398 struct {
399 unsigned cached_cq_tail;
400 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700401 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100402 struct wait_queue_head poll_wait;
403 struct wait_queue_head cq_wait;
404 unsigned cq_extra;
405 atomic_t cq_timeouts;
406 struct fasync_struct *cq_fasync;
407 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700408 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409
410 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700411 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700412
Jens Axboe89850fc2021-08-10 15:11:51 -0600413 spinlock_t timeout_lock;
414
Jens Axboedef596e2019-01-09 08:59:42 -0700415 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300416 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700417 * io_uring instances that don't use IORING_SETUP_SQPOLL.
418 * For SQPOLL, only the single threaded io_sq_thread() will
419 * manipulate the list, hence no extra locking is needed there.
420 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300421 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700422 struct hlist_head *cancel_hash;
423 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800424 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700425 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600426
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200427 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700428
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100429 /* slow path rsrc auxilary data, used by update/register */
430 struct {
431 struct io_rsrc_node *rsrc_backup_node;
432 struct io_mapped_ubuf *dummy_ubuf;
433 struct io_rsrc_data *file_data;
434 struct io_rsrc_data *buf_data;
435
436 struct delayed_work rsrc_put_work;
437 struct llist_head rsrc_put_llist;
438 struct list_head rsrc_ref_list;
439 spinlock_t rsrc_ref_lock;
440 };
441
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700442 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100443 struct {
444 #if defined(CONFIG_UNIX)
445 struct socket *ring_sock;
446 #endif
447 /* hashed buffered write serialization */
448 struct io_wq_hash *hash_map;
449
450 /* Only used for accounting purposes */
451 struct user_struct *user;
452 struct mm_struct *mm_account;
453
454 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100455 struct llist_head fallback_llist;
456 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100457 struct work_struct exit_work;
458 struct list_head tctx_list;
459 struct completion ref_comp;
460 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100463struct io_uring_task {
464 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100465 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100466 struct xarray xa;
467 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100468 const struct io_ring_ctx *last;
469 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100470 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100471 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100472 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100473
474 spinlock_t task_lock;
475 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100476 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100477 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100478};
479
Jens Axboe09bb8392019-03-13 12:39:28 -0600480/*
481 * First field must be the file pointer in all the
482 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
483 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700484struct io_poll_iocb {
485 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000486 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700487 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600488 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700489 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700490 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700491};
492
Pavel Begunkov9d805892021-04-13 02:58:40 +0100493struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000494 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100495 u64 old_user_data;
496 u64 new_user_data;
497 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600498 bool update_events;
499 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000500};
501
Jens Axboeb5dba592019-12-11 14:02:38 -0700502struct io_close {
503 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700504 int fd;
505};
506
Jens Axboead8a48a2019-11-15 08:49:11 -0700507struct io_timeout_data {
508 struct io_kiocb *req;
509 struct hrtimer timer;
510 struct timespec64 ts;
511 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600512 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700513};
514
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700515struct io_accept {
516 struct file *file;
517 struct sockaddr __user *addr;
518 int __user *addr_len;
519 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100520 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600521 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700522};
523
524struct io_sync {
525 struct file *file;
526 loff_t len;
527 loff_t off;
528 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700529 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700530};
531
Jens Axboefbf23842019-12-17 18:45:56 -0700532struct io_cancel {
533 struct file *file;
534 u64 addr;
535};
536
Jens Axboeb29472e2019-12-17 18:50:29 -0700537struct io_timeout {
538 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300539 u32 off;
540 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300541 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000542 /* head of the link, used by linked timeouts only */
543 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600544 /* for linked completions */
545 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700546};
547
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100548struct io_timeout_rem {
549 struct file *file;
550 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000551
552 /* timeout update */
553 struct timespec64 ts;
554 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600555 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100556};
557
Jens Axboe9adbd452019-12-20 08:45:55 -0700558struct io_rw {
559 /* NOTE: kiocb has the file as the first member, so don't do it here */
560 struct kiocb kiocb;
561 u64 addr;
562 u64 len;
563};
564
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700565struct io_connect {
566 struct file *file;
567 struct sockaddr __user *addr;
568 int addr_len;
569};
570
Jens Axboee47293f2019-12-20 08:58:21 -0700571struct io_sr_msg {
572 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700573 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100574 struct compat_msghdr __user *umsg_compat;
575 struct user_msghdr __user *umsg;
576 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700577 };
Jens Axboee47293f2019-12-20 08:58:21 -0700578 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700579 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700580 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700581 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700582};
583
Jens Axboe15b71ab2019-12-11 11:20:36 -0700584struct io_open {
585 struct file *file;
586 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100587 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700588 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700589 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600590 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700591};
592
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000593struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700594 struct file *file;
595 u64 arg;
596 u32 nr_args;
597 u32 offset;
598};
599
Jens Axboe4840e412019-12-25 22:03:45 -0700600struct io_fadvise {
601 struct file *file;
602 u64 offset;
603 u32 len;
604 u32 advice;
605};
606
Jens Axboec1ca7572019-12-25 22:18:28 -0700607struct io_madvise {
608 struct file *file;
609 u64 addr;
610 u32 len;
611 u32 advice;
612};
613
Jens Axboe3e4827b2020-01-08 15:18:09 -0700614struct io_epoll {
615 struct file *file;
616 int epfd;
617 int op;
618 int fd;
619 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700620};
621
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300622struct io_splice {
623 struct file *file_out;
624 struct file *file_in;
625 loff_t off_out;
626 loff_t off_in;
627 u64 len;
628 unsigned int flags;
629};
630
Jens Axboeddf0322d2020-02-23 16:41:33 -0700631struct io_provide_buf {
632 struct file *file;
633 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100634 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700635 __u32 bgid;
636 __u16 nbufs;
637 __u16 bid;
638};
639
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700640struct io_statx {
641 struct file *file;
642 int dfd;
643 unsigned int mask;
644 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700645 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700646 struct statx __user *buffer;
647};
648
Jens Axboe36f4fa62020-09-05 11:14:22 -0600649struct io_shutdown {
650 struct file *file;
651 int how;
652};
653
Jens Axboe80a261f2020-09-28 14:23:58 -0600654struct io_rename {
655 struct file *file;
656 int old_dfd;
657 int new_dfd;
658 struct filename *oldpath;
659 struct filename *newpath;
660 int flags;
661};
662
Jens Axboe14a11432020-09-28 14:27:37 -0600663struct io_unlink {
664 struct file *file;
665 int dfd;
666 int flags;
667 struct filename *filename;
668};
669
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700670struct io_mkdir {
671 struct file *file;
672 int dfd;
673 umode_t mode;
674 struct filename *filename;
675};
676
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700677struct io_symlink {
678 struct file *file;
679 int new_dfd;
680 struct filename *oldpath;
681 struct filename *newpath;
682};
683
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700684struct io_hardlink {
685 struct file *file;
686 int old_dfd;
687 int new_dfd;
688 struct filename *oldpath;
689 struct filename *newpath;
690 int flags;
691};
692
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300693struct io_completion {
694 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000695 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300696};
697
Jens Axboef499a022019-12-02 16:28:46 -0700698struct io_async_connect {
699 struct sockaddr_storage address;
700};
701
Jens Axboe03b12302019-12-02 18:50:25 -0700702struct io_async_msghdr {
703 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000704 /* points to an allocated iov, if NULL we use fast_iov instead */
705 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700706 struct sockaddr __user *uaddr;
707 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700708 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700709};
710
Jens Axboef67676d2019-12-02 11:03:47 -0700711struct io_async_rw {
712 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600713 const struct iovec *free_iovec;
714 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600715 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600716 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700717};
718
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300719enum {
720 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
721 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
722 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
723 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
724 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700725 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300726
Pavel Begunkovdddca222021-04-27 16:13:52 +0100727 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100728 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300729 REQ_F_INFLIGHT_BIT,
730 REQ_F_CUR_POS_BIT,
731 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300732 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300733 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700734 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700735 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000736 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600737 REQ_F_REISSUE_BIT,
Pavel Begunkov8c130822021-03-22 01:58:32 +0000738 REQ_F_DONT_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100739 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100740 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100741 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700742 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100743 REQ_F_NOWAIT_READ_BIT,
744 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700745 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700746
747 /* not a real bit, just to check we're not overflowing the space */
748 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300749};
750
751enum {
752 /* ctx owns file */
753 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
754 /* drain existing IO first */
755 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
756 /* linked sqes */
757 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
758 /* doesn't sever on completion < 0 */
759 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
760 /* IOSQE_ASYNC */
761 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700762 /* IOSQE_BUFFER_SELECT */
763 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300764
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300765 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100766 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000767 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300768 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
769 /* read/write uses file position */
770 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
771 /* must not punt to workers */
772 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100773 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300774 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300775 /* needs cleanup */
776 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700777 /* already went through poll handler */
778 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700779 /* buffer already selected */
780 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000781 /* completion is deferred through io_comp_state */
782 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600783 /* caller should reissue async */
784 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov8c130822021-03-22 01:58:32 +0000785 /* don't attempt request reissue, see io_rw_reissue() */
786 REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700787 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100788 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700789 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100790 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700791 /* regular file */
792 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100793 /* has creds assigned */
794 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100795 /* skip refcounting if not set */
796 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100797 /* there is a linked timeout that has to be armed */
798 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700799};
800
801struct async_poll {
802 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600803 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300804};
805
Pavel Begunkovf237c302021-08-18 12:42:46 +0100806typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100807
Jens Axboe7cbf1722021-02-10 00:03:20 +0000808struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100809 union {
810 struct io_wq_work_node node;
811 struct llist_node fallback_node;
812 };
813 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000814};
815
Pavel Begunkov992da012021-06-10 16:37:37 +0100816enum {
817 IORING_RSRC_FILE = 0,
818 IORING_RSRC_BUFFER = 1,
819};
820
Jens Axboe09bb8392019-03-13 12:39:28 -0600821/*
822 * NOTE! Each of the iocb union members has the file pointer
823 * as the first entry in their struct definition. So you can
824 * access the file pointer through any of the sub-structs,
825 * or directly as just 'ki_filp' in this struct.
826 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700827struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700828 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600829 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700830 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700831 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100832 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700833 struct io_accept accept;
834 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700835 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700836 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100837 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700838 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700839 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700840 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700841 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000842 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700843 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700844 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700845 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300846 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700847 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700848 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600849 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600850 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600851 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700852 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700853 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700854 struct io_hardlink hardlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300855 /* use only after cleaning per-op data, see io_clean_op() */
856 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700857 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700858
Jens Axboee8c2bc12020-08-15 18:44:09 -0700859 /* opcode allocated if it needs to store data for async defer */
860 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700861 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800862 /* polled IO has completed */
863 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700864
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700865 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300866 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700867
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300868 struct io_ring_ctx *ctx;
869 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700870 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300871 struct task_struct *task;
872 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700873
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000874 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000875 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700876
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100877 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300878 struct list_head inflight_entry;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100879 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300880 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
881 struct hlist_node hash_node;
882 struct async_poll *apoll;
883 struct io_wq_work work;
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100884 const struct cred *creds;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +0100885
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100886 /* store used ubuf, so we can prevent reloading */
887 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888};
889
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000890struct io_tctx_node {
891 struct list_head ctx_node;
892 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000893 struct io_ring_ctx *ctx;
894};
895
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300896struct io_defer_entry {
897 struct list_head list;
898 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300899 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300900};
901
Jens Axboed3656342019-12-18 09:50:26 -0700902struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700903 /* needs req->file assigned */
904 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700905 /* hash wq insertion if file is a regular file */
906 unsigned hash_reg_file : 1;
907 /* unbound wq insertion if file is a non-regular file */
908 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700909 /* opcode is not supported by this kernel */
910 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700911 /* set if opcode supports polled "wait" */
912 unsigned pollin : 1;
913 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700914 /* op supports buffer selection */
915 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000916 /* do prep async if is going to be punted */
917 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600918 /* should block plug */
919 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700920 /* size of async data needed, if any */
921 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700922};
923
Jens Axboe09186822020-10-13 15:01:40 -0600924static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300925 [IORING_OP_NOP] = {},
926 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700927 .needs_file = 1,
928 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700929 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700930 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000931 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600932 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700933 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700936 .needs_file = 1,
937 .hash_reg_file = 1,
938 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700939 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000940 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600941 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700942 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700945 .needs_file = 1,
946 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300947 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700948 .needs_file = 1,
949 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700950 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600951 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700952 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700953 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700955 .needs_file = 1,
956 .hash_reg_file = 1,
957 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700958 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600959 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700960 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700963 .needs_file = 1,
964 .unbound_nonreg_file = 1,
965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_POLL_REMOVE] = {},
967 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700968 .needs_file = 1,
969 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300970 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700971 .needs_file = 1,
972 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700973 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000974 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700975 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700976 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300977 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700978 .needs_file = 1,
979 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700980 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700981 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000982 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700983 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700984 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300985 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700986 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700987 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000988 [IORING_OP_TIMEOUT_REMOVE] = {
989 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000990 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300991 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700992 .needs_file = 1,
993 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700994 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700995 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300996 [IORING_OP_ASYNC_CANCEL] = {},
997 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700998 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700999 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001000 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001001 .needs_file = 1,
1002 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001003 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001004 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001005 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001006 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001007 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001008 .needs_file = 1,
1009 },
Jens Axboe44526be2021-02-15 13:32:18 -07001010 [IORING_OP_OPENAT] = {},
1011 [IORING_OP_CLOSE] = {},
1012 [IORING_OP_FILES_UPDATE] = {},
1013 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001014 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001015 .needs_file = 1,
1016 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001017 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001018 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001019 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001020 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001021 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001022 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001023 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001024 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001025 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001026 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001027 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001028 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001029 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001030 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001031 .needs_file = 1,
1032 },
Jens Axboe44526be2021-02-15 13:32:18 -07001033 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001034 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001035 .needs_file = 1,
1036 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001037 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001038 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001039 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001040 .needs_file = 1,
1041 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001042 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001043 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001044 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001045 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001046 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001047 [IORING_OP_EPOLL_CTL] = {
1048 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001049 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001050 [IORING_OP_SPLICE] = {
1051 .needs_file = 1,
1052 .hash_reg_file = 1,
1053 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001054 },
1055 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001056 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001057 [IORING_OP_TEE] = {
1058 .needs_file = 1,
1059 .hash_reg_file = 1,
1060 .unbound_nonreg_file = 1,
1061 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001062 [IORING_OP_SHUTDOWN] = {
1063 .needs_file = 1,
1064 },
Jens Axboe44526be2021-02-15 13:32:18 -07001065 [IORING_OP_RENAMEAT] = {},
1066 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001067 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001068 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001069 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001070};
1071
Pavel Begunkov0756a862021-08-15 10:40:25 +01001072/* requests with any of those set should undergo io_disarm_next() */
1073#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1074
Pavel Begunkov7a612352021-03-09 00:37:59 +00001075static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001076static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001077static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1078 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001079 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001080static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001081
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001082static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1083 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001084static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001085static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001086static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001087static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001088static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001089 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001090 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001091static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001092static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001093 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001094static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001095static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001096
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001097static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01001098static void io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001099static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001100
Pavel Begunkovb9445592021-08-25 12:25:45 +01001101static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1102 unsigned int issue_flags, u32 slot_index);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001103static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001104
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105static struct kmem_cache *req_cachep;
1106
Jens Axboe09186822020-10-13 15:01:40 -06001107static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001108
1109struct sock *io_uring_get_socket(struct file *file)
1110{
1111#if defined(CONFIG_UNIX)
1112 if (file->f_op == &io_uring_fops) {
1113 struct io_ring_ctx *ctx = file->private_data;
1114
1115 return ctx->ring_sock->sk;
1116 }
1117#endif
1118 return NULL;
1119}
1120EXPORT_SYMBOL(io_uring_get_socket);
1121
Pavel Begunkovf237c302021-08-18 12:42:46 +01001122static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1123{
1124 if (!*locked) {
1125 mutex_lock(&ctx->uring_lock);
1126 *locked = true;
1127 }
1128}
1129
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001130#define io_for_each_link(pos, head) \
1131 for (pos = (head); pos; pos = pos->link)
1132
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001133/*
1134 * Shamelessly stolen from the mm implementation of page reference checking,
1135 * see commit f958d7b528b1 for details.
1136 */
1137#define req_ref_zero_or_close_to_overflow(req) \
1138 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1139
1140static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1141{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001142 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001143 return atomic_inc_not_zero(&req->refs);
1144}
1145
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001146static inline bool req_ref_put_and_test(struct io_kiocb *req)
1147{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001148 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1149 return true;
1150
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001151 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1152 return atomic_dec_and_test(&req->refs);
1153}
1154
1155static inline void req_ref_put(struct io_kiocb *req)
1156{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001157 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001158 WARN_ON_ONCE(req_ref_put_and_test(req));
1159}
1160
1161static inline void req_ref_get(struct io_kiocb *req)
1162{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001163 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001164 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1165 atomic_inc(&req->refs);
1166}
1167
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001168static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001169{
1170 if (!(req->flags & REQ_F_REFCOUNT)) {
1171 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001172 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001173 }
1174}
1175
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001176static inline void io_req_set_refcount(struct io_kiocb *req)
1177{
1178 __io_req_set_refcount(req, 1);
1179}
1180
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001181static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001182{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001183 struct io_ring_ctx *ctx = req->ctx;
1184
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001185 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001186 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001187 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001188 }
1189}
1190
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001191static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1192{
1193 bool got = percpu_ref_tryget(ref);
1194
1195 /* already at zero, wait for ->release() */
1196 if (!got)
1197 wait_for_completion(compl);
1198 percpu_ref_resurrect(ref);
1199 if (got)
1200 percpu_ref_put(ref);
1201}
1202
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001203static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1204 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001205{
1206 struct io_kiocb *req;
1207
Pavel Begunkov68207682021-03-22 01:58:25 +00001208 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001209 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001210 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001211 return true;
1212
1213 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001214 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001215 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001216 }
1217 return false;
1218}
1219
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001220static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001221{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001222 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001223}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001224
Hao Xua8295b92021-08-27 17:46:09 +08001225static inline void req_fail_link_node(struct io_kiocb *req, int res)
1226{
1227 req_set_fail(req);
1228 req->result = res;
1229}
1230
Jens Axboe2b188cc2019-01-07 10:46:33 -07001231static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1232{
1233 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1234
Jens Axboe0f158b42020-05-14 17:18:39 -06001235 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001236}
1237
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001238static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1239{
1240 return !req->timeout.off;
1241}
1242
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001243static void io_fallback_req_func(struct work_struct *work)
1244{
1245 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1246 fallback_work.work);
1247 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1248 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001249 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001250
1251 percpu_ref_get(&ctx->refs);
1252 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001253 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001254
Pavel Begunkovf237c302021-08-18 12:42:46 +01001255 if (locked) {
1256 if (ctx->submit_state.compl_nr)
1257 io_submit_flush_completions(ctx);
1258 mutex_unlock(&ctx->uring_lock);
1259 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001260 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001261
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001262}
1263
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1265{
1266 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001267 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268
1269 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1270 if (!ctx)
1271 return NULL;
1272
Jens Axboe78076bb2019-12-04 19:56:40 -07001273 /*
1274 * Use 5 bits less than the max cq entries, that should give us around
1275 * 32 entries per hash list if totally full and uniformly spread.
1276 */
1277 hash_bits = ilog2(p->cq_entries);
1278 hash_bits -= 5;
1279 if (hash_bits <= 0)
1280 hash_bits = 1;
1281 ctx->cancel_hash_bits = hash_bits;
1282 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1283 GFP_KERNEL);
1284 if (!ctx->cancel_hash)
1285 goto err;
1286 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1287
Pavel Begunkov62248432021-04-28 13:11:29 +01001288 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1289 if (!ctx->dummy_ubuf)
1290 goto err;
1291 /* set invalid range, so io_import_fixed() fails meeting it */
1292 ctx->dummy_ubuf->ubuf = -1UL;
1293
Roman Gushchin21482892019-05-07 10:01:48 -07001294 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001295 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1296 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297
1298 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001299 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001300 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001301 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001302 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001303 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001304 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001305 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001307 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001309 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001310 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001311 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001312 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001313 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001314 spin_lock_init(&ctx->rsrc_ref_lock);
1315 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001316 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1317 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001318 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001319 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001320 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001321 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001323err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001324 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001325 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001326 kfree(ctx);
1327 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328}
1329
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001330static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1331{
1332 struct io_rings *r = ctx->rings;
1333
1334 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1335 ctx->cq_extra--;
1336}
1337
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001338static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001339{
Jens Axboe2bc99302020-07-09 09:43:27 -06001340 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1341 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001342
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001343 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001344 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001345
Bob Liu9d858b22019-11-13 18:06:25 +08001346 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001347}
1348
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001349#define FFS_ASYNC_READ 0x1UL
1350#define FFS_ASYNC_WRITE 0x2UL
1351#ifdef CONFIG_64BIT
1352#define FFS_ISREG 0x4UL
1353#else
1354#define FFS_ISREG 0x0UL
1355#endif
1356#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1357
1358static inline bool io_req_ffs_set(struct io_kiocb *req)
1359{
1360 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1361}
1362
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001363static void io_req_track_inflight(struct io_kiocb *req)
1364{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001365 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001366 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001367 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001368 }
1369}
1370
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001371static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1372{
1373 req->flags &= ~REQ_F_LINK_TIMEOUT;
1374}
1375
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001376static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1377{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001378 if (WARN_ON_ONCE(!req->link))
1379 return NULL;
1380
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001381 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1382 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001383
1384 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001385 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001386 __io_req_set_refcount(req->link, 2);
1387 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001388}
1389
1390static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1391{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001392 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001393 return NULL;
1394 return __io_prep_linked_timeout(req);
1395}
1396
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001397static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001398{
Jens Axboed3656342019-12-18 09:50:26 -07001399 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001400 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001401
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001402 if (!(req->flags & REQ_F_CREDS)) {
1403 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001404 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001405 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001406
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001407 req->work.list.next = NULL;
1408 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001409 if (req->flags & REQ_F_FORCE_ASYNC)
1410 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1411
Jens Axboed3656342019-12-18 09:50:26 -07001412 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001413 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001414 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001415 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001416 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001417 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001418 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001419
1420 switch (req->opcode) {
1421 case IORING_OP_SPLICE:
1422 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001423 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1424 req->work.flags |= IO_WQ_WORK_UNBOUND;
1425 break;
1426 }
Jens Axboe561fb042019-10-24 07:25:42 -06001427}
1428
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001429static void io_prep_async_link(struct io_kiocb *req)
1430{
1431 struct io_kiocb *cur;
1432
Pavel Begunkov44eff402021-07-26 14:14:31 +01001433 if (req->flags & REQ_F_LINK_TIMEOUT) {
1434 struct io_ring_ctx *ctx = req->ctx;
1435
Jens Axboe79ebeae2021-08-10 15:18:27 -06001436 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001437 io_for_each_link(cur, req)
1438 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001439 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001440 } else {
1441 io_for_each_link(cur, req)
1442 io_prep_async_work(cur);
1443 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001444}
1445
Pavel Begunkovf237c302021-08-18 12:42:46 +01001446static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001447{
Jackie Liua197f662019-11-08 08:09:12 -07001448 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001449 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001450 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001451
Pavel Begunkovf237c302021-08-18 12:42:46 +01001452 /* must not take the lock, NULL it as a precaution */
1453 locked = NULL;
1454
Jens Axboe3bfe6102021-02-16 14:15:30 -07001455 BUG_ON(!tctx);
1456 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001457
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001458 /* init ->work of the whole link before punting */
1459 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001460
1461 /*
1462 * Not expected to happen, but if we do have a bug where this _can_
1463 * happen, catch it here and ensure the request is marked as
1464 * canceled. That will make io-wq go through the usual work cancel
1465 * procedure rather than attempt to run this request (or create a new
1466 * worker for it).
1467 */
1468 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1469 req->work.flags |= IO_WQ_WORK_CANCEL;
1470
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001471 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1472 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001473 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001474 if (link)
1475 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001476}
1477
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001478static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001479 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001480 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001481{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001482 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001483
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001484 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001485 if (status)
1486 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001487 atomic_set(&req->ctx->cq_timeouts,
1488 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001489 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001490 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001491 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001492 }
1493}
1494
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001495static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001496{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001497 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001498 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1499 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001500
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001501 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001502 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001503 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001504 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001505 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001506 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001507}
1508
Pavel Begunkov360428f2020-05-30 14:54:17 +03001509static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001510 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001511{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001512 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001513
Jens Axboe79ebeae2021-08-10 15:18:27 -06001514 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001515 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001516 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001517 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001518 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001519
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001520 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001521 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001522
1523 /*
1524 * Since seq can easily wrap around over time, subtract
1525 * the last seq at which timeouts were flushed before comparing.
1526 * Assuming not more than 2^31-1 events have happened since,
1527 * these subtractions won't have wrapped, so we can check if
1528 * target is in [last_seq, current_seq] by comparing the two.
1529 */
1530 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1531 events_got = seq - ctx->cq_last_tm_flush;
1532 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001533 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001534
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001535 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001536 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001537 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001538 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001539 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001540}
1541
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001542static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001543{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001544 if (ctx->off_timeout_used)
1545 io_flush_timeouts(ctx);
1546 if (ctx->drain_active)
1547 io_queue_deferred(ctx);
1548}
1549
1550static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1551{
1552 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1553 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001554 /* order cqe stores with ring update */
1555 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001556}
1557
Jens Axboe90554202020-09-03 12:12:41 -06001558static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1559{
1560 struct io_rings *r = ctx->rings;
1561
Pavel Begunkova566c552021-05-16 22:58:08 +01001562 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001563}
1564
Pavel Begunkov888aae22021-01-19 13:32:39 +00001565static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1566{
1567 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1568}
1569
Pavel Begunkovd068b502021-05-16 22:58:11 +01001570static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571{
Hristo Venev75b28af2019-08-26 17:23:46 +00001572 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001573 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001574
Stefan Bühler115e12e2019-04-24 23:54:18 +02001575 /*
1576 * writes to the cq entry need to come after reading head; the
1577 * control dependency is enough as we're using WRITE_ONCE to
1578 * fill the cq entry
1579 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001580 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001581 return NULL;
1582
Pavel Begunkov888aae22021-01-19 13:32:39 +00001583 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001584 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001585}
1586
Jens Axboef2842ab2020-01-08 11:04:00 -07001587static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1588{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001589 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001590 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001591 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1592 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001593 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001594}
1595
Jens Axboe2c5d7632021-08-21 07:21:19 -06001596/*
1597 * This should only get called when at least one event has been posted.
1598 * Some applications rely on the eventfd notification count only changing
1599 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1600 * 1:1 relationship between how many times this function is called (and
1601 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1602 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001603static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001604{
Jens Axboe5fd46172021-08-06 14:04:31 -06001605 /*
1606 * wake_up_all() may seem excessive, but io_wake_function() and
1607 * io_should_wake() handle the termination of the loop and only
1608 * wake as many waiters as we need to.
1609 */
1610 if (wq_has_sleeper(&ctx->cq_wait))
1611 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001612 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1613 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001614 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001615 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001616 if (waitqueue_active(&ctx->poll_wait)) {
1617 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001618 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1619 }
Jens Axboe8c838782019-03-12 15:48:16 -06001620}
1621
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001622static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1623{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001624 /* see waitqueue_active() comment */
1625 smp_mb();
1626
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001627 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001628 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001629 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001630 }
1631 if (io_should_trigger_evfd(ctx))
1632 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001633 if (waitqueue_active(&ctx->poll_wait)) {
1634 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001635 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1636 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001637}
1638
Jens Axboec4a2ed72019-11-21 21:01:26 -07001639/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001640static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641{
Jens Axboeb18032b2021-01-24 16:58:56 -07001642 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001643
Pavel Begunkova566c552021-05-16 22:58:08 +01001644 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001645 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001646
Jens Axboeb18032b2021-01-24 16:58:56 -07001647 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001648 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001649 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001650 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001651 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001652
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001653 if (!cqe && !force)
1654 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001655 ocqe = list_first_entry(&ctx->cq_overflow_list,
1656 struct io_overflow_cqe, list);
1657 if (cqe)
1658 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1659 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001660 io_account_cq_overflow(ctx);
1661
Jens Axboeb18032b2021-01-24 16:58:56 -07001662 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001663 list_del(&ocqe->list);
1664 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001665 }
1666
Pavel Begunkov09e88402020-12-17 00:24:38 +00001667 all_flushed = list_empty(&ctx->cq_overflow_list);
1668 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001669 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001670 WRITE_ONCE(ctx->rings->sq_flags,
1671 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001672 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001673
Jens Axboeb18032b2021-01-24 16:58:56 -07001674 if (posted)
1675 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001676 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001677 if (posted)
1678 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001679 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001680}
1681
Pavel Begunkov90f67362021-08-09 20:18:12 +01001682static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001683{
Jens Axboeca0a2652021-03-04 17:15:48 -07001684 bool ret = true;
1685
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001686 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001687 /* iopoll syncs against uring_lock, not completion_lock */
1688 if (ctx->flags & IORING_SETUP_IOPOLL)
1689 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001690 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001691 if (ctx->flags & IORING_SETUP_IOPOLL)
1692 mutex_unlock(&ctx->uring_lock);
1693 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001694
1695 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001696}
1697
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001698/* must to be called somewhat shortly after putting a request */
1699static inline void io_put_task(struct task_struct *task, int nr)
1700{
1701 struct io_uring_task *tctx = task->io_uring;
1702
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001703 if (likely(task == current)) {
1704 tctx->cached_refs += nr;
1705 } else {
1706 percpu_counter_sub(&tctx->inflight, nr);
1707 if (unlikely(atomic_read(&tctx->in_idle)))
1708 wake_up(&tctx->wait);
1709 put_task_struct_many(task, nr);
1710 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001711}
1712
Pavel Begunkov9a108672021-08-27 11:55:01 +01001713static void io_task_refs_refill(struct io_uring_task *tctx)
1714{
1715 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1716
1717 percpu_counter_add(&tctx->inflight, refill);
1718 refcount_add(refill, &current->usage);
1719 tctx->cached_refs += refill;
1720}
1721
1722static inline void io_get_task_refs(int nr)
1723{
1724 struct io_uring_task *tctx = current->io_uring;
1725
1726 tctx->cached_refs -= nr;
1727 if (unlikely(tctx->cached_refs < 0))
1728 io_task_refs_refill(tctx);
1729}
1730
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001731static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1732 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001734 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001736 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1737 if (!ocqe) {
1738 /*
1739 * If we're in ring overflow flush mode, or in task cancel mode,
1740 * or cannot allocate an overflow entry, then we need to drop it
1741 * on the floor.
1742 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001743 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001744 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001746 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001747 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001748 WRITE_ONCE(ctx->rings->sq_flags,
1749 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1750
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001751 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001752 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001753 ocqe->cqe.res = res;
1754 ocqe->cqe.flags = cflags;
1755 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1756 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001757}
1758
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001759static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1760 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001761{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001762 struct io_uring_cqe *cqe;
1763
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001764 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001765
1766 /*
1767 * If we can't get a cq entry, userspace overflowed the
1768 * submission (by quite a lot). Increment the overflow count in
1769 * the ring.
1770 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001771 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001772 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001773 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774 WRITE_ONCE(cqe->res, res);
1775 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001776 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001778 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779}
1780
Pavel Begunkov8d133262021-04-11 01:46:33 +01001781/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001782static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1783 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001784{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001785 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001786}
1787
Pavel Begunkov7a612352021-03-09 00:37:59 +00001788static void io_req_complete_post(struct io_kiocb *req, long res,
1789 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001791 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792
Jens Axboe79ebeae2021-08-10 15:18:27 -06001793 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001794 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001795 /*
1796 * If we're the last reference to this request, add to our locked
1797 * free_list cache.
1798 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001799 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001800 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001801 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001802 io_disarm_next(req);
1803 if (req->link) {
1804 io_req_task_queue(req->link);
1805 req->link = NULL;
1806 }
1807 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001808 io_dismantle_req(req);
1809 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001810 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001811 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001812 } else {
1813 if (!percpu_ref_tryget(&ctx->refs))
1814 req = NULL;
1815 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001816 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001817 spin_unlock(&ctx->completion_lock);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001818
Pavel Begunkov180f8292021-03-14 20:57:09 +00001819 if (req) {
1820 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001821 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001822 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001823}
1824
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001825static inline bool io_req_needs_clean(struct io_kiocb *req)
1826{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001827 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001828}
1829
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001830static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001831 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001832{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001833 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001834 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001835 req->result = res;
1836 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001837 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001838}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001839
Pavel Begunkov889fca72021-02-10 00:03:09 +00001840static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1841 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001842{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001843 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1844 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001845 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001846 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001847}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001848
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001849static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001850{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001851 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001852}
1853
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001854static void io_req_complete_failed(struct io_kiocb *req, long res)
1855{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001856 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001857 io_req_complete_post(req, res, 0);
1858}
1859
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001860static void io_req_complete_fail_submit(struct io_kiocb *req)
1861{
1862 /*
1863 * We don't submit, fail them all, for that replace hardlinks with
1864 * normal links. Extra REQ_F_LINK is tolerated.
1865 */
1866 req->flags &= ~REQ_F_HARDLINK;
1867 req->flags |= REQ_F_LINK;
1868 io_req_complete_failed(req, req->result);
1869}
1870
Pavel Begunkov864ea922021-08-09 13:04:08 +01001871/*
1872 * Don't initialise the fields below on every allocation, but do that in
1873 * advance and keep them valid across allocations.
1874 */
1875static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1876{
1877 req->ctx = ctx;
1878 req->link = NULL;
1879 req->async_data = NULL;
1880 /* not necessary, but safer to zero */
1881 req->result = 0;
1882}
1883
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001884static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001885 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001886{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001887 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001888 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001889 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001890 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001891}
1892
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001893/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001894static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001895{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001896 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001897 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001898
Jens Axboec7dae4b2021-02-09 19:53:37 -07001899 /*
1900 * If we have more than a batch's worth of requests in our IRQ side
1901 * locked cache, grab the lock and move them over to our submission
1902 * side cache.
1903 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001904 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001905 io_flush_cached_locked_reqs(ctx, state);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001906
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001907 nr = state->free_reqs;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001908 while (!list_empty(&state->free_list)) {
1909 struct io_kiocb *req = list_first_entry(&state->free_list,
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001910 struct io_kiocb, inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001911
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001912 list_del(&req->inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001913 state->reqs[nr++] = req;
1914 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001915 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001917
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001918 state->free_reqs = nr;
1919 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001920}
1921
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001922/*
1923 * A request might get retired back into the request caches even before opcode
1924 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1925 * Because of that, io_alloc_req() should be called only under ->uring_lock
1926 * and with extra caution to not get a request that is still worked on.
1927 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001928static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001929 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001930{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001931 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001932 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1933 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001934
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01001935 BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001936
Pavel Begunkov864ea922021-08-09 13:04:08 +01001937 if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
1938 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001939
Pavel Begunkov864ea922021-08-09 13:04:08 +01001940 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1941 state->reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001942
Pavel Begunkov864ea922021-08-09 13:04:08 +01001943 /*
1944 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1945 * retry single alloc to be on the safe side.
1946 */
1947 if (unlikely(ret <= 0)) {
1948 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1949 if (!state->reqs[0])
1950 return NULL;
1951 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001953
1954 for (i = 0; i < ret; i++)
1955 io_preinit_req(state->reqs[i], ctx);
1956 state->free_reqs = ret;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001957got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001958 state->free_reqs--;
1959 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001960}
1961
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001962static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001963{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001964 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001965 fput(file);
1966}
1967
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001968static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001970 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001971
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001972 if (io_req_needs_clean(req))
1973 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001974 if (!(flags & REQ_F_FIXED_FILE))
1975 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001976 if (req->fixed_rsrc_refs)
1977 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001978 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001979 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001980 req->async_data = NULL;
1981 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001982}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001983
Pavel Begunkov216578e2020-10-13 09:44:00 +01001984static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001985{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001986 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001987
Pavel Begunkov216578e2020-10-13 09:44:00 +01001988 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001989 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001990
Jens Axboe79ebeae2021-08-10 15:18:27 -06001991 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001992 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001993 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001994 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001995
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001996 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001997}
1998
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001999static inline void io_remove_next_linked(struct io_kiocb *req)
2000{
2001 struct io_kiocb *nxt = req->link;
2002
2003 req->link = nxt->link;
2004 nxt->link = NULL;
2005}
2006
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002007static bool io_kill_linked_timeout(struct io_kiocb *req)
2008 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002009 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002010{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002011 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002012
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002013 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002014 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002015
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002016 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002017 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002018 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002019 list_del(&link->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002020 io_cqring_fill_event(link->ctx, link->user_data,
2021 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002022 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002023 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002024 }
2025 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002026 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002027}
2028
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002029static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002030 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002031{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002032 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06002033
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002034 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002035 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002036 long res = -ECANCELED;
2037
2038 if (link->flags & REQ_F_FAIL)
2039 res = link->result;
2040
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002041 nxt = link->link;
2042 link->link = NULL;
2043
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002044 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08002045 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002046 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002047 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002048 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002049}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002050
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002051static bool io_disarm_next(struct io_kiocb *req)
2052 __must_hold(&req->ctx->completion_lock)
2053{
2054 bool posted = false;
2055
Pavel Begunkov0756a862021-08-15 10:40:25 +01002056 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2057 struct io_kiocb *link = req->link;
2058
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002059 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002060 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2061 io_remove_next_linked(req);
2062 io_cqring_fill_event(link->ctx, link->user_data,
2063 -ECANCELED, 0);
2064 io_put_req_deferred(link);
2065 posted = true;
2066 }
2067 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002068 struct io_ring_ctx *ctx = req->ctx;
2069
2070 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002071 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002072 spin_unlock_irq(&ctx->timeout_lock);
2073 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002074 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002075 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002076 posted |= (req->link != NULL);
2077 io_fail_links(req);
2078 }
2079 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002080}
2081
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002082static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002083{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002084 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002085
Jens Axboe9e645e112019-05-10 16:07:28 -06002086 /*
2087 * If LINK is set, we have dependent requests in this chain. If we
2088 * didn't fail this request, queue the first one up, moving any other
2089 * dependencies to the next request. In case of failure, fail the rest
2090 * of the chain.
2091 */
Pavel Begunkov0756a862021-08-15 10:40:25 +01002092 if (req->flags & IO_DISARM_MASK) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002093 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002094 bool posted;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002095
Jens Axboe79ebeae2021-08-10 15:18:27 -06002096 spin_lock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002097 posted = io_disarm_next(req);
2098 if (posted)
2099 io_commit_cqring(req->ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002100 spin_unlock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002101 if (posted)
2102 io_cqring_ev_posted(ctx);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002103 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002104 nxt = req->link;
2105 req->link = NULL;
2106 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002107}
Jens Axboe2665abf2019-11-05 12:40:47 -07002108
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002109static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002110{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002111 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002112 return NULL;
2113 return __io_req_find_next(req);
2114}
2115
Pavel Begunkovf237c302021-08-18 12:42:46 +01002116static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002117{
2118 if (!ctx)
2119 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002120 if (*locked) {
Hao Xu99c8bc52021-08-21 06:19:54 +08002121 if (ctx->submit_state.compl_nr)
2122 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002123 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002124 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002125 }
2126 percpu_ref_put(&ctx->refs);
2127}
2128
Jens Axboe7cbf1722021-02-10 00:03:20 +00002129static void tctx_task_work(struct callback_head *cb)
2130{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002131 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002132 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002133 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2134 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002135
Pavel Begunkov16f72072021-06-17 18:14:09 +01002136 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002137 struct io_wq_work_node *node;
2138
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002139 if (!tctx->task_list.first && locked && ctx->submit_state.compl_nr)
2140 io_submit_flush_completions(ctx);
2141
Pavel Begunkov3f184072021-06-17 18:14:06 +01002142 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002143 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002144 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002145 if (!node)
2146 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002147 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002148 if (!node)
2149 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002150
Pavel Begunkov6294f362021-08-10 17:53:55 +01002151 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002152 struct io_wq_work_node *next = node->next;
2153 struct io_kiocb *req = container_of(node, struct io_kiocb,
2154 io_task_work.node);
2155
2156 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002157 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002158 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002159 /* if not contended, grab and improve batching */
2160 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002161 percpu_ref_get(&ctx->refs);
2162 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002163 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002164 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002165 } while (node);
2166
Jens Axboe7cbf1722021-02-10 00:03:20 +00002167 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002168 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002169
Pavel Begunkovf237c302021-08-18 12:42:46 +01002170 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002171}
2172
Pavel Begunkove09ee512021-07-01 13:26:05 +01002173static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002174{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002175 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002176 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002177 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002178 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002179 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002180 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002181
2182 WARN_ON_ONCE(!tctx);
2183
Jens Axboe0b81e802021-02-16 10:33:53 -07002184 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002185 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002186 running = tctx->task_running;
2187 if (!running)
2188 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002189 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002190
2191 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002192 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002193 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002194
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002195 /*
2196 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2197 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2198 * processing task_work. There's no reliable way to tell if TWA_RESUME
2199 * will do the job.
2200 */
2201 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002202 if (!task_work_add(tsk, &tctx->task_work, notify)) {
2203 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002204 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002205 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002206
Pavel Begunkove09ee512021-07-01 13:26:05 +01002207 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002208 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002209 node = tctx->task_list.first;
2210 INIT_WQ_LIST(&tctx->task_list);
2211 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002212
Pavel Begunkove09ee512021-07-01 13:26:05 +01002213 while (node) {
2214 req = container_of(node, struct io_kiocb, io_task_work.node);
2215 node = node->next;
2216 if (llist_add(&req->io_task_work.fallback_node,
2217 &req->ctx->fallback_llist))
2218 schedule_delayed_work(&req->ctx->fallback_work, 1);
2219 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002220}
2221
Pavel Begunkovf237c302021-08-18 12:42:46 +01002222static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002223{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002224 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002225
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002226 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002227 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002228 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002229}
2230
Pavel Begunkovf237c302021-08-18 12:42:46 +01002231static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002232{
2233 struct io_ring_ctx *ctx = req->ctx;
2234
Pavel Begunkovf237c302021-08-18 12:42:46 +01002235 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002236 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002237 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002238 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002239 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002240 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002241}
2242
Pavel Begunkova3df76982021-02-18 22:32:52 +00002243static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2244{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002245 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002246 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002247 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002248}
2249
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002250static void io_req_task_queue(struct io_kiocb *req)
2251{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002252 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002253 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002254}
2255
Jens Axboe773af692021-07-27 10:25:55 -06002256static void io_req_task_queue_reissue(struct io_kiocb *req)
2257{
2258 req->io_task_work.func = io_queue_async_work;
2259 io_req_task_work_add(req);
2260}
2261
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002262static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002263{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002264 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002265
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002266 if (nxt)
2267 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002268}
2269
Jens Axboe9e645e112019-05-10 16:07:28 -06002270static void io_free_req(struct io_kiocb *req)
2271{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002272 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002273 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002274}
2275
Pavel Begunkovf237c302021-08-18 12:42:46 +01002276static void io_free_req_work(struct io_kiocb *req, bool *locked)
2277{
2278 io_free_req(req);
2279}
2280
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002281struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002282 struct task_struct *task;
2283 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002284 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002285};
2286
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002287static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002288{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002289 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002290 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002291 rb->task = NULL;
2292}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002293
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002294static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2295 struct req_batch *rb)
2296{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002297 if (rb->ctx_refs)
2298 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002299 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002300 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002301}
2302
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002303static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2304 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002305{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002306 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002307 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002308
Jens Axboee3bc8e92020-09-24 08:45:57 -06002309 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002310 if (rb->task)
2311 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002312 rb->task = req->task;
2313 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002314 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002315 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002316 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002317
Pavel Begunkovbd759042021-02-12 03:23:50 +00002318 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002319 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002320 else
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002321 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002322}
2323
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01002324static void io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002325 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002326{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002327 struct io_submit_state *state = &ctx->submit_state;
2328 int i, nr = state->compl_nr;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002329 struct req_batch rb;
2330
Jens Axboe79ebeae2021-08-10 15:18:27 -06002331 spin_lock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002332 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002333 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002334
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002335 __io_cqring_fill_event(ctx, req->user_data, req->result,
2336 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002337 }
2338 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002339 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002340 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002341
2342 io_init_req_batch(&rb);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002343 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002344 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov905c1722021-02-10 00:03:14 +00002345
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002346 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002347 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002348 }
2349
2350 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002351 state->compl_nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002352}
2353
Jens Axboeba816ad2019-09-28 11:36:45 -06002354/*
2355 * Drop reference to request, return next in chain (if there is one) if this
2356 * was the last reference to this request.
2357 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002358static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002359{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002360 struct io_kiocb *nxt = NULL;
2361
Jens Axboede9b4cc2021-02-24 13:28:27 -07002362 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002363 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002364 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002365 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002366 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367}
2368
Pavel Begunkov0d850352021-03-19 17:22:37 +00002369static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002370{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002371 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002372 io_free_req(req);
2373}
2374
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002375static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002376{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002377 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002378 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002379 io_req_task_work_add(req);
2380 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002381}
2382
Pavel Begunkov6c503152021-01-04 20:36:36 +00002383static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002384{
2385 /* See comment at the top of this file */
2386 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002387 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002388}
2389
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002390static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2391{
2392 struct io_rings *rings = ctx->rings;
2393
2394 /* make sure SQ entry isn't read before tail */
2395 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2396}
2397
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002398static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002399{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002400 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002401
Jens Axboebcda7ba2020-02-23 16:42:51 -07002402 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2403 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002404 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002405 kfree(kbuf);
2406 return cflags;
2407}
2408
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002409static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2410{
2411 struct io_buffer *kbuf;
2412
Pavel Begunkovae421d92021-08-17 20:28:08 +01002413 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2414 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002415 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2416 return io_put_kbuf(req, kbuf);
2417}
2418
Jens Axboe4c6e2772020-07-01 11:29:10 -06002419static inline bool io_run_task_work(void)
2420{
Nadav Amitef98eb02021-08-07 17:13:41 -07002421 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002422 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002423 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002424 return true;
2425 }
2426
2427 return false;
2428}
2429
Jens Axboedef596e2019-01-09 08:59:42 -07002430/*
2431 * Find and free completed poll iocbs
2432 */
2433static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002434 struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002435{
Jens Axboe8237e042019-12-28 10:48:22 -07002436 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002437 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002438
2439 /* order with ->result store in io_complete_rw_iopoll() */
2440 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002441
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002442 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002443 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002444 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002445 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002446
Pavel Begunkova8576af2021-08-15 10:40:21 +01002447 if (READ_ONCE(req->result) == -EAGAIN &&
Pavel Begunkov8c130822021-03-22 01:58:32 +00002448 !(req->flags & REQ_F_DONT_REISSUE)) {
Pavel Begunkovf1613402021-02-11 18:28:21 +00002449 req->iopoll_completed = 0;
Jens Axboe773af692021-07-27 10:25:55 -06002450 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002451 continue;
Pavel Begunkovf1613402021-02-11 18:28:21 +00002452 }
2453
Pavel Begunkovae421d92021-08-17 20:28:08 +01002454 __io_cqring_fill_event(ctx, req->user_data, req->result,
2455 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002456 (*nr_events)++;
2457
Jens Axboede9b4cc2021-02-24 13:28:27 -07002458 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002459 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002460 }
Jens Axboedef596e2019-01-09 08:59:42 -07002461
Jens Axboe09bb8392019-03-13 12:39:28 -06002462 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002463 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002464 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002465}
2466
Jens Axboedef596e2019-01-09 08:59:42 -07002467static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002468 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002469{
2470 struct io_kiocb *req, *tmp;
2471 LIST_HEAD(done);
2472 bool spin;
Jens Axboedef596e2019-01-09 08:59:42 -07002473
2474 /*
2475 * Only spin for completions if we don't have multiple devices hanging
2476 * off our complete list, and we're under the requested amount.
2477 */
Hao Xu915b3dd2021-06-28 05:37:30 +08002478 spin = !ctx->poll_multi_queue && *nr_events < min;
Jens Axboedef596e2019-01-09 08:59:42 -07002479
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002480 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002481 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002482 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002483
2484 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002485 * Move completed and retryable entries to our local lists.
2486 * If we find a request that requires polling, break out
2487 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002488 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002489 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002490 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002491 continue;
2492 }
2493 if (!list_empty(&done))
2494 break;
2495
2496 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002497 if (unlikely(ret < 0))
2498 return ret;
2499 else if (ret)
2500 spin = false;
Jens Axboedef596e2019-01-09 08:59:42 -07002501
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002502 /* iopoll may have completed current req */
2503 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002504 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002505 }
2506
2507 if (!list_empty(&done))
Pavel Begunkova8576af2021-08-15 10:40:21 +01002508 io_iopoll_complete(ctx, nr_events, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002509
Pavel Begunkova2416e12021-08-09 13:04:09 +01002510 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002511}
2512
2513/*
Jens Axboedef596e2019-01-09 08:59:42 -07002514 * We can't just wait for polled events to come to us, we have to actively
2515 * find and complete them.
2516 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002517static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002518{
2519 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2520 return;
2521
2522 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002523 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002524 unsigned int nr_events = 0;
2525
Pavel Begunkova8576af2021-08-15 10:40:21 +01002526 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002527
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002528 /* let it sleep and repeat later if can't complete a request */
2529 if (nr_events == 0)
2530 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002531 /*
2532 * Ensure we allow local-to-the-cpu processing to take place,
2533 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002534 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002535 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002536 if (need_resched()) {
2537 mutex_unlock(&ctx->uring_lock);
2538 cond_resched();
2539 mutex_lock(&ctx->uring_lock);
2540 }
Jens Axboedef596e2019-01-09 08:59:42 -07002541 }
2542 mutex_unlock(&ctx->uring_lock);
2543}
2544
Pavel Begunkov7668b922020-07-07 16:36:21 +03002545static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002546{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002547 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002548 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002549
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002550 /*
2551 * We disallow the app entering submit/complete with polling, but we
2552 * still need to lock the ring to prevent racing with polled issue
2553 * that got punted to a workqueue.
2554 */
2555 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002556 /*
2557 * Don't enter poll loop if we already have events pending.
2558 * If we do, we can potentially be spinning for commands that
2559 * already triggered a CQE (eg in error).
2560 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002561 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002562 __io_cqring_overflow_flush(ctx, false);
2563 if (io_cqring_events(ctx))
2564 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002565 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002566 /*
2567 * If a submit got punted to a workqueue, we can have the
2568 * application entering polling for a command before it gets
2569 * issued. That app will hold the uring_lock for the duration
2570 * of the poll right here, so we need to take a breather every
2571 * now and then to ensure that the issue has a chance to add
2572 * the poll to the issued list. Otherwise we can spin here
2573 * forever, while the workqueue is stuck trying to acquire the
2574 * very same mutex.
2575 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002576 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002577 u32 tail = ctx->cached_cq_tail;
2578
Jens Axboe500f9fb2019-08-19 12:15:59 -06002579 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002580 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002581 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002582
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002583 /* some requests don't go through iopoll_list */
2584 if (tail != ctx->cached_cq_tail ||
2585 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002586 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002587 }
Pavel Begunkova8576af2021-08-15 10:40:21 +01002588 ret = io_do_iopoll(ctx, &nr_events, min);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002589 } while (!ret && nr_events < min && !need_resched());
2590out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002591 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002592 return ret;
2593}
2594
Jens Axboe491381ce2019-10-17 09:20:46 -06002595static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002596{
Jens Axboe491381ce2019-10-17 09:20:46 -06002597 /*
2598 * Tell lockdep we inherited freeze protection from submission
2599 * thread.
2600 */
2601 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002602 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002603
Pavel Begunkov1c986792021-03-22 01:58:31 +00002604 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2605 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002606 }
2607}
2608
Jens Axboeb63534c2020-06-04 11:28:00 -06002609#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002610static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002611{
Pavel Begunkovab454432021-03-22 01:58:33 +00002612 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002613
Pavel Begunkovab454432021-03-22 01:58:33 +00002614 if (!rw)
2615 return !io_req_prep_async(req);
2616 /* may have left rw->iter inconsistent on -EIOCBQUEUED */
2617 iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter));
2618 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002619}
Jens Axboeb63534c2020-06-04 11:28:00 -06002620
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002621static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002622{
Jens Axboe355afae2020-09-02 09:30:31 -06002623 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002624 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002625
Jens Axboe355afae2020-09-02 09:30:31 -06002626 if (!S_ISBLK(mode) && !S_ISREG(mode))
2627 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002628 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2629 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002630 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002631 /*
2632 * If ref is dying, we might be running poll reap from the exit work.
2633 * Don't attempt to reissue from that path, just let it fail with
2634 * -EAGAIN.
2635 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002636 if (percpu_ref_is_dying(&ctx->refs))
2637 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002638 /*
2639 * Play it safe and assume not safe to re-import and reissue if we're
2640 * not in the original thread group (or in task context).
2641 */
2642 if (!same_thread_group(req->task, current) || !in_task())
2643 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002644 return true;
2645}
Jens Axboee82ad482021-04-02 19:45:34 -06002646#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002647static bool io_resubmit_prep(struct io_kiocb *req)
2648{
2649 return false;
2650}
Jens Axboee82ad482021-04-02 19:45:34 -06002651static bool io_rw_should_reissue(struct io_kiocb *req)
2652{
2653 return false;
2654}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002655#endif
2656
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002657static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002658{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002659 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2660 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002661 if (res != req->result) {
2662 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2663 io_rw_should_reissue(req)) {
2664 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002665 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002666 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002667 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002668 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002669 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002670 return false;
2671}
2672
Pavel Begunkovf237c302021-08-18 12:42:46 +01002673static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002674{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002675 unsigned int cflags = io_put_rw_kbuf(req);
2676 long res = req->result;
2677
2678 if (*locked) {
2679 struct io_ring_ctx *ctx = req->ctx;
2680 struct io_submit_state *state = &ctx->submit_state;
2681
2682 io_req_complete_state(req, res, cflags);
2683 state->compl_reqs[state->compl_nr++] = req;
2684 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
2685 io_submit_flush_completions(ctx);
2686 } else {
2687 io_req_complete_post(req, res, cflags);
2688 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002689}
2690
2691static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2692 unsigned int issue_flags)
2693{
2694 if (__io_complete_rw_common(req, res))
2695 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002696 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002697}
2698
2699static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2700{
Jens Axboe9adbd452019-12-20 08:45:55 -07002701 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002702
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002703 if (__io_complete_rw_common(req, res))
2704 return;
2705 req->result = res;
2706 req->io_task_work.func = io_req_task_complete;
2707 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002708}
2709
Jens Axboedef596e2019-01-09 08:59:42 -07002710static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2711{
Jens Axboe9adbd452019-12-20 08:45:55 -07002712 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002713
Jens Axboe491381ce2019-10-17 09:20:46 -06002714 if (kiocb->ki_flags & IOCB_WRITE)
2715 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002716 if (unlikely(res != req->result)) {
Jens Axboea1ff1e32021-04-12 06:40:02 -06002717 if (!(res == -EAGAIN && io_rw_should_reissue(req) &&
2718 io_resubmit_prep(req))) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002719 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002720 req->flags |= REQ_F_DONT_REISSUE;
2721 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002722 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002723
2724 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002725 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002726 smp_wmb();
2727 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002728}
2729
2730/*
2731 * After the iocb has been issued, it's safe to be found on the poll list.
2732 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002733 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002734 * accessing the kiocb cookie.
2735 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002736static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002737{
2738 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002739 const bool in_async = io_wq_current_is_worker();
2740
2741 /* workqueue context doesn't hold uring_lock, grab it now */
2742 if (unlikely(in_async))
2743 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002744
2745 /*
2746 * Track whether we have multiple files in our lists. This will impact
2747 * how we do polling eventually, not spinning if we're on potentially
2748 * different devices.
2749 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002750 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002751 ctx->poll_multi_queue = false;
2752 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002753 struct io_kiocb *list_req;
Hao Xu915b3dd2021-06-28 05:37:30 +08002754 unsigned int queue_num0, queue_num1;
Jens Axboedef596e2019-01-09 08:59:42 -07002755
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002756 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002757 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002758
2759 if (list_req->file != req->file) {
2760 ctx->poll_multi_queue = true;
2761 } else {
2762 queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie);
2763 queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie);
2764 if (queue_num0 != queue_num1)
2765 ctx->poll_multi_queue = true;
2766 }
Jens Axboedef596e2019-01-09 08:59:42 -07002767 }
2768
2769 /*
2770 * For fast devices, IO may have already completed. If it has, add
2771 * it to the front so we find it first.
2772 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002773 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002774 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002775 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002776 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002777
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002778 if (unlikely(in_async)) {
2779 /*
2780 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2781 * in sq thread task context or in io worker task context. If
2782 * current task context is sq thread, we don't need to check
2783 * whether should wake up sq thread.
2784 */
2785 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2786 wq_has_sleeper(&ctx->sq_data->wait))
2787 wake_up(&ctx->sq_data->wait);
2788
2789 mutex_unlock(&ctx->uring_lock);
2790 }
Jens Axboedef596e2019-01-09 08:59:42 -07002791}
2792
Jens Axboe4503b762020-06-01 10:00:27 -06002793static bool io_bdev_nowait(struct block_device *bdev)
2794{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002795 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002796}
2797
Jens Axboe2b188cc2019-01-07 10:46:33 -07002798/*
2799 * If we tracked the file through the SCM inflight mechanism, we could support
2800 * any file. For now, just ensure that anything potentially problematic is done
2801 * inline.
2802 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002803static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002804{
2805 umode_t mode = file_inode(file)->i_mode;
2806
Jens Axboe4503b762020-06-01 10:00:27 -06002807 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002808 if (IS_ENABLED(CONFIG_BLOCK) &&
2809 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002810 return true;
2811 return false;
2812 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002813 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002814 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002815 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002816 if (IS_ENABLED(CONFIG_BLOCK) &&
2817 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002818 file->f_op != &io_uring_fops)
2819 return true;
2820 return false;
2821 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002822
Jens Axboec5b85622020-06-09 19:23:05 -06002823 /* any ->read/write should understand O_NONBLOCK */
2824 if (file->f_flags & O_NONBLOCK)
2825 return true;
2826
Jens Axboeaf197f52020-04-28 13:15:06 -06002827 if (!(file->f_mode & FMODE_NOWAIT))
2828 return false;
2829
2830 if (rw == READ)
2831 return file->f_op->read_iter != NULL;
2832
2833 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002834}
2835
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002836static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002837{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002838 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002839 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002840 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002841 return true;
2842
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002843 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002844}
2845
Pavel Begunkova88fc402020-09-30 22:57:53 +03002846static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002847{
Jens Axboedef596e2019-01-09 08:59:42 -07002848 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002849 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002850 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002851 unsigned ioprio;
2852 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002853
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002854 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002855 req->flags |= REQ_F_ISREG;
2856
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002858 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002859 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002860 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002861 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002863 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2864 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2865 if (unlikely(ret))
2866 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002867
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002868 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2869 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2870 req->flags |= REQ_F_NOWAIT;
2871
Jens Axboe2b188cc2019-01-07 10:46:33 -07002872 ioprio = READ_ONCE(sqe->ioprio);
2873 if (ioprio) {
2874 ret = ioprio_check_cap(ioprio);
2875 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002876 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002877
2878 kiocb->ki_ioprio = ioprio;
2879 } else
2880 kiocb->ki_ioprio = get_current_ioprio();
2881
Jens Axboedef596e2019-01-09 08:59:42 -07002882 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002883 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2884 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002885 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002886
Jens Axboe394918e2021-03-08 11:40:23 -07002887 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002888 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002889 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002890 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002891 if (kiocb->ki_flags & IOCB_HIPRI)
2892 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002893 kiocb->ki_complete = io_complete_rw;
2894 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002895
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002896 if (req->opcode == IORING_OP_READ_FIXED ||
2897 req->opcode == IORING_OP_WRITE_FIXED) {
2898 req->imu = NULL;
2899 io_req_set_rsrc_node(req);
2900 }
2901
Jens Axboe3529d8c2019-12-19 18:24:38 -07002902 req->rw.addr = READ_ONCE(sqe->addr);
2903 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002904 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002905 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002906}
2907
2908static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2909{
2910 switch (ret) {
2911 case -EIOCBQUEUED:
2912 break;
2913 case -ERESTARTSYS:
2914 case -ERESTARTNOINTR:
2915 case -ERESTARTNOHAND:
2916 case -ERESTART_RESTARTBLOCK:
2917 /*
2918 * We can't just restart the syscall, since previously
2919 * submitted sqes may already be in progress. Just fail this
2920 * IO with EINTR.
2921 */
2922 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002923 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002924 default:
2925 kiocb->ki_complete(kiocb, ret, 0);
2926 }
2927}
2928
Jens Axboea1d7c392020-06-22 11:09:46 -06002929static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002930 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002931{
Jens Axboeba042912019-12-25 16:33:42 -07002932 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002933 struct io_async_rw *io = req->async_data;
Pavel Begunkov97284632021-04-08 19:28:03 +01002934 bool check_reissue = kiocb->ki_complete == io_complete_rw;
Jens Axboeba042912019-12-25 16:33:42 -07002935
Jens Axboe227c0c92020-08-13 11:51:40 -06002936 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002937 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002938 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002939 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002940 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002941 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002942 }
2943
Jens Axboeba042912019-12-25 16:33:42 -07002944 if (req->flags & REQ_F_CUR_POS)
2945 req->file->f_pos = kiocb->ki_pos;
Hao Xue149bd742021-06-28 05:48:05 +08002946 if (ret >= 0 && check_reissue)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002947 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002948 else
2949 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002950
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01002951 if (check_reissue && (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002952 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002953 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002954 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002955 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002956 req_set_fail(req);
Pavel Begunkovae421d92021-08-17 20:28:08 +01002957 __io_req_complete(req, issue_flags, ret,
2958 io_put_rw_kbuf(req));
Pavel Begunkov97284632021-04-08 19:28:03 +01002959 }
2960 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002961}
2962
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002963static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2964 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002965{
Jens Axboe9adbd452019-12-20 08:45:55 -07002966 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002967 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002968 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002969
Pavel Begunkov75769e32021-04-01 15:43:54 +01002970 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002971 return -EFAULT;
2972 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002973 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002974 return -EFAULT;
2975
2976 /*
2977 * May not be a start of buffer, set size appropriately
2978 * and advance us to the beginning.
2979 */
2980 offset = buf_addr - imu->ubuf;
2981 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002982
2983 if (offset) {
2984 /*
2985 * Don't use iov_iter_advance() here, as it's really slow for
2986 * using the latter parts of a big fixed buffer - it iterates
2987 * over each segment manually. We can cheat a bit here, because
2988 * we know that:
2989 *
2990 * 1) it's a BVEC iter, we set it up
2991 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2992 * first and last bvec
2993 *
2994 * So just find our index, and adjust the iterator afterwards.
2995 * If the offset is within the first bvec (or the whole first
2996 * bvec, just use iov_iter_advance(). This makes it easier
2997 * since we can just skip the first segment, which may not
2998 * be PAGE_SIZE aligned.
2999 */
3000 const struct bio_vec *bvec = imu->bvec;
3001
3002 if (offset <= bvec->bv_len) {
3003 iov_iter_advance(iter, offset);
3004 } else {
3005 unsigned long seg_skip;
3006
3007 /* skip first vec */
3008 offset -= bvec->bv_len;
3009 seg_skip = 1 + (offset >> PAGE_SHIFT);
3010
3011 iter->bvec = bvec + seg_skip;
3012 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003013 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003014 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003015 }
3016 }
3017
Pavel Begunkov847595d2021-02-04 13:52:06 +00003018 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003019}
3020
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003021static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3022{
3023 struct io_ring_ctx *ctx = req->ctx;
3024 struct io_mapped_ubuf *imu = req->imu;
3025 u16 index, buf_index = req->buf_index;
3026
3027 if (likely(!imu)) {
3028 if (unlikely(buf_index >= ctx->nr_user_bufs))
3029 return -EFAULT;
3030 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3031 imu = READ_ONCE(ctx->user_bufs[index]);
3032 req->imu = imu;
3033 }
3034 return __io_import_fixed(req, rw, iter, imu);
3035}
3036
Jens Axboebcda7ba2020-02-23 16:42:51 -07003037static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3038{
3039 if (needs_lock)
3040 mutex_unlock(&ctx->uring_lock);
3041}
3042
3043static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3044{
3045 /*
3046 * "Normal" inline submissions always hold the uring_lock, since we
3047 * grab it from the system call. Same is true for the SQPOLL offload.
3048 * The only exception is when we've detached the request and issue it
3049 * from an async worker thread, grab the lock for that case.
3050 */
3051 if (needs_lock)
3052 mutex_lock(&ctx->uring_lock);
3053}
3054
3055static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3056 int bgid, struct io_buffer *kbuf,
3057 bool needs_lock)
3058{
3059 struct io_buffer *head;
3060
3061 if (req->flags & REQ_F_BUFFER_SELECTED)
3062 return kbuf;
3063
3064 io_ring_submit_lock(req->ctx, needs_lock);
3065
3066 lockdep_assert_held(&req->ctx->uring_lock);
3067
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003068 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003069 if (head) {
3070 if (!list_empty(&head->list)) {
3071 kbuf = list_last_entry(&head->list, struct io_buffer,
3072 list);
3073 list_del(&kbuf->list);
3074 } else {
3075 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003076 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003077 }
3078 if (*len > kbuf->len)
3079 *len = kbuf->len;
3080 } else {
3081 kbuf = ERR_PTR(-ENOBUFS);
3082 }
3083
3084 io_ring_submit_unlock(req->ctx, needs_lock);
3085
3086 return kbuf;
3087}
3088
Jens Axboe4d954c22020-02-27 07:31:19 -07003089static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3090 bool needs_lock)
3091{
3092 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003093 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003094
3095 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003096 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003097 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3098 if (IS_ERR(kbuf))
3099 return kbuf;
3100 req->rw.addr = (u64) (unsigned long) kbuf;
3101 req->flags |= REQ_F_BUFFER_SELECTED;
3102 return u64_to_user_ptr(kbuf->addr);
3103}
3104
3105#ifdef CONFIG_COMPAT
3106static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3107 bool needs_lock)
3108{
3109 struct compat_iovec __user *uiov;
3110 compat_ssize_t clen;
3111 void __user *buf;
3112 ssize_t len;
3113
3114 uiov = u64_to_user_ptr(req->rw.addr);
3115 if (!access_ok(uiov, sizeof(*uiov)))
3116 return -EFAULT;
3117 if (__get_user(clen, &uiov->iov_len))
3118 return -EFAULT;
3119 if (clen < 0)
3120 return -EINVAL;
3121
3122 len = clen;
3123 buf = io_rw_buffer_select(req, &len, needs_lock);
3124 if (IS_ERR(buf))
3125 return PTR_ERR(buf);
3126 iov[0].iov_base = buf;
3127 iov[0].iov_len = (compat_size_t) len;
3128 return 0;
3129}
3130#endif
3131
3132static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3133 bool needs_lock)
3134{
3135 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3136 void __user *buf;
3137 ssize_t len;
3138
3139 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3140 return -EFAULT;
3141
3142 len = iov[0].iov_len;
3143 if (len < 0)
3144 return -EINVAL;
3145 buf = io_rw_buffer_select(req, &len, needs_lock);
3146 if (IS_ERR(buf))
3147 return PTR_ERR(buf);
3148 iov[0].iov_base = buf;
3149 iov[0].iov_len = len;
3150 return 0;
3151}
3152
3153static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3154 bool needs_lock)
3155{
Jens Axboedddb3e22020-06-04 11:27:01 -06003156 if (req->flags & REQ_F_BUFFER_SELECTED) {
3157 struct io_buffer *kbuf;
3158
3159 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3160 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3161 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003162 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003163 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003164 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003165 return -EINVAL;
3166
3167#ifdef CONFIG_COMPAT
3168 if (req->ctx->compat)
3169 return io_compat_import(req, iov, needs_lock);
3170#endif
3171
3172 return __io_iov_buffer_select(req, iov, needs_lock);
3173}
3174
Pavel Begunkov847595d2021-02-04 13:52:06 +00003175static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3176 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003177{
Jens Axboe9adbd452019-12-20 08:45:55 -07003178 void __user *buf = u64_to_user_ptr(req->rw.addr);
3179 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003180 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003181 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003182
Pavel Begunkov7d009162019-11-25 23:14:40 +03003183 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003184 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003185 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003186 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003187
Jens Axboebcda7ba2020-02-23 16:42:51 -07003188 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003189 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003190 return -EINVAL;
3191
Jens Axboe3a6820f2019-12-22 15:19:35 -07003192 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003193 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003194 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003195 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003196 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003197 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003198 }
3199
Jens Axboe3a6820f2019-12-22 15:19:35 -07003200 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3201 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003202 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003203 }
3204
Jens Axboe4d954c22020-02-27 07:31:19 -07003205 if (req->flags & REQ_F_BUFFER_SELECT) {
3206 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003207 if (!ret)
3208 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003209 *iovec = NULL;
3210 return ret;
3211 }
3212
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003213 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3214 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003215}
3216
Jens Axboe0fef9482020-08-26 10:36:20 -06003217static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3218{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003219 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003220}
3221
Jens Axboe32960612019-09-23 11:05:34 -06003222/*
3223 * For files that don't have ->read_iter() and ->write_iter(), handle them
3224 * by looping over ->read() or ->write() manually.
3225 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003226static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003227{
Jens Axboe4017eb92020-10-22 14:14:12 -06003228 struct kiocb *kiocb = &req->rw.kiocb;
3229 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003230 ssize_t ret = 0;
3231
3232 /*
3233 * Don't support polled IO through this interface, and we can't
3234 * support non-blocking either. For the latter, this just causes
3235 * the kiocb to be handled from an async context.
3236 */
3237 if (kiocb->ki_flags & IOCB_HIPRI)
3238 return -EOPNOTSUPP;
3239 if (kiocb->ki_flags & IOCB_NOWAIT)
3240 return -EAGAIN;
3241
3242 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003243 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003244 ssize_t nr;
3245
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003246 if (!iov_iter_is_bvec(iter)) {
3247 iovec = iov_iter_iovec(iter);
3248 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003249 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3250 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003251 }
3252
Jens Axboe32960612019-09-23 11:05:34 -06003253 if (rw == READ) {
3254 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003255 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003256 } else {
3257 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003258 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003259 }
3260
3261 if (nr < 0) {
3262 if (!ret)
3263 ret = nr;
3264 break;
3265 }
Jens Axboe16c8d2d2021-09-12 06:45:07 -06003266 if (!iov_iter_is_bvec(iter)) {
3267 iov_iter_advance(iter, nr);
3268 } else {
3269 req->rw.len -= nr;
3270 req->rw.addr += nr;
3271 }
Jens Axboe32960612019-09-23 11:05:34 -06003272 ret += nr;
3273 if (nr != iovec.iov_len)
3274 break;
Jens Axboe32960612019-09-23 11:05:34 -06003275 }
3276
3277 return ret;
3278}
3279
Jens Axboeff6165b2020-08-13 09:47:43 -06003280static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3281 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003282{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003283 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003284
Jens Axboeff6165b2020-08-13 09:47:43 -06003285 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003286 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003287 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003288 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003289 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003290 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003291 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003292 unsigned iov_off = 0;
3293
3294 rw->iter.iov = rw->fast_iov;
3295 if (iter->iov != fast_iov) {
3296 iov_off = iter->iov - fast_iov;
3297 rw->iter.iov += iov_off;
3298 }
3299 if (rw->fast_iov != fast_iov)
3300 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003301 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003302 } else {
3303 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003304 }
3305}
3306
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003307static inline int io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003308{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003309 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3310 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3311 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003312}
3313
Jens Axboeff6165b2020-08-13 09:47:43 -06003314static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3315 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003316 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003317{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003318 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003319 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003320 if (!req->async_data) {
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003321 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003322 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003323 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003324 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003325
Jens Axboeff6165b2020-08-13 09:47:43 -06003326 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003327 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003328 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003329}
3330
Pavel Begunkov73debe62020-09-30 22:57:54 +03003331static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003332{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003333 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003334 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003335 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003336
Pavel Begunkov2846c482020-11-07 13:16:27 +00003337 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003338 if (unlikely(ret < 0))
3339 return ret;
3340
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003341 iorw->bytes_done = 0;
3342 iorw->free_iovec = iov;
3343 if (iov)
3344 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003345 return 0;
3346}
3347
Pavel Begunkov73debe62020-09-30 22:57:54 +03003348static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003349{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003350 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3351 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003352 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003353}
3354
Jens Axboec1dd91d2020-08-03 16:43:59 -06003355/*
3356 * This is our waitqueue callback handler, registered through lock_page_async()
3357 * when we initially tried to do the IO with the iocb armed our waitqueue.
3358 * This gets called when the page is unlocked, and we generally expect that to
3359 * happen when the page IO is completed and the page is now uptodate. This will
3360 * queue a task_work based retry of the operation, attempting to copy the data
3361 * again. If the latter fails because the page was NOT uptodate, then we will
3362 * do a thread based blocking retry of the operation. That's the unexpected
3363 * slow path.
3364 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003365static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3366 int sync, void *arg)
3367{
3368 struct wait_page_queue *wpq;
3369 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003370 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003371
3372 wpq = container_of(wait, struct wait_page_queue, wait);
3373
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003374 if (!wake_page_match(wpq, key))
3375 return 0;
3376
Hao Xuc8d317a2020-09-29 20:00:45 +08003377 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003378 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003379 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003380 return 1;
3381}
3382
Jens Axboec1dd91d2020-08-03 16:43:59 -06003383/*
3384 * This controls whether a given IO request should be armed for async page
3385 * based retry. If we return false here, the request is handed to the async
3386 * worker threads for retry. If we're doing buffered reads on a regular file,
3387 * we prepare a private wait_page_queue entry and retry the operation. This
3388 * will either succeed because the page is now uptodate and unlocked, or it
3389 * will register a callback when the page is unlocked at IO completion. Through
3390 * that callback, io_uring uses task_work to setup a retry of the operation.
3391 * That retry will attempt the buffered read again. The retry will generally
3392 * succeed, or in rare cases where it fails, we then fall back to using the
3393 * async worker threads for a blocking retry.
3394 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003395static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003396{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003397 struct io_async_rw *rw = req->async_data;
3398 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003399 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003400
3401 /* never retry for NOWAIT, we just complete with -EAGAIN */
3402 if (req->flags & REQ_F_NOWAIT)
3403 return false;
3404
Jens Axboe227c0c92020-08-13 11:51:40 -06003405 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003406 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003407 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003408
Jens Axboebcf5a062020-05-22 09:24:42 -06003409 /*
3410 * just use poll if we can, and don't attempt if the fs doesn't
3411 * support callback based unlocks
3412 */
3413 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3414 return false;
3415
Jens Axboe3b2a4432020-08-16 10:58:43 -07003416 wait->wait.func = io_async_buf_func;
3417 wait->wait.private = req;
3418 wait->wait.flags = 0;
3419 INIT_LIST_HEAD(&wait->wait.entry);
3420 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003421 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003422 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003423 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003424}
3425
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003426static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003427{
3428 if (req->file->f_op->read_iter)
3429 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003430 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003431 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003432 else
3433 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003434}
3435
Ming Lei7db30432021-08-21 23:07:51 +08003436static bool need_read_all(struct io_kiocb *req)
3437{
3438 return req->flags & REQ_F_ISREG ||
3439 S_ISBLK(file_inode(req->file)->i_mode);
3440}
3441
Pavel Begunkov889fca72021-02-10 00:03:09 +00003442static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003443{
3444 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003445 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003446 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003447 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003448 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003449 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003450
Pavel Begunkov2846c482020-11-07 13:16:27 +00003451 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003452 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003453 iovec = NULL;
3454 } else {
3455 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3456 if (ret < 0)
3457 return ret;
3458 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003459 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003460 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003461
Jens Axboefd6c2e42019-12-18 12:19:41 -07003462 /* Ensure we clear previously set non-block flag */
3463 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003464 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003465 else
3466 kiocb->ki_flags |= IOCB_NOWAIT;
3467
Pavel Begunkov24c74672020-06-21 13:09:51 +03003468 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003469 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003470 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003471 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003472 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003473
Pavel Begunkov632546c2020-11-07 13:16:26 +00003474 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003475 if (unlikely(ret)) {
3476 kfree(iovec);
3477 return ret;
3478 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003479
Jens Axboe227c0c92020-08-13 11:51:40 -06003480 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003481
Jens Axboe230d50d2021-04-01 20:41:15 -06003482 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003483 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003484 /* IOPOLL retry should happen for io-wq threads */
3485 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003486 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003487 /* no retry on NONBLOCK nor RWF_NOWAIT */
3488 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003489 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003490 /* some cases will consume bytes even on error returns */
Pavel Begunkov89c2b3b2021-08-23 11:18:45 +01003491 iov_iter_reexpand(iter, iter->count + iter->truncated);
Pavel Begunkov632546c2020-11-07 13:16:26 +00003492 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003493 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003494 } else if (ret == -EIOCBQUEUED) {
3495 goto out_free;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003496 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003497 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003498 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003499 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003500 }
3501
Jens Axboe227c0c92020-08-13 11:51:40 -06003502 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003503 if (ret2)
3504 return ret2;
3505
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003506 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003507 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003508 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003509 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003510
Pavel Begunkovb23df912021-02-04 13:52:04 +00003511 do {
3512 io_size -= ret;
3513 rw->bytes_done += ret;
3514 /* if we can retry, do so with the callbacks armed */
3515 if (!io_rw_should_retry(req)) {
3516 kiocb->ki_flags &= ~IOCB_WAITQ;
3517 return -EAGAIN;
3518 }
3519
3520 /*
3521 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3522 * we get -EIOCBQUEUED, then we'll get a notification when the
3523 * desired page gets unlocked. We can also get a partial read
3524 * here, and if we do, then just retry at the new offset.
3525 */
3526 ret = io_iter_do_read(req, iter);
3527 if (ret == -EIOCBQUEUED)
3528 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003529 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003530 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003531 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003532done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003533 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003534out_free:
3535 /* it's faster to check here then delegate to kfree */
3536 if (iovec)
3537 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003538 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003539}
3540
Pavel Begunkov73debe62020-09-30 22:57:54 +03003541static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003542{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003543 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3544 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003545 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003546}
3547
Pavel Begunkov889fca72021-02-10 00:03:09 +00003548static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003549{
3550 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003551 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003552 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003553 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003554 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003555 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003556
Pavel Begunkov2846c482020-11-07 13:16:27 +00003557 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003558 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003559 iovec = NULL;
3560 } else {
3561 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3562 if (ret < 0)
3563 return ret;
3564 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003565 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003566 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003567
Jens Axboefd6c2e42019-12-18 12:19:41 -07003568 /* Ensure we clear previously set non-block flag */
3569 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003570 kiocb->ki_flags &= ~IOCB_NOWAIT;
3571 else
3572 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003573
Pavel Begunkov24c74672020-06-21 13:09:51 +03003574 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003575 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003576 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003577
Jens Axboe10d59342019-12-09 20:16:22 -07003578 /* file path doesn't support NOWAIT for non-direct_IO */
3579 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3580 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003581 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003582
Pavel Begunkov632546c2020-11-07 13:16:26 +00003583 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003584 if (unlikely(ret))
3585 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003586
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003587 /*
3588 * Open-code file_start_write here to grab freeze protection,
3589 * which will be released by another thread in
3590 * io_complete_rw(). Fool lockdep by telling it the lock got
3591 * released so that it doesn't complain about the held lock when
3592 * we return to userspace.
3593 */
3594 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003595 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003596 __sb_writers_release(file_inode(req->file)->i_sb,
3597 SB_FREEZE_WRITE);
3598 }
3599 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003600
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003601 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003602 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003603 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003604 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003605 else
3606 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003607
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003608 if (req->flags & REQ_F_REISSUE) {
3609 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003610 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003611 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003612
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003613 /*
3614 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3615 * retry them without IOCB_NOWAIT.
3616 */
3617 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3618 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003619 /* no retry on NONBLOCK nor RWF_NOWAIT */
3620 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003621 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003622 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003623 /* IOPOLL retry should happen for io-wq threads */
3624 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3625 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003626done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003627 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003628 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003629copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003630 /* some cases will consume bytes even on error returns */
Pavel Begunkov89c2b3b2021-08-23 11:18:45 +01003631 iov_iter_reexpand(iter, iter->count + iter->truncated);
Pavel Begunkov632546c2020-11-07 13:16:26 +00003632 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003633 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003634 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003635 }
Jens Axboe31b51512019-01-18 22:56:34 -07003636out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003637 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003638 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003639 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003640 return ret;
3641}
3642
Jens Axboe80a261f2020-09-28 14:23:58 -06003643static int io_renameat_prep(struct io_kiocb *req,
3644 const struct io_uring_sqe *sqe)
3645{
3646 struct io_rename *ren = &req->rename;
3647 const char __user *oldf, *newf;
3648
Jens Axboeed7eb252021-06-23 09:04:13 -06003649 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3650 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003651 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003652 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003653 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3654 return -EBADF;
3655
3656 ren->old_dfd = READ_ONCE(sqe->fd);
3657 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3658 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3659 ren->new_dfd = READ_ONCE(sqe->len);
3660 ren->flags = READ_ONCE(sqe->rename_flags);
3661
3662 ren->oldpath = getname(oldf);
3663 if (IS_ERR(ren->oldpath))
3664 return PTR_ERR(ren->oldpath);
3665
3666 ren->newpath = getname(newf);
3667 if (IS_ERR(ren->newpath)) {
3668 putname(ren->oldpath);
3669 return PTR_ERR(ren->newpath);
3670 }
3671
3672 req->flags |= REQ_F_NEED_CLEANUP;
3673 return 0;
3674}
3675
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003676static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003677{
3678 struct io_rename *ren = &req->rename;
3679 int ret;
3680
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003681 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003682 return -EAGAIN;
3683
3684 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3685 ren->newpath, ren->flags);
3686
3687 req->flags &= ~REQ_F_NEED_CLEANUP;
3688 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003689 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003690 io_req_complete(req, ret);
3691 return 0;
3692}
3693
Jens Axboe14a11432020-09-28 14:27:37 -06003694static int io_unlinkat_prep(struct io_kiocb *req,
3695 const struct io_uring_sqe *sqe)
3696{
3697 struct io_unlink *un = &req->unlink;
3698 const char __user *fname;
3699
Jens Axboe22634bc2021-06-23 09:07:45 -06003700 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3701 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003702 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3703 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003704 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003705 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3706 return -EBADF;
3707
3708 un->dfd = READ_ONCE(sqe->fd);
3709
3710 un->flags = READ_ONCE(sqe->unlink_flags);
3711 if (un->flags & ~AT_REMOVEDIR)
3712 return -EINVAL;
3713
3714 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3715 un->filename = getname(fname);
3716 if (IS_ERR(un->filename))
3717 return PTR_ERR(un->filename);
3718
3719 req->flags |= REQ_F_NEED_CLEANUP;
3720 return 0;
3721}
3722
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003723static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003724{
3725 struct io_unlink *un = &req->unlink;
3726 int ret;
3727
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003728 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003729 return -EAGAIN;
3730
3731 if (un->flags & AT_REMOVEDIR)
3732 ret = do_rmdir(un->dfd, un->filename);
3733 else
3734 ret = do_unlinkat(un->dfd, un->filename);
3735
3736 req->flags &= ~REQ_F_NEED_CLEANUP;
3737 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003738 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003739 io_req_complete(req, ret);
3740 return 0;
3741}
3742
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003743static int io_mkdirat_prep(struct io_kiocb *req,
3744 const struct io_uring_sqe *sqe)
3745{
3746 struct io_mkdir *mkd = &req->mkdir;
3747 const char __user *fname;
3748
3749 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3750 return -EINVAL;
3751 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3752 sqe->splice_fd_in)
3753 return -EINVAL;
3754 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3755 return -EBADF;
3756
3757 mkd->dfd = READ_ONCE(sqe->fd);
3758 mkd->mode = READ_ONCE(sqe->len);
3759
3760 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3761 mkd->filename = getname(fname);
3762 if (IS_ERR(mkd->filename))
3763 return PTR_ERR(mkd->filename);
3764
3765 req->flags |= REQ_F_NEED_CLEANUP;
3766 return 0;
3767}
3768
3769static int io_mkdirat(struct io_kiocb *req, int issue_flags)
3770{
3771 struct io_mkdir *mkd = &req->mkdir;
3772 int ret;
3773
3774 if (issue_flags & IO_URING_F_NONBLOCK)
3775 return -EAGAIN;
3776
3777 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3778
3779 req->flags &= ~REQ_F_NEED_CLEANUP;
3780 if (ret < 0)
3781 req_set_fail(req);
3782 io_req_complete(req, ret);
3783 return 0;
3784}
3785
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003786static int io_symlinkat_prep(struct io_kiocb *req,
3787 const struct io_uring_sqe *sqe)
3788{
3789 struct io_symlink *sl = &req->symlink;
3790 const char __user *oldpath, *newpath;
3791
3792 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3793 return -EINVAL;
3794 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3795 sqe->splice_fd_in)
3796 return -EINVAL;
3797 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3798 return -EBADF;
3799
3800 sl->new_dfd = READ_ONCE(sqe->fd);
3801 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3802 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3803
3804 sl->oldpath = getname(oldpath);
3805 if (IS_ERR(sl->oldpath))
3806 return PTR_ERR(sl->oldpath);
3807
3808 sl->newpath = getname(newpath);
3809 if (IS_ERR(sl->newpath)) {
3810 putname(sl->oldpath);
3811 return PTR_ERR(sl->newpath);
3812 }
3813
3814 req->flags |= REQ_F_NEED_CLEANUP;
3815 return 0;
3816}
3817
3818static int io_symlinkat(struct io_kiocb *req, int issue_flags)
3819{
3820 struct io_symlink *sl = &req->symlink;
3821 int ret;
3822
3823 if (issue_flags & IO_URING_F_NONBLOCK)
3824 return -EAGAIN;
3825
3826 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3827
3828 req->flags &= ~REQ_F_NEED_CLEANUP;
3829 if (ret < 0)
3830 req_set_fail(req);
3831 io_req_complete(req, ret);
3832 return 0;
3833}
3834
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003835static int io_linkat_prep(struct io_kiocb *req,
3836 const struct io_uring_sqe *sqe)
3837{
3838 struct io_hardlink *lnk = &req->hardlink;
3839 const char __user *oldf, *newf;
3840
3841 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3842 return -EINVAL;
3843 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3844 return -EINVAL;
3845 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3846 return -EBADF;
3847
3848 lnk->old_dfd = READ_ONCE(sqe->fd);
3849 lnk->new_dfd = READ_ONCE(sqe->len);
3850 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3851 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3852 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3853
3854 lnk->oldpath = getname(oldf);
3855 if (IS_ERR(lnk->oldpath))
3856 return PTR_ERR(lnk->oldpath);
3857
3858 lnk->newpath = getname(newf);
3859 if (IS_ERR(lnk->newpath)) {
3860 putname(lnk->oldpath);
3861 return PTR_ERR(lnk->newpath);
3862 }
3863
3864 req->flags |= REQ_F_NEED_CLEANUP;
3865 return 0;
3866}
3867
3868static int io_linkat(struct io_kiocb *req, int issue_flags)
3869{
3870 struct io_hardlink *lnk = &req->hardlink;
3871 int ret;
3872
3873 if (issue_flags & IO_URING_F_NONBLOCK)
3874 return -EAGAIN;
3875
3876 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3877 lnk->newpath, lnk->flags);
3878
3879 req->flags &= ~REQ_F_NEED_CLEANUP;
3880 if (ret < 0)
3881 req_set_fail(req);
3882 io_req_complete(req, ret);
3883 return 0;
3884}
3885
Jens Axboe36f4fa62020-09-05 11:14:22 -06003886static int io_shutdown_prep(struct io_kiocb *req,
3887 const struct io_uring_sqe *sqe)
3888{
3889#if defined(CONFIG_NET)
3890 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3891 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003892 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3893 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003894 return -EINVAL;
3895
3896 req->shutdown.how = READ_ONCE(sqe->len);
3897 return 0;
3898#else
3899 return -EOPNOTSUPP;
3900#endif
3901}
3902
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003903static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003904{
3905#if defined(CONFIG_NET)
3906 struct socket *sock;
3907 int ret;
3908
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003909 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003910 return -EAGAIN;
3911
Linus Torvalds48aba792020-12-16 12:44:05 -08003912 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003913 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003914 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003915
3916 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003917 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003918 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003919 io_req_complete(req, ret);
3920 return 0;
3921#else
3922 return -EOPNOTSUPP;
3923#endif
3924}
3925
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003926static int __io_splice_prep(struct io_kiocb *req,
3927 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003928{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003929 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003930 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003931
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003932 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3933 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003934
3935 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003936 sp->len = READ_ONCE(sqe->len);
3937 sp->flags = READ_ONCE(sqe->splice_flags);
3938
3939 if (unlikely(sp->flags & ~valid_flags))
3940 return -EINVAL;
3941
Pavel Begunkov62906e82021-08-10 14:52:47 +01003942 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003943 (sp->flags & SPLICE_F_FD_IN_FIXED));
3944 if (!sp->file_in)
3945 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003946 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003947 return 0;
3948}
3949
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003950static int io_tee_prep(struct io_kiocb *req,
3951 const struct io_uring_sqe *sqe)
3952{
3953 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3954 return -EINVAL;
3955 return __io_splice_prep(req, sqe);
3956}
3957
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003958static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003959{
3960 struct io_splice *sp = &req->splice;
3961 struct file *in = sp->file_in;
3962 struct file *out = sp->file_out;
3963 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3964 long ret = 0;
3965
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003966 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003967 return -EAGAIN;
3968 if (sp->len)
3969 ret = do_tee(in, out, sp->len, flags);
3970
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003971 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3972 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003973 req->flags &= ~REQ_F_NEED_CLEANUP;
3974
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003975 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003976 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003977 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003978 return 0;
3979}
3980
3981static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3982{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003983 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003984
3985 sp->off_in = READ_ONCE(sqe->splice_off_in);
3986 sp->off_out = READ_ONCE(sqe->off);
3987 return __io_splice_prep(req, sqe);
3988}
3989
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003990static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003991{
3992 struct io_splice *sp = &req->splice;
3993 struct file *in = sp->file_in;
3994 struct file *out = sp->file_out;
3995 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3996 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003997 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003998
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003999 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004000 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004001
4002 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4003 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004004
Jens Axboe948a7742020-05-17 14:21:38 -06004005 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004006 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004007
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004008 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4009 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004010 req->flags &= ~REQ_F_NEED_CLEANUP;
4011
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004012 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004013 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004014 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004015 return 0;
4016}
4017
Jens Axboe2b188cc2019-01-07 10:46:33 -07004018/*
4019 * IORING_OP_NOP just posts a completion event, nothing else.
4020 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004021static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004022{
4023 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004024
Jens Axboedef596e2019-01-09 08:59:42 -07004025 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4026 return -EINVAL;
4027
Pavel Begunkov889fca72021-02-10 00:03:09 +00004028 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004029 return 0;
4030}
4031
Pavel Begunkov1155c762021-02-18 18:29:38 +00004032static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004033{
Jens Axboe6b063142019-01-10 22:13:58 -07004034 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004035
Jens Axboe09bb8392019-03-13 12:39:28 -06004036 if (!req->file)
4037 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004038
Jens Axboe6b063142019-01-10 22:13:58 -07004039 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004040 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004041 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4042 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004043 return -EINVAL;
4044
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004045 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4046 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4047 return -EINVAL;
4048
4049 req->sync.off = READ_ONCE(sqe->off);
4050 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004051 return 0;
4052}
4053
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004054static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004055{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004056 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004057 int ret;
4058
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004059 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004060 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004061 return -EAGAIN;
4062
Jens Axboe9adbd452019-12-20 08:45:55 -07004063 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004064 end > 0 ? end : LLONG_MAX,
4065 req->sync.flags & IORING_FSYNC_DATASYNC);
4066 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004067 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004068 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004069 return 0;
4070}
4071
Jens Axboed63d1b52019-12-10 10:38:56 -07004072static int io_fallocate_prep(struct io_kiocb *req,
4073 const struct io_uring_sqe *sqe)
4074{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004075 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4076 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004077 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004078 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4079 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004080
4081 req->sync.off = READ_ONCE(sqe->off);
4082 req->sync.len = READ_ONCE(sqe->addr);
4083 req->sync.mode = READ_ONCE(sqe->len);
4084 return 0;
4085}
4086
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004087static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004088{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004089 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004090
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004091 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004092 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004093 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004094 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4095 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004096 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004097 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004098 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004099 return 0;
4100}
4101
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004102static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004103{
Jens Axboef8748882020-01-08 17:47:02 -07004104 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004105 int ret;
4106
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004107 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4108 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004109 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004110 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004111 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004112 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004113
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004114 /* open.how should be already initialised */
4115 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004116 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004117
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004118 req->open.dfd = READ_ONCE(sqe->fd);
4119 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004120 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004121 if (IS_ERR(req->open.filename)) {
4122 ret = PTR_ERR(req->open.filename);
4123 req->open.filename = NULL;
4124 return ret;
4125 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004126
4127 req->open.file_slot = READ_ONCE(sqe->file_index);
4128 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4129 return -EINVAL;
4130
Jens Axboe4022e7a2020-03-19 19:23:18 -06004131 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004132 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004133 return 0;
4134}
4135
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004136static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4137{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004138 u64 mode = READ_ONCE(sqe->len);
4139 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004140
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004141 req->open.how = build_open_how(flags, mode);
4142 return __io_openat_prep(req, sqe);
4143}
4144
Jens Axboecebdb982020-01-08 17:59:24 -07004145static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4146{
4147 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004148 size_t len;
4149 int ret;
4150
Jens Axboecebdb982020-01-08 17:59:24 -07004151 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4152 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004153 if (len < OPEN_HOW_SIZE_VER0)
4154 return -EINVAL;
4155
4156 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4157 len);
4158 if (ret)
4159 return ret;
4160
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004161 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004162}
4163
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004164static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004165{
4166 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004167 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004168 bool resolve_nonblock, nonblock_set;
4169 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004170 int ret;
4171
Jens Axboecebdb982020-01-08 17:59:24 -07004172 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004173 if (ret)
4174 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004175 nonblock_set = op.open_flag & O_NONBLOCK;
4176 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004177 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004178 /*
4179 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4180 * it'll always -EAGAIN
4181 */
4182 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4183 return -EAGAIN;
4184 op.lookup_flags |= LOOKUP_CACHED;
4185 op.open_flag |= O_NONBLOCK;
4186 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004187
Pavel Begunkovb9445592021-08-25 12:25:45 +01004188 if (!fixed) {
4189 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4190 if (ret < 0)
4191 goto err;
4192 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004193
4194 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004195 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004196 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004197 * We could hang on to this 'fd' on retrying, but seems like
4198 * marginal gain for something that is now known to be a slower
4199 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004200 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004201 if (!fixed)
4202 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004203
4204 ret = PTR_ERR(file);
4205 /* only retry if RESOLVE_CACHED wasn't already set by application */
4206 if (ret == -EAGAIN &&
4207 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4208 return -EAGAIN;
4209 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004210 }
4211
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004212 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4213 file->f_flags &= ~O_NONBLOCK;
4214 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004215
4216 if (!fixed)
4217 fd_install(ret, file);
4218 else
4219 ret = io_install_fixed_file(req, file, issue_flags,
4220 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004221err:
4222 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004223 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004224 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004225 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004226 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004227 return 0;
4228}
4229
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004230static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004231{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004232 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004233}
4234
Jens Axboe067524e2020-03-02 16:32:28 -07004235static int io_remove_buffers_prep(struct io_kiocb *req,
4236 const struct io_uring_sqe *sqe)
4237{
4238 struct io_provide_buf *p = &req->pbuf;
4239 u64 tmp;
4240
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004241 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4242 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004243 return -EINVAL;
4244
4245 tmp = READ_ONCE(sqe->fd);
4246 if (!tmp || tmp > USHRT_MAX)
4247 return -EINVAL;
4248
4249 memset(p, 0, sizeof(*p));
4250 p->nbufs = tmp;
4251 p->bgid = READ_ONCE(sqe->buf_group);
4252 return 0;
4253}
4254
4255static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4256 int bgid, unsigned nbufs)
4257{
4258 unsigned i = 0;
4259
4260 /* shouldn't happen */
4261 if (!nbufs)
4262 return 0;
4263
4264 /* the head kbuf is the list itself */
4265 while (!list_empty(&buf->list)) {
4266 struct io_buffer *nxt;
4267
4268 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4269 list_del(&nxt->list);
4270 kfree(nxt);
4271 if (++i == nbufs)
4272 return i;
4273 }
4274 i++;
4275 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004276 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004277
4278 return i;
4279}
4280
Pavel Begunkov889fca72021-02-10 00:03:09 +00004281static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004282{
4283 struct io_provide_buf *p = &req->pbuf;
4284 struct io_ring_ctx *ctx = req->ctx;
4285 struct io_buffer *head;
4286 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004287 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004288
4289 io_ring_submit_lock(ctx, !force_nonblock);
4290
4291 lockdep_assert_held(&ctx->uring_lock);
4292
4293 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004294 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004295 if (head)
4296 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004297 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004298 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004299
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004300 /* complete before unlock, IOPOLL may need the lock */
4301 __io_req_complete(req, issue_flags, ret, 0);
4302 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004303 return 0;
4304}
4305
Jens Axboeddf0322d2020-02-23 16:41:33 -07004306static int io_provide_buffers_prep(struct io_kiocb *req,
4307 const struct io_uring_sqe *sqe)
4308{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004309 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004310 struct io_provide_buf *p = &req->pbuf;
4311 u64 tmp;
4312
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004313 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004314 return -EINVAL;
4315
4316 tmp = READ_ONCE(sqe->fd);
4317 if (!tmp || tmp > USHRT_MAX)
4318 return -E2BIG;
4319 p->nbufs = tmp;
4320 p->addr = READ_ONCE(sqe->addr);
4321 p->len = READ_ONCE(sqe->len);
4322
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004323 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4324 &size))
4325 return -EOVERFLOW;
4326 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4327 return -EOVERFLOW;
4328
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004329 size = (unsigned long)p->len * p->nbufs;
4330 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004331 return -EFAULT;
4332
4333 p->bgid = READ_ONCE(sqe->buf_group);
4334 tmp = READ_ONCE(sqe->off);
4335 if (tmp > USHRT_MAX)
4336 return -E2BIG;
4337 p->bid = tmp;
4338 return 0;
4339}
4340
4341static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4342{
4343 struct io_buffer *buf;
4344 u64 addr = pbuf->addr;
4345 int i, bid = pbuf->bid;
4346
4347 for (i = 0; i < pbuf->nbufs; i++) {
4348 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4349 if (!buf)
4350 break;
4351
4352 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004353 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004354 buf->bid = bid;
4355 addr += pbuf->len;
4356 bid++;
4357 if (!*head) {
4358 INIT_LIST_HEAD(&buf->list);
4359 *head = buf;
4360 } else {
4361 list_add_tail(&buf->list, &(*head)->list);
4362 }
4363 }
4364
4365 return i ? i : -ENOMEM;
4366}
4367
Pavel Begunkov889fca72021-02-10 00:03:09 +00004368static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004369{
4370 struct io_provide_buf *p = &req->pbuf;
4371 struct io_ring_ctx *ctx = req->ctx;
4372 struct io_buffer *head, *list;
4373 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004374 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004375
4376 io_ring_submit_lock(ctx, !force_nonblock);
4377
4378 lockdep_assert_held(&ctx->uring_lock);
4379
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004380 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004381
4382 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004383 if (ret >= 0 && !list) {
4384 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4385 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004386 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004387 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004388 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004389 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004390 /* complete before unlock, IOPOLL may need the lock */
4391 __io_req_complete(req, issue_flags, ret, 0);
4392 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004393 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004394}
4395
Jens Axboe3e4827b2020-01-08 15:18:09 -07004396static int io_epoll_ctl_prep(struct io_kiocb *req,
4397 const struct io_uring_sqe *sqe)
4398{
4399#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004400 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004401 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004402 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004403 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004404
4405 req->epoll.epfd = READ_ONCE(sqe->fd);
4406 req->epoll.op = READ_ONCE(sqe->len);
4407 req->epoll.fd = READ_ONCE(sqe->off);
4408
4409 if (ep_op_has_event(req->epoll.op)) {
4410 struct epoll_event __user *ev;
4411
4412 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4413 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4414 return -EFAULT;
4415 }
4416
4417 return 0;
4418#else
4419 return -EOPNOTSUPP;
4420#endif
4421}
4422
Pavel Begunkov889fca72021-02-10 00:03:09 +00004423static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004424{
4425#if defined(CONFIG_EPOLL)
4426 struct io_epoll *ie = &req->epoll;
4427 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004428 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004429
4430 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4431 if (force_nonblock && ret == -EAGAIN)
4432 return -EAGAIN;
4433
4434 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004435 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004436 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004437 return 0;
4438#else
4439 return -EOPNOTSUPP;
4440#endif
4441}
4442
Jens Axboec1ca7572019-12-25 22:18:28 -07004443static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4444{
4445#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004446 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004447 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004448 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4449 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004450
4451 req->madvise.addr = READ_ONCE(sqe->addr);
4452 req->madvise.len = READ_ONCE(sqe->len);
4453 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4454 return 0;
4455#else
4456 return -EOPNOTSUPP;
4457#endif
4458}
4459
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004460static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004461{
4462#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4463 struct io_madvise *ma = &req->madvise;
4464 int ret;
4465
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004466 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004467 return -EAGAIN;
4468
Minchan Kim0726b012020-10-17 16:14:50 -07004469 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004470 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004471 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004472 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004473 return 0;
4474#else
4475 return -EOPNOTSUPP;
4476#endif
4477}
4478
Jens Axboe4840e412019-12-25 22:03:45 -07004479static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4480{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004481 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004482 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004483 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4484 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004485
4486 req->fadvise.offset = READ_ONCE(sqe->off);
4487 req->fadvise.len = READ_ONCE(sqe->len);
4488 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4489 return 0;
4490}
4491
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004492static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004493{
4494 struct io_fadvise *fa = &req->fadvise;
4495 int ret;
4496
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004497 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004498 switch (fa->advice) {
4499 case POSIX_FADV_NORMAL:
4500 case POSIX_FADV_RANDOM:
4501 case POSIX_FADV_SEQUENTIAL:
4502 break;
4503 default:
4504 return -EAGAIN;
4505 }
4506 }
Jens Axboe4840e412019-12-25 22:03:45 -07004507
4508 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4509 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004510 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004511 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004512 return 0;
4513}
4514
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004515static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4516{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004517 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004518 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004519 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004520 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004521 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004522 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004523
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004524 req->statx.dfd = READ_ONCE(sqe->fd);
4525 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004526 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004527 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4528 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004529
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004530 return 0;
4531}
4532
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004533static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004534{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004535 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004536 int ret;
4537
Pavel Begunkov59d70012021-03-22 01:58:30 +00004538 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004539 return -EAGAIN;
4540
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004541 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4542 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004543
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004544 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004545 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004546 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004547 return 0;
4548}
4549
Jens Axboeb5dba592019-12-11 14:02:38 -07004550static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4551{
Jens Axboe14587a462020-09-05 11:36:08 -06004552 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004553 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004554 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004555 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004556 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004557 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004558 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004559
4560 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004561 return 0;
4562}
4563
Pavel Begunkov889fca72021-02-10 00:03:09 +00004564static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004565{
Jens Axboe9eac1902021-01-19 15:50:37 -07004566 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004567 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004568 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004569 struct file *file = NULL;
4570 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004571
Jens Axboe9eac1902021-01-19 15:50:37 -07004572 spin_lock(&files->file_lock);
4573 fdt = files_fdtable(files);
4574 if (close->fd >= fdt->max_fds) {
4575 spin_unlock(&files->file_lock);
4576 goto err;
4577 }
4578 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004579 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004580 spin_unlock(&files->file_lock);
4581 file = NULL;
4582 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004583 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004584
4585 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004586 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004587 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004588 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004589 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004590
Jens Axboe9eac1902021-01-19 15:50:37 -07004591 ret = __close_fd_get_file(close->fd, &file);
4592 spin_unlock(&files->file_lock);
4593 if (ret < 0) {
4594 if (ret == -ENOENT)
4595 ret = -EBADF;
4596 goto err;
4597 }
4598
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004599 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004600 ret = filp_close(file, current->files);
4601err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004602 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004603 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004604 if (file)
4605 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004606 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004607 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004608}
4609
Pavel Begunkov1155c762021-02-18 18:29:38 +00004610static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004611{
4612 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004613
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004614 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4615 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004616 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4617 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004618 return -EINVAL;
4619
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004620 req->sync.off = READ_ONCE(sqe->off);
4621 req->sync.len = READ_ONCE(sqe->len);
4622 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004623 return 0;
4624}
4625
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004626static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004627{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004628 int ret;
4629
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004630 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004631 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004632 return -EAGAIN;
4633
Jens Axboe9adbd452019-12-20 08:45:55 -07004634 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004635 req->sync.flags);
4636 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004637 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004638 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004639 return 0;
4640}
4641
YueHaibing469956e2020-03-04 15:53:52 +08004642#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004643static int io_setup_async_msg(struct io_kiocb *req,
4644 struct io_async_msghdr *kmsg)
4645{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004646 struct io_async_msghdr *async_msg = req->async_data;
4647
4648 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004649 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004650 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004651 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004652 return -ENOMEM;
4653 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004654 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004655 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004656 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004657 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004658 /* if were using fast_iov, set it to the new one */
4659 if (!async_msg->free_iov)
4660 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4661
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004662 return -EAGAIN;
4663}
4664
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004665static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4666 struct io_async_msghdr *iomsg)
4667{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004668 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004669 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004670 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004671 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004672}
4673
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004674static int io_sendmsg_prep_async(struct io_kiocb *req)
4675{
4676 int ret;
4677
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004678 ret = io_sendmsg_copy_hdr(req, req->async_data);
4679 if (!ret)
4680 req->flags |= REQ_F_NEED_CLEANUP;
4681 return ret;
4682}
4683
Jens Axboe3529d8c2019-12-19 18:24:38 -07004684static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004685{
Jens Axboee47293f2019-12-20 08:58:21 -07004686 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004687
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004688 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4689 return -EINVAL;
4690
Pavel Begunkov270a5942020-07-12 20:41:04 +03004691 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004692 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004693 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4694 if (sr->msg_flags & MSG_DONTWAIT)
4695 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004696
Jens Axboed8768362020-02-27 14:17:49 -07004697#ifdef CONFIG_COMPAT
4698 if (req->ctx->compat)
4699 sr->msg_flags |= MSG_CMSG_COMPAT;
4700#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004701 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004702}
4703
Pavel Begunkov889fca72021-02-10 00:03:09 +00004704static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004705{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004706 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004707 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004708 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004709 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004710 int ret;
4711
Florent Revestdba4a922020-12-04 12:36:04 +01004712 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004713 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004714 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004715
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004716 kmsg = req->async_data;
4717 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004718 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004719 if (ret)
4720 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004721 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004722 }
4723
Pavel Begunkov04411802021-04-01 15:44:00 +01004724 flags = req->sr_msg.msg_flags;
4725 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004726 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004727 if (flags & MSG_WAITALL)
4728 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4729
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004730 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004731 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004732 return io_setup_async_msg(req, kmsg);
4733 if (ret == -ERESTARTSYS)
4734 ret = -EINTR;
4735
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004736 /* fast path, check for non-NULL to avoid function call */
4737 if (kmsg->free_iov)
4738 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004739 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004740 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004741 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004742 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004743 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004744}
4745
Pavel Begunkov889fca72021-02-10 00:03:09 +00004746static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004747{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004748 struct io_sr_msg *sr = &req->sr_msg;
4749 struct msghdr msg;
4750 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004751 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004752 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004753 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004754 int ret;
4755
Florent Revestdba4a922020-12-04 12:36:04 +01004756 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004757 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004758 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004759
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004760 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4761 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004762 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004763
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004764 msg.msg_name = NULL;
4765 msg.msg_control = NULL;
4766 msg.msg_controllen = 0;
4767 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004768
Pavel Begunkov04411802021-04-01 15:44:00 +01004769 flags = req->sr_msg.msg_flags;
4770 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004771 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004772 if (flags & MSG_WAITALL)
4773 min_ret = iov_iter_count(&msg.msg_iter);
4774
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004775 msg.msg_flags = flags;
4776 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004777 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004778 return -EAGAIN;
4779 if (ret == -ERESTARTSYS)
4780 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004781
Stefan Metzmacher00312752021-03-20 20:33:36 +01004782 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004783 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004784 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004785 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004786}
4787
Pavel Begunkov1400e692020-07-12 20:41:05 +03004788static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4789 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004790{
4791 struct io_sr_msg *sr = &req->sr_msg;
4792 struct iovec __user *uiov;
4793 size_t iov_len;
4794 int ret;
4795
Pavel Begunkov1400e692020-07-12 20:41:05 +03004796 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4797 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004798 if (ret)
4799 return ret;
4800
4801 if (req->flags & REQ_F_BUFFER_SELECT) {
4802 if (iov_len > 1)
4803 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004804 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004805 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004806 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004807 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004808 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004809 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004810 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004811 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004812 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004813 if (ret > 0)
4814 ret = 0;
4815 }
4816
4817 return ret;
4818}
4819
4820#ifdef CONFIG_COMPAT
4821static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004822 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004823{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004824 struct io_sr_msg *sr = &req->sr_msg;
4825 struct compat_iovec __user *uiov;
4826 compat_uptr_t ptr;
4827 compat_size_t len;
4828 int ret;
4829
Pavel Begunkov4af34172021-04-11 01:46:30 +01004830 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4831 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004832 if (ret)
4833 return ret;
4834
4835 uiov = compat_ptr(ptr);
4836 if (req->flags & REQ_F_BUFFER_SELECT) {
4837 compat_ssize_t clen;
4838
4839 if (len > 1)
4840 return -EINVAL;
4841 if (!access_ok(uiov, sizeof(*uiov)))
4842 return -EFAULT;
4843 if (__get_user(clen, &uiov->iov_len))
4844 return -EFAULT;
4845 if (clen < 0)
4846 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004847 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004848 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004849 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004850 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004851 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004852 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004853 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004854 if (ret < 0)
4855 return ret;
4856 }
4857
4858 return 0;
4859}
Jens Axboe03b12302019-12-02 18:50:25 -07004860#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004861
Pavel Begunkov1400e692020-07-12 20:41:05 +03004862static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4863 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004864{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004865 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004866
4867#ifdef CONFIG_COMPAT
4868 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004869 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004870#endif
4871
Pavel Begunkov1400e692020-07-12 20:41:05 +03004872 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004873}
4874
Jens Axboebcda7ba2020-02-23 16:42:51 -07004875static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004876 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004877{
4878 struct io_sr_msg *sr = &req->sr_msg;
4879 struct io_buffer *kbuf;
4880
Jens Axboebcda7ba2020-02-23 16:42:51 -07004881 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4882 if (IS_ERR(kbuf))
4883 return kbuf;
4884
4885 sr->kbuf = kbuf;
4886 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004887 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004888}
4889
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004890static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4891{
4892 return io_put_kbuf(req, req->sr_msg.kbuf);
4893}
4894
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004895static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004896{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004897 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004898
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004899 ret = io_recvmsg_copy_hdr(req, req->async_data);
4900 if (!ret)
4901 req->flags |= REQ_F_NEED_CLEANUP;
4902 return ret;
4903}
4904
4905static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4906{
4907 struct io_sr_msg *sr = &req->sr_msg;
4908
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004909 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4910 return -EINVAL;
4911
Pavel Begunkov270a5942020-07-12 20:41:04 +03004912 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004913 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004914 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004915 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4916 if (sr->msg_flags & MSG_DONTWAIT)
4917 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004918
Jens Axboed8768362020-02-27 14:17:49 -07004919#ifdef CONFIG_COMPAT
4920 if (req->ctx->compat)
4921 sr->msg_flags |= MSG_CMSG_COMPAT;
4922#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004923 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004924}
4925
Pavel Begunkov889fca72021-02-10 00:03:09 +00004926static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004927{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004928 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004929 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004930 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004931 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004932 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004933 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004934 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004935
Florent Revestdba4a922020-12-04 12:36:04 +01004936 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004937 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004938 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004939
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004940 kmsg = req->async_data;
4941 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004942 ret = io_recvmsg_copy_hdr(req, &iomsg);
4943 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004944 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004945 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004946 }
4947
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004948 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004949 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004950 if (IS_ERR(kbuf))
4951 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004952 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004953 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4954 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004955 1, req->sr_msg.len);
4956 }
4957
Pavel Begunkov04411802021-04-01 15:44:00 +01004958 flags = req->sr_msg.msg_flags;
4959 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004960 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004961 if (flags & MSG_WAITALL)
4962 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4963
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004964 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4965 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004966 if (force_nonblock && ret == -EAGAIN)
4967 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004968 if (ret == -ERESTARTSYS)
4969 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004970
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004971 if (req->flags & REQ_F_BUFFER_SELECTED)
4972 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004973 /* fast path, check for non-NULL to avoid function call */
4974 if (kmsg->free_iov)
4975 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004976 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004977 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004978 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004979 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004980 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004981}
4982
Pavel Begunkov889fca72021-02-10 00:03:09 +00004983static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004984{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004985 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004986 struct io_sr_msg *sr = &req->sr_msg;
4987 struct msghdr msg;
4988 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004989 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004990 struct iovec iov;
4991 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004992 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004993 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004994 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004995
Florent Revestdba4a922020-12-04 12:36:04 +01004996 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004997 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004998 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004999
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005000 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005001 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005002 if (IS_ERR(kbuf))
5003 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005004 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005005 }
5006
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005007 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005008 if (unlikely(ret))
5009 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005010
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005011 msg.msg_name = NULL;
5012 msg.msg_control = NULL;
5013 msg.msg_controllen = 0;
5014 msg.msg_namelen = 0;
5015 msg.msg_iocb = NULL;
5016 msg.msg_flags = 0;
5017
Pavel Begunkov04411802021-04-01 15:44:00 +01005018 flags = req->sr_msg.msg_flags;
5019 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005020 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005021 if (flags & MSG_WAITALL)
5022 min_ret = iov_iter_count(&msg.msg_iter);
5023
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005024 ret = sock_recvmsg(sock, &msg, flags);
5025 if (force_nonblock && ret == -EAGAIN)
5026 return -EAGAIN;
5027 if (ret == -ERESTARTSYS)
5028 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005029out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005030 if (req->flags & REQ_F_BUFFER_SELECTED)
5031 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01005032 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005033 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005034 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005035 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005036}
5037
Jens Axboe3529d8c2019-12-19 18:24:38 -07005038static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005039{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005040 struct io_accept *accept = &req->accept;
5041
Jens Axboe14587a462020-09-05 11:36:08 -06005042 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005043 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005044 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005045 return -EINVAL;
5046
Jens Axboed55e5f52019-12-11 16:12:15 -07005047 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5048 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005049 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005050 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005051
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005052 accept->file_slot = READ_ONCE(sqe->file_index);
5053 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5054 (accept->flags & SOCK_CLOEXEC)))
5055 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005056 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5057 return -EINVAL;
5058 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5059 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005060 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005061}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005062
Pavel Begunkov889fca72021-02-10 00:03:09 +00005063static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005064{
5065 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005066 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005067 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005068 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005069 struct file *file;
5070 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005071
Jiufei Xuee697dee2020-06-10 13:41:59 +08005072 if (req->file->f_flags & O_NONBLOCK)
5073 req->flags |= REQ_F_NOWAIT;
5074
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005075 if (!fixed) {
5076 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5077 if (unlikely(fd < 0))
5078 return fd;
5079 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005080 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5081 accept->flags);
5082 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005083 if (!fixed)
5084 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005085 ret = PTR_ERR(file);
5086 if (ret == -EAGAIN && force_nonblock)
5087 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005088 if (ret == -ERESTARTSYS)
5089 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005090 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005091 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005092 fd_install(fd, file);
5093 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005094 } else {
5095 ret = io_install_fixed_file(req, file, issue_flags,
5096 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005097 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005098 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005099 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005100}
5101
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005102static int io_connect_prep_async(struct io_kiocb *req)
5103{
5104 struct io_async_connect *io = req->async_data;
5105 struct io_connect *conn = &req->connect;
5106
5107 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5108}
5109
Jens Axboe3529d8c2019-12-19 18:24:38 -07005110static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005111{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005112 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005113
Jens Axboe14587a462020-09-05 11:36:08 -06005114 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005115 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005116 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5117 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005118 return -EINVAL;
5119
Jens Axboe3529d8c2019-12-19 18:24:38 -07005120 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5121 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005122 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005123}
5124
Pavel Begunkov889fca72021-02-10 00:03:09 +00005125static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005126{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005127 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005128 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005129 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005130 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005131
Jens Axboee8c2bc12020-08-15 18:44:09 -07005132 if (req->async_data) {
5133 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005134 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005135 ret = move_addr_to_kernel(req->connect.addr,
5136 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005137 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005138 if (ret)
5139 goto out;
5140 io = &__io;
5141 }
5142
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005143 file_flags = force_nonblock ? O_NONBLOCK : 0;
5144
Jens Axboee8c2bc12020-08-15 18:44:09 -07005145 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005146 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005147 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005148 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005149 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005150 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005151 ret = -ENOMEM;
5152 goto out;
5153 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005154 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005155 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005156 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005157 if (ret == -ERESTARTSYS)
5158 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005159out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005160 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005161 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005162 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005163 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005164}
YueHaibing469956e2020-03-04 15:53:52 +08005165#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005166#define IO_NETOP_FN(op) \
5167static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5168{ \
5169 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005170}
5171
Jens Axboe99a10082021-02-19 09:35:19 -07005172#define IO_NETOP_PREP(op) \
5173IO_NETOP_FN(op) \
5174static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5175{ \
5176 return -EOPNOTSUPP; \
5177} \
5178
5179#define IO_NETOP_PREP_ASYNC(op) \
5180IO_NETOP_PREP(op) \
5181static int io_##op##_prep_async(struct io_kiocb *req) \
5182{ \
5183 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005184}
5185
Jens Axboe99a10082021-02-19 09:35:19 -07005186IO_NETOP_PREP_ASYNC(sendmsg);
5187IO_NETOP_PREP_ASYNC(recvmsg);
5188IO_NETOP_PREP_ASYNC(connect);
5189IO_NETOP_PREP(accept);
5190IO_NETOP_FN(send);
5191IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005192#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005193
Jens Axboed7718a92020-02-14 22:23:12 -07005194struct io_poll_table {
5195 struct poll_table_struct pt;
5196 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005197 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005198 int error;
5199};
5200
Jens Axboed7718a92020-02-14 22:23:12 -07005201static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005202 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005203{
Jens Axboed7718a92020-02-14 22:23:12 -07005204 /* for instances that support it check for an event match first: */
5205 if (mask && !(mask & poll->events))
5206 return 0;
5207
5208 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5209
5210 list_del_init(&poll->wait.entry);
5211
Jens Axboed7718a92020-02-14 22:23:12 -07005212 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005213 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005214
Jens Axboed7718a92020-02-14 22:23:12 -07005215 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005216 * If this fails, then the task is exiting. When a task exits, the
5217 * work gets canceled, so just cancel this request as well instead
5218 * of executing it. We can't safely execute it anyway, as we may not
5219 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005220 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005221 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005222 return 1;
5223}
5224
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005225static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5226 __acquires(&req->ctx->completion_lock)
5227{
5228 struct io_ring_ctx *ctx = req->ctx;
5229
Jens Axboe316319e2021-08-19 09:41:42 -06005230 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005231 if (unlikely(req->task->flags & PF_EXITING))
5232 WRITE_ONCE(poll->canceled, true);
5233
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005234 if (!req->result && !READ_ONCE(poll->canceled)) {
5235 struct poll_table_struct pt = { ._key = poll->events };
5236
5237 req->result = vfs_poll(req->file, &pt) & poll->events;
5238 }
5239
Jens Axboe79ebeae2021-08-10 15:18:27 -06005240 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005241 if (!req->result && !READ_ONCE(poll->canceled)) {
5242 add_wait_queue(poll->head, &poll->wait);
5243 return true;
5244 }
5245
5246 return false;
5247}
5248
Jens Axboed4e7cd32020-08-15 11:44:50 -07005249static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005250{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005251 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005252 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005253 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005254 return req->apoll->double_poll;
5255}
5256
5257static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5258{
5259 if (req->opcode == IORING_OP_POLL_ADD)
5260 return &req->poll;
5261 return &req->apoll->poll;
5262}
5263
5264static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005265 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005266{
5267 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005268
5269 lockdep_assert_held(&req->ctx->completion_lock);
5270
5271 if (poll && poll->head) {
5272 struct wait_queue_head *head = poll->head;
5273
Jens Axboe79ebeae2021-08-10 15:18:27 -06005274 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005275 list_del_init(&poll->wait.entry);
5276 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005277 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005278 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005279 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005280 }
5281}
5282
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005283static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005284 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005285{
5286 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005287 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005288 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005289
Pavel Begunkove27414b2021-04-09 09:13:20 +01005290 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005291 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005292 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005293 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005294 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005295 }
Jens Axboeb69de282021-03-17 08:37:41 -06005296 if (req->poll.events & EPOLLONESHOT)
5297 flags = 0;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005298 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005299 req->poll.done = true;
5300 flags = 0;
5301 }
Hao Xu7b289c32021-04-13 15:20:39 +08005302 if (flags & IORING_CQE_F_MORE)
5303 ctx->cq_extra++;
5304
Jens Axboe88e41cf2021-02-22 22:08:01 -07005305 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005306}
5307
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005308static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
5309 __must_hold(&req->ctx->completion_lock)
5310{
5311 bool done;
5312
5313 done = __io_poll_complete(req, mask);
5314 io_commit_cqring(req->ctx);
5315 return done;
5316}
5317
Pavel Begunkovf237c302021-08-18 12:42:46 +01005318static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005319{
Jens Axboe6d816e02020-08-11 08:04:14 -06005320 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005321 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005322
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005323 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005324 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005325 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005326 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005327
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005328 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005329 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005330 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005331 hash_del(&req->hash_node);
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005332 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005333 req->result = 0;
5334 add_wait_queue(req->poll.head, &req->poll.wait);
5335 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005336 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005337 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005338 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005339
Jens Axboe88e41cf2021-02-22 22:08:01 -07005340 if (done) {
5341 nxt = io_put_req_find_next(req);
5342 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005343 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005344 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005345 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005346}
5347
5348static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5349 int sync, void *key)
5350{
5351 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005352 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005353 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005354 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005355
5356 /* for instances that support it check for an event match first: */
5357 if (mask && !(mask & poll->events))
5358 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005359 if (!(poll->events & EPOLLONESHOT))
5360 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005361
Jens Axboe8706e042020-09-28 08:38:54 -06005362 list_del_init(&wait->entry);
5363
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005364 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005365 bool done;
5366
Jens Axboe79ebeae2021-08-10 15:18:27 -06005367 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005368 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005369 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005370 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005371 /* make sure double remove sees this as being gone */
5372 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005373 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005374 if (!done) {
5375 /* use wait func handler, so it matches the rq type */
5376 poll->wait.func(&poll->wait, mode, sync, key);
5377 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005378 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005379 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005380 return 1;
5381}
5382
5383static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5384 wait_queue_func_t wake_func)
5385{
5386 poll->head = NULL;
5387 poll->done = false;
5388 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005389#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5390 /* mask in events that we always want/need */
5391 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005392 INIT_LIST_HEAD(&poll->wait.entry);
5393 init_waitqueue_func_entry(&poll->wait, wake_func);
5394}
5395
5396static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005397 struct wait_queue_head *head,
5398 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005399{
5400 struct io_kiocb *req = pt->req;
5401
5402 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005403 * The file being polled uses multiple waitqueues for poll handling
5404 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5405 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005406 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005407 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005408 struct io_poll_iocb *poll_one = poll;
5409
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005410 /* double add on the same waitqueue head, ignore */
5411 if (poll_one->head == head)
5412 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005413 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005414 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005415 if ((*poll_ptr)->head == head)
5416 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005417 pt->error = -EINVAL;
5418 return;
5419 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005420 /*
5421 * Can't handle multishot for double wait for now, turn it
5422 * into one-shot mode.
5423 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005424 if (!(poll_one->events & EPOLLONESHOT))
5425 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005426 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5427 if (!poll) {
5428 pt->error = -ENOMEM;
5429 return;
5430 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005431 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005432 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005433 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005434 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005435 }
5436
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005437 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005438 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005439
5440 if (poll->events & EPOLLEXCLUSIVE)
5441 add_wait_queue_exclusive(head, &poll->wait);
5442 else
5443 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005444}
5445
5446static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5447 struct poll_table_struct *p)
5448{
5449 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005450 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005451
Jens Axboe807abcb2020-07-17 17:09:27 -06005452 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005453}
5454
Pavel Begunkovf237c302021-08-18 12:42:46 +01005455static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005456{
Jens Axboed7718a92020-02-14 22:23:12 -07005457 struct async_poll *apoll = req->apoll;
5458 struct io_ring_ctx *ctx = req->ctx;
5459
Olivier Langlois236daeae2021-05-31 02:36:37 -04005460 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005461
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005462 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005463 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005464 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005465 }
5466
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005467 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005468 io_poll_remove_double(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005469 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005470
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005471 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005472 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005473 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005474 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005475}
5476
5477static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5478 void *key)
5479{
5480 struct io_kiocb *req = wait->private;
5481 struct io_poll_iocb *poll = &req->apoll->poll;
5482
5483 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5484 key_to_poll(key));
5485
5486 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5487}
5488
5489static void io_poll_req_insert(struct io_kiocb *req)
5490{
5491 struct io_ring_ctx *ctx = req->ctx;
5492 struct hlist_head *list;
5493
5494 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5495 hlist_add_head(&req->hash_node, list);
5496}
5497
5498static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5499 struct io_poll_iocb *poll,
5500 struct io_poll_table *ipt, __poll_t mask,
5501 wait_queue_func_t wake_func)
5502 __acquires(&ctx->completion_lock)
5503{
5504 struct io_ring_ctx *ctx = req->ctx;
5505 bool cancel = false;
5506
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005507 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005508 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005509 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005510 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005511
5512 ipt->pt._key = mask;
5513 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005514 ipt->error = 0;
5515 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005516
Jens Axboed7718a92020-02-14 22:23:12 -07005517 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005518 if (unlikely(!ipt->nr_entries) && !ipt->error)
5519 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005520
Jens Axboe79ebeae2021-08-10 15:18:27 -06005521 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005522 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005523 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005524 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005525 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005526 if (unlikely(list_empty(&poll->wait.entry))) {
5527 if (ipt->error)
5528 cancel = true;
5529 ipt->error = 0;
5530 mask = 0;
5531 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005532 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005533 list_del_init(&poll->wait.entry);
5534 else if (cancel)
5535 WRITE_ONCE(poll->canceled, true);
5536 else if (!poll->done) /* actually waiting for an event */
5537 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005538 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005539 }
5540
5541 return mask;
5542}
5543
Olivier Langlois59b735a2021-06-22 05:17:39 -07005544enum {
5545 IO_APOLL_OK,
5546 IO_APOLL_ABORTED,
5547 IO_APOLL_READY
5548};
5549
5550static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005551{
5552 const struct io_op_def *def = &io_op_defs[req->opcode];
5553 struct io_ring_ctx *ctx = req->ctx;
5554 struct async_poll *apoll;
5555 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005556 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005557 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005558
5559 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005560 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005561 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005562 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005563 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005564 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005565
5566 if (def->pollin) {
5567 rw = READ;
5568 mask |= POLLIN | POLLRDNORM;
5569
5570 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5571 if ((req->opcode == IORING_OP_RECVMSG) &&
5572 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5573 mask &= ~POLLIN;
5574 } else {
5575 rw = WRITE;
5576 mask |= POLLOUT | POLLWRNORM;
5577 }
5578
Jens Axboe9dab14b2020-08-25 12:27:50 -06005579 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005580 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005581 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005582
5583 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5584 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005585 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005586 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005587 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005588 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005589 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005590 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005591
5592 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5593 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005594 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005595 if (ret || ipt.error)
5596 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5597
Olivier Langlois236daeae2021-05-31 02:36:37 -04005598 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5599 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005600 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005601}
5602
5603static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005604 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005605 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005606{
Jens Axboeb41e9852020-02-17 09:52:41 -07005607 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005608
Jens Axboe50826202021-02-23 09:02:26 -07005609 if (!poll->head)
5610 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005611 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005612 if (do_cancel)
5613 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005614 if (!list_empty(&poll->wait.entry)) {
5615 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005616 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005617 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005618 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005619 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005620 return do_complete;
5621}
5622
Pavel Begunkov5d709042021-08-09 20:18:13 +01005623static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005624 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005625{
5626 bool do_complete;
5627
Jens Axboed4e7cd32020-08-15 11:44:50 -07005628 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005629 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005630
Jens Axboeb41e9852020-02-17 09:52:41 -07005631 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005632 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005633 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005634 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005635 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005636 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005637 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005638}
5639
Jens Axboe76e1b642020-09-26 15:05:03 -06005640/*
5641 * Returns true if we found and killed one or more poll requests
5642 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005643static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005644 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005645{
Jens Axboe78076bb2019-12-04 19:56:40 -07005646 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005647 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005648 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005649
Jens Axboe79ebeae2021-08-10 15:18:27 -06005650 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005651 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5652 struct hlist_head *list;
5653
5654 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005655 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005656 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005657 posted += io_poll_remove_one(req);
5658 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005659 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005660 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005661
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005662 if (posted)
5663 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005664
5665 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005666}
5667
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005668static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5669 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005670 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005671{
Jens Axboe78076bb2019-12-04 19:56:40 -07005672 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005673 struct io_kiocb *req;
5674
Jens Axboe78076bb2019-12-04 19:56:40 -07005675 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5676 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005677 if (sqe_addr != req->user_data)
5678 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005679 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5680 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005681 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005682 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005683 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005684}
5685
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005686static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5687 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005688 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005689{
5690 struct io_kiocb *req;
5691
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005692 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005693 if (!req)
5694 return -ENOENT;
5695 if (io_poll_remove_one(req))
5696 return 0;
5697
5698 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005699}
5700
Pavel Begunkov9096af32021-04-14 13:38:36 +01005701static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5702 unsigned int flags)
5703{
5704 u32 events;
5705
5706 events = READ_ONCE(sqe->poll32_events);
5707#ifdef __BIG_ENDIAN
5708 events = swahw32(events);
5709#endif
5710 if (!(flags & IORING_POLL_ADD_MULTI))
5711 events |= EPOLLONESHOT;
5712 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5713}
5714
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005715static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005716 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005717{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005718 struct io_poll_update *upd = &req->poll_update;
5719 u32 flags;
5720
Jens Axboe221c5eb2019-01-17 09:41:58 -07005721 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5722 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005723 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005724 return -EINVAL;
5725 flags = READ_ONCE(sqe->len);
5726 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5727 IORING_POLL_ADD_MULTI))
5728 return -EINVAL;
5729 /* meaningless without update */
5730 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005731 return -EINVAL;
5732
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005733 upd->old_user_data = READ_ONCE(sqe->addr);
5734 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5735 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005736
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005737 upd->new_user_data = READ_ONCE(sqe->off);
5738 if (!upd->update_user_data && upd->new_user_data)
5739 return -EINVAL;
5740 if (upd->update_events)
5741 upd->events = io_poll_parse_events(sqe, flags);
5742 else if (sqe->poll32_events)
5743 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005744
Jens Axboe221c5eb2019-01-17 09:41:58 -07005745 return 0;
5746}
5747
Jens Axboe221c5eb2019-01-17 09:41:58 -07005748static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5749 void *key)
5750{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005751 struct io_kiocb *req = wait->private;
5752 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005753
Jens Axboed7718a92020-02-14 22:23:12 -07005754 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005755}
5756
Jens Axboe221c5eb2019-01-17 09:41:58 -07005757static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5758 struct poll_table_struct *p)
5759{
5760 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5761
Jens Axboee8c2bc12020-08-15 18:44:09 -07005762 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005763}
5764
Jens Axboe3529d8c2019-12-19 18:24:38 -07005765static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005766{
5767 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005768 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005769
5770 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5771 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005772 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005773 return -EINVAL;
5774 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005775 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005776 return -EINVAL;
5777
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005778 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005779 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005780 return 0;
5781}
5782
Pavel Begunkov61e98202021-02-10 00:03:08 +00005783static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005784{
5785 struct io_poll_iocb *poll = &req->poll;
5786 struct io_ring_ctx *ctx = req->ctx;
5787 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005788 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005789
Jens Axboed7718a92020-02-14 22:23:12 -07005790 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005791
Jens Axboed7718a92020-02-14 22:23:12 -07005792 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5793 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005794
Jens Axboe8c838782019-03-12 15:48:16 -06005795 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005796 ipt.error = 0;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005797 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005798 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005799 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005800
Jens Axboe8c838782019-03-12 15:48:16 -06005801 if (mask) {
5802 io_cqring_ev_posted(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005803 if (poll->events & EPOLLONESHOT)
5804 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005805 }
Jens Axboe8c838782019-03-12 15:48:16 -06005806 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005807}
5808
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005809static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005810{
5811 struct io_ring_ctx *ctx = req->ctx;
5812 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005813 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005814 int ret;
5815
Jens Axboe79ebeae2021-08-10 15:18:27 -06005816 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005817 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005818 if (!preq) {
5819 ret = -ENOENT;
5820 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005821 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005822
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005823 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5824 completing = true;
5825 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5826 goto err;
5827 }
5828
Jens Axboecb3b200e2021-04-06 09:49:31 -06005829 /*
5830 * Don't allow racy completion with singleshot, as we cannot safely
5831 * update those. For multishot, if we're racing with completion, just
5832 * let completion re-add it.
5833 */
5834 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5835 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5836 ret = -EALREADY;
5837 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005838 }
5839 /* we now have a detached poll request. reissue. */
5840 ret = 0;
5841err:
Jens Axboeb69de282021-03-17 08:37:41 -06005842 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005843 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005844 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005845 io_req_complete(req, ret);
5846 return 0;
5847 }
5848 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005849 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005850 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005851 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005852 preq->poll.events |= IO_POLL_UNMASK;
5853 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005854 if (req->poll_update.update_user_data)
5855 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005856 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005857
Jens Axboeb69de282021-03-17 08:37:41 -06005858 /* complete update request, we're done with it */
5859 io_req_complete(req, ret);
5860
Jens Axboecb3b200e2021-04-06 09:49:31 -06005861 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005862 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005863 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005864 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005865 io_req_complete(preq, ret);
5866 }
Jens Axboeb69de282021-03-17 08:37:41 -06005867 }
5868 return 0;
5869}
5870
Pavel Begunkovf237c302021-08-18 12:42:46 +01005871static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005872{
Jens Axboe89850fc2021-08-10 15:11:51 -06005873 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005874 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005875}
5876
Jens Axboe5262f562019-09-17 12:26:57 -06005877static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5878{
Jens Axboead8a48a2019-11-15 08:49:11 -07005879 struct io_timeout_data *data = container_of(timer,
5880 struct io_timeout_data, timer);
5881 struct io_kiocb *req = data->req;
5882 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005883 unsigned long flags;
5884
Jens Axboe89850fc2021-08-10 15:11:51 -06005885 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005886 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005887 atomic_set(&req->ctx->cq_timeouts,
5888 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005889 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005890
Jens Axboe89850fc2021-08-10 15:11:51 -06005891 req->io_task_work.func = io_req_task_timeout;
5892 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005893 return HRTIMER_NORESTART;
5894}
5895
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005896static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5897 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005898 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005899{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005900 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005901 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005902 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005903
5904 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005905 found = user_data == req->user_data;
5906 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005907 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005908 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005909 if (!found)
5910 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005911
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005912 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005913 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005914 return ERR_PTR(-EALREADY);
5915 list_del_init(&req->timeout.list);
5916 return req;
5917}
5918
5919static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005920 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005921 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005922{
5923 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5924
5925 if (IS_ERR(req))
5926 return PTR_ERR(req);
5927
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005928 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005929 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005930 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005931 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005932}
5933
Jens Axboe50c1df22021-08-27 17:11:06 -06005934static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5935{
5936 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5937 case IORING_TIMEOUT_BOOTTIME:
5938 return CLOCK_BOOTTIME;
5939 case IORING_TIMEOUT_REALTIME:
5940 return CLOCK_REALTIME;
5941 default:
5942 /* can't happen, vetted at prep time */
5943 WARN_ON_ONCE(1);
5944 fallthrough;
5945 case 0:
5946 return CLOCK_MONOTONIC;
5947 }
5948}
5949
Pavel Begunkovf1042b62021-08-28 19:54:39 -06005950static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5951 struct timespec64 *ts, enum hrtimer_mode mode)
5952 __must_hold(&ctx->timeout_lock)
5953{
5954 struct io_timeout_data *io;
5955 struct io_kiocb *req;
5956 bool found = false;
5957
5958 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
5959 found = user_data == req->user_data;
5960 if (found)
5961 break;
5962 }
5963 if (!found)
5964 return -ENOENT;
5965
5966 io = req->async_data;
5967 if (hrtimer_try_to_cancel(&io->timer) == -1)
5968 return -EALREADY;
5969 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
5970 io->timer.function = io_link_timeout_fn;
5971 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
5972 return 0;
5973}
5974
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005975static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5976 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06005977 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005978{
5979 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5980 struct io_timeout_data *data;
5981
5982 if (IS_ERR(req))
5983 return PTR_ERR(req);
5984
5985 req->timeout.off = 0; /* noseq */
5986 data = req->async_data;
5987 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06005988 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005989 data->timer.function = io_timeout_fn;
5990 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5991 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005992}
5993
Jens Axboe3529d8c2019-12-19 18:24:38 -07005994static int io_timeout_remove_prep(struct io_kiocb *req,
5995 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005996{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005997 struct io_timeout_rem *tr = &req->timeout_rem;
5998
Jens Axboeb29472e2019-12-17 18:50:29 -07005999 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6000 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006001 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6002 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006003 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006004 return -EINVAL;
6005
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006006 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006007 tr->addr = READ_ONCE(sqe->addr);
6008 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006009 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6010 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6011 return -EINVAL;
6012 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6013 tr->ltimeout = true;
6014 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006015 return -EINVAL;
6016 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6017 return -EFAULT;
6018 } else if (tr->flags) {
6019 /* timeout removal doesn't support flags */
6020 return -EINVAL;
6021 }
6022
Jens Axboeb29472e2019-12-17 18:50:29 -07006023 return 0;
6024}
6025
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006026static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6027{
6028 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6029 : HRTIMER_MODE_REL;
6030}
6031
Jens Axboe11365042019-10-16 09:08:32 -06006032/*
6033 * Remove or update an existing timeout command
6034 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006035static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006036{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006037 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006038 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006039 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006040
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006041 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6042 spin_lock(&ctx->completion_lock);
6043 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006044 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006045 spin_unlock_irq(&ctx->timeout_lock);
6046 spin_unlock(&ctx->completion_lock);
6047 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006048 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6049
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006050 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006051 if (tr->ltimeout)
6052 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6053 else
6054 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006055 spin_unlock_irq(&ctx->timeout_lock);
6056 }
Jens Axboe11365042019-10-16 09:08:32 -06006057
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006058 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006059 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006060 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006061 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006062}
6063
Jens Axboe3529d8c2019-12-19 18:24:38 -07006064static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006065 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006066{
Jens Axboead8a48a2019-11-15 08:49:11 -07006067 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006068 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006069 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006070
Jens Axboead8a48a2019-11-15 08:49:11 -07006071 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006072 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006073 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6074 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006075 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006076 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006077 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006078 flags = READ_ONCE(sqe->timeout_flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006079 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK))
6080 return -EINVAL;
6081 /* more than one clock specified is invalid, obviously */
6082 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006083 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006084
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006085 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006086 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006087 if (unlikely(off && !req->ctx->off_timeout_used))
6088 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006089
Jens Axboee8c2bc12020-08-15 18:44:09 -07006090 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006091 return -ENOMEM;
6092
Jens Axboee8c2bc12020-08-15 18:44:09 -07006093 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006094 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006095 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006096
6097 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006098 return -EFAULT;
6099
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006100 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006101 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006102
6103 if (is_timeout_link) {
6104 struct io_submit_link *link = &req->ctx->submit_state.link;
6105
6106 if (!link->head)
6107 return -EINVAL;
6108 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6109 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006110 req->timeout.head = link->last;
6111 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006112 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006113 return 0;
6114}
6115
Pavel Begunkov61e98202021-02-10 00:03:08 +00006116static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006117{
Jens Axboead8a48a2019-11-15 08:49:11 -07006118 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006119 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006120 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006121 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006122
Jens Axboe89850fc2021-08-10 15:11:51 -06006123 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006124
Jens Axboe5262f562019-09-17 12:26:57 -06006125 /*
6126 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006127 * timeout event to be satisfied. If it isn't set, then this is
6128 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006129 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006130 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006131 entry = ctx->timeout_list.prev;
6132 goto add;
6133 }
Jens Axboe5262f562019-09-17 12:26:57 -06006134
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006135 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6136 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006137
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006138 /* Update the last seq here in case io_flush_timeouts() hasn't.
6139 * This is safe because ->completion_lock is held, and submissions
6140 * and completions are never mixed in the same ->completion_lock section.
6141 */
6142 ctx->cq_last_tm_flush = tail;
6143
Jens Axboe5262f562019-09-17 12:26:57 -06006144 /*
6145 * Insertion sort, ensuring the first entry in the list is always
6146 * the one we need first.
6147 */
Jens Axboe5262f562019-09-17 12:26:57 -06006148 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006149 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6150 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006151
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006152 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006153 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006154 /* nxt.seq is behind @tail, otherwise would've been completed */
6155 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006156 break;
6157 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006158add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006159 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006160 data->timer.function = io_timeout_fn;
6161 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006162 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006163 return 0;
6164}
6165
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006166struct io_cancel_data {
6167 struct io_ring_ctx *ctx;
6168 u64 user_data;
6169};
6170
Jens Axboe62755e32019-10-28 21:49:21 -06006171static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006172{
Jens Axboe62755e32019-10-28 21:49:21 -06006173 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006174 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006175
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006176 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006177}
6178
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006179static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6180 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006181{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006182 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006183 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006184 int ret = 0;
6185
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006186 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006187 return -ENOENT;
6188
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006189 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006190 switch (cancel_ret) {
6191 case IO_WQ_CANCEL_OK:
6192 ret = 0;
6193 break;
6194 case IO_WQ_CANCEL_RUNNING:
6195 ret = -EALREADY;
6196 break;
6197 case IO_WQ_CANCEL_NOTFOUND:
6198 ret = -ENOENT;
6199 break;
6200 }
6201
Jens Axboee977d6d2019-11-05 12:39:45 -07006202 return ret;
6203}
6204
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006205static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006206{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006207 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006208 int ret;
6209
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006210 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006211
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006212 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006213 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006214 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006215
6216 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006217 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006218 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006219 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006220 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006221 goto out;
6222 ret = io_poll_cancel(ctx, sqe_addr, false);
6223out:
6224 spin_unlock(&ctx->completion_lock);
6225 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006226}
6227
Jens Axboe3529d8c2019-12-19 18:24:38 -07006228static int io_async_cancel_prep(struct io_kiocb *req,
6229 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006230{
Jens Axboefbf23842019-12-17 18:45:56 -07006231 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006232 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006233 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6234 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006235 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6236 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006237 return -EINVAL;
6238
Jens Axboefbf23842019-12-17 18:45:56 -07006239 req->cancel.addr = READ_ONCE(sqe->addr);
6240 return 0;
6241}
6242
Pavel Begunkov61e98202021-02-10 00:03:08 +00006243static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006244{
6245 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006246 u64 sqe_addr = req->cancel.addr;
6247 struct io_tctx_node *node;
6248 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006249
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006250 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006251 if (ret != -ENOENT)
6252 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006253
6254 /* slow path, try all io-wq's */
6255 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
6256 ret = -ENOENT;
6257 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6258 struct io_uring_task *tctx = node->task->io_uring;
6259
Pavel Begunkov58f99372021-03-12 16:25:55 +00006260 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6261 if (ret != -ENOENT)
6262 break;
6263 }
6264 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00006265done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006266 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006267 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006268 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006269 return 0;
6270}
6271
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006272static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006273 const struct io_uring_sqe *sqe)
6274{
Daniele Albano61710e42020-07-18 14:15:16 -06006275 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6276 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006277 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006278 return -EINVAL;
6279
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006280 req->rsrc_update.offset = READ_ONCE(sqe->off);
6281 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6282 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006283 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006284 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006285 return 0;
6286}
6287
Pavel Begunkov889fca72021-02-10 00:03:09 +00006288static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006289{
6290 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006291 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006292 int ret;
6293
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006294 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006295 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006296
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006297 up.offset = req->rsrc_update.offset;
6298 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006299 up.nr = 0;
6300 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006301 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006302
6303 mutex_lock(&ctx->uring_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006304 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006305 &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006306 mutex_unlock(&ctx->uring_lock);
6307
6308 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006309 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006310 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006311 return 0;
6312}
6313
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006314static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006315{
Jens Axboed625c6e2019-12-17 19:53:05 -07006316 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006317 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006318 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006319 case IORING_OP_READV:
6320 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006321 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006322 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006323 case IORING_OP_WRITEV:
6324 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006325 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006326 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006327 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006328 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006329 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006330 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006331 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006332 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006333 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006334 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006335 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006336 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006337 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006338 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006339 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006340 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006341 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006342 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006343 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006344 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006345 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006346 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006347 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006348 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006349 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006350 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006351 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006352 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006353 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006354 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006355 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006356 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006357 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006358 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006359 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006360 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006361 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006362 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006363 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006364 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006365 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006366 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006367 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006368 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006369 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006370 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006371 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006372 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006373 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006374 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006375 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006376 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006377 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006378 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006379 case IORING_OP_SHUTDOWN:
6380 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006381 case IORING_OP_RENAMEAT:
6382 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006383 case IORING_OP_UNLINKAT:
6384 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006385 case IORING_OP_MKDIRAT:
6386 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006387 case IORING_OP_SYMLINKAT:
6388 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006389 case IORING_OP_LINKAT:
6390 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006391 }
6392
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006393 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6394 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006395 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006396}
6397
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006398static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006399{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006400 if (!io_op_defs[req->opcode].needs_async_setup)
6401 return 0;
6402 if (WARN_ON_ONCE(req->async_data))
6403 return -EFAULT;
6404 if (io_alloc_async_data(req))
6405 return -EAGAIN;
6406
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006407 switch (req->opcode) {
6408 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006409 return io_rw_prep_async(req, READ);
6410 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006411 return io_rw_prep_async(req, WRITE);
6412 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006413 return io_sendmsg_prep_async(req);
6414 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006415 return io_recvmsg_prep_async(req);
6416 case IORING_OP_CONNECT:
6417 return io_connect_prep_async(req);
6418 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006419 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6420 req->opcode);
6421 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006422}
6423
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006424static u32 io_get_sequence(struct io_kiocb *req)
6425{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006426 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006427
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006428 /* need original cached_sq_head, but it was increased for each req */
6429 io_for_each_link(req, req)
6430 seq--;
6431 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006432}
6433
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006434static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006435{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006436 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006437 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006438 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006439 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006440 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006441
Pavel Begunkovb8ce1b92021-08-31 14:13:11 +01006442 if (req->flags & REQ_F_FAIL) {
6443 io_req_complete_fail_submit(req);
6444 return true;
6445 }
6446
Pavel Begunkov3c199662021-06-15 16:47:57 +01006447 /*
6448 * If we need to drain a request in the middle of a link, drain the
6449 * head request and the next request/link after the current link.
6450 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6451 * maintained for every request of our link.
6452 */
6453 if (ctx->drain_next) {
6454 req->flags |= REQ_F_IO_DRAIN;
6455 ctx->drain_next = false;
6456 }
6457 /* not interested in head, start from the first linked */
6458 io_for_each_link(pos, req->link) {
6459 if (pos->flags & REQ_F_IO_DRAIN) {
6460 ctx->drain_next = true;
6461 req->flags |= REQ_F_IO_DRAIN;
6462 break;
6463 }
6464 }
6465
Jens Axboedef596e2019-01-09 08:59:42 -07006466 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006467 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006468 !(req->flags & REQ_F_IO_DRAIN))) {
6469 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006470 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006471 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006472
6473 seq = io_get_sequence(req);
6474 /* Still a chance to pass the sequence check */
6475 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006476 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006477
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006478 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006479 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006480 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006481 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006482 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006483 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006484 ret = -ENOMEM;
6485fail:
6486 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006487 return true;
6488 }
Jens Axboe31b51512019-01-18 22:56:34 -07006489
Jens Axboe79ebeae2021-08-10 15:18:27 -06006490 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006491 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006492 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006493 kfree(de);
Pavel Begunkovf237c302021-08-18 12:42:46 +01006494 io_queue_async_work(req, NULL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006495 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006496 }
6497
6498 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006499 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006500 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006501 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006502 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006503 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006504}
6505
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006506static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006507{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006508 if (req->flags & REQ_F_BUFFER_SELECTED) {
6509 switch (req->opcode) {
6510 case IORING_OP_READV:
6511 case IORING_OP_READ_FIXED:
6512 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006513 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006514 break;
6515 case IORING_OP_RECVMSG:
6516 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006517 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006518 break;
6519 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006520 }
6521
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006522 if (req->flags & REQ_F_NEED_CLEANUP) {
6523 switch (req->opcode) {
6524 case IORING_OP_READV:
6525 case IORING_OP_READ_FIXED:
6526 case IORING_OP_READ:
6527 case IORING_OP_WRITEV:
6528 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006529 case IORING_OP_WRITE: {
6530 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006531
6532 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006533 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006534 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006535 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006536 case IORING_OP_SENDMSG: {
6537 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006538
6539 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006540 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006541 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006542 case IORING_OP_SPLICE:
6543 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006544 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6545 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006546 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006547 case IORING_OP_OPENAT:
6548 case IORING_OP_OPENAT2:
6549 if (req->open.filename)
6550 putname(req->open.filename);
6551 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006552 case IORING_OP_RENAMEAT:
6553 putname(req->rename.oldpath);
6554 putname(req->rename.newpath);
6555 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006556 case IORING_OP_UNLINKAT:
6557 putname(req->unlink.filename);
6558 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006559 case IORING_OP_MKDIRAT:
6560 putname(req->mkdir.filename);
6561 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006562 case IORING_OP_SYMLINKAT:
6563 putname(req->symlink.oldpath);
6564 putname(req->symlink.newpath);
6565 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006566 case IORING_OP_LINKAT:
6567 putname(req->hardlink.oldpath);
6568 putname(req->hardlink.newpath);
6569 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006570 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006571 }
Jens Axboe75652a302021-04-15 09:52:40 -06006572 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6573 kfree(req->apoll->double_poll);
6574 kfree(req->apoll);
6575 req->apoll = NULL;
6576 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006577 if (req->flags & REQ_F_INFLIGHT) {
6578 struct io_uring_task *tctx = req->task->io_uring;
6579
6580 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006581 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006582 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006583 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006584
6585 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006586}
6587
Pavel Begunkov889fca72021-02-10 00:03:09 +00006588static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006589{
Jens Axboeedafcce2019-01-09 09:16:05 -07006590 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006591 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006592 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006593
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006594 if ((req->flags & REQ_F_CREDS) && req->creds != current_cred())
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006595 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006596
Jens Axboed625c6e2019-12-17 19:53:05 -07006597 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006598 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006599 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006600 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006601 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006602 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006603 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006604 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006605 break;
6606 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006607 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006608 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006609 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006610 break;
6611 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006612 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006613 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006614 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006615 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006616 break;
6617 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006618 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006619 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006620 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006621 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006622 break;
6623 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006624 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006625 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006626 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006627 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006628 break;
6629 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006630 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006631 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006632 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006633 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006634 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006635 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006636 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006637 break;
6638 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006639 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006640 break;
6641 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006642 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006643 break;
6644 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006645 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006646 break;
6647 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006648 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006649 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006650 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006651 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006652 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006653 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006654 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006655 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006656 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006657 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006658 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006659 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006660 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006661 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006662 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006663 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006664 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006665 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006666 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006667 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006668 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006669 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006670 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006671 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006672 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006673 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006674 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006675 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006676 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006677 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006678 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006679 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006680 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006681 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006682 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006683 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006684 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006685 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006686 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006687 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006688 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006689 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006690 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006691 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006692 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006693 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006694 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006695 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006696 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006697 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006698 case IORING_OP_MKDIRAT:
6699 ret = io_mkdirat(req, issue_flags);
6700 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006701 case IORING_OP_SYMLINKAT:
6702 ret = io_symlinkat(req, issue_flags);
6703 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006704 case IORING_OP_LINKAT:
6705 ret = io_linkat(req, issue_flags);
6706 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006707 default:
6708 ret = -EINVAL;
6709 break;
6710 }
Jens Axboe31b51512019-01-18 22:56:34 -07006711
Jens Axboe5730b272021-02-27 15:57:30 -07006712 if (creds)
6713 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006714 if (ret)
6715 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006716 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006717 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6718 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006719
6720 return 0;
6721}
6722
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006723static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6724{
6725 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6726
6727 req = io_put_req_find_next(req);
6728 return req ? &req->work : NULL;
6729}
6730
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006731static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006732{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006733 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006734 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006735 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006736
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006737 /* one will be dropped by ->io_free_work() after returning to io-wq */
6738 if (!(req->flags & REQ_F_REFCOUNT))
6739 __io_req_set_refcount(req, 2);
6740 else
6741 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006742
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006743 timeout = io_prep_linked_timeout(req);
6744 if (timeout)
6745 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006746
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006747 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006748 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006749 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006750
Jens Axboe561fb042019-10-24 07:25:42 -06006751 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006752 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006753 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006754 /*
6755 * We can get EAGAIN for polled IO even though we're
6756 * forcing a sync submission from here, since we can't
6757 * wait for request slots on the block side.
6758 */
6759 if (ret != -EAGAIN)
6760 break;
6761 cond_resched();
6762 } while (1);
6763 }
Jens Axboe31b51512019-01-18 22:56:34 -07006764
Pavel Begunkova3df76982021-02-18 22:32:52 +00006765 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006766 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006767 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006768}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006769
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006770static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006771 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006772{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006773 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006774}
6775
Jens Axboe09bb8392019-03-13 12:39:28 -06006776static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6777 int index)
6778{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006779 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006780
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006781 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006782}
6783
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006784static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006785{
6786 unsigned long file_ptr = (unsigned long) file;
6787
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006788 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006789 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006790 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006791 file_ptr |= FFS_ASYNC_WRITE;
6792 if (S_ISREG(file_inode(file)->i_mode))
6793 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006794 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006795}
6796
Pavel Begunkovac177052021-08-09 13:04:02 +01006797static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6798 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006799{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006800 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006801 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006802
Pavel Begunkovac177052021-08-09 13:04:02 +01006803 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6804 return NULL;
6805 fd = array_index_nospec(fd, ctx->nr_user_files);
6806 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6807 file = (struct file *) (file_ptr & FFS_MASK);
6808 file_ptr &= ~FFS_MASK;
6809 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006810 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006811 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006812 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006813}
6814
Pavel Begunkovac177052021-08-09 13:04:02 +01006815static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006816 struct io_kiocb *req, int fd)
6817{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006818 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006819
6820 trace_io_uring_file_get(ctx, fd);
6821
6822 /* we don't allow fixed io_uring files */
6823 if (file && unlikely(file->f_op == &io_uring_fops))
6824 io_req_track_inflight(req);
6825 return file;
6826}
6827
6828static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006829 struct io_kiocb *req, int fd, bool fixed)
6830{
6831 if (fixed)
6832 return io_file_get_fixed(ctx, req, fd);
6833 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006834 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006835}
6836
Pavel Begunkovf237c302021-08-18 12:42:46 +01006837static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006838{
6839 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006840 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006841
6842 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006843 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006844 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006845 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006846 } else {
6847 io_req_complete_post(req, -ETIME, 0);
6848 }
6849}
6850
Jens Axboe2665abf2019-11-05 12:40:47 -07006851static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6852{
Jens Axboead8a48a2019-11-15 08:49:11 -07006853 struct io_timeout_data *data = container_of(timer,
6854 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006855 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006857 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006858
Jens Axboe89b263f2021-08-10 15:14:18 -06006859 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006860 prev = req->timeout.head;
6861 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006862
6863 /*
6864 * We don't expect the list to be empty, that will only happen if we
6865 * race with the completion of the linked work.
6866 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006867 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006868 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006869 if (!req_ref_inc_not_zero(prev))
6870 prev = NULL;
6871 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006872 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006873 req->timeout.prev = prev;
6874 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006875
Jens Axboe89b263f2021-08-10 15:14:18 -06006876 req->io_task_work.func = io_req_task_link_timeout;
6877 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006878 return HRTIMER_NORESTART;
6879}
6880
Pavel Begunkovde968c12021-03-19 17:22:33 +00006881static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006882{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006883 struct io_ring_ctx *ctx = req->ctx;
6884
Jens Axboe89b263f2021-08-10 15:14:18 -06006885 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006886 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006887 * If the back reference is NULL, then our linked request finished
6888 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006889 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006890 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006891 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006892
Jens Axboead8a48a2019-11-15 08:49:11 -07006893 data->timer.function = io_link_timeout_fn;
6894 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6895 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006896 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006897 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006898 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006899 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006900 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006901}
6902
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006903static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006904 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006905{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006906 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006907 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006908
Olivier Langlois59b735a2021-06-22 05:17:39 -07006909issue_sqe:
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006910 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006911
6912 /*
6913 * We async punt it if the file wasn't marked NOWAIT, or if the file
6914 * doesn't support non-blocking read/write attempts
6915 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006916 if (likely(!ret)) {
Pavel Begunkove342c802021-01-19 13:32:47 +00006917 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006918 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006919 struct io_submit_state *state = &ctx->submit_state;
Jens Axboee65ef562019-03-12 10:16:44 -06006920
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006921 state->compl_reqs[state->compl_nr++] = req;
6922 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006923 io_submit_flush_completions(ctx);
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006924 return;
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006925 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006926
6927 linked_timeout = io_prep_linked_timeout(req);
6928 if (linked_timeout)
6929 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006930 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006931 linked_timeout = io_prep_linked_timeout(req);
6932
Olivier Langlois59b735a2021-06-22 05:17:39 -07006933 switch (io_arm_poll_handler(req)) {
6934 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006935 if (linked_timeout)
6936 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006937 goto issue_sqe;
6938 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006939 /*
6940 * Queued up for async execution, worker will release
6941 * submit reference when the iocb is actually submitted.
6942 */
Pavel Begunkovf237c302021-08-18 12:42:46 +01006943 io_queue_async_work(req, NULL);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006944 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006945 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006946
6947 if (linked_timeout)
6948 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006949 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006950 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006951 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006952}
6953
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006954static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006955 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006956{
Pavel Begunkov10c66902021-06-15 16:47:56 +01006957 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006958 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08006959
Hao Xua8295b92021-08-27 17:46:09 +08006960 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006961 __io_queue_sqe(req);
Hao Xua8295b92021-08-27 17:46:09 +08006962 } else if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01006963 io_req_complete_fail_submit(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006964 } else {
6965 int ret = io_req_prep_async(req);
6966
6967 if (unlikely(ret))
6968 io_req_complete_failed(req, ret);
6969 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01006970 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07006971 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006972}
6973
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006974/*
6975 * Check SQE restrictions (opcode and flags).
6976 *
6977 * Returns 'true' if SQE is allowed, 'false' otherwise.
6978 */
6979static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6980 struct io_kiocb *req,
6981 unsigned int sqe_flags)
6982{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006983 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006984 return true;
6985
6986 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6987 return false;
6988
6989 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6990 ctx->restrictions.sqe_flags_required)
6991 return false;
6992
6993 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6994 ctx->restrictions.sqe_flags_required))
6995 return false;
6996
6997 return true;
6998}
6999
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007000static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007001 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007002 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007003{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007004 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007005 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007006 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007007
Pavel Begunkov864ea922021-08-09 13:04:08 +01007008 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007009 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007010 /* same numerical values with corresponding REQ_F_*, safe to copy */
7011 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007012 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007013 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007014 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007015 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007016
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007017 /* enforce forwards compatibility on users */
Pavel Begunkovdddca222021-04-27 16:13:52 +01007018 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007019 return -EINVAL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007020 if (unlikely(req->opcode >= IORING_OP_LAST))
7021 return -EINVAL;
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01007022 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007023 return -EACCES;
7024
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007025 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
7026 !io_op_defs[req->opcode].buffer_select)
7027 return -EOPNOTSUPP;
Pavel Begunkov3c199662021-06-15 16:47:57 +01007028 if (unlikely(sqe_flags & IOSQE_IO_DRAIN))
7029 ctx->drain_active = true;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007030
Jens Axboe003e8dc2021-03-06 09:22:27 -07007031 personality = READ_ONCE(sqe->personality);
7032 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007033 req->creds = xa_load(&ctx->personalities, personality);
7034 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07007035 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007036 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01007037 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007038 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007039 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007040
Jens Axboe27926b62020-10-28 09:33:23 -06007041 /*
7042 * Plug now if we have more than 1 IO left after this, and the target
7043 * is potentially a read/write to block based storage.
7044 */
7045 if (!state->plug_started && state->ios_left > 1 &&
7046 io_op_defs[req->opcode].plug) {
7047 blk_start_plug(&state->plug);
7048 state->plug_started = true;
7049 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007050
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007051 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01007052 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007053 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007054 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007055 ret = -EBADF;
7056 }
7057
Pavel Begunkov71b547c2020-10-10 18:34:09 +01007058 state->ios_left--;
7059 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007060}
7061
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007062static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007063 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007064 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007065{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007066 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007067 int ret;
7068
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007069 ret = io_init_req(ctx, req, sqe);
7070 if (unlikely(ret)) {
7071fail_req:
Hao Xua8295b92021-08-27 17:46:09 +08007072 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007073 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007074 /*
7075 * we can judge a link req is failed or cancelled by if
7076 * REQ_F_FAIL is set, but the head is an exception since
7077 * it may be set REQ_F_FAIL because of other req's failure
7078 * so let's leverage req->result to distinguish if a head
7079 * is set REQ_F_FAIL because of its failure or other req's
7080 * failure so that we can set the correct ret code for it.
7081 * init result here to avoid affecting the normal path.
7082 */
7083 if (!(link->head->flags & REQ_F_FAIL))
7084 req_fail_link_node(link->head, -ECANCELED);
7085 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7086 /*
7087 * the current req is a normal req, we should return
7088 * error and thus break the submittion loop.
7089 */
7090 io_req_complete_failed(req, ret);
7091 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007092 }
Hao Xua8295b92021-08-27 17:46:09 +08007093 req_fail_link_node(req, ret);
7094 } else {
7095 ret = io_req_prep(req, sqe);
7096 if (unlikely(ret))
7097 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007098 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007099
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007100 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007101 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7102 req->flags, true,
7103 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007104
Jens Axboe6c271ce2019-01-10 11:22:30 -07007105 /*
7106 * If we already have a head request, queue this one for async
7107 * submittal once the head completes. If we don't have a head but
7108 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7109 * submitted sync once the chain is complete. If none of those
7110 * conditions are true (normal request), then just queue it.
7111 */
7112 if (link->head) {
7113 struct io_kiocb *head = link->head;
7114
Hao Xua8295b92021-08-27 17:46:09 +08007115 if (!(req->flags & REQ_F_FAIL)) {
7116 ret = io_req_prep_async(req);
7117 if (unlikely(ret)) {
7118 req_fail_link_node(req, ret);
7119 if (!(head->flags & REQ_F_FAIL))
7120 req_fail_link_node(head, -ECANCELED);
7121 }
7122 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007123 trace_io_uring_link(ctx, req, head);
7124 link->last->link = req;
7125 link->last = req;
7126
7127 /* last request of a link, enqueue the link */
7128 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7129 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01007130 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007131 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007132 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007133 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08007134 link->head = req;
7135 link->last = req;
7136 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007137 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007138 }
7139 }
7140
7141 return 0;
7142}
7143
7144/*
7145 * Batched submission is done, ensure local IO is flushed out.
7146 */
7147static void io_submit_state_end(struct io_submit_state *state,
7148 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03007149{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007150 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007151 io_queue_sqe(state->link.head);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01007152 if (state->compl_nr)
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01007153 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007154 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007155 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007156}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007157
Jens Axboe9e645e112019-05-10 16:07:28 -06007158/*
7159 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007160 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007161static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007162 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007163{
7164 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07007165 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007166 /* set only head, no need to init link_last in advance */
7167 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007168}
7169
Jens Axboe193155c2020-02-22 23:22:19 -07007170static void io_commit_sqring(struct io_ring_ctx *ctx)
7171{
Jens Axboe75c6a032020-01-28 10:15:23 -07007172 struct io_rings *rings = ctx->rings;
7173
7174 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007175 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007176 * since once we write the new head, the application could
7177 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007178 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007179 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007180}
7181
Jens Axboe9e645e112019-05-10 16:07:28 -06007182/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007183 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007184 * that is mapped by userspace. This means that care needs to be taken to
7185 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007186 * being a good citizen. If members of the sqe are validated and then later
7187 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007188 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007189 */
7190static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007191{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007192 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007193 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007194
7195 /*
7196 * The cached sq head (or cq tail) serves two purposes:
7197 *
7198 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007199 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007200 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007201 * though the application is the one updating it.
7202 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007203 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007204 if (likely(head < ctx->sq_entries))
7205 return &ctx->sq_sqes[head];
7206
7207 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007208 ctx->cq_extra--;
7209 WRITE_ONCE(ctx->rings->sq_dropped,
7210 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007211 return NULL;
7212}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007213
Jens Axboe0f212202020-09-13 13:09:39 -06007214static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007215 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007216{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007217 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007218
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03007219 /* make sure SQ entry isn't read before tail */
7220 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03007221 if (!percpu_ref_tryget_many(&ctx->refs, nr))
7222 return -EAGAIN;
Pavel Begunkov9a108672021-08-27 11:55:01 +01007223 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007224
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007225 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007226 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007227 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007228 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007229
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007230 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03007231 if (unlikely(!req)) {
7232 if (!submitted)
7233 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007234 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007235 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007236 sqe = io_get_sqe(ctx);
7237 if (unlikely(!sqe)) {
Hao Xu0c6e1d72021-08-26 01:58:56 +08007238 list_add(&req->inflight_entry, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007239 break;
7240 }
Jens Axboed3656342019-12-18 09:50:26 -07007241 /* will complete beyond this point, count as submitted */
7242 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007243 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007244 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007245 }
7246
Pavel Begunkov9466f432020-01-25 22:34:01 +03007247 if (unlikely(submitted != nr)) {
7248 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007249 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007250
Pavel Begunkov09899b12021-06-14 02:36:22 +01007251 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06007252 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03007253 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007254
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007255 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007256 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7257 io_commit_sqring(ctx);
7258
Jens Axboe6c271ce2019-01-10 11:22:30 -07007259 return submitted;
7260}
7261
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007262static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7263{
7264 return READ_ONCE(sqd->state);
7265}
7266
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007267static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7268{
7269 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007270 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007271 WRITE_ONCE(ctx->rings->sq_flags,
7272 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007273 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007274}
7275
7276static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7277{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007278 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007279 WRITE_ONCE(ctx->rings->sq_flags,
7280 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007281 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007282}
7283
Xiaoguang Wang08369242020-11-03 14:15:59 +08007284static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007285{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007286 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007287 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007288
Jens Axboec8d1ba52020-09-14 11:07:26 -06007289 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007290 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007291 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7292 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007293
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007294 if (!list_empty(&ctx->iopoll_list) || to_submit) {
7295 unsigned nr_events = 0;
Pavel Begunkov948e1942021-06-24 15:09:55 +01007296 const struct cred *creds = NULL;
7297
7298 if (ctx->sq_creds != current_cred())
7299 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007300
Xiaoguang Wang08369242020-11-03 14:15:59 +08007301 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007302 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkova8576af2021-08-15 10:40:21 +01007303 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007304
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007305 /*
7306 * Don't submit if refs are dying, good for io_uring_register(),
7307 * but also it is relied upon by io_ring_exit_work()
7308 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007309 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7310 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007311 ret = io_submit_sqes(ctx, to_submit);
7312 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007313
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007314 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7315 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007316 if (creds)
7317 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007318 }
Jens Axboe90554202020-09-03 12:12:41 -06007319
Xiaoguang Wang08369242020-11-03 14:15:59 +08007320 return ret;
7321}
7322
7323static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7324{
7325 struct io_ring_ctx *ctx;
7326 unsigned sq_thread_idle = 0;
7327
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007328 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7329 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007330 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007331}
7332
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007333static bool io_sqd_handle_event(struct io_sq_data *sqd)
7334{
7335 bool did_sig = false;
7336 struct ksignal ksig;
7337
7338 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7339 signal_pending(current)) {
7340 mutex_unlock(&sqd->lock);
7341 if (signal_pending(current))
7342 did_sig = get_signal(&ksig);
7343 cond_resched();
7344 mutex_lock(&sqd->lock);
7345 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007346 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7347}
7348
Jens Axboe6c271ce2019-01-10 11:22:30 -07007349static int io_sq_thread(void *data)
7350{
Jens Axboe69fb2132020-09-14 11:16:23 -06007351 struct io_sq_data *sqd = data;
7352 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007353 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007354 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007355 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007356
Pavel Begunkov696ee882021-04-01 09:55:04 +01007357 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007358 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007359
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007360 if (sqd->sq_cpu != -1)
7361 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7362 else
7363 set_cpus_allowed_ptr(current, cpu_online_mask);
7364 current->flags |= PF_NO_SETAFFINITY;
7365
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007366 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007367 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007368 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007369
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007370 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7371 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007372 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007373 timeout = jiffies + sqd->sq_thread_idle;
7374 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007375
Jens Axboee95eee22020-09-08 09:11:32 -06007376 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007377 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007378 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007379
Xiaoguang Wang08369242020-11-03 14:15:59 +08007380 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7381 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007382 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007383 if (io_run_task_work())
7384 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007385
Xiaoguang Wang08369242020-11-03 14:15:59 +08007386 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007387 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007388 if (sqt_spin)
7389 timeout = jiffies + sqd->sq_thread_idle;
7390 continue;
7391 }
7392
Xiaoguang Wang08369242020-11-03 14:15:59 +08007393 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007394 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007395 bool needs_sched = true;
7396
Hao Xu724cb4f2021-04-21 23:19:11 +08007397 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007398 io_ring_set_wakeup_flag(ctx);
7399
Hao Xu724cb4f2021-04-21 23:19:11 +08007400 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7401 !list_empty_careful(&ctx->iopoll_list)) {
7402 needs_sched = false;
7403 break;
7404 }
7405 if (io_sqring_entries(ctx)) {
7406 needs_sched = false;
7407 break;
7408 }
7409 }
7410
7411 if (needs_sched) {
7412 mutex_unlock(&sqd->lock);
7413 schedule();
7414 mutex_lock(&sqd->lock);
7415 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007416 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7417 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007418 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007419
7420 finish_wait(&sqd->wait, &wait);
7421 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007422 }
7423
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007424 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007425 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007426 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007427 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007428 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007429 mutex_unlock(&sqd->lock);
7430
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007431 complete(&sqd->exited);
7432 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007433}
7434
Jens Axboebda52162019-09-24 13:47:15 -06007435struct io_wait_queue {
7436 struct wait_queue_entry wq;
7437 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007438 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007439 unsigned nr_timeouts;
7440};
7441
Pavel Begunkov6c503152021-01-04 20:36:36 +00007442static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007443{
7444 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007445 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007446
7447 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007448 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007449 * started waiting. For timeouts, we always want to return to userspace,
7450 * regardless of event count.
7451 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007452 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007453}
7454
7455static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7456 int wake_flags, void *key)
7457{
7458 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7459 wq);
7460
Pavel Begunkov6c503152021-01-04 20:36:36 +00007461 /*
7462 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7463 * the task, and the next invocation will do it.
7464 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007465 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007466 return autoremove_wake_function(curr, mode, wake_flags, key);
7467 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007468}
7469
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007470static int io_run_task_work_sig(void)
7471{
7472 if (io_run_task_work())
7473 return 1;
7474 if (!signal_pending(current))
7475 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007476 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007477 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007478 return -EINTR;
7479}
7480
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007481/* when returns >0, the caller should retry */
7482static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7483 struct io_wait_queue *iowq,
7484 signed long *timeout)
7485{
7486 int ret;
7487
7488 /* make sure we run task_work before checking for signals */
7489 ret = io_run_task_work_sig();
7490 if (ret || io_should_wake(iowq))
7491 return ret;
7492 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007493 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007494 return 1;
7495
7496 *timeout = schedule_timeout(*timeout);
7497 return !*timeout ? -ETIME : 1;
7498}
7499
Jens Axboe2b188cc2019-01-07 10:46:33 -07007500/*
7501 * Wait until events become available, if we don't already have some. The
7502 * application must reap them itself, as they reside on the shared cq ring.
7503 */
7504static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007505 const sigset_t __user *sig, size_t sigsz,
7506 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007507{
Pavel Begunkov902910992021-08-09 09:07:32 -06007508 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007509 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007510 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7511 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007512
Jens Axboeb41e9852020-02-17 09:52:41 -07007513 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007514 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007515 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007516 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007517 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007518 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007519 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007520
7521 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007522#ifdef CONFIG_COMPAT
7523 if (in_compat_syscall())
7524 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007525 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007526 else
7527#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007528 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007529
Jens Axboe2b188cc2019-01-07 10:46:33 -07007530 if (ret)
7531 return ret;
7532 }
7533
Hao Xuc73ebb62020-11-03 10:54:37 +08007534 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007535 struct timespec64 ts;
7536
Hao Xuc73ebb62020-11-03 10:54:37 +08007537 if (get_timespec64(&ts, uts))
7538 return -EFAULT;
7539 timeout = timespec64_to_jiffies(&ts);
7540 }
7541
Pavel Begunkov902910992021-08-09 09:07:32 -06007542 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7543 iowq.wq.private = current;
7544 INIT_LIST_HEAD(&iowq.wq.entry);
7545 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007546 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007547 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007548
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007549 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007550 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007551 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007552 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007553 ret = -EBUSY;
7554 break;
7555 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007556 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007557 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007558 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007559 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007560 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007561 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007562
Jens Axboeb7db41c2020-07-04 08:55:50 -06007563 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007564
Hristo Venev75b28af2019-08-26 17:23:46 +00007565 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007566}
7567
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007568static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007569{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007570 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007571
7572 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007573 kfree(table[i]);
7574 kfree(table);
7575}
7576
7577static void **io_alloc_page_table(size_t size)
7578{
7579 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7580 size_t init_size = size;
7581 void **table;
7582
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007583 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007584 if (!table)
7585 return NULL;
7586
7587 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007588 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007589
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007590 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007591 if (!table[i]) {
7592 io_free_page_table(table, init_size);
7593 return NULL;
7594 }
7595 size -= this_size;
7596 }
7597 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007598}
7599
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007600static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7601{
7602 percpu_ref_exit(&ref_node->refs);
7603 kfree(ref_node);
7604}
7605
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007606static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7607{
7608 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7609 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7610 unsigned long flags;
7611 bool first_add = false;
7612
7613 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7614 node->done = true;
7615
7616 while (!list_empty(&ctx->rsrc_ref_list)) {
7617 node = list_first_entry(&ctx->rsrc_ref_list,
7618 struct io_rsrc_node, node);
7619 /* recycle ref nodes in order */
7620 if (!node->done)
7621 break;
7622 list_del(&node->node);
7623 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7624 }
7625 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7626
7627 if (first_add)
7628 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7629}
7630
7631static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7632{
7633 struct io_rsrc_node *ref_node;
7634
7635 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7636 if (!ref_node)
7637 return NULL;
7638
7639 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7640 0, GFP_KERNEL)) {
7641 kfree(ref_node);
7642 return NULL;
7643 }
7644 INIT_LIST_HEAD(&ref_node->node);
7645 INIT_LIST_HEAD(&ref_node->rsrc_list);
7646 ref_node->done = false;
7647 return ref_node;
7648}
7649
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007650static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7651 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007652{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007653 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7654 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007655
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007656 if (data_to_kill) {
7657 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007658
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007659 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007660 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007661 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007662 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007663
Pavel Begunkov3e942492021-04-11 01:46:34 +01007664 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007665 percpu_ref_kill(&rsrc_node->refs);
7666 ctx->rsrc_node = NULL;
7667 }
7668
7669 if (!ctx->rsrc_node) {
7670 ctx->rsrc_node = ctx->rsrc_backup_node;
7671 ctx->rsrc_backup_node = NULL;
7672 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007673}
7674
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007675static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007676{
7677 if (ctx->rsrc_backup_node)
7678 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007679 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007680 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7681}
7682
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007683static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007684{
7685 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007686
Pavel Begunkov215c3902021-04-01 15:43:48 +01007687 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007688 if (data->quiesce)
7689 return -ENXIO;
7690
7691 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007692 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007693 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007694 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007695 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007696 io_rsrc_node_switch(ctx, data);
7697
Pavel Begunkov3e942492021-04-11 01:46:34 +01007698 /* kill initial ref, already quiesced if zero */
7699 if (atomic_dec_and_test(&data->refs))
7700 break;
Jens Axboec018db42021-08-09 08:15:50 -06007701 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007702 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007703 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007704 if (!ret) {
7705 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007706 break;
Jens Axboec018db42021-08-09 08:15:50 -06007707 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007708
Pavel Begunkov3e942492021-04-11 01:46:34 +01007709 atomic_inc(&data->refs);
7710 /* wait for all works potentially completing data->done */
7711 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007712 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007713
Hao Xu8bad28d2021-02-19 17:19:36 +08007714 ret = io_run_task_work_sig();
7715 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007716 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007717 data->quiesce = false;
7718
Hao Xu8bad28d2021-02-19 17:19:36 +08007719 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007720}
7721
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007722static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7723{
7724 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7725 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7726
7727 return &data->tags[table_idx][off];
7728}
7729
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007730static void io_rsrc_data_free(struct io_rsrc_data *data)
7731{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007732 size_t size = data->nr * sizeof(data->tags[0][0]);
7733
7734 if (data->tags)
7735 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007736 kfree(data);
7737}
7738
Pavel Begunkovd878c812021-06-14 02:36:18 +01007739static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7740 u64 __user *utags, unsigned nr,
7741 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007742{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007743 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007744 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007745 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007746
7747 data = kzalloc(sizeof(*data), GFP_KERNEL);
7748 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007749 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007750 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007751 if (!data->tags) {
7752 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007753 return -ENOMEM;
7754 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007755
7756 data->nr = nr;
7757 data->ctx = ctx;
7758 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007759 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007760 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007761 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007762 u64 *tag_slot = io_get_tag_slot(data, i);
7763
7764 if (copy_from_user(tag_slot, &utags[i],
7765 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007766 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007767 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007768 }
7769
Pavel Begunkov3e942492021-04-11 01:46:34 +01007770 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007771 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007772 *pdata = data;
7773 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007774fail:
7775 io_rsrc_data_free(data);
7776 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007777}
7778
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007779static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7780{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007781 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7782 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007783 return !!table->files;
7784}
7785
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007786static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007787{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007788 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007789 table->files = NULL;
7790}
7791
Jens Axboe2b188cc2019-01-07 10:46:33 -07007792static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7793{
7794#if defined(CONFIG_UNIX)
7795 if (ctx->ring_sock) {
7796 struct sock *sock = ctx->ring_sock->sk;
7797 struct sk_buff *skb;
7798
7799 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7800 kfree_skb(skb);
7801 }
7802#else
7803 int i;
7804
7805 for (i = 0; i < ctx->nr_user_files; i++) {
7806 struct file *file;
7807
7808 file = io_file_from_index(ctx, i);
7809 if (file)
7810 fput(file);
7811 }
7812#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007813 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007814 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007815 ctx->file_data = NULL;
7816 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007817}
7818
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007819static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7820{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007821 int ret;
7822
Pavel Begunkov08480402021-04-13 02:58:38 +01007823 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007824 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007825 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7826 if (!ret)
7827 __io_sqe_files_unregister(ctx);
7828 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007829}
7830
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007831static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007832 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007833{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007834 WARN_ON_ONCE(sqd->thread == current);
7835
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007836 /*
7837 * Do the dance but not conditional clear_bit() because it'd race with
7838 * other threads incrementing park_pending and setting the bit.
7839 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007840 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007841 if (atomic_dec_return(&sqd->park_pending))
7842 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007843 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007844}
7845
Jens Axboe86e0d672021-03-05 08:44:39 -07007846static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007847 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007848{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007849 WARN_ON_ONCE(sqd->thread == current);
7850
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007851 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007852 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007853 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007854 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007855 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007856}
7857
7858static void io_sq_thread_stop(struct io_sq_data *sqd)
7859{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007860 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007861 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007862
Jens Axboe05962f92021-03-06 13:58:48 -07007863 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007864 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007865 if (sqd->thread)
7866 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007867 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007868 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007869}
7870
Jens Axboe534ca6d2020-09-02 13:52:19 -06007871static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007872{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007873 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007874 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7875
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007876 io_sq_thread_stop(sqd);
7877 kfree(sqd);
7878 }
7879}
7880
7881static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7882{
7883 struct io_sq_data *sqd = ctx->sq_data;
7884
7885 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007886 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007887 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007888 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007889 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007890
7891 io_put_sq_data(sqd);
7892 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007893 }
7894}
7895
Jens Axboeaa061652020-09-02 14:50:27 -06007896static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7897{
7898 struct io_ring_ctx *ctx_attach;
7899 struct io_sq_data *sqd;
7900 struct fd f;
7901
7902 f = fdget(p->wq_fd);
7903 if (!f.file)
7904 return ERR_PTR(-ENXIO);
7905 if (f.file->f_op != &io_uring_fops) {
7906 fdput(f);
7907 return ERR_PTR(-EINVAL);
7908 }
7909
7910 ctx_attach = f.file->private_data;
7911 sqd = ctx_attach->sq_data;
7912 if (!sqd) {
7913 fdput(f);
7914 return ERR_PTR(-EINVAL);
7915 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007916 if (sqd->task_tgid != current->tgid) {
7917 fdput(f);
7918 return ERR_PTR(-EPERM);
7919 }
Jens Axboeaa061652020-09-02 14:50:27 -06007920
7921 refcount_inc(&sqd->refs);
7922 fdput(f);
7923 return sqd;
7924}
7925
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007926static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7927 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007928{
7929 struct io_sq_data *sqd;
7930
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007931 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007932 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7933 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007934 if (!IS_ERR(sqd)) {
7935 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007936 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007937 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007938 /* fall through for EPERM case, setup new sqd/task */
7939 if (PTR_ERR(sqd) != -EPERM)
7940 return sqd;
7941 }
Jens Axboeaa061652020-09-02 14:50:27 -06007942
Jens Axboe534ca6d2020-09-02 13:52:19 -06007943 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7944 if (!sqd)
7945 return ERR_PTR(-ENOMEM);
7946
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007947 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007948 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007949 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007950 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007951 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007952 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007953 return sqd;
7954}
7955
Jens Axboe6b063142019-01-10 22:13:58 -07007956#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007957/*
7958 * Ensure the UNIX gc is aware of our file set, so we are certain that
7959 * the io_uring can be safely unregistered on process exit, even if we have
7960 * loops in the file referencing.
7961 */
7962static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7963{
7964 struct sock *sk = ctx->ring_sock->sk;
7965 struct scm_fp_list *fpl;
7966 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007967 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007968
Jens Axboe6b063142019-01-10 22:13:58 -07007969 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7970 if (!fpl)
7971 return -ENOMEM;
7972
7973 skb = alloc_skb(0, GFP_KERNEL);
7974 if (!skb) {
7975 kfree(fpl);
7976 return -ENOMEM;
7977 }
7978
7979 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007980
Jens Axboe08a45172019-10-03 08:11:03 -06007981 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007982 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007983 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007984 struct file *file = io_file_from_index(ctx, i + offset);
7985
7986 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007987 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007988 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007989 unix_inflight(fpl->user, fpl->fp[nr_files]);
7990 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007991 }
7992
Jens Axboe08a45172019-10-03 08:11:03 -06007993 if (nr_files) {
7994 fpl->max = SCM_MAX_FD;
7995 fpl->count = nr_files;
7996 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007997 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007998 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7999 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008000
Jens Axboe08a45172019-10-03 08:11:03 -06008001 for (i = 0; i < nr_files; i++)
8002 fput(fpl->fp[i]);
8003 } else {
8004 kfree_skb(skb);
8005 kfree(fpl);
8006 }
Jens Axboe6b063142019-01-10 22:13:58 -07008007
8008 return 0;
8009}
8010
8011/*
8012 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8013 * causes regular reference counting to break down. We rely on the UNIX
8014 * garbage collection to take care of this problem for us.
8015 */
8016static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8017{
8018 unsigned left, total;
8019 int ret = 0;
8020
8021 total = 0;
8022 left = ctx->nr_user_files;
8023 while (left) {
8024 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008025
8026 ret = __io_sqe_files_scm(ctx, this_files, total);
8027 if (ret)
8028 break;
8029 left -= this_files;
8030 total += this_files;
8031 }
8032
8033 if (!ret)
8034 return 0;
8035
8036 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008037 struct file *file = io_file_from_index(ctx, total);
8038
8039 if (file)
8040 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008041 total++;
8042 }
8043
8044 return ret;
8045}
8046#else
8047static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8048{
8049 return 0;
8050}
8051#endif
8052
Pavel Begunkov47e90392021-04-01 15:43:56 +01008053static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008054{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008055 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008056#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008057 struct sock *sock = ctx->ring_sock->sk;
8058 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8059 struct sk_buff *skb;
8060 int i;
8061
8062 __skb_queue_head_init(&list);
8063
8064 /*
8065 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8066 * remove this entry and rearrange the file array.
8067 */
8068 skb = skb_dequeue(head);
8069 while (skb) {
8070 struct scm_fp_list *fp;
8071
8072 fp = UNIXCB(skb).fp;
8073 for (i = 0; i < fp->count; i++) {
8074 int left;
8075
8076 if (fp->fp[i] != file)
8077 continue;
8078
8079 unix_notinflight(fp->user, fp->fp[i]);
8080 left = fp->count - 1 - i;
8081 if (left) {
8082 memmove(&fp->fp[i], &fp->fp[i + 1],
8083 left * sizeof(struct file *));
8084 }
8085 fp->count--;
8086 if (!fp->count) {
8087 kfree_skb(skb);
8088 skb = NULL;
8089 } else {
8090 __skb_queue_tail(&list, skb);
8091 }
8092 fput(file);
8093 file = NULL;
8094 break;
8095 }
8096
8097 if (!file)
8098 break;
8099
8100 __skb_queue_tail(&list, skb);
8101
8102 skb = skb_dequeue(head);
8103 }
8104
8105 if (skb_peek(&list)) {
8106 spin_lock_irq(&head->lock);
8107 while ((skb = __skb_dequeue(&list)) != NULL)
8108 __skb_queue_tail(head, skb);
8109 spin_unlock_irq(&head->lock);
8110 }
8111#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008112 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008113#endif
8114}
8115
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008116static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008117{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008118 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008119 struct io_ring_ctx *ctx = rsrc_data->ctx;
8120 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008121
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008122 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8123 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008124
8125 if (prsrc->tag) {
8126 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008127
8128 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008129 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008130 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01008131 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008132 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008133 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008134 io_cqring_ev_posted(ctx);
8135 io_ring_submit_unlock(ctx, lock_ring);
8136 }
8137
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008138 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008139 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008140 }
8141
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008142 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008143 if (atomic_dec_and_test(&rsrc_data->refs))
8144 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008145}
8146
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008147static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008148{
8149 struct io_ring_ctx *ctx;
8150 struct llist_node *node;
8151
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008152 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8153 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008154
8155 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008156 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008157 struct llist_node *next = node->next;
8158
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008159 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008160 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008161 node = next;
8162 }
8163}
8164
Jens Axboe05f3fb32019-12-09 11:22:50 -07008165static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008166 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008167{
8168 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008169 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008170 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008171 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008172
8173 if (ctx->file_data)
8174 return -EBUSY;
8175 if (!nr_args)
8176 return -EINVAL;
8177 if (nr_args > IORING_MAX_FIXED_FILES)
8178 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008179 if (nr_args > rlimit(RLIMIT_NOFILE))
8180 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008181 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008182 if (ret)
8183 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008184 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8185 &ctx->file_data);
8186 if (ret)
8187 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008188
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008189 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008190 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008191 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008192
Jens Axboe05f3fb32019-12-09 11:22:50 -07008193 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008194 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008195 ret = -EFAULT;
8196 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008197 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008198 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008199 if (fd == -1) {
8200 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008201 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008202 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008203 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008204 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008205
Jens Axboe05f3fb32019-12-09 11:22:50 -07008206 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008207 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008208 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008209 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008210
8211 /*
8212 * Don't allow io_uring instances to be registered. If UNIX
8213 * isn't enabled, then this causes a reference cycle and this
8214 * instance can never get freed. If UNIX is enabled we'll
8215 * handle it just fine, but there's still no point in allowing
8216 * a ring fd as it doesn't support regular read/write anyway.
8217 */
8218 if (file->f_op == &io_uring_fops) {
8219 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008220 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008221 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008222 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008223 }
8224
Jens Axboe05f3fb32019-12-09 11:22:50 -07008225 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008226 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008227 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008228 return ret;
8229 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008230
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008231 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008232 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008233out_fput:
8234 for (i = 0; i < ctx->nr_user_files; i++) {
8235 file = io_file_from_index(ctx, i);
8236 if (file)
8237 fput(file);
8238 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008239 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008240 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008241out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008242 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008243 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008244 return ret;
8245}
8246
Jens Axboec3a31e62019-10-03 13:59:56 -06008247static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8248 int index)
8249{
8250#if defined(CONFIG_UNIX)
8251 struct sock *sock = ctx->ring_sock->sk;
8252 struct sk_buff_head *head = &sock->sk_receive_queue;
8253 struct sk_buff *skb;
8254
8255 /*
8256 * See if we can merge this file into an existing skb SCM_RIGHTS
8257 * file set. If there's no room, fall back to allocating a new skb
8258 * and filling it in.
8259 */
8260 spin_lock_irq(&head->lock);
8261 skb = skb_peek(head);
8262 if (skb) {
8263 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8264
8265 if (fpl->count < SCM_MAX_FD) {
8266 __skb_unlink(skb, head);
8267 spin_unlock_irq(&head->lock);
8268 fpl->fp[fpl->count] = get_file(file);
8269 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8270 fpl->count++;
8271 spin_lock_irq(&head->lock);
8272 __skb_queue_head(head, skb);
8273 } else {
8274 skb = NULL;
8275 }
8276 }
8277 spin_unlock_irq(&head->lock);
8278
8279 if (skb) {
8280 fput(file);
8281 return 0;
8282 }
8283
8284 return __io_sqe_files_scm(ctx, 1, index);
8285#else
8286 return 0;
8287#endif
8288}
8289
Pavel Begunkovb9445592021-08-25 12:25:45 +01008290static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8291 unsigned int issue_flags, u32 slot_index)
8292{
8293 struct io_ring_ctx *ctx = req->ctx;
8294 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
8295 struct io_fixed_file *file_slot;
8296 int ret = -EBADF;
8297
8298 io_ring_submit_lock(ctx, !force_nonblock);
8299 if (file->f_op == &io_uring_fops)
8300 goto err;
8301 ret = -ENXIO;
8302 if (!ctx->file_data)
8303 goto err;
8304 ret = -EINVAL;
8305 if (slot_index >= ctx->nr_user_files)
8306 goto err;
8307
8308 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8309 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
8310 ret = -EBADF;
8311 if (file_slot->file_ptr)
8312 goto err;
8313
8314 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8315 io_fixed_file_set(file_slot, file);
8316 ret = io_sqe_file_register(ctx, file, slot_index);
8317 if (ret) {
8318 file_slot->file_ptr = 0;
8319 goto err;
8320 }
8321
8322 ret = 0;
8323err:
8324 io_ring_submit_unlock(ctx, !force_nonblock);
8325 if (ret)
8326 fput(file);
8327 return ret;
8328}
8329
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008330static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
Pavel Begunkove7c78372021-04-01 15:43:45 +01008331 struct io_rsrc_node *node, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008332{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008333 struct io_rsrc_put *prsrc;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008334
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008335 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8336 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08008337 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008338
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008339 prsrc->tag = *io_get_tag_slot(data, idx);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008340 prsrc->rsrc = rsrc;
Pavel Begunkove7c78372021-04-01 15:43:45 +01008341 list_add(&prsrc->list, &node->rsrc_list);
Hillf Dantona5318d32020-03-23 17:47:15 +08008342 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008343}
8344
8345static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008346 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008347 unsigned nr_args)
8348{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008349 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008350 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008351 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008352 struct io_fixed_file *file_slot;
8353 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008354 int fd, i, err = 0;
8355 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008356 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008357
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008358 if (!ctx->file_data)
8359 return -ENXIO;
8360 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008361 return -EINVAL;
8362
Pavel Begunkov67973b92021-01-26 13:51:09 +00008363 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008364 u64 tag = 0;
8365
8366 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8367 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008368 err = -EFAULT;
8369 break;
8370 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008371 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8372 err = -EINVAL;
8373 break;
8374 }
noah4e0377a2021-01-26 15:23:28 -05008375 if (fd == IORING_REGISTER_FILES_SKIP)
8376 continue;
8377
Pavel Begunkov67973b92021-01-26 13:51:09 +00008378 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008379 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008380
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008381 if (file_slot->file_ptr) {
8382 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008383 err = io_queue_rsrc_removal(data, up->offset + done,
8384 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008385 if (err)
8386 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008387 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008388 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008389 }
8390 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008391 file = fget(fd);
8392 if (!file) {
8393 err = -EBADF;
8394 break;
8395 }
8396 /*
8397 * Don't allow io_uring instances to be registered. If
8398 * UNIX isn't enabled, then this causes a reference
8399 * cycle and this instance can never get freed. If UNIX
8400 * is enabled we'll handle it just fine, but there's
8401 * still no point in allowing a ring fd as it doesn't
8402 * support regular read/write anyway.
8403 */
8404 if (file->f_op == &io_uring_fops) {
8405 fput(file);
8406 err = -EBADF;
8407 break;
8408 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008409 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008410 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008411 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008412 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008413 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008414 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008415 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008416 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008417 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008418 }
8419
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008420 if (needs_switch)
8421 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008422 return done ? done : err;
8423}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008424
Jens Axboe685fe7f2021-03-08 09:37:51 -07008425static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8426 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008427{
Jens Axboee9418942021-02-19 12:33:30 -07008428 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008429 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008430 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008431
Yang Yingliang362a9e62021-07-20 16:38:05 +08008432 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008433 hash = ctx->hash_map;
8434 if (!hash) {
8435 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008436 if (!hash) {
8437 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008438 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008439 }
Jens Axboee9418942021-02-19 12:33:30 -07008440 refcount_set(&hash->refs, 1);
8441 init_waitqueue_head(&hash->wait);
8442 ctx->hash_map = hash;
8443 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008444 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008445
8446 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008447 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008448 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008449 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008450
Jens Axboed25e3a32021-02-16 11:41:41 -07008451 /* Do QD, or 4 * CPUS, whatever is smallest */
8452 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008453
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008454 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008455}
8456
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008457static int io_uring_alloc_task_context(struct task_struct *task,
8458 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008459{
8460 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008461 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008462
Pavel Begunkov09899b12021-06-14 02:36:22 +01008463 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008464 if (unlikely(!tctx))
8465 return -ENOMEM;
8466
Jens Axboed8a6df12020-10-15 16:24:45 -06008467 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8468 if (unlikely(ret)) {
8469 kfree(tctx);
8470 return ret;
8471 }
8472
Jens Axboe685fe7f2021-03-08 09:37:51 -07008473 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008474 if (IS_ERR(tctx->io_wq)) {
8475 ret = PTR_ERR(tctx->io_wq);
8476 percpu_counter_destroy(&tctx->inflight);
8477 kfree(tctx);
8478 return ret;
8479 }
8480
Jens Axboe0f212202020-09-13 13:09:39 -06008481 xa_init(&tctx->xa);
8482 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008483 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008484 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008485 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008486 spin_lock_init(&tctx->task_lock);
8487 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008488 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008489 return 0;
8490}
8491
8492void __io_uring_free(struct task_struct *tsk)
8493{
8494 struct io_uring_task *tctx = tsk->io_uring;
8495
8496 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008497 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008498 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008499
Jens Axboed8a6df12020-10-15 16:24:45 -06008500 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008501 kfree(tctx);
8502 tsk->io_uring = NULL;
8503}
8504
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008505static int io_sq_offload_create(struct io_ring_ctx *ctx,
8506 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008507{
8508 int ret;
8509
Jens Axboed25e3a32021-02-16 11:41:41 -07008510 /* Retain compatibility with failing for an invalid attach attempt */
8511 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8512 IORING_SETUP_ATTACH_WQ) {
8513 struct fd f;
8514
8515 f = fdget(p->wq_fd);
8516 if (!f.file)
8517 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008518 if (f.file->f_op != &io_uring_fops) {
8519 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008520 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008521 }
8522 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008523 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008524 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008525 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008526 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008527 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008528
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008529 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008530 if (IS_ERR(sqd)) {
8531 ret = PTR_ERR(sqd);
8532 goto err;
8533 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008534
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008535 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008536 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008537 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8538 if (!ctx->sq_thread_idle)
8539 ctx->sq_thread_idle = HZ;
8540
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008541 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008542 list_add(&ctx->sqd_list, &sqd->ctx_list);
8543 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008544 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008545 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008546 io_sq_thread_unpark(sqd);
8547
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008548 if (ret < 0)
8549 goto err;
8550 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008551 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008552
Jens Axboe6c271ce2019-01-10 11:22:30 -07008553 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008554 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008555
Jens Axboe917257d2019-04-13 09:28:55 -06008556 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008557 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008558 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008559 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008560 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008561 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008562 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008563
8564 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008565 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008566 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8567 if (IS_ERR(tsk)) {
8568 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008569 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008570 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008571
Jens Axboe46fe18b2021-03-04 12:39:36 -07008572 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008573 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008574 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008575 if (ret)
8576 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008577 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8578 /* Can't have SQ_AFF without SQPOLL */
8579 ret = -EINVAL;
8580 goto err;
8581 }
8582
Jens Axboe2b188cc2019-01-07 10:46:33 -07008583 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008584err_sqpoll:
8585 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008586err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008587 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008588 return ret;
8589}
8590
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008591static inline void __io_unaccount_mem(struct user_struct *user,
8592 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008593{
8594 atomic_long_sub(nr_pages, &user->locked_vm);
8595}
8596
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008597static inline int __io_account_mem(struct user_struct *user,
8598 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008599{
8600 unsigned long page_limit, cur_pages, new_pages;
8601
8602 /* Don't allow more pages than we can safely lock */
8603 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8604
8605 do {
8606 cur_pages = atomic_long_read(&user->locked_vm);
8607 new_pages = cur_pages + nr_pages;
8608 if (new_pages > page_limit)
8609 return -ENOMEM;
8610 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8611 new_pages) != cur_pages);
8612
8613 return 0;
8614}
8615
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008616static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008617{
Jens Axboe62e398b2021-02-21 16:19:37 -07008618 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008619 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008620
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008621 if (ctx->mm_account)
8622 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008623}
8624
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008625static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008626{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008627 int ret;
8628
Jens Axboe62e398b2021-02-21 16:19:37 -07008629 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008630 ret = __io_account_mem(ctx->user, nr_pages);
8631 if (ret)
8632 return ret;
8633 }
8634
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008635 if (ctx->mm_account)
8636 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008637
8638 return 0;
8639}
8640
Jens Axboe2b188cc2019-01-07 10:46:33 -07008641static void io_mem_free(void *ptr)
8642{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008643 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008644
Mark Rutland52e04ef2019-04-30 17:30:21 +01008645 if (!ptr)
8646 return;
8647
8648 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008649 if (put_page_testzero(page))
8650 free_compound_page(page);
8651}
8652
8653static void *io_mem_alloc(size_t size)
8654{
8655 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008656 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008657
8658 return (void *) __get_free_pages(gfp_flags, get_order(size));
8659}
8660
Hristo Venev75b28af2019-08-26 17:23:46 +00008661static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8662 size_t *sq_offset)
8663{
8664 struct io_rings *rings;
8665 size_t off, sq_array_size;
8666
8667 off = struct_size(rings, cqes, cq_entries);
8668 if (off == SIZE_MAX)
8669 return SIZE_MAX;
8670
8671#ifdef CONFIG_SMP
8672 off = ALIGN(off, SMP_CACHE_BYTES);
8673 if (off == 0)
8674 return SIZE_MAX;
8675#endif
8676
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008677 if (sq_offset)
8678 *sq_offset = off;
8679
Hristo Venev75b28af2019-08-26 17:23:46 +00008680 sq_array_size = array_size(sizeof(u32), sq_entries);
8681 if (sq_array_size == SIZE_MAX)
8682 return SIZE_MAX;
8683
8684 if (check_add_overflow(off, sq_array_size, &off))
8685 return SIZE_MAX;
8686
Hristo Venev75b28af2019-08-26 17:23:46 +00008687 return off;
8688}
8689
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008690static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008691{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008692 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008693 unsigned int i;
8694
Pavel Begunkov62248432021-04-28 13:11:29 +01008695 if (imu != ctx->dummy_ubuf) {
8696 for (i = 0; i < imu->nr_bvecs; i++)
8697 unpin_user_page(imu->bvec[i].bv_page);
8698 if (imu->acct_pages)
8699 io_unaccount_mem(ctx, imu->acct_pages);
8700 kvfree(imu);
8701 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008702 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008703}
8704
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008705static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8706{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008707 io_buffer_unmap(ctx, &prsrc->buf);
8708 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008709}
8710
8711static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008712{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008713 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008714
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008715 for (i = 0; i < ctx->nr_user_bufs; i++)
8716 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008717 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008718 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008719 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008720 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008721 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008722}
8723
Jens Axboeedafcce2019-01-09 09:16:05 -07008724static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8725{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008726 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008727
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008728 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008729 return -ENXIO;
8730
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008731 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8732 if (!ret)
8733 __io_sqe_buffers_unregister(ctx);
8734 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008735}
8736
8737static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8738 void __user *arg, unsigned index)
8739{
8740 struct iovec __user *src;
8741
8742#ifdef CONFIG_COMPAT
8743 if (ctx->compat) {
8744 struct compat_iovec __user *ciovs;
8745 struct compat_iovec ciov;
8746
8747 ciovs = (struct compat_iovec __user *) arg;
8748 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8749 return -EFAULT;
8750
Jens Axboed55e5f52019-12-11 16:12:15 -07008751 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008752 dst->iov_len = ciov.iov_len;
8753 return 0;
8754 }
8755#endif
8756 src = (struct iovec __user *) arg;
8757 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8758 return -EFAULT;
8759 return 0;
8760}
8761
Jens Axboede293932020-09-17 16:19:16 -06008762/*
8763 * Not super efficient, but this is just a registration time. And we do cache
8764 * the last compound head, so generally we'll only do a full search if we don't
8765 * match that one.
8766 *
8767 * We check if the given compound head page has already been accounted, to
8768 * avoid double accounting it. This allows us to account the full size of the
8769 * page, not just the constituent pages of a huge page.
8770 */
8771static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8772 int nr_pages, struct page *hpage)
8773{
8774 int i, j;
8775
8776 /* check current page array */
8777 for (i = 0; i < nr_pages; i++) {
8778 if (!PageCompound(pages[i]))
8779 continue;
8780 if (compound_head(pages[i]) == hpage)
8781 return true;
8782 }
8783
8784 /* check previously registered pages */
8785 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008786 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008787
8788 for (j = 0; j < imu->nr_bvecs; j++) {
8789 if (!PageCompound(imu->bvec[j].bv_page))
8790 continue;
8791 if (compound_head(imu->bvec[j].bv_page) == hpage)
8792 return true;
8793 }
8794 }
8795
8796 return false;
8797}
8798
8799static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8800 int nr_pages, struct io_mapped_ubuf *imu,
8801 struct page **last_hpage)
8802{
8803 int i, ret;
8804
Pavel Begunkov216e5832021-05-29 12:01:02 +01008805 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008806 for (i = 0; i < nr_pages; i++) {
8807 if (!PageCompound(pages[i])) {
8808 imu->acct_pages++;
8809 } else {
8810 struct page *hpage;
8811
8812 hpage = compound_head(pages[i]);
8813 if (hpage == *last_hpage)
8814 continue;
8815 *last_hpage = hpage;
8816 if (headpage_already_acct(ctx, pages, i, hpage))
8817 continue;
8818 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8819 }
8820 }
8821
8822 if (!imu->acct_pages)
8823 return 0;
8824
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008825 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008826 if (ret)
8827 imu->acct_pages = 0;
8828 return ret;
8829}
8830
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008831static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008832 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008833 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008834{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008835 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008836 struct vm_area_struct **vmas = NULL;
8837 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008838 unsigned long off, start, end, ubuf;
8839 size_t size;
8840 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008841
Pavel Begunkov62248432021-04-28 13:11:29 +01008842 if (!iov->iov_base) {
8843 *pimu = ctx->dummy_ubuf;
8844 return 0;
8845 }
8846
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008847 ubuf = (unsigned long) iov->iov_base;
8848 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8849 start = ubuf >> PAGE_SHIFT;
8850 nr_pages = end - start;
8851
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008852 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008853 ret = -ENOMEM;
8854
8855 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8856 if (!pages)
8857 goto done;
8858
8859 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8860 GFP_KERNEL);
8861 if (!vmas)
8862 goto done;
8863
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008864 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008865 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008866 goto done;
8867
8868 ret = 0;
8869 mmap_read_lock(current->mm);
8870 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8871 pages, vmas);
8872 if (pret == nr_pages) {
8873 /* don't support file backed memory */
8874 for (i = 0; i < nr_pages; i++) {
8875 struct vm_area_struct *vma = vmas[i];
8876
Pavel Begunkov40dad762021-06-09 15:26:54 +01008877 if (vma_is_shmem(vma))
8878 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008879 if (vma->vm_file &&
8880 !is_file_hugepages(vma->vm_file)) {
8881 ret = -EOPNOTSUPP;
8882 break;
8883 }
8884 }
8885 } else {
8886 ret = pret < 0 ? pret : -EFAULT;
8887 }
8888 mmap_read_unlock(current->mm);
8889 if (ret) {
8890 /*
8891 * if we did partial map, or found file backed vmas,
8892 * release any pages we did get
8893 */
8894 if (pret > 0)
8895 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008896 goto done;
8897 }
8898
8899 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8900 if (ret) {
8901 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008902 goto done;
8903 }
8904
8905 off = ubuf & ~PAGE_MASK;
8906 size = iov->iov_len;
8907 for (i = 0; i < nr_pages; i++) {
8908 size_t vec_len;
8909
8910 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8911 imu->bvec[i].bv_page = pages[i];
8912 imu->bvec[i].bv_len = vec_len;
8913 imu->bvec[i].bv_offset = off;
8914 off = 0;
8915 size -= vec_len;
8916 }
8917 /* store original address for later verification */
8918 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008919 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008920 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008921 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008922 ret = 0;
8923done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008924 if (ret)
8925 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008926 kvfree(pages);
8927 kvfree(vmas);
8928 return ret;
8929}
8930
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008931static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008932{
Pavel Begunkov87094462021-04-11 01:46:36 +01008933 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8934 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008935}
8936
8937static int io_buffer_validate(struct iovec *iov)
8938{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008939 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8940
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008941 /*
8942 * Don't impose further limits on the size and buffer
8943 * constraints here, we'll -EINVAL later when IO is
8944 * submitted if they are wrong.
8945 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008946 if (!iov->iov_base)
8947 return iov->iov_len ? -EFAULT : 0;
8948 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008949 return -EFAULT;
8950
8951 /* arbitrary limit, but we need something */
8952 if (iov->iov_len > SZ_1G)
8953 return -EFAULT;
8954
Pavel Begunkov50e96982021-03-24 22:59:01 +00008955 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8956 return -EOVERFLOW;
8957
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008958 return 0;
8959}
8960
8961static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008962 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008963{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008964 struct page *last_hpage = NULL;
8965 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008966 int i, ret;
8967 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008968
Pavel Begunkov87094462021-04-11 01:46:36 +01008969 if (ctx->user_bufs)
8970 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01008971 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01008972 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008973 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008974 if (ret)
8975 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008976 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
8977 if (ret)
8978 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008979 ret = io_buffers_map_alloc(ctx, nr_args);
8980 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08008981 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008982 return ret;
8983 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008984
Pavel Begunkov87094462021-04-11 01:46:36 +01008985 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07008986 ret = io_copy_iov(ctx, &iov, arg, i);
8987 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008988 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008989 ret = io_buffer_validate(&iov);
8990 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008991 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008992 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008993 ret = -EINVAL;
8994 break;
8995 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008996
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008997 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
8998 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008999 if (ret)
9000 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009001 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009002
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009003 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009004
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009005 ctx->buf_data = data;
9006 if (ret)
9007 __io_sqe_buffers_unregister(ctx);
9008 else
9009 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009010 return ret;
9011}
9012
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009013static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9014 struct io_uring_rsrc_update2 *up,
9015 unsigned int nr_args)
9016{
9017 u64 __user *tags = u64_to_user_ptr(up->tags);
9018 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009019 struct page *last_hpage = NULL;
9020 bool needs_switch = false;
9021 __u32 done;
9022 int i, err;
9023
9024 if (!ctx->buf_data)
9025 return -ENXIO;
9026 if (up->offset + nr_args > ctx->nr_user_bufs)
9027 return -EINVAL;
9028
9029 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009030 struct io_mapped_ubuf *imu;
9031 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009032 u64 tag = 0;
9033
9034 err = io_copy_iov(ctx, &iov, iovs, done);
9035 if (err)
9036 break;
9037 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9038 err = -EFAULT;
9039 break;
9040 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009041 err = io_buffer_validate(&iov);
9042 if (err)
9043 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009044 if (!iov.iov_base && tag) {
9045 err = -EINVAL;
9046 break;
9047 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009048 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9049 if (err)
9050 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009051
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009052 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009053 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009054 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9055 ctx->rsrc_node, ctx->user_bufs[i]);
9056 if (unlikely(err)) {
9057 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009058 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009059 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009060 ctx->user_bufs[i] = NULL;
9061 needs_switch = true;
9062 }
9063
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009064 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009065 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009066 }
9067
9068 if (needs_switch)
9069 io_rsrc_node_switch(ctx, ctx->buf_data);
9070 return done ? done : err;
9071}
9072
Jens Axboe9b402842019-04-11 11:45:41 -06009073static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9074{
9075 __s32 __user *fds = arg;
9076 int fd;
9077
9078 if (ctx->cq_ev_fd)
9079 return -EBUSY;
9080
9081 if (copy_from_user(&fd, fds, sizeof(*fds)))
9082 return -EFAULT;
9083
9084 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9085 if (IS_ERR(ctx->cq_ev_fd)) {
9086 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009087
Jens Axboe9b402842019-04-11 11:45:41 -06009088 ctx->cq_ev_fd = NULL;
9089 return ret;
9090 }
9091
9092 return 0;
9093}
9094
9095static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9096{
9097 if (ctx->cq_ev_fd) {
9098 eventfd_ctx_put(ctx->cq_ev_fd);
9099 ctx->cq_ev_fd = NULL;
9100 return 0;
9101 }
9102
9103 return -ENXIO;
9104}
9105
Jens Axboe5a2e7452020-02-23 16:23:11 -07009106static void io_destroy_buffers(struct io_ring_ctx *ctx)
9107{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009108 struct io_buffer *buf;
9109 unsigned long index;
9110
9111 xa_for_each(&ctx->io_buffers, index, buf)
9112 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009113}
9114
Pavel Begunkov72558342021-08-09 20:18:09 +01009115static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00009116{
Jens Axboe68e68ee2021-02-13 09:00:02 -07009117 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00009118
Pavel Begunkovbb943b82021-08-09 20:18:10 +01009119 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
9120 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00009121 kmem_cache_free(req_cachep, req);
9122 }
9123}
9124
Jens Axboe4010fec2021-02-27 15:04:18 -07009125static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009126{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009127 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009128
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009129 mutex_lock(&ctx->uring_lock);
9130
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009131 if (state->free_reqs) {
9132 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
9133 state->free_reqs = 0;
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00009134 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009135
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009136 io_flush_cached_locked_reqs(ctx, state);
9137 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009138 mutex_unlock(&ctx->uring_lock);
9139}
9140
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009141static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009142{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009143 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009144 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009145}
9146
Jens Axboe2b188cc2019-01-07 10:46:33 -07009147static void io_ring_ctx_free(struct io_ring_ctx *ctx)
9148{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009149 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009150
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009151 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009152 mmdrop(ctx->mm_account);
9153 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009154 }
Jens Axboedef596e2019-01-09 08:59:42 -07009155
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009156 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9157 io_wait_rsrc_data(ctx->buf_data);
9158 io_wait_rsrc_data(ctx->file_data);
9159
Hao Xu8bad28d2021-02-19 17:19:36 +08009160 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009161 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009162 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009163 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009164 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009165 if (ctx->rings)
9166 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009167 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009168 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009169 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009170 if (ctx->sq_creds)
9171 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009172
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009173 /* there are no registered resources left, nobody uses it */
9174 if (ctx->rsrc_node)
9175 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009176 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009177 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009178 flush_delayed_work(&ctx->rsrc_put_work);
9179
9180 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9181 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009182
9183#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07009184 if (ctx->ring_sock) {
9185 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009186 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07009187 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009188#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009189 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009190
Hristo Venev75b28af2019-08-26 17:23:46 +00009191 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009192 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009193
9194 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009195 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009196 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009197 if (ctx->hash_map)
9198 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009199 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009200 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009201 kfree(ctx);
9202}
9203
9204static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9205{
9206 struct io_ring_ctx *ctx = file->private_data;
9207 __poll_t mask = 0;
9208
Pavel Begunkov311997b2021-06-14 23:37:28 +01009209 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009210 /*
9211 * synchronizes with barrier from wq_has_sleeper call in
9212 * io_commit_cqring
9213 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009214 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009215 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009216 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009217
9218 /*
9219 * Don't flush cqring overflow list here, just do a simple check.
9220 * Otherwise there could possible be ABBA deadlock:
9221 * CPU0 CPU1
9222 * ---- ----
9223 * lock(&ctx->uring_lock);
9224 * lock(&ep->mtx);
9225 * lock(&ctx->uring_lock);
9226 * lock(&ep->mtx);
9227 *
9228 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9229 * pushs them to do the flush.
9230 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009231 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009232 mask |= EPOLLIN | EPOLLRDNORM;
9233
9234 return mask;
9235}
9236
9237static int io_uring_fasync(int fd, struct file *file, int on)
9238{
9239 struct io_ring_ctx *ctx = file->private_data;
9240
9241 return fasync_helper(fd, file, on, &ctx->cq_fasync);
9242}
9243
Yejune Deng0bead8c2020-12-24 11:02:20 +08009244static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009245{
Jens Axboe4379bf82021-02-15 13:40:22 -07009246 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009247
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009248 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009249 if (creds) {
9250 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009251 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009252 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009253
9254 return -EINVAL;
9255}
9256
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009257struct io_tctx_exit {
9258 struct callback_head task_work;
9259 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009260 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009261};
9262
9263static void io_tctx_exit_cb(struct callback_head *cb)
9264{
9265 struct io_uring_task *tctx = current->io_uring;
9266 struct io_tctx_exit *work;
9267
9268 work = container_of(cb, struct io_tctx_exit, task_work);
9269 /*
9270 * When @in_idle, we're in cancellation and it's racy to remove the
9271 * node. It'll be removed by the end of cancellation, just ignore it.
9272 */
9273 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009274 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009275 complete(&work->completion);
9276}
9277
Pavel Begunkov28090c12021-04-25 23:34:45 +01009278static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
9279{
9280 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9281
9282 return req->ctx == data;
9283}
9284
Jens Axboe85faa7b2020-04-09 18:14:00 -06009285static void io_ring_exit_work(struct work_struct *work)
9286{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009287 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009288 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009289 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009290 struct io_tctx_exit exit;
9291 struct io_tctx_node *node;
9292 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009293
Jens Axboe56952e92020-06-17 15:00:04 -06009294 /*
9295 * If we're doing polled IO and end up having requests being
9296 * submitted async (out-of-line), then completions can come in while
9297 * we're waiting for refs to drop. We need to reap these manually,
9298 * as nobody else will be looking for them.
9299 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009300 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009301 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009302 if (ctx->sq_data) {
9303 struct io_sq_data *sqd = ctx->sq_data;
9304 struct task_struct *tsk;
9305
9306 io_sq_thread_park(sqd);
9307 tsk = sqd->thread;
9308 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9309 io_wq_cancel_cb(tsk->io_uring->io_wq,
9310 io_cancel_ctx_cb, ctx, true);
9311 io_sq_thread_unpark(sqd);
9312 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009313
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009314 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9315 /* there is little hope left, don't run it too often */
9316 interval = HZ * 60;
9317 }
9318 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009319
Pavel Begunkov7f006512021-04-14 13:38:34 +01009320 init_completion(&exit.completion);
9321 init_task_work(&exit.task_work, io_tctx_exit_cb);
9322 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009323 /*
9324 * Some may use context even when all refs and requests have been put,
9325 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009326 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009327 * this lock/unlock section also waits them to finish.
9328 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009329 mutex_lock(&ctx->uring_lock);
9330 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009331 WARN_ON_ONCE(time_after(jiffies, timeout));
9332
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009333 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9334 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009335 /* don't spin on a single task if cancellation failed */
9336 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009337 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9338 if (WARN_ON_ONCE(ret))
9339 continue;
9340 wake_up_process(node->task);
9341
9342 mutex_unlock(&ctx->uring_lock);
9343 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009344 mutex_lock(&ctx->uring_lock);
9345 }
9346 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009347 spin_lock(&ctx->completion_lock);
9348 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009349
Jens Axboe85faa7b2020-04-09 18:14:00 -06009350 io_ring_ctx_free(ctx);
9351}
9352
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009353/* Returns true if we found and killed one or more timeouts */
9354static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009355 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009356{
9357 struct io_kiocb *req, *tmp;
9358 int canceled = 0;
9359
Jens Axboe79ebeae2021-08-10 15:18:27 -06009360 spin_lock(&ctx->completion_lock);
9361 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009362 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009363 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009364 io_kill_timeout(req, -ECANCELED);
9365 canceled++;
9366 }
9367 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009368 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009369 if (canceled != 0)
9370 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009371 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009372 if (canceled != 0)
9373 io_cqring_ev_posted(ctx);
9374 return canceled != 0;
9375}
9376
Jens Axboe2b188cc2019-01-07 10:46:33 -07009377static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9378{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009379 unsigned long index;
9380 struct creds *creds;
9381
Jens Axboe2b188cc2019-01-07 10:46:33 -07009382 mutex_lock(&ctx->uring_lock);
9383 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009384 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009385 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009386 xa_for_each(&ctx->personalities, index, creds)
9387 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009388 mutex_unlock(&ctx->uring_lock);
9389
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009390 io_kill_timeouts(ctx, NULL, true);
9391 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009392
Jens Axboe15dff282019-11-13 09:09:23 -07009393 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009394 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009395
Jens Axboe85faa7b2020-04-09 18:14:00 -06009396 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009397 /*
9398 * Use system_unbound_wq to avoid spawning tons of event kworkers
9399 * if we're exiting a ton of rings at the same time. It just adds
9400 * noise and overhead, there's no discernable change in runtime
9401 * over using system_wq.
9402 */
9403 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009404}
9405
9406static int io_uring_release(struct inode *inode, struct file *file)
9407{
9408 struct io_ring_ctx *ctx = file->private_data;
9409
9410 file->private_data = NULL;
9411 io_ring_ctx_wait_and_kill(ctx);
9412 return 0;
9413}
9414
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009415struct io_task_cancel {
9416 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009417 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009418};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009419
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009420static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009421{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009422 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009423 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009424 bool ret;
9425
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009426 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009427 struct io_ring_ctx *ctx = req->ctx;
9428
9429 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009430 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009431 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009432 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009433 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009434 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009435 }
9436 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009437}
9438
Pavel Begunkove1915f72021-03-11 23:29:35 +00009439static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009440 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009441{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009442 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009443 LIST_HEAD(list);
9444
Jens Axboe79ebeae2021-08-10 15:18:27 -06009445 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009446 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009447 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009448 list_cut_position(&list, &ctx->defer_list, &de->list);
9449 break;
9450 }
9451 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009452 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009453 if (list_empty(&list))
9454 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009455
9456 while (!list_empty(&list)) {
9457 de = list_first_entry(&list, struct io_defer_entry, list);
9458 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009459 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009460 kfree(de);
9461 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009462 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009463}
9464
Pavel Begunkov1b007642021-03-06 11:02:17 +00009465static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9466{
9467 struct io_tctx_node *node;
9468 enum io_wq_cancel cret;
9469 bool ret = false;
9470
9471 mutex_lock(&ctx->uring_lock);
9472 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9473 struct io_uring_task *tctx = node->task->io_uring;
9474
9475 /*
9476 * io_wq will stay alive while we hold uring_lock, because it's
9477 * killed after ctx nodes, which requires to take the lock.
9478 */
9479 if (!tctx || !tctx->io_wq)
9480 continue;
9481 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9482 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9483 }
9484 mutex_unlock(&ctx->uring_lock);
9485
9486 return ret;
9487}
9488
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009489static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9490 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009491 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009492{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009493 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009494 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009495
9496 while (1) {
9497 enum io_wq_cancel cret;
9498 bool ret = false;
9499
Pavel Begunkov1b007642021-03-06 11:02:17 +00009500 if (!task) {
9501 ret |= io_uring_try_cancel_iowq(ctx);
9502 } else if (tctx && tctx->io_wq) {
9503 /*
9504 * Cancels requests of all rings, not only @ctx, but
9505 * it's fine as the task is in exit/exec.
9506 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009507 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009508 &cancel, true);
9509 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9510 }
9511
9512 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009513 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009514 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009515 while (!list_empty_careful(&ctx->iopoll_list)) {
9516 io_iopoll_try_reap_events(ctx);
9517 ret = true;
9518 }
9519 }
9520
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009521 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9522 ret |= io_poll_remove_all(ctx, task, cancel_all);
9523 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009524 if (task)
9525 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009526 if (!ret)
9527 break;
9528 cond_resched();
9529 }
9530}
9531
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009532static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009533{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009534 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009535 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009536 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009537
9538 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009539 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009540 if (unlikely(ret))
9541 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009542 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009543 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009544 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9545 node = kmalloc(sizeof(*node), GFP_KERNEL);
9546 if (!node)
9547 return -ENOMEM;
9548 node->ctx = ctx;
9549 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009550
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009551 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9552 node, GFP_KERNEL));
9553 if (ret) {
9554 kfree(node);
9555 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009556 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009557
9558 mutex_lock(&ctx->uring_lock);
9559 list_add(&node->ctx_node, &ctx->tctx_list);
9560 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009561 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009562 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009563 return 0;
9564}
9565
9566/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009567 * Note that this task has used io_uring. We use it for cancelation purposes.
9568 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009569static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009570{
9571 struct io_uring_task *tctx = current->io_uring;
9572
9573 if (likely(tctx && tctx->last == ctx))
9574 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009575 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009576}
9577
9578/*
Jens Axboe0f212202020-09-13 13:09:39 -06009579 * Remove this io_uring_file -> task mapping.
9580 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009581static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009582{
9583 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009584 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009585
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009586 if (!tctx)
9587 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009588 node = xa_erase(&tctx->xa, index);
9589 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009590 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009591
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009592 WARN_ON_ONCE(current != node->task);
9593 WARN_ON_ONCE(list_empty(&node->ctx_node));
9594
9595 mutex_lock(&node->ctx->uring_lock);
9596 list_del(&node->ctx_node);
9597 mutex_unlock(&node->ctx->uring_lock);
9598
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009599 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009600 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009601 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009602}
9603
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009604static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009605{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009606 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009607 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009608 unsigned long index;
9609
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009610 xa_for_each(&tctx->xa, index, node)
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009611 io_uring_del_tctx_node(index);
Marco Elverb16ef422021-05-27 11:25:48 +02009612 if (wq) {
9613 /*
9614 * Must be after io_uring_del_task_file() (removes nodes under
9615 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9616 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009617 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009618 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009619 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009620}
9621
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009622static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009623{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009624 if (tracked)
9625 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009626 return percpu_counter_sum(&tctx->inflight);
9627}
9628
Pavel Begunkov09899b12021-06-14 02:36:22 +01009629static void io_uring_drop_tctx_refs(struct task_struct *task)
9630{
9631 struct io_uring_task *tctx = task->io_uring;
9632 unsigned int refs = tctx->cached_refs;
9633
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009634 if (refs) {
9635 tctx->cached_refs = 0;
9636 percpu_counter_sub(&tctx->inflight, refs);
9637 put_task_struct_many(task, refs);
9638 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009639}
9640
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009641/*
9642 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9643 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9644 */
9645static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009646{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009647 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009648 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009649 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009650 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009651
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009652 WARN_ON_ONCE(sqd && sqd->thread != current);
9653
Palash Oswal6d042ff2021-04-27 18:21:49 +05309654 if (!current->io_uring)
9655 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009656 if (tctx->io_wq)
9657 io_wq_exit_start(tctx->io_wq);
9658
Jens Axboefdaf0832020-10-30 09:37:30 -06009659 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009660 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009661 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009662 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009663 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009664 if (!inflight)
9665 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009666
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009667 if (!sqd) {
9668 struct io_tctx_node *node;
9669 unsigned long index;
9670
9671 xa_for_each(&tctx->xa, index, node) {
9672 /* sqpoll task will cancel all its requests */
9673 if (node->ctx->sq_data)
9674 continue;
9675 io_uring_try_cancel_requests(node->ctx, current,
9676 cancel_all);
9677 }
9678 } else {
9679 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9680 io_uring_try_cancel_requests(ctx, current,
9681 cancel_all);
9682 }
9683
9684 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009685 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009686 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009687 * If we've seen completions, retry without waiting. This
9688 * avoids a race where a completion comes in before we did
9689 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009690 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009691 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009692 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009693 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009694 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009695 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009696
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009697 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009698 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009699 /* for exec all current's requests should be gone, kill tctx */
9700 __io_uring_free(current);
9701 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009702}
9703
Hao Xuf552a272021-08-12 12:14:35 +08009704void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009705{
Hao Xuf552a272021-08-12 12:14:35 +08009706 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009707}
9708
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009709static void *io_uring_validate_mmap_request(struct file *file,
9710 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009711{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009712 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009713 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009714 struct page *page;
9715 void *ptr;
9716
9717 switch (offset) {
9718 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009719 case IORING_OFF_CQ_RING:
9720 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009721 break;
9722 case IORING_OFF_SQES:
9723 ptr = ctx->sq_sqes;
9724 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009725 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009726 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009727 }
9728
9729 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009730 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009731 return ERR_PTR(-EINVAL);
9732
9733 return ptr;
9734}
9735
9736#ifdef CONFIG_MMU
9737
9738static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9739{
9740 size_t sz = vma->vm_end - vma->vm_start;
9741 unsigned long pfn;
9742 void *ptr;
9743
9744 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9745 if (IS_ERR(ptr))
9746 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009747
9748 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9749 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9750}
9751
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009752#else /* !CONFIG_MMU */
9753
9754static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9755{
9756 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9757}
9758
9759static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9760{
9761 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9762}
9763
9764static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9765 unsigned long addr, unsigned long len,
9766 unsigned long pgoff, unsigned long flags)
9767{
9768 void *ptr;
9769
9770 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9771 if (IS_ERR(ptr))
9772 return PTR_ERR(ptr);
9773
9774 return (unsigned long) ptr;
9775}
9776
9777#endif /* !CONFIG_MMU */
9778
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009779static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009780{
9781 DEFINE_WAIT(wait);
9782
9783 do {
9784 if (!io_sqring_full(ctx))
9785 break;
Jens Axboe90554202020-09-03 12:12:41 -06009786 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9787
9788 if (!io_sqring_full(ctx))
9789 break;
Jens Axboe90554202020-09-03 12:12:41 -06009790 schedule();
9791 } while (!signal_pending(current));
9792
9793 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009794 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009795}
9796
Hao Xuc73ebb62020-11-03 10:54:37 +08009797static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9798 struct __kernel_timespec __user **ts,
9799 const sigset_t __user **sig)
9800{
9801 struct io_uring_getevents_arg arg;
9802
9803 /*
9804 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9805 * is just a pointer to the sigset_t.
9806 */
9807 if (!(flags & IORING_ENTER_EXT_ARG)) {
9808 *sig = (const sigset_t __user *) argp;
9809 *ts = NULL;
9810 return 0;
9811 }
9812
9813 /*
9814 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9815 * timespec and sigset_t pointers if good.
9816 */
9817 if (*argsz != sizeof(arg))
9818 return -EINVAL;
9819 if (copy_from_user(&arg, argp, sizeof(arg)))
9820 return -EFAULT;
9821 *sig = u64_to_user_ptr(arg.sigmask);
9822 *argsz = arg.sigmask_sz;
9823 *ts = u64_to_user_ptr(arg.ts);
9824 return 0;
9825}
9826
Jens Axboe2b188cc2019-01-07 10:46:33 -07009827SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009828 u32, min_complete, u32, flags, const void __user *, argp,
9829 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009830{
9831 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009832 int submitted = 0;
9833 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009834 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009835
Jens Axboe4c6e2772020-07-01 11:29:10 -06009836 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009837
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009838 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9839 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009840 return -EINVAL;
9841
9842 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009843 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009844 return -EBADF;
9845
9846 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009847 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009848 goto out_fput;
9849
9850 ret = -ENXIO;
9851 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009852 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009853 goto out_fput;
9854
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009855 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009856 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009857 goto out;
9858
Jens Axboe6c271ce2019-01-10 11:22:30 -07009859 /*
9860 * For SQ polling, the thread will do all submissions and completions.
9861 * Just return the requested submit count, and wake the thread if
9862 * we were asked to.
9863 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009864 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009865 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009866 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009867
Jens Axboe21f96522021-08-14 09:04:40 -06009868 if (unlikely(ctx->sq_data->thread == NULL)) {
9869 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009870 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009871 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009872 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009873 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009874 if (flags & IORING_ENTER_SQ_WAIT) {
9875 ret = io_sqpoll_wait_sq(ctx);
9876 if (ret)
9877 goto out;
9878 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009879 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009880 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009881 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009882 if (unlikely(ret))
9883 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009884 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009885 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009886 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009887
9888 if (submitted != to_submit)
9889 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009890 }
9891 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009892 const sigset_t __user *sig;
9893 struct __kernel_timespec __user *ts;
9894
9895 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9896 if (unlikely(ret))
9897 goto out;
9898
Jens Axboe2b188cc2019-01-07 10:46:33 -07009899 min_complete = min(min_complete, ctx->cq_entries);
9900
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009901 /*
9902 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9903 * space applications don't need to do io completion events
9904 * polling again, they can rely on io_sq_thread to do polling
9905 * work, which can reduce cpu usage and uring_lock contention.
9906 */
9907 if (ctx->flags & IORING_SETUP_IOPOLL &&
9908 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009909 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009910 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009911 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009912 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009913 }
9914
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009915out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009916 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009917out_fput:
9918 fdput(f);
9919 return submitted ? submitted : ret;
9920}
9921
Tobias Klauserbebdb652020-02-26 18:38:32 +01009922#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009923static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9924 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009925{
Jens Axboe87ce9552020-01-30 08:25:34 -07009926 struct user_namespace *uns = seq_user_ns(m);
9927 struct group_info *gi;
9928 kernel_cap_t cap;
9929 unsigned __capi;
9930 int g;
9931
9932 seq_printf(m, "%5d\n", id);
9933 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9934 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9935 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9936 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9937 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9938 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9939 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9940 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9941 seq_puts(m, "\n\tGroups:\t");
9942 gi = cred->group_info;
9943 for (g = 0; g < gi->ngroups; g++) {
9944 seq_put_decimal_ull(m, g ? " " : "",
9945 from_kgid_munged(uns, gi->gid[g]));
9946 }
9947 seq_puts(m, "\n\tCapEff:\t");
9948 cap = cred->cap_effective;
9949 CAP_FOR_EACH_U32(__capi)
9950 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9951 seq_putc(m, '\n');
9952 return 0;
9953}
9954
9955static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9956{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009957 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009958 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009959 int i;
9960
Jens Axboefad8e0d2020-09-28 08:57:48 -06009961 /*
9962 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9963 * since fdinfo case grabs it in the opposite direction of normal use
9964 * cases. If we fail to get the lock, we just don't iterate any
9965 * structures that could be going away outside the io_uring mutex.
9966 */
9967 has_lock = mutex_trylock(&ctx->uring_lock);
9968
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009969 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009970 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009971 if (!sq->thread)
9972 sq = NULL;
9973 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009974
9975 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9976 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009977 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009978 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -07009979 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009980
Jens Axboe87ce9552020-01-30 08:25:34 -07009981 if (f)
9982 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9983 else
9984 seq_printf(m, "%5u: <none>\n", i);
9985 }
9986 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009987 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009988 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +01009989 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -07009990
Pavel Begunkov4751f532021-04-01 15:43:55 +01009991 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -07009992 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009993 if (has_lock && !xa_empty(&ctx->personalities)) {
9994 unsigned long index;
9995 const struct cred *cred;
9996
Jens Axboe87ce9552020-01-30 08:25:34 -07009997 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009998 xa_for_each(&ctx->personalities, index, cred)
9999 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010000 }
Jens Axboed7718a92020-02-14 22:23:12 -070010001 seq_printf(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010002 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010003 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10004 struct hlist_head *list = &ctx->cancel_hash[i];
10005 struct io_kiocb *req;
10006
10007 hlist_for_each_entry(req, list, hash_node)
10008 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10009 req->task->task_works != NULL);
10010 }
Jens Axboe79ebeae2021-08-10 15:18:27 -060010011 spin_unlock(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010012 if (has_lock)
10013 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010014}
10015
10016static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
10017{
10018 struct io_ring_ctx *ctx = f->private_data;
10019
10020 if (percpu_ref_tryget(&ctx->refs)) {
10021 __io_uring_show_fdinfo(ctx, m);
10022 percpu_ref_put(&ctx->refs);
10023 }
10024}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010025#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010026
Jens Axboe2b188cc2019-01-07 10:46:33 -070010027static const struct file_operations io_uring_fops = {
10028 .release = io_uring_release,
10029 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010030#ifndef CONFIG_MMU
10031 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10032 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10033#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010034 .poll = io_uring_poll,
10035 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010036#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010037 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010038#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010039};
10040
10041static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10042 struct io_uring_params *p)
10043{
Hristo Venev75b28af2019-08-26 17:23:46 +000010044 struct io_rings *rings;
10045 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010046
Jens Axboebd740482020-08-05 12:58:23 -060010047 /* make sure these are sane, as we already accounted them */
10048 ctx->sq_entries = p->sq_entries;
10049 ctx->cq_entries = p->cq_entries;
10050
Hristo Venev75b28af2019-08-26 17:23:46 +000010051 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10052 if (size == SIZE_MAX)
10053 return -EOVERFLOW;
10054
10055 rings = io_mem_alloc(size);
10056 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010057 return -ENOMEM;
10058
Hristo Venev75b28af2019-08-26 17:23:46 +000010059 ctx->rings = rings;
10060 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10061 rings->sq_ring_mask = p->sq_entries - 1;
10062 rings->cq_ring_mask = p->cq_entries - 1;
10063 rings->sq_ring_entries = p->sq_entries;
10064 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010065
10066 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010067 if (size == SIZE_MAX) {
10068 io_mem_free(ctx->rings);
10069 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010070 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010071 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010072
10073 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010074 if (!ctx->sq_sqes) {
10075 io_mem_free(ctx->rings);
10076 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010077 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010078 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010079
Jens Axboe2b188cc2019-01-07 10:46:33 -070010080 return 0;
10081}
10082
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010083static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10084{
10085 int ret, fd;
10086
10087 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10088 if (fd < 0)
10089 return fd;
10090
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010091 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010092 if (ret) {
10093 put_unused_fd(fd);
10094 return ret;
10095 }
10096 fd_install(fd, file);
10097 return fd;
10098}
10099
Jens Axboe2b188cc2019-01-07 10:46:33 -070010100/*
10101 * Allocate an anonymous fd, this is what constitutes the application
10102 * visible backing of an io_uring instance. The application mmaps this
10103 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10104 * we have to tie this fd to a socket for file garbage collection purposes.
10105 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010106static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010107{
10108 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010109#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010110 int ret;
10111
Jens Axboe2b188cc2019-01-07 10:46:33 -070010112 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10113 &ctx->ring_sock);
10114 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010115 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010116#endif
10117
Jens Axboe2b188cc2019-01-07 10:46:33 -070010118 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
10119 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010120#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010121 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010122 sock_release(ctx->ring_sock);
10123 ctx->ring_sock = NULL;
10124 } else {
10125 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010126 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010127#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010128 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010129}
10130
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010131static int io_uring_create(unsigned entries, struct io_uring_params *p,
10132 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010133{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010134 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010135 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010136 int ret;
10137
Jens Axboe8110c1a2019-12-28 15:39:54 -070010138 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010139 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010140 if (entries > IORING_MAX_ENTRIES) {
10141 if (!(p->flags & IORING_SETUP_CLAMP))
10142 return -EINVAL;
10143 entries = IORING_MAX_ENTRIES;
10144 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010145
10146 /*
10147 * Use twice as many entries for the CQ ring. It's possible for the
10148 * application to drive a higher depth than the size of the SQ ring,
10149 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010150 * some flexibility in overcommitting a bit. If the application has
10151 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10152 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010153 */
10154 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010155 if (p->flags & IORING_SETUP_CQSIZE) {
10156 /*
10157 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10158 * to a power-of-two, if it isn't already. We do NOT impose
10159 * any cq vs sq ring sizing.
10160 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010161 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010162 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010163 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10164 if (!(p->flags & IORING_SETUP_CLAMP))
10165 return -EINVAL;
10166 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10167 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010168 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10169 if (p->cq_entries < p->sq_entries)
10170 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010171 } else {
10172 p->cq_entries = 2 * p->sq_entries;
10173 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010174
Jens Axboe2b188cc2019-01-07 10:46:33 -070010175 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010176 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010177 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010178 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010179 if (!capable(CAP_IPC_LOCK))
10180 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010181
10182 /*
10183 * This is just grabbed for accounting purposes. When a process exits,
10184 * the mm is exited and dropped before the files, hence we need to hang
10185 * on to this mm purely for the purposes of being able to unaccount
10186 * memory (locked/pinned vm). It's not used for anything else.
10187 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010188 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010189 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010190
Jens Axboe2b188cc2019-01-07 10:46:33 -070010191 ret = io_allocate_scq_urings(ctx, p);
10192 if (ret)
10193 goto err;
10194
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010195 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010196 if (ret)
10197 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010198 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010199 ret = io_rsrc_node_switch_start(ctx);
10200 if (ret)
10201 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010202 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010203
Jens Axboe2b188cc2019-01-07 10:46:33 -070010204 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010205 p->sq_off.head = offsetof(struct io_rings, sq.head);
10206 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10207 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10208 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10209 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10210 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10211 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010212
10213 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010214 p->cq_off.head = offsetof(struct io_rings, cq.head);
10215 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10216 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10217 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10218 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10219 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010220 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010221
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010222 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10223 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010224 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010225 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010226 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10227 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010228
10229 if (copy_to_user(params, p, sizeof(*p))) {
10230 ret = -EFAULT;
10231 goto err;
10232 }
Jens Axboed1719f72020-07-30 13:43:53 -060010233
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010234 file = io_uring_get_file(ctx);
10235 if (IS_ERR(file)) {
10236 ret = PTR_ERR(file);
10237 goto err;
10238 }
10239
Jens Axboed1719f72020-07-30 13:43:53 -060010240 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010241 * Install ring fd as the very last thing, so we don't risk someone
10242 * having closed it before we finish setup
10243 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010244 ret = io_uring_install_fd(ctx, file);
10245 if (ret < 0) {
10246 /* fput will clean it up */
10247 fput(file);
10248 return ret;
10249 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010250
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010251 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010252 return ret;
10253err:
10254 io_ring_ctx_wait_and_kill(ctx);
10255 return ret;
10256}
10257
10258/*
10259 * Sets up an aio uring context, and returns the fd. Applications asks for a
10260 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10261 * params structure passed in.
10262 */
10263static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10264{
10265 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010266 int i;
10267
10268 if (copy_from_user(&p, params, sizeof(p)))
10269 return -EFAULT;
10270 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10271 if (p.resv[i])
10272 return -EINVAL;
10273 }
10274
Jens Axboe6c271ce2019-01-10 11:22:30 -070010275 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010276 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010277 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10278 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010279 return -EINVAL;
10280
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010281 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010282}
10283
10284SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10285 struct io_uring_params __user *, params)
10286{
10287 return io_uring_setup(entries, params);
10288}
10289
Jens Axboe66f4af92020-01-16 15:36:52 -070010290static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
10291{
10292 struct io_uring_probe *p;
10293 size_t size;
10294 int i, ret;
10295
10296 size = struct_size(p, ops, nr_args);
10297 if (size == SIZE_MAX)
10298 return -EOVERFLOW;
10299 p = kzalloc(size, GFP_KERNEL);
10300 if (!p)
10301 return -ENOMEM;
10302
10303 ret = -EFAULT;
10304 if (copy_from_user(p, arg, size))
10305 goto out;
10306 ret = -EINVAL;
10307 if (memchr_inv(p, 0, size))
10308 goto out;
10309
10310 p->last_op = IORING_OP_LAST - 1;
10311 if (nr_args > IORING_OP_LAST)
10312 nr_args = IORING_OP_LAST;
10313
10314 for (i = 0; i < nr_args; i++) {
10315 p->ops[i].op = i;
10316 if (!io_op_defs[i].not_supported)
10317 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10318 }
10319 p->ops_len = i;
10320
10321 ret = 0;
10322 if (copy_to_user(arg, p, size))
10323 ret = -EFAULT;
10324out:
10325 kfree(p);
10326 return ret;
10327}
10328
Jens Axboe071698e2020-01-28 10:04:42 -070010329static int io_register_personality(struct io_ring_ctx *ctx)
10330{
Jens Axboe4379bf82021-02-15 13:40:22 -070010331 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010332 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010333 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010334
Jens Axboe4379bf82021-02-15 13:40:22 -070010335 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010336
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010337 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10338 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010339 if (ret < 0) {
10340 put_cred(creds);
10341 return ret;
10342 }
10343 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010344}
10345
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010346static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10347 unsigned int nr_args)
10348{
10349 struct io_uring_restriction *res;
10350 size_t size;
10351 int i, ret;
10352
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010353 /* Restrictions allowed only if rings started disabled */
10354 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10355 return -EBADFD;
10356
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010357 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010358 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010359 return -EBUSY;
10360
10361 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10362 return -EINVAL;
10363
10364 size = array_size(nr_args, sizeof(*res));
10365 if (size == SIZE_MAX)
10366 return -EOVERFLOW;
10367
10368 res = memdup_user(arg, size);
10369 if (IS_ERR(res))
10370 return PTR_ERR(res);
10371
10372 ret = 0;
10373
10374 for (i = 0; i < nr_args; i++) {
10375 switch (res[i].opcode) {
10376 case IORING_RESTRICTION_REGISTER_OP:
10377 if (res[i].register_op >= IORING_REGISTER_LAST) {
10378 ret = -EINVAL;
10379 goto out;
10380 }
10381
10382 __set_bit(res[i].register_op,
10383 ctx->restrictions.register_op);
10384 break;
10385 case IORING_RESTRICTION_SQE_OP:
10386 if (res[i].sqe_op >= IORING_OP_LAST) {
10387 ret = -EINVAL;
10388 goto out;
10389 }
10390
10391 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10392 break;
10393 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10394 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10395 break;
10396 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10397 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10398 break;
10399 default:
10400 ret = -EINVAL;
10401 goto out;
10402 }
10403 }
10404
10405out:
10406 /* Reset all restrictions if an error happened */
10407 if (ret != 0)
10408 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10409 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010410 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010411
10412 kfree(res);
10413 return ret;
10414}
10415
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010416static int io_register_enable_rings(struct io_ring_ctx *ctx)
10417{
10418 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10419 return -EBADFD;
10420
10421 if (ctx->restrictions.registered)
10422 ctx->restricted = 1;
10423
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010424 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10425 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10426 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010427 return 0;
10428}
10429
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010430static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010431 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010432 unsigned nr_args)
10433{
10434 __u32 tmp;
10435 int err;
10436
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010437 if (up->resv)
10438 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010439 if (check_add_overflow(up->offset, nr_args, &tmp))
10440 return -EOVERFLOW;
10441 err = io_rsrc_node_switch_start(ctx);
10442 if (err)
10443 return err;
10444
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010445 switch (type) {
10446 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010447 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010448 case IORING_RSRC_BUFFER:
10449 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010450 }
10451 return -EINVAL;
10452}
10453
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010454static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10455 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010456{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010457 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010458
10459 if (!nr_args)
10460 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010461 memset(&up, 0, sizeof(up));
10462 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10463 return -EFAULT;
10464 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10465}
10466
10467static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010468 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010469{
10470 struct io_uring_rsrc_update2 up;
10471
10472 if (size != sizeof(up))
10473 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010474 if (copy_from_user(&up, arg, sizeof(up)))
10475 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010476 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010477 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010478 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010479}
10480
Pavel Begunkov792e3582021-04-25 14:32:21 +010010481static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010482 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010483{
10484 struct io_uring_rsrc_register rr;
10485
10486 /* keep it extendible */
10487 if (size != sizeof(rr))
10488 return -EINVAL;
10489
10490 memset(&rr, 0, sizeof(rr));
10491 if (copy_from_user(&rr, arg, size))
10492 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010493 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010494 return -EINVAL;
10495
Pavel Begunkov992da012021-06-10 16:37:37 +010010496 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010497 case IORING_RSRC_FILE:
10498 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10499 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010500 case IORING_RSRC_BUFFER:
10501 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10502 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010503 }
10504 return -EINVAL;
10505}
10506
Jens Axboefe764212021-06-17 10:19:54 -060010507static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10508 unsigned len)
10509{
10510 struct io_uring_task *tctx = current->io_uring;
10511 cpumask_var_t new_mask;
10512 int ret;
10513
10514 if (!tctx || !tctx->io_wq)
10515 return -EINVAL;
10516
10517 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10518 return -ENOMEM;
10519
10520 cpumask_clear(new_mask);
10521 if (len > cpumask_size())
10522 len = cpumask_size();
10523
10524 if (copy_from_user(new_mask, arg, len)) {
10525 free_cpumask_var(new_mask);
10526 return -EFAULT;
10527 }
10528
10529 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10530 free_cpumask_var(new_mask);
10531 return ret;
10532}
10533
10534static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10535{
10536 struct io_uring_task *tctx = current->io_uring;
10537
10538 if (!tctx || !tctx->io_wq)
10539 return -EINVAL;
10540
10541 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10542}
10543
Jens Axboe2e480052021-08-27 11:33:19 -060010544static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10545 void __user *arg)
10546{
Jens Axboefa846932021-09-01 14:15:59 -060010547 struct io_uring_task *tctx = NULL;
10548 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010549 __u32 new_count[2];
10550 int i, ret;
10551
Jens Axboe2e480052021-08-27 11:33:19 -060010552 if (copy_from_user(new_count, arg, sizeof(new_count)))
10553 return -EFAULT;
10554 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10555 if (new_count[i] > INT_MAX)
10556 return -EINVAL;
10557
Jens Axboefa846932021-09-01 14:15:59 -060010558 if (ctx->flags & IORING_SETUP_SQPOLL) {
10559 sqd = ctx->sq_data;
10560 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010561 /*
10562 * Observe the correct sqd->lock -> ctx->uring_lock
10563 * ordering. Fine to drop uring_lock here, we hold
10564 * a ref to the ctx.
10565 */
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010566 refcount_inc(&sqd->refs);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010567 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010568 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010569 mutex_lock(&ctx->uring_lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010570 if (sqd->thread)
10571 tctx = sqd->thread->io_uring;
Jens Axboefa846932021-09-01 14:15:59 -060010572 }
10573 } else {
10574 tctx = current->io_uring;
10575 }
10576
10577 ret = -EINVAL;
10578 if (!tctx || !tctx->io_wq)
10579 goto err;
10580
Jens Axboe2e480052021-08-27 11:33:19 -060010581 ret = io_wq_max_workers(tctx->io_wq, new_count);
10582 if (ret)
Jens Axboefa846932021-09-01 14:15:59 -060010583 goto err;
10584
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010585 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010586 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010587 io_put_sq_data(sqd);
10588 }
Jens Axboe2e480052021-08-27 11:33:19 -060010589
10590 if (copy_to_user(arg, new_count, sizeof(new_count)))
10591 return -EFAULT;
10592
10593 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010594err:
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010595 if (sqd) {
Jens Axboefa846932021-09-01 14:15:59 -060010596 mutex_unlock(&sqd->lock);
Jens Axboe41d3a6b2021-09-13 13:08:51 -060010597 io_put_sq_data(sqd);
10598 }
Jens Axboefa846932021-09-01 14:15:59 -060010599 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010600}
10601
Jens Axboe071698e2020-01-28 10:04:42 -070010602static bool io_register_op_must_quiesce(int op)
10603{
10604 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010605 case IORING_REGISTER_BUFFERS:
10606 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010607 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010608 case IORING_UNREGISTER_FILES:
10609 case IORING_REGISTER_FILES_UPDATE:
10610 case IORING_REGISTER_PROBE:
10611 case IORING_REGISTER_PERSONALITY:
10612 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010613 case IORING_REGISTER_FILES2:
10614 case IORING_REGISTER_FILES_UPDATE2:
10615 case IORING_REGISTER_BUFFERS2:
10616 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010617 case IORING_REGISTER_IOWQ_AFF:
10618 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010619 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010620 return false;
10621 default:
10622 return true;
10623 }
10624}
10625
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010626static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10627{
10628 long ret;
10629
10630 percpu_ref_kill(&ctx->refs);
10631
10632 /*
10633 * Drop uring mutex before waiting for references to exit. If another
10634 * thread is currently inside io_uring_enter() it might need to grab the
10635 * uring_lock to make progress. If we hold it here across the drain
10636 * wait, then we can deadlock. It's safe to drop the mutex here, since
10637 * no new references will come in after we've killed the percpu ref.
10638 */
10639 mutex_unlock(&ctx->uring_lock);
10640 do {
10641 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10642 if (!ret)
10643 break;
10644 ret = io_run_task_work_sig();
10645 } while (ret >= 0);
10646 mutex_lock(&ctx->uring_lock);
10647
10648 if (ret)
10649 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10650 return ret;
10651}
10652
Jens Axboeedafcce2019-01-09 09:16:05 -070010653static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10654 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010655 __releases(ctx->uring_lock)
10656 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010657{
10658 int ret;
10659
Jens Axboe35fa71a2019-04-22 10:23:23 -060010660 /*
10661 * We're inside the ring mutex, if the ref is already dying, then
10662 * someone else killed the ctx or is already going through
10663 * io_uring_register().
10664 */
10665 if (percpu_ref_is_dying(&ctx->refs))
10666 return -ENXIO;
10667
Pavel Begunkov75c40212021-04-15 13:07:40 +010010668 if (ctx->restricted) {
10669 if (opcode >= IORING_REGISTER_LAST)
10670 return -EINVAL;
10671 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10672 if (!test_bit(opcode, ctx->restrictions.register_op))
10673 return -EACCES;
10674 }
10675
Jens Axboe071698e2020-01-28 10:04:42 -070010676 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010677 ret = io_ctx_quiesce(ctx);
10678 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010679 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010680 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010681
10682 switch (opcode) {
10683 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010684 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010685 break;
10686 case IORING_UNREGISTER_BUFFERS:
10687 ret = -EINVAL;
10688 if (arg || nr_args)
10689 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010690 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010691 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010692 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010693 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010694 break;
10695 case IORING_UNREGISTER_FILES:
10696 ret = -EINVAL;
10697 if (arg || nr_args)
10698 break;
10699 ret = io_sqe_files_unregister(ctx);
10700 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010701 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010702 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010703 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010704 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010705 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010706 ret = -EINVAL;
10707 if (nr_args != 1)
10708 break;
10709 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010710 if (ret)
10711 break;
10712 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10713 ctx->eventfd_async = 1;
10714 else
10715 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010716 break;
10717 case IORING_UNREGISTER_EVENTFD:
10718 ret = -EINVAL;
10719 if (arg || nr_args)
10720 break;
10721 ret = io_eventfd_unregister(ctx);
10722 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010723 case IORING_REGISTER_PROBE:
10724 ret = -EINVAL;
10725 if (!arg || nr_args > 256)
10726 break;
10727 ret = io_probe(ctx, arg, nr_args);
10728 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010729 case IORING_REGISTER_PERSONALITY:
10730 ret = -EINVAL;
10731 if (arg || nr_args)
10732 break;
10733 ret = io_register_personality(ctx);
10734 break;
10735 case IORING_UNREGISTER_PERSONALITY:
10736 ret = -EINVAL;
10737 if (arg)
10738 break;
10739 ret = io_unregister_personality(ctx, nr_args);
10740 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010741 case IORING_REGISTER_ENABLE_RINGS:
10742 ret = -EINVAL;
10743 if (arg || nr_args)
10744 break;
10745 ret = io_register_enable_rings(ctx);
10746 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010747 case IORING_REGISTER_RESTRICTIONS:
10748 ret = io_register_restrictions(ctx, arg, nr_args);
10749 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010750 case IORING_REGISTER_FILES2:
10751 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010752 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010753 case IORING_REGISTER_FILES_UPDATE2:
10754 ret = io_register_rsrc_update(ctx, arg, nr_args,
10755 IORING_RSRC_FILE);
10756 break;
10757 case IORING_REGISTER_BUFFERS2:
10758 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10759 break;
10760 case IORING_REGISTER_BUFFERS_UPDATE:
10761 ret = io_register_rsrc_update(ctx, arg, nr_args,
10762 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010763 break;
Jens Axboefe764212021-06-17 10:19:54 -060010764 case IORING_REGISTER_IOWQ_AFF:
10765 ret = -EINVAL;
10766 if (!arg || !nr_args)
10767 break;
10768 ret = io_register_iowq_aff(ctx, arg, nr_args);
10769 break;
10770 case IORING_UNREGISTER_IOWQ_AFF:
10771 ret = -EINVAL;
10772 if (arg || nr_args)
10773 break;
10774 ret = io_unregister_iowq_aff(ctx);
10775 break;
Jens Axboe2e480052021-08-27 11:33:19 -060010776 case IORING_REGISTER_IOWQ_MAX_WORKERS:
10777 ret = -EINVAL;
10778 if (!arg || nr_args != 2)
10779 break;
10780 ret = io_register_iowq_max_workers(ctx, arg);
10781 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010782 default:
10783 ret = -EINVAL;
10784 break;
10785 }
10786
Jens Axboe071698e2020-01-28 10:04:42 -070010787 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010788 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010789 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010790 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010791 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010792 return ret;
10793}
10794
10795SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10796 void __user *, arg, unsigned int, nr_args)
10797{
10798 struct io_ring_ctx *ctx;
10799 long ret = -EBADF;
10800 struct fd f;
10801
10802 f = fdget(fd);
10803 if (!f.file)
10804 return -EBADF;
10805
10806 ret = -EOPNOTSUPP;
10807 if (f.file->f_op != &io_uring_fops)
10808 goto out_fput;
10809
10810 ctx = f.file->private_data;
10811
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010812 io_run_task_work();
10813
Jens Axboeedafcce2019-01-09 09:16:05 -070010814 mutex_lock(&ctx->uring_lock);
10815 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10816 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010817 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10818 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010819out_fput:
10820 fdput(f);
10821 return ret;
10822}
10823
Jens Axboe2b188cc2019-01-07 10:46:33 -070010824static int __init io_uring_init(void)
10825{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010826#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10827 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10828 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10829} while (0)
10830
10831#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10832 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10833 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10834 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10835 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10836 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10837 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10838 BUILD_BUG_SQE_ELEM(8, __u64, off);
10839 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10840 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010841 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010842 BUILD_BUG_SQE_ELEM(24, __u32, len);
10843 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10844 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10845 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10846 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010847 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10848 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010849 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10850 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10851 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10852 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10853 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10854 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10855 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10856 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010857 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010858 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10859 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010860 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010861 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010862 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010863 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010864
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010865 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10866 sizeof(struct io_uring_rsrc_update));
10867 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10868 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010010869
10870 /* ->buf_index is u16 */
10871 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
10872
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010873 /* should fit into one byte */
10874 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
10875
Jens Axboed3656342019-12-18 09:50:26 -070010876 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080010877 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010878
Jens Axboe91f245d2021-02-09 13:48:50 -070010879 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10880 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010881 return 0;
10882};
10883__initcall(io_uring_init);