blob: 388754b2478545b92d1c233512f92344fb8c4914 [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>
Paul Moore5bd21822021-02-16 19:46:48 -050082#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020084#define CREATE_TRACE_POINTS
85#include <trace/events/io_uring.h>
86
Jens Axboe2b188cc2019-01-07 10:46:33 -070087#include <uapi/linux/io_uring.h>
88
89#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060090#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
Daniel Xu5277dea2019-09-14 14:23:45 -070092#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060093#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Olivier Langlois4ce8ad92021-06-23 11:50:18 -070094#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
Jens Axboe65e19f52019-10-26 07:20:21 -060095
wangyangbo187f08c2021-08-19 13:56:57 +080096/* only define max */
Pavel Begunkov042b0d82021-08-09 13:04:01 +010097#define IORING_MAX_FIXED_FILES (1U << 15)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020098#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
99 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100
wangyangbo187f08c2021-08-19 13:56:57 +0800101#define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100102#define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
103#define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
104
Pavel Begunkov489809e2021-05-14 12:06:44 +0100105#define IORING_MAX_REG_BUFFERS (1U << 14)
106
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000107#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
108 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
109 IOSQE_BUFFER_SELECT)
Pavel Begunkovc8543572021-06-17 18:14:04 +0100110#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
111 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000112
Pavel Begunkov09899b12021-06-14 02:36:22 +0100113#define IO_TCTX_REFS_CACHE_NR (1U << 10)
114
Jens Axboe2b188cc2019-01-07 10:46:33 -0700115struct io_uring {
116 u32 head ____cacheline_aligned_in_smp;
117 u32 tail ____cacheline_aligned_in_smp;
118};
119
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 * This data is shared with the application through the mmap at offsets
122 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 *
124 * The offsets to the member fields are published through struct
125 * io_sqring_offsets when calling io_uring_setup.
126 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000127struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
129 * Head and tail offsets into the ring; the offsets need to be
130 * masked to get valid indices.
131 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 * The kernel controls head of the sq ring and the tail of the cq ring,
133 * and the application controls tail of the sq ring and the head of the
134 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000138 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200139 * ring_entries - 1)
140 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000141 u32 sq_ring_mask, cq_ring_mask;
142 /* Ring sizes (constant, power of 2) */
143 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Number of invalid entries dropped by the kernel due to
146 * invalid index stored in array
147 *
148 * Written by the kernel, shouldn't be modified by the
149 * application (i.e. get number of "new events" by comparing to
150 * cached value).
151 *
152 * After a new SQ head value was read by the application this
153 * counter includes all submissions that were dropped reaching
154 * the new SQ head (and possibly more).
155 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000156 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200157 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200158 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 *
160 * Written by the kernel, shouldn't be modified by the
161 * application.
162 *
163 * The application needs a full memory barrier before checking
164 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200168 * Runtime CQ flags
169 *
170 * Written by the application, shouldn't be modified by the
171 * kernel.
172 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100173 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200174 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200175 * Number of completion events lost because the queue was full;
176 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800177 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200178 * the completion queue.
179 *
180 * Written by the kernel, shouldn't be modified by the
181 * application (i.e. get number of "new events" by comparing to
182 * cached value).
183 *
184 * As completion events come in out of order this counter is not
185 * ordered with any other data.
186 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000187 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200188 /*
189 * Ring buffer of completion events.
190 *
191 * The kernel writes completion events fresh every time they are
192 * produced, so the application is allowed to modify pending
193 * entries.
194 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000195 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196};
197
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000198enum io_uring_cmd_flags {
199 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000200 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000201};
202
Jens Axboeedafcce2019-01-09 09:16:05 -0700203struct io_mapped_ubuf {
204 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100205 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700206 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600207 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100208 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700209};
210
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000211struct io_ring_ctx;
212
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000213struct io_overflow_cqe {
214 struct io_uring_cqe cqe;
215 struct list_head list;
216};
217
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100218struct io_fixed_file {
219 /* file * with additional FFS_* flags */
220 unsigned long file_ptr;
221};
222
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000223struct io_rsrc_put {
224 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100225 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000226 union {
227 void *rsrc;
228 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100229 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000230 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000231};
232
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100233struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100234 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700235};
236
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100237struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800238 struct percpu_ref refs;
239 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000240 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100241 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600242 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000243 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800244};
245
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100246typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
247
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100248struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700249 struct io_ring_ctx *ctx;
250
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100251 u64 **tags;
252 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100253 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100254 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700255 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800256 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700257};
258
Jens Axboe5a2e7452020-02-23 16:23:11 -0700259struct io_buffer {
260 struct list_head list;
261 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300262 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700263 __u16 bid;
264};
265
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200266struct io_restriction {
267 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
268 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
269 u8 sqe_flags_allowed;
270 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200271 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200272};
273
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700274enum {
275 IO_SQ_THREAD_SHOULD_STOP = 0,
276 IO_SQ_THREAD_SHOULD_PARK,
277};
278
Jens Axboe534ca6d2020-09-02 13:52:19 -0600279struct io_sq_data {
280 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000281 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000282 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600283
284 /* ctx's that are using this sqd */
285 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600286
Jens Axboe534ca6d2020-09-02 13:52:19 -0600287 struct task_struct *thread;
288 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800289
290 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700291 int sq_cpu;
292 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700293 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700294
295 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700296 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600297};
298
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000299#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000300#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000301#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000302
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000303struct io_submit_link {
304 struct io_kiocb *head;
305 struct io_kiocb *last;
306};
307
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000308struct io_submit_state {
309 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000310 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000311
312 /*
313 * io_kiocb alloc cache
314 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000315 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000316 unsigned int free_reqs;
317
318 bool plug_started;
319
320 /*
321 * Batch completion logic
322 */
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +0100323 struct io_kiocb *compl_reqs[IO_COMPL_BATCH];
324 unsigned int compl_nr;
325 /* inline/task_work completion list, under ->uring_lock */
326 struct list_head free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000327
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000328 unsigned int ios_left;
329};
330
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100332 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333 struct {
334 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100336 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700337 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800338 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800339 unsigned int drain_next: 1;
340 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200341 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100342 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100343 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100344 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100346 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100347 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100348 struct mutex uring_lock;
349
Hristo Venev75b28af2019-08-26 17:23:46 +0000350 /*
351 * Ring buffer of indices into array of io_uring_sqe, which is
352 * mmapped by the application using the IORING_OFF_SQES offset.
353 *
354 * This indirection could e.g. be used to assign fixed
355 * io_uring_sqe entries to operations and only submit them to
356 * the queue when needed.
357 *
358 * The kernel modifies neither the indices array nor the entries
359 * array.
360 */
361 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100362 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363 unsigned cached_sq_head;
364 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600365 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100366
367 /*
368 * Fixed resources fast path, should be accessed only under
369 * uring_lock, and updated through io_uring_register(2)
370 */
371 struct io_rsrc_node *rsrc_node;
372 struct io_file_table file_table;
373 unsigned nr_user_files;
374 unsigned nr_user_bufs;
375 struct io_mapped_ubuf **user_bufs;
376
377 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600378 struct list_head timeout_list;
Pavel Begunkovef9dd632021-08-28 19:54:38 -0600379 struct list_head ltimeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700380 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100381 struct xarray io_buffers;
382 struct xarray personalities;
383 u32 pers_next;
384 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700385 } ____cacheline_aligned_in_smp;
386
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100387 /* IRQ completion list, under ->completion_lock */
388 struct list_head locked_free_list;
389 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700390
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100391 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600392 struct io_sq_data *sq_data; /* if using sq thread polling */
393
Jens Axboe90554202020-09-03 12:12:41 -0600394 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600395 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000396
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100397 unsigned long check_cq_overflow;
398
Jens Axboe206aefd2019-11-07 18:27:42 -0700399 struct {
400 unsigned cached_cq_tail;
401 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700402 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100403 struct wait_queue_head poll_wait;
404 struct wait_queue_head cq_wait;
405 unsigned cq_extra;
406 atomic_t cq_timeouts;
407 struct fasync_struct *cq_fasync;
408 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700409 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410
411 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700412 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700413
Jens Axboe89850fc2021-08-10 15:11:51 -0600414 spinlock_t timeout_lock;
415
Jens Axboedef596e2019-01-09 08:59:42 -0700416 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300417 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700418 * io_uring instances that don't use IORING_SETUP_SQPOLL.
419 * For SQPOLL, only the single threaded io_sq_thread() will
420 * manipulate the list, hence no extra locking is needed there.
421 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300422 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700423 struct hlist_head *cancel_hash;
424 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800425 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700426 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600427
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200428 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700429
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100430 /* slow path rsrc auxilary data, used by update/register */
431 struct {
432 struct io_rsrc_node *rsrc_backup_node;
433 struct io_mapped_ubuf *dummy_ubuf;
434 struct io_rsrc_data *file_data;
435 struct io_rsrc_data *buf_data;
436
437 struct delayed_work rsrc_put_work;
438 struct llist_head rsrc_put_llist;
439 struct list_head rsrc_ref_list;
440 spinlock_t rsrc_ref_lock;
441 };
442
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700443 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100444 struct {
445 #if defined(CONFIG_UNIX)
446 struct socket *ring_sock;
447 #endif
448 /* hashed buffered write serialization */
449 struct io_wq_hash *hash_map;
450
451 /* Only used for accounting purposes */
452 struct user_struct *user;
453 struct mm_struct *mm_account;
454
455 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100456 struct llist_head fallback_llist;
457 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100458 struct work_struct exit_work;
459 struct list_head tctx_list;
460 struct completion ref_comp;
461 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462};
463
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100464struct io_uring_task {
465 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100466 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100467 struct xarray xa;
468 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100469 const struct io_ring_ctx *last;
470 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100472 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100473 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100474
475 spinlock_t task_lock;
476 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100477 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100478 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100479};
480
Jens Axboe09bb8392019-03-13 12:39:28 -0600481/*
482 * First field must be the file pointer in all the
483 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
484 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700485struct io_poll_iocb {
486 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000487 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700488 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600489 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700490 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700491 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700492};
493
Pavel Begunkov9d805892021-04-13 02:58:40 +0100494struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000495 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100496 u64 old_user_data;
497 u64 new_user_data;
498 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600499 bool update_events;
500 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000501};
502
Jens Axboeb5dba592019-12-11 14:02:38 -0700503struct io_close {
504 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700505 int fd;
506};
507
Jens Axboead8a48a2019-11-15 08:49:11 -0700508struct io_timeout_data {
509 struct io_kiocb *req;
510 struct hrtimer timer;
511 struct timespec64 ts;
512 enum hrtimer_mode mode;
Jens Axboe50c1df22021-08-27 17:11:06 -0600513 u32 flags;
Jens Axboead8a48a2019-11-15 08:49:11 -0700514};
515
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700516struct io_accept {
517 struct file *file;
518 struct sockaddr __user *addr;
519 int __user *addr_len;
520 int flags;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100521 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600522 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700523};
524
525struct io_sync {
526 struct file *file;
527 loff_t len;
528 loff_t off;
529 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700530 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700531};
532
Jens Axboefbf23842019-12-17 18:45:56 -0700533struct io_cancel {
534 struct file *file;
535 u64 addr;
536};
537
Jens Axboeb29472e2019-12-17 18:50:29 -0700538struct io_timeout {
539 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300540 u32 off;
541 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300542 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000543 /* head of the link, used by linked timeouts only */
544 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600545 /* for linked completions */
546 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700547};
548
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100549struct io_timeout_rem {
550 struct file *file;
551 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000552
553 /* timeout update */
554 struct timespec64 ts;
555 u32 flags;
Pavel Begunkovf1042b62021-08-28 19:54:39 -0600556 bool ltimeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100557};
558
Jens Axboe9adbd452019-12-20 08:45:55 -0700559struct io_rw {
560 /* NOTE: kiocb has the file as the first member, so don't do it here */
561 struct kiocb kiocb;
562 u64 addr;
563 u64 len;
564};
565
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700566struct io_connect {
567 struct file *file;
568 struct sockaddr __user *addr;
569 int addr_len;
570};
571
Jens Axboee47293f2019-12-20 08:58:21 -0700572struct io_sr_msg {
573 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700574 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100575 struct compat_msghdr __user *umsg_compat;
576 struct user_msghdr __user *umsg;
577 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700578 };
Jens Axboee47293f2019-12-20 08:58:21 -0700579 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700580 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700581 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700582 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700583};
584
Jens Axboe15b71ab2019-12-11 11:20:36 -0700585struct io_open {
586 struct file *file;
587 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100588 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700589 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700590 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600591 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700592};
593
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000594struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700595 struct file *file;
596 u64 arg;
597 u32 nr_args;
598 u32 offset;
599};
600
Jens Axboe4840e412019-12-25 22:03:45 -0700601struct io_fadvise {
602 struct file *file;
603 u64 offset;
604 u32 len;
605 u32 advice;
606};
607
Jens Axboec1ca7572019-12-25 22:18:28 -0700608struct io_madvise {
609 struct file *file;
610 u64 addr;
611 u32 len;
612 u32 advice;
613};
614
Jens Axboe3e4827b2020-01-08 15:18:09 -0700615struct io_epoll {
616 struct file *file;
617 int epfd;
618 int op;
619 int fd;
620 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621};
622
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300623struct io_splice {
624 struct file *file_out;
625 struct file *file_in;
626 loff_t off_out;
627 loff_t off_in;
628 u64 len;
629 unsigned int flags;
630};
631
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632struct io_provide_buf {
633 struct file *file;
634 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100635 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700636 __u32 bgid;
637 __u16 nbufs;
638 __u16 bid;
639};
640
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700641struct io_statx {
642 struct file *file;
643 int dfd;
644 unsigned int mask;
645 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700646 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700647 struct statx __user *buffer;
648};
649
Jens Axboe36f4fa62020-09-05 11:14:22 -0600650struct io_shutdown {
651 struct file *file;
652 int how;
653};
654
Jens Axboe80a261f2020-09-28 14:23:58 -0600655struct io_rename {
656 struct file *file;
657 int old_dfd;
658 int new_dfd;
659 struct filename *oldpath;
660 struct filename *newpath;
661 int flags;
662};
663
Jens Axboe14a11432020-09-28 14:27:37 -0600664struct io_unlink {
665 struct file *file;
666 int dfd;
667 int flags;
668 struct filename *filename;
669};
670
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700671struct io_mkdir {
672 struct file *file;
673 int dfd;
674 umode_t mode;
675 struct filename *filename;
676};
677
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700678struct io_symlink {
679 struct file *file;
680 int new_dfd;
681 struct filename *oldpath;
682 struct filename *newpath;
683};
684
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700685struct io_hardlink {
686 struct file *file;
687 int old_dfd;
688 int new_dfd;
689 struct filename *oldpath;
690 struct filename *newpath;
691 int flags;
692};
693
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300694struct io_completion {
695 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000696 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300697};
698
Jens Axboef499a022019-12-02 16:28:46 -0700699struct io_async_connect {
700 struct sockaddr_storage address;
701};
702
Jens Axboe03b12302019-12-02 18:50:25 -0700703struct io_async_msghdr {
704 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000705 /* points to an allocated iov, if NULL we use fast_iov instead */
706 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700707 struct sockaddr __user *uaddr;
708 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700709 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700710};
711
Jens Axboef67676d2019-12-02 11:03:47 -0700712struct io_async_rw {
713 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600714 const struct iovec *free_iovec;
715 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600716 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600717 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700718};
719
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300720enum {
721 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
722 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
723 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
724 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
725 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700726 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300727
Pavel Begunkovdddca222021-04-27 16:13:52 +0100728 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100729 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300730 REQ_F_INFLIGHT_BIT,
731 REQ_F_CUR_POS_BIT,
732 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300733 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300734 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700735 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700736 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000737 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600738 REQ_F_REISSUE_BIT,
Pavel Begunkov8c130822021-03-22 01:58:32 +0000739 REQ_F_DONT_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100740 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100741 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100742 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700743 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100744 REQ_F_NOWAIT_READ_BIT,
745 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700746 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700747
748 /* not a real bit, just to check we're not overflowing the space */
749 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300750};
751
752enum {
753 /* ctx owns file */
754 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
755 /* drain existing IO first */
756 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
757 /* linked sqes */
758 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
759 /* doesn't sever on completion < 0 */
760 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
761 /* IOSQE_ASYNC */
762 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700763 /* IOSQE_BUFFER_SELECT */
764 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300765
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300766 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100767 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000768 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300769 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
770 /* read/write uses file position */
771 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
772 /* must not punt to workers */
773 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100774 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300775 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300776 /* needs cleanup */
777 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700778 /* already went through poll handler */
779 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700780 /* buffer already selected */
781 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000782 /* completion is deferred through io_comp_state */
783 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600784 /* caller should reissue async */
785 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov8c130822021-03-22 01:58:32 +0000786 /* don't attempt request reissue, see io_rw_reissue() */
787 REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700788 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100789 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700790 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100791 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700792 /* regular file */
793 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100794 /* has creds assigned */
795 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100796 /* skip refcounting if not set */
797 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100798 /* there is a linked timeout that has to be armed */
799 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700800};
801
802struct async_poll {
803 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600804 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300805};
806
Pavel Begunkovf237c302021-08-18 12:42:46 +0100807typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100808
Jens Axboe7cbf1722021-02-10 00:03:20 +0000809struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100810 union {
811 struct io_wq_work_node node;
812 struct llist_node fallback_node;
813 };
814 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000815};
816
Pavel Begunkov992da012021-06-10 16:37:37 +0100817enum {
818 IORING_RSRC_FILE = 0,
819 IORING_RSRC_BUFFER = 1,
820};
821
Jens Axboe09bb8392019-03-13 12:39:28 -0600822/*
823 * NOTE! Each of the iocb union members has the file pointer
824 * as the first entry in their struct definition. So you can
825 * access the file pointer through any of the sub-structs,
826 * or directly as just 'ki_filp' in this struct.
827 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700829 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600830 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700831 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700832 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100833 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700834 struct io_accept accept;
835 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700836 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700837 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100838 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700839 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700840 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700841 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700842 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000843 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700844 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700845 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700846 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300847 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700848 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700849 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600850 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600851 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600852 struct io_unlink unlink;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +0700853 struct io_mkdir mkdir;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +0700854 struct io_symlink symlink;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +0700855 struct io_hardlink hardlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300856 /* use only after cleaning per-op data, see io_clean_op() */
857 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700858 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859
Jens Axboee8c2bc12020-08-15 18:44:09 -0700860 /* opcode allocated if it needs to store data for async defer */
861 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700862 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800863 /* polled IO has completed */
864 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700866 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300867 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700868
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300869 struct io_ring_ctx *ctx;
870 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700871 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300872 struct task_struct *task;
873 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700874
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000875 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000876 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700877
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100878 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300879 struct list_head inflight_entry;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100880 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300881 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
882 struct hlist_node hash_node;
883 struct async_poll *apoll;
884 struct io_wq_work work;
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100885 const struct cred *creds;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +0100886
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100887 /* store used ubuf, so we can prevent reloading */
888 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700889};
890
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000891struct io_tctx_node {
892 struct list_head ctx_node;
893 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000894 struct io_ring_ctx *ctx;
895};
896
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300897struct io_defer_entry {
898 struct list_head list;
899 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300900 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300901};
902
Jens Axboed3656342019-12-18 09:50:26 -0700903struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700904 /* needs req->file assigned */
905 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700906 /* hash wq insertion if file is a regular file */
907 unsigned hash_reg_file : 1;
908 /* unbound wq insertion if file is a non-regular file */
909 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700910 /* opcode is not supported by this kernel */
911 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700912 /* set if opcode supports polled "wait" */
913 unsigned pollin : 1;
914 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700915 /* op supports buffer selection */
916 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000917 /* do prep async if is going to be punted */
918 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600919 /* should block plug */
920 unsigned plug : 1;
Paul Moore5bd21822021-02-16 19:46:48 -0500921 /* skip auditing */
922 unsigned audit_skip : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700923 /* size of async data needed, if any */
924 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700925};
926
Jens Axboe09186822020-10-13 15:01:40 -0600927static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_NOP] = {},
929 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700930 .needs_file = 1,
931 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700932 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700933 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000934 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600935 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500936 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700937 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700938 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300939 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700940 .needs_file = 1,
941 .hash_reg_file = 1,
942 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700943 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000944 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600945 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500946 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700947 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700948 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700950 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500951 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700954 .needs_file = 1,
955 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700956 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600957 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500958 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700959 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700960 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300961 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700962 .needs_file = 1,
963 .hash_reg_file = 1,
964 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700965 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600966 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500967 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700968 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700969 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300970 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700971 .needs_file = 1,
972 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500973 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700974 },
Paul Moore5bd21822021-02-16 19:46:48 -0500975 [IORING_OP_POLL_REMOVE] = {
976 .audit_skip = 1,
977 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300978 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700979 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -0500980 .audit_skip = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700981 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300982 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700983 .needs_file = 1,
984 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700985 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000986 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700987 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700988 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300989 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700990 .needs_file = 1,
991 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700992 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700993 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000994 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700995 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700996 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300997 [IORING_OP_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -0500998 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700999 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001000 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001001 [IORING_OP_TIMEOUT_REMOVE] = {
1002 /* used by timeout updates' prep() */
Paul Moore5bd21822021-02-16 19:46:48 -05001003 .audit_skip = 1,
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00001004 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001005 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001006 .needs_file = 1,
1007 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001008 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -07001009 },
Paul Moore5bd21822021-02-16 19:46:48 -05001010 [IORING_OP_ASYNC_CANCEL] = {
1011 .audit_skip = 1,
1012 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001013 [IORING_OP_LINK_TIMEOUT] = {
Paul Moore5bd21822021-02-16 19:46:48 -05001014 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001015 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -07001016 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001017 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -07001018 .needs_file = 1,
1019 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001020 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +00001021 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001022 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -07001023 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001024 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -07001025 .needs_file = 1,
1026 },
Jens Axboe44526be2021-02-15 13:32:18 -07001027 [IORING_OP_OPENAT] = {},
1028 [IORING_OP_CLOSE] = {},
Paul Moore5bd21822021-02-16 19:46:48 -05001029 [IORING_OP_FILES_UPDATE] = {
1030 .audit_skip = 1,
1031 },
1032 [IORING_OP_STATX] = {
1033 .audit_skip = 1,
1034 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001035 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001036 .needs_file = 1,
1037 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001038 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001039 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001040 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001041 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001042 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001043 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001044 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -07001045 .needs_file = 1,
Jens Axboe7b3188e2021-08-30 19:37:41 -06001046 .hash_reg_file = 1,
Jens Axboe3a6820f2019-12-22 15:19:35 -07001047 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001048 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -06001049 .plug = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001050 .audit_skip = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -07001051 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -07001052 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001053 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001054 .needs_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001055 .audit_skip = 1,
Jens Axboe4840e412019-12-25 22:03:45 -07001056 },
Jens Axboe44526be2021-02-15 13:32:18 -07001057 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001058 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001059 .needs_file = 1,
1060 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001061 .pollout = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001062 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001063 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001064 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001065 .needs_file = 1,
1066 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001067 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001068 .buffer_select = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001069 .audit_skip = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001070 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001071 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001072 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001073 [IORING_OP_EPOLL_CTL] = {
1074 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001075 .audit_skip = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001076 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001077 [IORING_OP_SPLICE] = {
1078 .needs_file = 1,
1079 .hash_reg_file = 1,
1080 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001081 .audit_skip = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001082 },
Paul Moore5bd21822021-02-16 19:46:48 -05001083 [IORING_OP_PROVIDE_BUFFERS] = {
1084 .audit_skip = 1,
1085 },
1086 [IORING_OP_REMOVE_BUFFERS] = {
1087 .audit_skip = 1,
1088 },
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001089 [IORING_OP_TEE] = {
1090 .needs_file = 1,
1091 .hash_reg_file = 1,
1092 .unbound_nonreg_file = 1,
Paul Moore5bd21822021-02-16 19:46:48 -05001093 .audit_skip = 1,
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001094 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001095 [IORING_OP_SHUTDOWN] = {
1096 .needs_file = 1,
1097 },
Jens Axboe44526be2021-02-15 13:32:18 -07001098 [IORING_OP_RENAMEAT] = {},
1099 [IORING_OP_UNLINKAT] = {},
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07001100 [IORING_OP_MKDIRAT] = {},
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07001101 [IORING_OP_SYMLINKAT] = {},
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07001102 [IORING_OP_LINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001103};
1104
Pavel Begunkov0756a862021-08-15 10:40:25 +01001105/* requests with any of those set should undergo io_disarm_next() */
1106#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1107
Pavel Begunkov7a612352021-03-09 00:37:59 +00001108static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001109static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001110static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1111 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001112 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001113static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001114
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001115static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1116 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001117static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001118static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001119static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001120static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001121static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001122 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001123 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001124static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001125static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001126 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b942021-02-10 00:03:22 +00001127static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001128static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001129
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001130static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01001131static void io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001132static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001133
Pavel Begunkovb9445592021-08-25 12:25:45 +01001134static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1135 unsigned int issue_flags, u32 slot_index);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06001136static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer);
Pavel Begunkovb9445592021-08-25 12:25:45 +01001137
Jens Axboe2b188cc2019-01-07 10:46:33 -07001138static struct kmem_cache *req_cachep;
1139
Jens Axboe09186822020-10-13 15:01:40 -06001140static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141
1142struct sock *io_uring_get_socket(struct file *file)
1143{
1144#if defined(CONFIG_UNIX)
1145 if (file->f_op == &io_uring_fops) {
1146 struct io_ring_ctx *ctx = file->private_data;
1147
1148 return ctx->ring_sock->sk;
1149 }
1150#endif
1151 return NULL;
1152}
1153EXPORT_SYMBOL(io_uring_get_socket);
1154
Pavel Begunkovf237c302021-08-18 12:42:46 +01001155static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1156{
1157 if (!*locked) {
1158 mutex_lock(&ctx->uring_lock);
1159 *locked = true;
1160 }
1161}
1162
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001163#define io_for_each_link(pos, head) \
1164 for (pos = (head); pos; pos = pos->link)
1165
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001166/*
1167 * Shamelessly stolen from the mm implementation of page reference checking,
1168 * see commit f958d7b528b1 for details.
1169 */
1170#define req_ref_zero_or_close_to_overflow(req) \
1171 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1172
1173static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1174{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001175 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001176 return atomic_inc_not_zero(&req->refs);
1177}
1178
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001179static inline bool req_ref_put_and_test(struct io_kiocb *req)
1180{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001181 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1182 return true;
1183
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001184 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1185 return atomic_dec_and_test(&req->refs);
1186}
1187
1188static inline void req_ref_put(struct io_kiocb *req)
1189{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001190 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001191 WARN_ON_ONCE(req_ref_put_and_test(req));
1192}
1193
1194static inline void req_ref_get(struct io_kiocb *req)
1195{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001196 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001197 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1198 atomic_inc(&req->refs);
1199}
1200
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001201static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001202{
1203 if (!(req->flags & REQ_F_REFCOUNT)) {
1204 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001205 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001206 }
1207}
1208
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001209static inline void io_req_set_refcount(struct io_kiocb *req)
1210{
1211 __io_req_set_refcount(req, 1);
1212}
1213
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001214static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001215{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001216 struct io_ring_ctx *ctx = req->ctx;
1217
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001218 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001219 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001220 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001221 }
1222}
1223
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001224static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1225{
1226 bool got = percpu_ref_tryget(ref);
1227
1228 /* already at zero, wait for ->release() */
1229 if (!got)
1230 wait_for_completion(compl);
1231 percpu_ref_resurrect(ref);
1232 if (got)
1233 percpu_ref_put(ref);
1234}
1235
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001236static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1237 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001238{
1239 struct io_kiocb *req;
1240
Pavel Begunkov68207682021-03-22 01:58:25 +00001241 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001242 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001243 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001244 return true;
1245
1246 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001247 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001248 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001249 }
1250 return false;
1251}
1252
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001253static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001254{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001255 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001256}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001257
Hao Xua8295b92021-08-27 17:46:09 +08001258static inline void req_fail_link_node(struct io_kiocb *req, int res)
1259{
1260 req_set_fail(req);
1261 req->result = res;
1262}
1263
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1265{
1266 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1267
Jens Axboe0f158b42020-05-14 17:18:39 -06001268 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269}
1270
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001271static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1272{
1273 return !req->timeout.off;
1274}
1275
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001276static void io_fallback_req_func(struct work_struct *work)
1277{
1278 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1279 fallback_work.work);
1280 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1281 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001282 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001283
1284 percpu_ref_get(&ctx->refs);
1285 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001286 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001287
Pavel Begunkovf237c302021-08-18 12:42:46 +01001288 if (locked) {
1289 if (ctx->submit_state.compl_nr)
1290 io_submit_flush_completions(ctx);
1291 mutex_unlock(&ctx->uring_lock);
1292 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001293 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001294
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001295}
1296
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1298{
1299 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001300 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001301
1302 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1303 if (!ctx)
1304 return NULL;
1305
Jens Axboe78076bb2019-12-04 19:56:40 -07001306 /*
1307 * Use 5 bits less than the max cq entries, that should give us around
1308 * 32 entries per hash list if totally full and uniformly spread.
1309 */
1310 hash_bits = ilog2(p->cq_entries);
1311 hash_bits -= 5;
1312 if (hash_bits <= 0)
1313 hash_bits = 1;
1314 ctx->cancel_hash_bits = hash_bits;
1315 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1316 GFP_KERNEL);
1317 if (!ctx->cancel_hash)
1318 goto err;
1319 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1320
Pavel Begunkov62248432021-04-28 13:11:29 +01001321 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1322 if (!ctx->dummy_ubuf)
1323 goto err;
1324 /* set invalid range, so io_import_fixed() fails meeting it */
1325 ctx->dummy_ubuf->ubuf = -1UL;
1326
Roman Gushchin21482892019-05-07 10:01:48 -07001327 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001328 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1329 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330
1331 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001332 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001333 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001334 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001335 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001336 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001337 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001338 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001340 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001341 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001342 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001343 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001344 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001345 INIT_LIST_HEAD(&ctx->timeout_list);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06001346 INIT_LIST_HEAD(&ctx->ltimeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001347 spin_lock_init(&ctx->rsrc_ref_lock);
1348 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001349 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1350 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001351 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001352 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001353 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001354 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001356err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001357 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001358 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001359 kfree(ctx);
1360 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361}
1362
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001363static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1364{
1365 struct io_rings *r = ctx->rings;
1366
1367 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1368 ctx->cq_extra--;
1369}
1370
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001371static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001372{
Jens Axboe2bc99302020-07-09 09:43:27 -06001373 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1374 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001375
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001376 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001377 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001378
Bob Liu9d858b22019-11-13 18:06:25 +08001379 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001380}
1381
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001382#define FFS_ASYNC_READ 0x1UL
1383#define FFS_ASYNC_WRITE 0x2UL
1384#ifdef CONFIG_64BIT
1385#define FFS_ISREG 0x4UL
1386#else
1387#define FFS_ISREG 0x0UL
1388#endif
1389#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1390
1391static inline bool io_req_ffs_set(struct io_kiocb *req)
1392{
1393 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1394}
1395
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001396static void io_req_track_inflight(struct io_kiocb *req)
1397{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001398 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001399 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001400 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001401 }
1402}
1403
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001404static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1405{
1406 req->flags &= ~REQ_F_LINK_TIMEOUT;
1407}
1408
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001409static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1410{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001411 if (WARN_ON_ONCE(!req->link))
1412 return NULL;
1413
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001414 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1415 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001416
1417 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001418 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001419 __io_req_set_refcount(req->link, 2);
1420 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001421}
1422
1423static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1424{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001425 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001426 return NULL;
1427 return __io_prep_linked_timeout(req);
1428}
1429
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001430static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001431{
Jens Axboed3656342019-12-18 09:50:26 -07001432 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001433 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001434
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001435 if (!(req->flags & REQ_F_CREDS)) {
1436 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001437 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001438 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001439
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001440 req->work.list.next = NULL;
1441 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001442 if (req->flags & REQ_F_FORCE_ASYNC)
1443 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1444
Jens Axboed3656342019-12-18 09:50:26 -07001445 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001446 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001447 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001448 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001449 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001450 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001451 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001452
1453 switch (req->opcode) {
1454 case IORING_OP_SPLICE:
1455 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001456 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1457 req->work.flags |= IO_WQ_WORK_UNBOUND;
1458 break;
1459 }
Jens Axboe561fb042019-10-24 07:25:42 -06001460}
1461
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001462static void io_prep_async_link(struct io_kiocb *req)
1463{
1464 struct io_kiocb *cur;
1465
Pavel Begunkov44eff402021-07-26 14:14:31 +01001466 if (req->flags & REQ_F_LINK_TIMEOUT) {
1467 struct io_ring_ctx *ctx = req->ctx;
1468
Jens Axboe79ebeae2021-08-10 15:18:27 -06001469 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001470 io_for_each_link(cur, req)
1471 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001472 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001473 } else {
1474 io_for_each_link(cur, req)
1475 io_prep_async_work(cur);
1476 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001477}
1478
Pavel Begunkovf237c302021-08-18 12:42:46 +01001479static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001480{
Jackie Liua197f662019-11-08 08:09:12 -07001481 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001482 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001483 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001484
Pavel Begunkovf237c302021-08-18 12:42:46 +01001485 /* must not take the lock, NULL it as a precaution */
1486 locked = NULL;
1487
Jens Axboe3bfe6102021-02-16 14:15:30 -07001488 BUG_ON(!tctx);
1489 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001490
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001491 /* init ->work of the whole link before punting */
1492 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001493
1494 /*
1495 * Not expected to happen, but if we do have a bug where this _can_
1496 * happen, catch it here and ensure the request is marked as
1497 * canceled. That will make io-wq go through the usual work cancel
1498 * procedure rather than attempt to run this request (or create a new
1499 * worker for it).
1500 */
1501 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1502 req->work.flags |= IO_WQ_WORK_CANCEL;
1503
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001504 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1505 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001506 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001507 if (link)
1508 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001509}
1510
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001511static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001512 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001513 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001514{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001515 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001516
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001517 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov2ae2eb92021-09-09 13:56:27 +01001518 if (status)
1519 req_set_fail(req);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001520 atomic_set(&req->ctx->cq_timeouts,
1521 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001522 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001523 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001524 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001525 }
1526}
1527
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001528static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001529{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001530 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001531 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1532 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001533
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001534 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001535 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001536 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001537 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001538 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001539 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001540}
1541
Pavel Begunkov360428f2020-05-30 14:54:17 +03001542static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001543 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001544{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001545 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001546
Jens Axboe79ebeae2021-08-10 15:18:27 -06001547 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001548 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001549 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001550 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001551 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001552
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001553 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001554 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001555
1556 /*
1557 * Since seq can easily wrap around over time, subtract
1558 * the last seq at which timeouts were flushed before comparing.
1559 * Assuming not more than 2^31-1 events have happened since,
1560 * these subtractions won't have wrapped, so we can check if
1561 * target is in [last_seq, current_seq] by comparing the two.
1562 */
1563 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1564 events_got = seq - ctx->cq_last_tm_flush;
1565 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001566 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001567
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001568 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001569 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001570 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001571 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001572 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001573}
1574
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001575static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001576{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001577 if (ctx->off_timeout_used)
1578 io_flush_timeouts(ctx);
1579 if (ctx->drain_active)
1580 io_queue_deferred(ctx);
1581}
1582
1583static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1584{
1585 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1586 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001587 /* order cqe stores with ring update */
1588 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001589}
1590
Jens Axboe90554202020-09-03 12:12:41 -06001591static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1592{
1593 struct io_rings *r = ctx->rings;
1594
Pavel Begunkova566c552021-05-16 22:58:08 +01001595 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001596}
1597
Pavel Begunkov888aae22021-01-19 13:32:39 +00001598static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1599{
1600 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1601}
1602
Pavel Begunkovd068b502021-05-16 22:58:11 +01001603static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001604{
Hristo Venev75b28af2019-08-26 17:23:46 +00001605 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001606 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001607
Stefan Bühler115e12e2019-04-24 23:54:18 +02001608 /*
1609 * writes to the cq entry need to come after reading head; the
1610 * control dependency is enough as we're using WRITE_ONCE to
1611 * fill the cq entry
1612 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001613 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001614 return NULL;
1615
Pavel Begunkov888aae22021-01-19 13:32:39 +00001616 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001617 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001618}
1619
Jens Axboef2842ab2020-01-08 11:04:00 -07001620static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1621{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001622 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001623 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001624 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1625 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001626 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001627}
1628
Jens Axboe2c5d7632021-08-21 07:21:19 -06001629/*
1630 * This should only get called when at least one event has been posted.
1631 * Some applications rely on the eventfd notification count only changing
1632 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1633 * 1:1 relationship between how many times this function is called (and
1634 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1635 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001636static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001637{
Jens Axboe5fd46172021-08-06 14:04:31 -06001638 /*
1639 * wake_up_all() may seem excessive, but io_wake_function() and
1640 * io_should_wake() handle the termination of the loop and only
1641 * wake as many waiters as we need to.
1642 */
1643 if (wq_has_sleeper(&ctx->cq_wait))
1644 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001645 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1646 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001647 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001648 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001649 if (waitqueue_active(&ctx->poll_wait)) {
1650 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001651 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1652 }
Jens Axboe8c838782019-03-12 15:48:16 -06001653}
1654
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001655static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1656{
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001657 /* see waitqueue_active() comment */
1658 smp_mb();
1659
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001660 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkovc57a91fb2021-09-08 20:49:17 +01001661 if (waitqueue_active(&ctx->cq_wait))
Jens Axboe5fd46172021-08-06 14:04:31 -06001662 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001663 }
1664 if (io_should_trigger_evfd(ctx))
1665 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001666 if (waitqueue_active(&ctx->poll_wait)) {
1667 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001668 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1669 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001670}
1671
Jens Axboec4a2ed72019-11-21 21:01:26 -07001672/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001673static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674{
Jens Axboeb18032b2021-01-24 16:58:56 -07001675 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001676
Pavel Begunkova566c552021-05-16 22:58:08 +01001677 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001678 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001679
Jens Axboeb18032b2021-01-24 16:58:56 -07001680 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001681 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001682 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001683 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001684 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001685
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001686 if (!cqe && !force)
1687 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001688 ocqe = list_first_entry(&ctx->cq_overflow_list,
1689 struct io_overflow_cqe, list);
1690 if (cqe)
1691 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1692 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001693 io_account_cq_overflow(ctx);
1694
Jens Axboeb18032b2021-01-24 16:58:56 -07001695 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001696 list_del(&ocqe->list);
1697 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001698 }
1699
Pavel Begunkov09e88402020-12-17 00:24:38 +00001700 all_flushed = list_empty(&ctx->cq_overflow_list);
1701 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001702 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001703 WRITE_ONCE(ctx->rings->sq_flags,
1704 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001705 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001706
Jens Axboeb18032b2021-01-24 16:58:56 -07001707 if (posted)
1708 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001709 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001710 if (posted)
1711 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001712 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001713}
1714
Pavel Begunkov90f67362021-08-09 20:18:12 +01001715static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001716{
Jens Axboeca0a2652021-03-04 17:15:48 -07001717 bool ret = true;
1718
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001719 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001720 /* iopoll syncs against uring_lock, not completion_lock */
1721 if (ctx->flags & IORING_SETUP_IOPOLL)
1722 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001723 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001724 if (ctx->flags & IORING_SETUP_IOPOLL)
1725 mutex_unlock(&ctx->uring_lock);
1726 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001727
1728 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001729}
1730
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001731/* must to be called somewhat shortly after putting a request */
1732static inline void io_put_task(struct task_struct *task, int nr)
1733{
1734 struct io_uring_task *tctx = task->io_uring;
1735
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001736 if (likely(task == current)) {
1737 tctx->cached_refs += nr;
1738 } else {
1739 percpu_counter_sub(&tctx->inflight, nr);
1740 if (unlikely(atomic_read(&tctx->in_idle)))
1741 wake_up(&tctx->wait);
1742 put_task_struct_many(task, nr);
1743 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001744}
1745
Pavel Begunkov9a108672021-08-27 11:55:01 +01001746static void io_task_refs_refill(struct io_uring_task *tctx)
1747{
1748 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1749
1750 percpu_counter_add(&tctx->inflight, refill);
1751 refcount_add(refill, &current->usage);
1752 tctx->cached_refs += refill;
1753}
1754
1755static inline void io_get_task_refs(int nr)
1756{
1757 struct io_uring_task *tctx = current->io_uring;
1758
1759 tctx->cached_refs -= nr;
1760 if (unlikely(tctx->cached_refs < 0))
1761 io_task_refs_refill(tctx);
1762}
1763
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001764static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1765 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001766{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001767 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001769 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1770 if (!ocqe) {
1771 /*
1772 * If we're in ring overflow flush mode, or in task cancel mode,
1773 * or cannot allocate an overflow entry, then we need to drop it
1774 * on the floor.
1775 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001776 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001777 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001778 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001779 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001780 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001781 WRITE_ONCE(ctx->rings->sq_flags,
1782 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1783
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001784 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001785 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001786 ocqe->cqe.res = res;
1787 ocqe->cqe.flags = cflags;
1788 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1789 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790}
1791
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001792static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1793 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001794{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001795 struct io_uring_cqe *cqe;
1796
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001797 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001798
1799 /*
1800 * If we can't get a cq entry, userspace overflowed the
1801 * submission (by quite a lot). Increment the overflow count in
1802 * the ring.
1803 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001804 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001805 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001806 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001807 WRITE_ONCE(cqe->res, res);
1808 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001809 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001810 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001811 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001812}
1813
Pavel Begunkov8d133262021-04-11 01:46:33 +01001814/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001815static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1816 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001817{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001818 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001819}
1820
Pavel Begunkov7a612352021-03-09 00:37:59 +00001821static void io_req_complete_post(struct io_kiocb *req, long res,
1822 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001823{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001824 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825
Jens Axboe79ebeae2021-08-10 15:18:27 -06001826 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001827 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001828 /*
1829 * If we're the last reference to this request, add to our locked
1830 * free_list cache.
1831 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001832 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001833 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001834 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001835 io_disarm_next(req);
1836 if (req->link) {
1837 io_req_task_queue(req->link);
1838 req->link = NULL;
1839 }
1840 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001841 io_dismantle_req(req);
1842 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001843 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001844 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001845 } else {
1846 if (!percpu_ref_tryget(&ctx->refs))
1847 req = NULL;
1848 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001849 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001850 spin_unlock(&ctx->completion_lock);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001851
Pavel Begunkov180f8292021-03-14 20:57:09 +00001852 if (req) {
1853 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001854 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001855 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856}
1857
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001858static inline bool io_req_needs_clean(struct io_kiocb *req)
1859{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001860 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001861}
1862
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001863static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001864 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001865{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001866 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001867 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001868 req->result = res;
1869 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001870 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001871}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001872
Pavel Begunkov889fca72021-02-10 00:03:09 +00001873static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1874 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001875{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001876 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1877 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001878 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001879 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001880}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001881
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001882static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001883{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001884 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001885}
1886
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001887static void io_req_complete_failed(struct io_kiocb *req, long res)
1888{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001889 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001890 io_req_complete_post(req, res, 0);
1891}
1892
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01001893static void io_req_complete_fail_submit(struct io_kiocb *req)
1894{
1895 /*
1896 * We don't submit, fail them all, for that replace hardlinks with
1897 * normal links. Extra REQ_F_LINK is tolerated.
1898 */
1899 req->flags &= ~REQ_F_HARDLINK;
1900 req->flags |= REQ_F_LINK;
1901 io_req_complete_failed(req, req->result);
1902}
1903
Pavel Begunkov864ea922021-08-09 13:04:08 +01001904/*
1905 * Don't initialise the fields below on every allocation, but do that in
1906 * advance and keep them valid across allocations.
1907 */
1908static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1909{
1910 req->ctx = ctx;
1911 req->link = NULL;
1912 req->async_data = NULL;
1913 /* not necessary, but safer to zero */
1914 req->result = 0;
1915}
1916
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001917static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001918 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001919{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001920 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001921 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001922 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001923 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001924}
1925
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001926/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001927static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001928{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001929 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001930 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001931
Jens Axboec7dae4b2021-02-09 19:53:37 -07001932 /*
1933 * If we have more than a batch's worth of requests in our IRQ side
1934 * locked cache, grab the lock and move them over to our submission
1935 * side cache.
1936 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001937 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001938 io_flush_cached_locked_reqs(ctx, state);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001939
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001940 nr = state->free_reqs;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001941 while (!list_empty(&state->free_list)) {
1942 struct io_kiocb *req = list_first_entry(&state->free_list,
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001943 struct io_kiocb, inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001944
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001945 list_del(&req->inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001946 state->reqs[nr++] = req;
1947 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001948 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001949 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001950
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001951 state->free_reqs = nr;
1952 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001953}
1954
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001955/*
1956 * A request might get retired back into the request caches even before opcode
1957 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1958 * Because of that, io_alloc_req() should be called only under ->uring_lock
1959 * and with extra caution to not get a request that is still worked on.
1960 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001961static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001962 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001964 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001965 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1966 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01001968 BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969
Pavel Begunkov864ea922021-08-09 13:04:08 +01001970 if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
1971 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001972
Pavel Begunkov864ea922021-08-09 13:04:08 +01001973 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1974 state->reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001975
Pavel Begunkov864ea922021-08-09 13:04:08 +01001976 /*
1977 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1978 * retry single alloc to be on the safe side.
1979 */
1980 if (unlikely(ret <= 0)) {
1981 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1982 if (!state->reqs[0])
1983 return NULL;
1984 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001986
1987 for (i = 0; i < ret; i++)
1988 io_preinit_req(state->reqs[i], ctx);
1989 state->free_reqs = ret;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001990got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001991 state->free_reqs--;
1992 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001993}
1994
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001995static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001996{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001997 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001998 fput(file);
1999}
2000
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002001static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002{
Pavel Begunkov094bae42021-03-19 17:22:42 +00002003 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002004
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01002005 if (io_req_needs_clean(req))
2006 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00002007 if (!(flags & REQ_F_FIXED_FILE))
2008 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002009 if (req->fixed_rsrc_refs)
2010 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01002011 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00002012 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01002013 req->async_data = NULL;
2014 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03002015}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002016
Pavel Begunkov216578e2020-10-13 09:44:00 +01002017static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002018{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002019 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002020
Pavel Begunkov216578e2020-10-13 09:44:00 +01002021 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002022 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002023
Jens Axboe79ebeae2021-08-10 15:18:27 -06002024 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01002025 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01002026 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06002027 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01002028
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002029 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002030}
2031
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002032static inline void io_remove_next_linked(struct io_kiocb *req)
2033{
2034 struct io_kiocb *nxt = req->link;
2035
2036 req->link = nxt->link;
2037 nxt->link = NULL;
2038}
2039
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002040static bool io_kill_linked_timeout(struct io_kiocb *req)
2041 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06002042 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002043{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002044 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002045
Pavel Begunkovb97e7362021-08-15 10:40:23 +01002046 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002047 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002048
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002049 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002050 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01002051 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovef9dd632021-08-28 19:54:38 -06002052 list_del(&link->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002053 io_cqring_fill_event(link->ctx, link->user_data,
2054 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002055 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002056 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002057 }
2058 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00002059 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002060}
2061
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002062static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002063 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002064{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002065 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06002066
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002067 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002068 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08002069 long res = -ECANCELED;
2070
2071 if (link->flags & REQ_F_FAIL)
2072 res = link->result;
2073
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002074 nxt = link->link;
2075 link->link = NULL;
2076
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002077 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08002078 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002079 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002080 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002081 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002082}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002083
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002084static bool io_disarm_next(struct io_kiocb *req)
2085 __must_hold(&req->ctx->completion_lock)
2086{
2087 bool posted = false;
2088
Pavel Begunkov0756a862021-08-15 10:40:25 +01002089 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2090 struct io_kiocb *link = req->link;
2091
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002092 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002093 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2094 io_remove_next_linked(req);
2095 io_cqring_fill_event(link->ctx, link->user_data,
2096 -ECANCELED, 0);
2097 io_put_req_deferred(link);
2098 posted = true;
2099 }
2100 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002101 struct io_ring_ctx *ctx = req->ctx;
2102
2103 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002104 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002105 spin_unlock_irq(&ctx->timeout_lock);
2106 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002107 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002108 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002109 posted |= (req->link != NULL);
2110 io_fail_links(req);
2111 }
2112 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002113}
2114
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002115static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002116{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002117 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002118
Jens Axboe9e645e112019-05-10 16:07:28 -06002119 /*
2120 * If LINK is set, we have dependent requests in this chain. If we
2121 * didn't fail this request, queue the first one up, moving any other
2122 * dependencies to the next request. In case of failure, fail the rest
2123 * of the chain.
2124 */
Pavel Begunkov0756a862021-08-15 10:40:25 +01002125 if (req->flags & IO_DISARM_MASK) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002126 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002127 bool posted;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002128
Jens Axboe79ebeae2021-08-10 15:18:27 -06002129 spin_lock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002130 posted = io_disarm_next(req);
2131 if (posted)
2132 io_commit_cqring(req->ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002133 spin_unlock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002134 if (posted)
2135 io_cqring_ev_posted(ctx);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002136 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002137 nxt = req->link;
2138 req->link = NULL;
2139 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002140}
Jens Axboe2665abf2019-11-05 12:40:47 -07002141
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002142static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002143{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002144 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002145 return NULL;
2146 return __io_req_find_next(req);
2147}
2148
Pavel Begunkovf237c302021-08-18 12:42:46 +01002149static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002150{
2151 if (!ctx)
2152 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002153 if (*locked) {
Hao Xu99c8bc52021-08-21 06:19:54 +08002154 if (ctx->submit_state.compl_nr)
2155 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002156 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002157 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002158 }
2159 percpu_ref_put(&ctx->refs);
2160}
2161
Jens Axboe7cbf1722021-02-10 00:03:20 +00002162static void tctx_task_work(struct callback_head *cb)
2163{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002164 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002165 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002166 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2167 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002168
Pavel Begunkov16f72072021-06-17 18:14:09 +01002169 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002170 struct io_wq_work_node *node;
2171
Pavel Begunkov8d4ad412021-09-02 00:38:23 +01002172 if (!tctx->task_list.first && locked && ctx->submit_state.compl_nr)
2173 io_submit_flush_completions(ctx);
2174
Pavel Begunkov3f184072021-06-17 18:14:06 +01002175 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002176 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002177 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002178 if (!node)
2179 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002180 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002181 if (!node)
2182 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002183
Pavel Begunkov6294f362021-08-10 17:53:55 +01002184 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002185 struct io_wq_work_node *next = node->next;
2186 struct io_kiocb *req = container_of(node, struct io_kiocb,
2187 io_task_work.node);
2188
2189 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002190 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002191 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002192 /* if not contended, grab and improve batching */
2193 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002194 percpu_ref_get(&ctx->refs);
2195 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002196 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002197 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002198 } while (node);
2199
Jens Axboe7cbf1722021-02-10 00:03:20 +00002200 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002201 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002202
Pavel Begunkovf237c302021-08-18 12:42:46 +01002203 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002204}
2205
Pavel Begunkove09ee512021-07-01 13:26:05 +01002206static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002207{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002208 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002209 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002210 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002211 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002212 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002213 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002214
2215 WARN_ON_ONCE(!tctx);
2216
Jens Axboe0b81e802021-02-16 10:33:53 -07002217 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002218 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002219 running = tctx->task_running;
2220 if (!running)
2221 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002222 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002223
2224 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002225 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002226 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002227
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002228 /*
2229 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2230 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2231 * processing task_work. There's no reliable way to tell if TWA_RESUME
2232 * will do the job.
2233 */
2234 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002235 if (!task_work_add(tsk, &tctx->task_work, notify)) {
2236 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002237 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002238 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002239
Pavel Begunkove09ee512021-07-01 13:26:05 +01002240 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002241 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002242 node = tctx->task_list.first;
2243 INIT_WQ_LIST(&tctx->task_list);
2244 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002245
Pavel Begunkove09ee512021-07-01 13:26:05 +01002246 while (node) {
2247 req = container_of(node, struct io_kiocb, io_task_work.node);
2248 node = node->next;
2249 if (llist_add(&req->io_task_work.fallback_node,
2250 &req->ctx->fallback_llist))
2251 schedule_delayed_work(&req->ctx->fallback_work, 1);
2252 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002253}
2254
Pavel Begunkovf237c302021-08-18 12:42:46 +01002255static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002256{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002258
Pavel Begunkovb18a1a42021-08-25 20:51:39 +01002259 /* not needed for normal modes, but SQPOLL depends on it */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002260 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002261 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002262}
2263
Pavel Begunkovf237c302021-08-18 12:42:46 +01002264static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002265{
2266 struct io_ring_ctx *ctx = req->ctx;
2267
Pavel Begunkovf237c302021-08-18 12:42:46 +01002268 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002269 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002270 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b942021-02-10 00:03:22 +00002271 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002272 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002273 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002274}
2275
Pavel Begunkova3df76982021-02-18 22:32:52 +00002276static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2277{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002278 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002279 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002280 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002281}
2282
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002283static void io_req_task_queue(struct io_kiocb *req)
2284{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002285 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002286 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002287}
2288
Jens Axboe773af692021-07-27 10:25:55 -06002289static void io_req_task_queue_reissue(struct io_kiocb *req)
2290{
2291 req->io_task_work.func = io_queue_async_work;
2292 io_req_task_work_add(req);
2293}
2294
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002295static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002296{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002297 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002298
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002299 if (nxt)
2300 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002301}
2302
Jens Axboe9e645e112019-05-10 16:07:28 -06002303static void io_free_req(struct io_kiocb *req)
2304{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002305 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002306 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002307}
2308
Pavel Begunkovf237c302021-08-18 12:42:46 +01002309static void io_free_req_work(struct io_kiocb *req, bool *locked)
2310{
2311 io_free_req(req);
2312}
2313
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002314struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002315 struct task_struct *task;
2316 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002317 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002318};
2319
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002320static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002321{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002322 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002323 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002324 rb->task = NULL;
2325}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002326
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002327static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2328 struct req_batch *rb)
2329{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002330 if (rb->ctx_refs)
2331 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002332 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002333 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002334}
2335
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002336static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2337 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002338{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002339 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002340 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002341
Jens Axboee3bc8e92020-09-24 08:45:57 -06002342 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002343 if (rb->task)
2344 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002345 rb->task = req->task;
2346 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002347 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002348 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002349 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002350
Pavel Begunkovbd759042021-02-12 03:23:50 +00002351 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002352 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002353 else
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002354 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002355}
2356
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01002357static void io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002358 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002359{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002360 struct io_submit_state *state = &ctx->submit_state;
2361 int i, nr = state->compl_nr;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002362 struct req_batch rb;
2363
Jens Axboe79ebeae2021-08-10 15:18:27 -06002364 spin_lock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002365 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002366 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002367
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002368 __io_cqring_fill_event(ctx, req->user_data, req->result,
2369 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002370 }
2371 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002372 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002373 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002374
2375 io_init_req_batch(&rb);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002376 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002377 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov905c1722021-02-10 00:03:14 +00002378
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002379 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002380 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002381 }
2382
2383 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002384 state->compl_nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002385}
2386
Jens Axboeba816ad2019-09-28 11:36:45 -06002387/*
2388 * Drop reference to request, return next in chain (if there is one) if this
2389 * was the last reference to this request.
2390 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002391static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002392{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002393 struct io_kiocb *nxt = NULL;
2394
Jens Axboede9b4cc2021-02-24 13:28:27 -07002395 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002396 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002397 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002398 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002399 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400}
2401
Pavel Begunkov0d850352021-03-19 17:22:37 +00002402static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002404 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002405 io_free_req(req);
2406}
2407
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002408static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002409{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002410 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002411 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002412 io_req_task_work_add(req);
2413 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002414}
2415
Pavel Begunkov6c503152021-01-04 20:36:36 +00002416static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002417{
2418 /* See comment at the top of this file */
2419 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002420 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002421}
2422
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002423static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2424{
2425 struct io_rings *rings = ctx->rings;
2426
2427 /* make sure SQ entry isn't read before tail */
2428 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2429}
2430
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002431static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002432{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002433 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002434
Jens Axboebcda7ba2020-02-23 16:42:51 -07002435 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2436 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002437 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002438 kfree(kbuf);
2439 return cflags;
2440}
2441
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002442static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2443{
2444 struct io_buffer *kbuf;
2445
Pavel Begunkovae421d92021-08-17 20:28:08 +01002446 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2447 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002448 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2449 return io_put_kbuf(req, kbuf);
2450}
2451
Jens Axboe4c6e2772020-07-01 11:29:10 -06002452static inline bool io_run_task_work(void)
2453{
Nadav Amitef98eb02021-08-07 17:13:41 -07002454 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002455 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002456 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002457 return true;
2458 }
2459
2460 return false;
2461}
2462
Jens Axboedef596e2019-01-09 08:59:42 -07002463/*
2464 * Find and free completed poll iocbs
2465 */
2466static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002467 struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002468{
Jens Axboe8237e042019-12-28 10:48:22 -07002469 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002470 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002471
2472 /* order with ->result store in io_complete_rw_iopoll() */
2473 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002474
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002475 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002476 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002477 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002478 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002479
Pavel Begunkova8576af2021-08-15 10:40:21 +01002480 if (READ_ONCE(req->result) == -EAGAIN &&
Pavel Begunkov8c130822021-03-22 01:58:32 +00002481 !(req->flags & REQ_F_DONT_REISSUE)) {
Pavel Begunkovf1613402021-02-11 18:28:21 +00002482 req->iopoll_completed = 0;
Jens Axboe773af692021-07-27 10:25:55 -06002483 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002484 continue;
Pavel Begunkovf1613402021-02-11 18:28:21 +00002485 }
2486
Pavel Begunkovae421d92021-08-17 20:28:08 +01002487 __io_cqring_fill_event(ctx, req->user_data, req->result,
2488 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002489 (*nr_events)++;
2490
Jens Axboede9b4cc2021-02-24 13:28:27 -07002491 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002492 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002493 }
Jens Axboedef596e2019-01-09 08:59:42 -07002494
Jens Axboe09bb8392019-03-13 12:39:28 -06002495 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002496 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002497 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002498}
2499
Jens Axboedef596e2019-01-09 08:59:42 -07002500static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002501 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002502{
2503 struct io_kiocb *req, *tmp;
2504 LIST_HEAD(done);
2505 bool spin;
Jens Axboedef596e2019-01-09 08:59:42 -07002506
2507 /*
2508 * Only spin for completions if we don't have multiple devices hanging
2509 * off our complete list, and we're under the requested amount.
2510 */
Hao Xu915b3dd2021-06-28 05:37:30 +08002511 spin = !ctx->poll_multi_queue && *nr_events < min;
Jens Axboedef596e2019-01-09 08:59:42 -07002512
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002513 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002514 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002515 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002516
2517 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002518 * Move completed and retryable entries to our local lists.
2519 * If we find a request that requires polling, break out
2520 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002521 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002522 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002523 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002524 continue;
2525 }
2526 if (!list_empty(&done))
2527 break;
2528
2529 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002530 if (unlikely(ret < 0))
2531 return ret;
2532 else if (ret)
2533 spin = false;
Jens Axboedef596e2019-01-09 08:59:42 -07002534
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002535 /* iopoll may have completed current req */
2536 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002537 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002538 }
2539
2540 if (!list_empty(&done))
Pavel Begunkova8576af2021-08-15 10:40:21 +01002541 io_iopoll_complete(ctx, nr_events, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002542
Pavel Begunkova2416e12021-08-09 13:04:09 +01002543 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002544}
2545
2546/*
Jens Axboedef596e2019-01-09 08:59:42 -07002547 * We can't just wait for polled events to come to us, we have to actively
2548 * find and complete them.
2549 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002550static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002551{
2552 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2553 return;
2554
2555 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002556 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002557 unsigned int nr_events = 0;
2558
Pavel Begunkova8576af2021-08-15 10:40:21 +01002559 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002560
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002561 /* let it sleep and repeat later if can't complete a request */
2562 if (nr_events == 0)
2563 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002564 /*
2565 * Ensure we allow local-to-the-cpu processing to take place,
2566 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002567 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002568 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002569 if (need_resched()) {
2570 mutex_unlock(&ctx->uring_lock);
2571 cond_resched();
2572 mutex_lock(&ctx->uring_lock);
2573 }
Jens Axboedef596e2019-01-09 08:59:42 -07002574 }
2575 mutex_unlock(&ctx->uring_lock);
2576}
2577
Pavel Begunkov7668b922020-07-07 16:36:21 +03002578static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002579{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002580 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002581 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002582
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002583 /*
2584 * We disallow the app entering submit/complete with polling, but we
2585 * still need to lock the ring to prevent racing with polled issue
2586 * that got punted to a workqueue.
2587 */
2588 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002589 /*
2590 * Don't enter poll loop if we already have events pending.
2591 * If we do, we can potentially be spinning for commands that
2592 * already triggered a CQE (eg in error).
2593 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002594 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002595 __io_cqring_overflow_flush(ctx, false);
2596 if (io_cqring_events(ctx))
2597 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002598 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002599 /*
2600 * If a submit got punted to a workqueue, we can have the
2601 * application entering polling for a command before it gets
2602 * issued. That app will hold the uring_lock for the duration
2603 * of the poll right here, so we need to take a breather every
2604 * now and then to ensure that the issue has a chance to add
2605 * the poll to the issued list. Otherwise we can spin here
2606 * forever, while the workqueue is stuck trying to acquire the
2607 * very same mutex.
2608 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002609 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002610 u32 tail = ctx->cached_cq_tail;
2611
Jens Axboe500f9fb2019-08-19 12:15:59 -06002612 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002613 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002614 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002615
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002616 /* some requests don't go through iopoll_list */
2617 if (tail != ctx->cached_cq_tail ||
2618 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002619 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002620 }
Pavel Begunkova8576af2021-08-15 10:40:21 +01002621 ret = io_do_iopoll(ctx, &nr_events, min);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002622 } while (!ret && nr_events < min && !need_resched());
2623out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002624 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002625 return ret;
2626}
2627
Jens Axboe491381ce2019-10-17 09:20:46 -06002628static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002629{
Jens Axboe491381ce2019-10-17 09:20:46 -06002630 /*
2631 * Tell lockdep we inherited freeze protection from submission
2632 * thread.
2633 */
2634 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002635 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002636
Pavel Begunkov1c986792021-03-22 01:58:31 +00002637 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2638 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639 }
2640}
2641
Jens Axboeb63534c2020-06-04 11:28:00 -06002642#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002643static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002644{
Pavel Begunkovab454432021-03-22 01:58:33 +00002645 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002646
Pavel Begunkovab454432021-03-22 01:58:33 +00002647 if (!rw)
2648 return !io_req_prep_async(req);
2649 /* may have left rw->iter inconsistent on -EIOCBQUEUED */
2650 iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter));
2651 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002652}
Jens Axboeb63534c2020-06-04 11:28:00 -06002653
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002654static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002655{
Jens Axboe355afae2020-09-02 09:30:31 -06002656 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002657 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002658
Jens Axboe355afae2020-09-02 09:30:31 -06002659 if (!S_ISBLK(mode) && !S_ISREG(mode))
2660 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002661 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2662 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002663 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002664 /*
2665 * If ref is dying, we might be running poll reap from the exit work.
2666 * Don't attempt to reissue from that path, just let it fail with
2667 * -EAGAIN.
2668 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002669 if (percpu_ref_is_dying(&ctx->refs))
2670 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002671 /*
2672 * Play it safe and assume not safe to re-import and reissue if we're
2673 * not in the original thread group (or in task context).
2674 */
2675 if (!same_thread_group(req->task, current) || !in_task())
2676 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002677 return true;
2678}
Jens Axboee82ad482021-04-02 19:45:34 -06002679#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002680static bool io_resubmit_prep(struct io_kiocb *req)
2681{
2682 return false;
2683}
Jens Axboee82ad482021-04-02 19:45:34 -06002684static bool io_rw_should_reissue(struct io_kiocb *req)
2685{
2686 return false;
2687}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002688#endif
2689
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002690static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002691{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002692 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2693 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002694 if (res != req->result) {
2695 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2696 io_rw_should_reissue(req)) {
2697 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002698 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002699 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002700 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002701 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002702 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002703 return false;
2704}
2705
Pavel Begunkovf237c302021-08-18 12:42:46 +01002706static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002707{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002708 unsigned int cflags = io_put_rw_kbuf(req);
2709 long res = req->result;
2710
2711 if (*locked) {
2712 struct io_ring_ctx *ctx = req->ctx;
2713 struct io_submit_state *state = &ctx->submit_state;
2714
2715 io_req_complete_state(req, res, cflags);
2716 state->compl_reqs[state->compl_nr++] = req;
2717 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
2718 io_submit_flush_completions(ctx);
2719 } else {
2720 io_req_complete_post(req, res, cflags);
2721 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002722}
2723
2724static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2725 unsigned int issue_flags)
2726{
2727 if (__io_complete_rw_common(req, res))
2728 return;
Pavel Begunkov63637852021-09-02 00:38:22 +01002729 __io_req_complete(req, issue_flags, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002730}
2731
2732static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2733{
Jens Axboe9adbd452019-12-20 08:45:55 -07002734 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002735
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002736 if (__io_complete_rw_common(req, res))
2737 return;
2738 req->result = res;
2739 req->io_task_work.func = io_req_task_complete;
2740 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002741}
2742
Jens Axboedef596e2019-01-09 08:59:42 -07002743static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2744{
Jens Axboe9adbd452019-12-20 08:45:55 -07002745 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002746
Jens Axboe491381ce2019-10-17 09:20:46 -06002747 if (kiocb->ki_flags & IOCB_WRITE)
2748 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002749 if (unlikely(res != req->result)) {
Jens Axboea1ff1e32021-04-12 06:40:02 -06002750 if (!(res == -EAGAIN && io_rw_should_reissue(req) &&
2751 io_resubmit_prep(req))) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002752 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002753 req->flags |= REQ_F_DONT_REISSUE;
2754 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002755 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002756
2757 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002758 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002759 smp_wmb();
2760 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002761}
2762
2763/*
2764 * After the iocb has been issued, it's safe to be found on the poll list.
2765 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002766 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002767 * accessing the kiocb cookie.
2768 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002769static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002770{
2771 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002772 const bool in_async = io_wq_current_is_worker();
2773
2774 /* workqueue context doesn't hold uring_lock, grab it now */
2775 if (unlikely(in_async))
2776 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002777
2778 /*
2779 * Track whether we have multiple files in our lists. This will impact
2780 * how we do polling eventually, not spinning if we're on potentially
2781 * different devices.
2782 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002783 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002784 ctx->poll_multi_queue = false;
2785 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002786 struct io_kiocb *list_req;
Hao Xu915b3dd2021-06-28 05:37:30 +08002787 unsigned int queue_num0, queue_num1;
Jens Axboedef596e2019-01-09 08:59:42 -07002788
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002789 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002790 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002791
2792 if (list_req->file != req->file) {
2793 ctx->poll_multi_queue = true;
2794 } else {
2795 queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie);
2796 queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie);
2797 if (queue_num0 != queue_num1)
2798 ctx->poll_multi_queue = true;
2799 }
Jens Axboedef596e2019-01-09 08:59:42 -07002800 }
2801
2802 /*
2803 * For fast devices, IO may have already completed. If it has, add
2804 * it to the front so we find it first.
2805 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002806 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002807 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002808 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002809 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002810
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002811 if (unlikely(in_async)) {
2812 /*
2813 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2814 * in sq thread task context or in io worker task context. If
2815 * current task context is sq thread, we don't need to check
2816 * whether should wake up sq thread.
2817 */
2818 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2819 wq_has_sleeper(&ctx->sq_data->wait))
2820 wake_up(&ctx->sq_data->wait);
2821
2822 mutex_unlock(&ctx->uring_lock);
2823 }
Jens Axboedef596e2019-01-09 08:59:42 -07002824}
2825
Jens Axboe4503b762020-06-01 10:00:27 -06002826static bool io_bdev_nowait(struct block_device *bdev)
2827{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002828 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002829}
2830
Jens Axboe2b188cc2019-01-07 10:46:33 -07002831/*
2832 * If we tracked the file through the SCM inflight mechanism, we could support
2833 * any file. For now, just ensure that anything potentially problematic is done
2834 * inline.
2835 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002836static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002837{
2838 umode_t mode = file_inode(file)->i_mode;
2839
Jens Axboe4503b762020-06-01 10:00:27 -06002840 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002841 if (IS_ENABLED(CONFIG_BLOCK) &&
2842 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002843 return true;
2844 return false;
2845 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002846 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002847 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002848 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002849 if (IS_ENABLED(CONFIG_BLOCK) &&
2850 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002851 file->f_op != &io_uring_fops)
2852 return true;
2853 return false;
2854 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002855
Jens Axboec5b85622020-06-09 19:23:05 -06002856 /* any ->read/write should understand O_NONBLOCK */
2857 if (file->f_flags & O_NONBLOCK)
2858 return true;
2859
Jens Axboeaf197f52020-04-28 13:15:06 -06002860 if (!(file->f_mode & FMODE_NOWAIT))
2861 return false;
2862
2863 if (rw == READ)
2864 return file->f_op->read_iter != NULL;
2865
2866 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002867}
2868
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002869static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002870{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002871 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002872 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002873 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002874 return true;
2875
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002876 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002877}
2878
Pavel Begunkova88fc402020-09-30 22:57:53 +03002879static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002880{
Jens Axboedef596e2019-01-09 08:59:42 -07002881 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002882 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002883 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002884 unsigned ioprio;
2885 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002886
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002887 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002888 req->flags |= REQ_F_ISREG;
2889
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002891 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002892 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002893 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002894 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002896 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2897 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2898 if (unlikely(ret))
2899 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002900
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002901 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2902 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2903 req->flags |= REQ_F_NOWAIT;
2904
Jens Axboe2b188cc2019-01-07 10:46:33 -07002905 ioprio = READ_ONCE(sqe->ioprio);
2906 if (ioprio) {
2907 ret = ioprio_check_cap(ioprio);
2908 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002909 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002910
2911 kiocb->ki_ioprio = ioprio;
2912 } else
2913 kiocb->ki_ioprio = get_current_ioprio();
2914
Jens Axboedef596e2019-01-09 08:59:42 -07002915 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002916 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2917 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002918 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002919
Jens Axboe394918e2021-03-08 11:40:23 -07002920 kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
Jens Axboedef596e2019-01-09 08:59:42 -07002921 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002922 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002923 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002924 if (kiocb->ki_flags & IOCB_HIPRI)
2925 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002926 kiocb->ki_complete = io_complete_rw;
2927 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002928
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002929 if (req->opcode == IORING_OP_READ_FIXED ||
2930 req->opcode == IORING_OP_WRITE_FIXED) {
2931 req->imu = NULL;
2932 io_req_set_rsrc_node(req);
2933 }
2934
Jens Axboe3529d8c2019-12-19 18:24:38 -07002935 req->rw.addr = READ_ONCE(sqe->addr);
2936 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002937 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002938 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939}
2940
2941static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2942{
2943 switch (ret) {
2944 case -EIOCBQUEUED:
2945 break;
2946 case -ERESTARTSYS:
2947 case -ERESTARTNOINTR:
2948 case -ERESTARTNOHAND:
2949 case -ERESTART_RESTARTBLOCK:
2950 /*
2951 * We can't just restart the syscall, since previously
2952 * submitted sqes may already be in progress. Just fail this
2953 * IO with EINTR.
2954 */
2955 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002956 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002957 default:
2958 kiocb->ki_complete(kiocb, ret, 0);
2959 }
2960}
2961
Jens Axboea1d7c392020-06-22 11:09:46 -06002962static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002963 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002964{
Jens Axboeba042912019-12-25 16:33:42 -07002965 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002966 struct io_async_rw *io = req->async_data;
Pavel Begunkov97284632021-04-08 19:28:03 +01002967 bool check_reissue = kiocb->ki_complete == io_complete_rw;
Jens Axboeba042912019-12-25 16:33:42 -07002968
Jens Axboe227c0c92020-08-13 11:51:40 -06002969 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002970 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002971 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002972 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002973 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002974 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002975 }
2976
Jens Axboeba042912019-12-25 16:33:42 -07002977 if (req->flags & REQ_F_CUR_POS)
2978 req->file->f_pos = kiocb->ki_pos;
Hao Xue149bd742021-06-28 05:48:05 +08002979 if (ret >= 0 && check_reissue)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002980 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002981 else
2982 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002983
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01002984 if (check_reissue && (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002985 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002986 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002987 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002988 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002989 req_set_fail(req);
Pavel Begunkovae421d92021-08-17 20:28:08 +01002990 __io_req_complete(req, issue_flags, ret,
2991 io_put_rw_kbuf(req));
Pavel Begunkov97284632021-04-08 19:28:03 +01002992 }
2993 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002994}
2995
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002996static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2997 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002998{
Jens Axboe9adbd452019-12-20 08:45:55 -07002999 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01003000 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003001 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07003002
Pavel Begunkov75769e32021-04-01 15:43:54 +01003003 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07003004 return -EFAULT;
3005 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01003006 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07003007 return -EFAULT;
3008
3009 /*
3010 * May not be a start of buffer, set size appropriately
3011 * and advance us to the beginning.
3012 */
3013 offset = buf_addr - imu->ubuf;
3014 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003015
3016 if (offset) {
3017 /*
3018 * Don't use iov_iter_advance() here, as it's really slow for
3019 * using the latter parts of a big fixed buffer - it iterates
3020 * over each segment manually. We can cheat a bit here, because
3021 * we know that:
3022 *
3023 * 1) it's a BVEC iter, we set it up
3024 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3025 * first and last bvec
3026 *
3027 * So just find our index, and adjust the iterator afterwards.
3028 * If the offset is within the first bvec (or the whole first
3029 * bvec, just use iov_iter_advance(). This makes it easier
3030 * since we can just skip the first segment, which may not
3031 * be PAGE_SIZE aligned.
3032 */
3033 const struct bio_vec *bvec = imu->bvec;
3034
3035 if (offset <= bvec->bv_len) {
3036 iov_iter_advance(iter, offset);
3037 } else {
3038 unsigned long seg_skip;
3039
3040 /* skip first vec */
3041 offset -= bvec->bv_len;
3042 seg_skip = 1 + (offset >> PAGE_SHIFT);
3043
3044 iter->bvec = bvec + seg_skip;
3045 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003046 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003047 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003048 }
3049 }
3050
Pavel Begunkov847595d2021-02-04 13:52:06 +00003051 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003052}
3053
Pavel Begunkoveae071c2021-04-25 14:32:24 +01003054static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
3055{
3056 struct io_ring_ctx *ctx = req->ctx;
3057 struct io_mapped_ubuf *imu = req->imu;
3058 u16 index, buf_index = req->buf_index;
3059
3060 if (likely(!imu)) {
3061 if (unlikely(buf_index >= ctx->nr_user_bufs))
3062 return -EFAULT;
3063 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3064 imu = READ_ONCE(ctx->user_bufs[index]);
3065 req->imu = imu;
3066 }
3067 return __io_import_fixed(req, rw, iter, imu);
3068}
3069
Jens Axboebcda7ba2020-02-23 16:42:51 -07003070static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3071{
3072 if (needs_lock)
3073 mutex_unlock(&ctx->uring_lock);
3074}
3075
3076static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3077{
3078 /*
3079 * "Normal" inline submissions always hold the uring_lock, since we
3080 * grab it from the system call. Same is true for the SQPOLL offload.
3081 * The only exception is when we've detached the request and issue it
3082 * from an async worker thread, grab the lock for that case.
3083 */
3084 if (needs_lock)
3085 mutex_lock(&ctx->uring_lock);
3086}
3087
3088static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3089 int bgid, struct io_buffer *kbuf,
3090 bool needs_lock)
3091{
3092 struct io_buffer *head;
3093
3094 if (req->flags & REQ_F_BUFFER_SELECTED)
3095 return kbuf;
3096
3097 io_ring_submit_lock(req->ctx, needs_lock);
3098
3099 lockdep_assert_held(&req->ctx->uring_lock);
3100
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003101 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003102 if (head) {
3103 if (!list_empty(&head->list)) {
3104 kbuf = list_last_entry(&head->list, struct io_buffer,
3105 list);
3106 list_del(&kbuf->list);
3107 } else {
3108 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003109 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003110 }
3111 if (*len > kbuf->len)
3112 *len = kbuf->len;
3113 } else {
3114 kbuf = ERR_PTR(-ENOBUFS);
3115 }
3116
3117 io_ring_submit_unlock(req->ctx, needs_lock);
3118
3119 return kbuf;
3120}
3121
Jens Axboe4d954c22020-02-27 07:31:19 -07003122static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3123 bool needs_lock)
3124{
3125 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003126 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003127
3128 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003129 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003130 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3131 if (IS_ERR(kbuf))
3132 return kbuf;
3133 req->rw.addr = (u64) (unsigned long) kbuf;
3134 req->flags |= REQ_F_BUFFER_SELECTED;
3135 return u64_to_user_ptr(kbuf->addr);
3136}
3137
3138#ifdef CONFIG_COMPAT
3139static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3140 bool needs_lock)
3141{
3142 struct compat_iovec __user *uiov;
3143 compat_ssize_t clen;
3144 void __user *buf;
3145 ssize_t len;
3146
3147 uiov = u64_to_user_ptr(req->rw.addr);
3148 if (!access_ok(uiov, sizeof(*uiov)))
3149 return -EFAULT;
3150 if (__get_user(clen, &uiov->iov_len))
3151 return -EFAULT;
3152 if (clen < 0)
3153 return -EINVAL;
3154
3155 len = clen;
3156 buf = io_rw_buffer_select(req, &len, needs_lock);
3157 if (IS_ERR(buf))
3158 return PTR_ERR(buf);
3159 iov[0].iov_base = buf;
3160 iov[0].iov_len = (compat_size_t) len;
3161 return 0;
3162}
3163#endif
3164
3165static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3166 bool needs_lock)
3167{
3168 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3169 void __user *buf;
3170 ssize_t len;
3171
3172 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3173 return -EFAULT;
3174
3175 len = iov[0].iov_len;
3176 if (len < 0)
3177 return -EINVAL;
3178 buf = io_rw_buffer_select(req, &len, needs_lock);
3179 if (IS_ERR(buf))
3180 return PTR_ERR(buf);
3181 iov[0].iov_base = buf;
3182 iov[0].iov_len = len;
3183 return 0;
3184}
3185
3186static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3187 bool needs_lock)
3188{
Jens Axboedddb3e22020-06-04 11:27:01 -06003189 if (req->flags & REQ_F_BUFFER_SELECTED) {
3190 struct io_buffer *kbuf;
3191
3192 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3193 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3194 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003195 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003196 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003197 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003198 return -EINVAL;
3199
3200#ifdef CONFIG_COMPAT
3201 if (req->ctx->compat)
3202 return io_compat_import(req, iov, needs_lock);
3203#endif
3204
3205 return __io_iov_buffer_select(req, iov, needs_lock);
3206}
3207
Pavel Begunkov847595d2021-02-04 13:52:06 +00003208static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3209 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003210{
Jens Axboe9adbd452019-12-20 08:45:55 -07003211 void __user *buf = u64_to_user_ptr(req->rw.addr);
3212 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003213 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003214 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003215
Pavel Begunkov7d009162019-11-25 23:14:40 +03003216 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003217 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003218 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003219 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003220
Jens Axboebcda7ba2020-02-23 16:42:51 -07003221 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003222 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003223 return -EINVAL;
3224
Jens Axboe3a6820f2019-12-22 15:19:35 -07003225 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003226 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003227 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003228 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003229 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003230 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003231 }
3232
Jens Axboe3a6820f2019-12-22 15:19:35 -07003233 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3234 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003235 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003236 }
3237
Jens Axboe4d954c22020-02-27 07:31:19 -07003238 if (req->flags & REQ_F_BUFFER_SELECT) {
3239 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003240 if (!ret)
3241 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003242 *iovec = NULL;
3243 return ret;
3244 }
3245
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003246 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3247 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003248}
3249
Jens Axboe0fef9482020-08-26 10:36:20 -06003250static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3251{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003252 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003253}
3254
Jens Axboe32960612019-09-23 11:05:34 -06003255/*
3256 * For files that don't have ->read_iter() and ->write_iter(), handle them
3257 * by looping over ->read() or ->write() manually.
3258 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003259static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003260{
Jens Axboe4017eb92020-10-22 14:14:12 -06003261 struct kiocb *kiocb = &req->rw.kiocb;
3262 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003263 ssize_t ret = 0;
3264
3265 /*
3266 * Don't support polled IO through this interface, and we can't
3267 * support non-blocking either. For the latter, this just causes
3268 * the kiocb to be handled from an async context.
3269 */
3270 if (kiocb->ki_flags & IOCB_HIPRI)
3271 return -EOPNOTSUPP;
3272 if (kiocb->ki_flags & IOCB_NOWAIT)
3273 return -EAGAIN;
3274
3275 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003276 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003277 ssize_t nr;
3278
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003279 if (!iov_iter_is_bvec(iter)) {
3280 iovec = iov_iter_iovec(iter);
3281 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003282 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3283 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003284 }
3285
Jens Axboe32960612019-09-23 11:05:34 -06003286 if (rw == READ) {
3287 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003288 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003289 } else {
3290 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003291 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003292 }
3293
3294 if (nr < 0) {
3295 if (!ret)
3296 ret = nr;
3297 break;
3298 }
3299 ret += nr;
3300 if (nr != iovec.iov_len)
3301 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003302 req->rw.len -= nr;
3303 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003304 iov_iter_advance(iter, nr);
3305 }
3306
3307 return ret;
3308}
3309
Jens Axboeff6165b2020-08-13 09:47:43 -06003310static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3311 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003312{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003313 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003314
Jens Axboeff6165b2020-08-13 09:47:43 -06003315 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003316 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003317 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003318 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003319 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003320 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003321 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003322 unsigned iov_off = 0;
3323
3324 rw->iter.iov = rw->fast_iov;
3325 if (iter->iov != fast_iov) {
3326 iov_off = iter->iov - fast_iov;
3327 rw->iter.iov += iov_off;
3328 }
3329 if (rw->fast_iov != fast_iov)
3330 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003331 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003332 } else {
3333 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003334 }
3335}
3336
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003337static inline int io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003338{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003339 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3340 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3341 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003342}
3343
Jens Axboeff6165b2020-08-13 09:47:43 -06003344static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3345 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003346 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003347{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003348 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003349 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003350 if (!req->async_data) {
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003351 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003352 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003353 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003354 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003355
Jens Axboeff6165b2020-08-13 09:47:43 -06003356 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003357 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003358 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003359}
3360
Pavel Begunkov73debe62020-09-30 22:57:54 +03003361static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003362{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003363 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003364 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003365 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003366
Pavel Begunkov2846c482020-11-07 13:16:27 +00003367 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003368 if (unlikely(ret < 0))
3369 return ret;
3370
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003371 iorw->bytes_done = 0;
3372 iorw->free_iovec = iov;
3373 if (iov)
3374 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003375 return 0;
3376}
3377
Pavel Begunkov73debe62020-09-30 22:57:54 +03003378static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003379{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003380 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3381 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003382 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003383}
3384
Jens Axboec1dd91d2020-08-03 16:43:59 -06003385/*
3386 * This is our waitqueue callback handler, registered through lock_page_async()
3387 * when we initially tried to do the IO with the iocb armed our waitqueue.
3388 * This gets called when the page is unlocked, and we generally expect that to
3389 * happen when the page IO is completed and the page is now uptodate. This will
3390 * queue a task_work based retry of the operation, attempting to copy the data
3391 * again. If the latter fails because the page was NOT uptodate, then we will
3392 * do a thread based blocking retry of the operation. That's the unexpected
3393 * slow path.
3394 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003395static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3396 int sync, void *arg)
3397{
3398 struct wait_page_queue *wpq;
3399 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003400 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003401
3402 wpq = container_of(wait, struct wait_page_queue, wait);
3403
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003404 if (!wake_page_match(wpq, key))
3405 return 0;
3406
Hao Xuc8d317a2020-09-29 20:00:45 +08003407 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003408 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003409 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003410 return 1;
3411}
3412
Jens Axboec1dd91d2020-08-03 16:43:59 -06003413/*
3414 * This controls whether a given IO request should be armed for async page
3415 * based retry. If we return false here, the request is handed to the async
3416 * worker threads for retry. If we're doing buffered reads on a regular file,
3417 * we prepare a private wait_page_queue entry and retry the operation. This
3418 * will either succeed because the page is now uptodate and unlocked, or it
3419 * will register a callback when the page is unlocked at IO completion. Through
3420 * that callback, io_uring uses task_work to setup a retry of the operation.
3421 * That retry will attempt the buffered read again. The retry will generally
3422 * succeed, or in rare cases where it fails, we then fall back to using the
3423 * async worker threads for a blocking retry.
3424 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003425static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003426{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003427 struct io_async_rw *rw = req->async_data;
3428 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003429 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003430
3431 /* never retry for NOWAIT, we just complete with -EAGAIN */
3432 if (req->flags & REQ_F_NOWAIT)
3433 return false;
3434
Jens Axboe227c0c92020-08-13 11:51:40 -06003435 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003436 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003437 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003438
Jens Axboebcf5a062020-05-22 09:24:42 -06003439 /*
3440 * just use poll if we can, and don't attempt if the fs doesn't
3441 * support callback based unlocks
3442 */
3443 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3444 return false;
3445
Jens Axboe3b2a4432020-08-16 10:58:43 -07003446 wait->wait.func = io_async_buf_func;
3447 wait->wait.private = req;
3448 wait->wait.flags = 0;
3449 INIT_LIST_HEAD(&wait->wait.entry);
3450 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003451 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003452 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003453 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003454}
3455
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003456static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003457{
3458 if (req->file->f_op->read_iter)
3459 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003460 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003461 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003462 else
3463 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003464}
3465
Ming Lei7db30432021-08-21 23:07:51 +08003466static bool need_read_all(struct io_kiocb *req)
3467{
3468 return req->flags & REQ_F_ISREG ||
3469 S_ISBLK(file_inode(req->file)->i_mode);
3470}
3471
Pavel Begunkov889fca72021-02-10 00:03:09 +00003472static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003473{
3474 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003475 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003476 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003477 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003478 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003479 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003480
Pavel Begunkov2846c482020-11-07 13:16:27 +00003481 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003482 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003483 iovec = NULL;
3484 } else {
3485 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3486 if (ret < 0)
3487 return ret;
3488 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003489 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003490 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003491
Jens Axboefd6c2e42019-12-18 12:19:41 -07003492 /* Ensure we clear previously set non-block flag */
3493 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003494 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003495 else
3496 kiocb->ki_flags |= IOCB_NOWAIT;
3497
Pavel Begunkov24c74672020-06-21 13:09:51 +03003498 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003499 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003500 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003501 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003502 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003503
Pavel Begunkov632546c2020-11-07 13:16:26 +00003504 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003505 if (unlikely(ret)) {
3506 kfree(iovec);
3507 return ret;
3508 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003509
Jens Axboe227c0c92020-08-13 11:51:40 -06003510 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003511
Jens Axboe230d50d2021-04-01 20:41:15 -06003512 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f2332021-04-08 01:54:39 +01003513 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003514 /* IOPOLL retry should happen for io-wq threads */
3515 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003516 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003517 /* no retry on NONBLOCK nor RWF_NOWAIT */
3518 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003519 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003520 /* some cases will consume bytes even on error returns */
Pavel Begunkov89c2b3b2021-08-23 11:18:45 +01003521 iov_iter_reexpand(iter, iter->count + iter->truncated);
Pavel Begunkov632546c2020-11-07 13:16:26 +00003522 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003523 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003524 } else if (ret == -EIOCBQUEUED) {
3525 goto out_free;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003526 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Ming Lei7db30432021-08-21 23:07:51 +08003527 (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003528 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003529 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003530 }
3531
Jens Axboe227c0c92020-08-13 11:51:40 -06003532 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003533 if (ret2)
3534 return ret2;
3535
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003536 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003537 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003538 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003539 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003540
Pavel Begunkovb23df912021-02-04 13:52:04 +00003541 do {
3542 io_size -= ret;
3543 rw->bytes_done += ret;
3544 /* if we can retry, do so with the callbacks armed */
3545 if (!io_rw_should_retry(req)) {
3546 kiocb->ki_flags &= ~IOCB_WAITQ;
3547 return -EAGAIN;
3548 }
3549
3550 /*
3551 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3552 * we get -EIOCBQUEUED, then we'll get a notification when the
3553 * desired page gets unlocked. We can also get a partial read
3554 * here, and if we do, then just retry at the new offset.
3555 */
3556 ret = io_iter_do_read(req, iter);
3557 if (ret == -EIOCBQUEUED)
3558 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003559 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003560 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003561 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003562done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003563 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003564out_free:
3565 /* it's faster to check here then delegate to kfree */
3566 if (iovec)
3567 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003568 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003569}
3570
Pavel Begunkov73debe62020-09-30 22:57:54 +03003571static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003572{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003573 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3574 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003575 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003576}
3577
Pavel Begunkov889fca72021-02-10 00:03:09 +00003578static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003579{
3580 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003581 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003582 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003583 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003584 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003585 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003586
Pavel Begunkov2846c482020-11-07 13:16:27 +00003587 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003588 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003589 iovec = NULL;
3590 } else {
3591 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3592 if (ret < 0)
3593 return ret;
3594 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003595 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003596 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597
Jens Axboefd6c2e42019-12-18 12:19:41 -07003598 /* Ensure we clear previously set non-block flag */
3599 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003600 kiocb->ki_flags &= ~IOCB_NOWAIT;
3601 else
3602 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003603
Pavel Begunkov24c74672020-06-21 13:09:51 +03003604 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003605 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003606 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003607
Jens Axboe10d59342019-12-09 20:16:22 -07003608 /* file path doesn't support NOWAIT for non-direct_IO */
3609 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3610 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003611 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003612
Pavel Begunkov632546c2020-11-07 13:16:26 +00003613 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003614 if (unlikely(ret))
3615 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003616
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003617 /*
3618 * Open-code file_start_write here to grab freeze protection,
3619 * which will be released by another thread in
3620 * io_complete_rw(). Fool lockdep by telling it the lock got
3621 * released so that it doesn't complain about the held lock when
3622 * we return to userspace.
3623 */
3624 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003625 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003626 __sb_writers_release(file_inode(req->file)->i_sb,
3627 SB_FREEZE_WRITE);
3628 }
3629 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003630
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003631 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003632 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003633 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003634 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003635 else
3636 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003637
Pavel Begunkov6ad7f2332021-04-08 01:54:39 +01003638 if (req->flags & REQ_F_REISSUE) {
3639 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003640 ret2 = -EAGAIN;
Pavel Begunkov6ad7f2332021-04-08 01:54:39 +01003641 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003642
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003643 /*
3644 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3645 * retry them without IOCB_NOWAIT.
3646 */
3647 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3648 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003649 /* no retry on NONBLOCK nor RWF_NOWAIT */
3650 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003651 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003652 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003653 /* IOPOLL retry should happen for io-wq threads */
3654 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3655 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003656done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003657 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003658 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003659copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003660 /* some cases will consume bytes even on error returns */
Pavel Begunkov89c2b3b2021-08-23 11:18:45 +01003661 iov_iter_reexpand(iter, iter->count + iter->truncated);
Pavel Begunkov632546c2020-11-07 13:16:26 +00003662 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003663 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003664 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003665 }
Jens Axboe31b51512019-01-18 22:56:34 -07003666out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003667 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003668 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003669 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003670 return ret;
3671}
3672
Jens Axboe80a261f2020-09-28 14:23:58 -06003673static int io_renameat_prep(struct io_kiocb *req,
3674 const struct io_uring_sqe *sqe)
3675{
3676 struct io_rename *ren = &req->rename;
3677 const char __user *oldf, *newf;
3678
Jens Axboeed7eb252021-06-23 09:04:13 -06003679 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3680 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003681 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003682 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003683 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3684 return -EBADF;
3685
3686 ren->old_dfd = READ_ONCE(sqe->fd);
3687 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3688 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3689 ren->new_dfd = READ_ONCE(sqe->len);
3690 ren->flags = READ_ONCE(sqe->rename_flags);
3691
3692 ren->oldpath = getname(oldf);
3693 if (IS_ERR(ren->oldpath))
3694 return PTR_ERR(ren->oldpath);
3695
3696 ren->newpath = getname(newf);
3697 if (IS_ERR(ren->newpath)) {
3698 putname(ren->oldpath);
3699 return PTR_ERR(ren->newpath);
3700 }
3701
3702 req->flags |= REQ_F_NEED_CLEANUP;
3703 return 0;
3704}
3705
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003706static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003707{
3708 struct io_rename *ren = &req->rename;
3709 int ret;
3710
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003711 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003712 return -EAGAIN;
3713
3714 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3715 ren->newpath, ren->flags);
3716
3717 req->flags &= ~REQ_F_NEED_CLEANUP;
3718 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003719 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003720 io_req_complete(req, ret);
3721 return 0;
3722}
3723
Jens Axboe14a11432020-09-28 14:27:37 -06003724static int io_unlinkat_prep(struct io_kiocb *req,
3725 const struct io_uring_sqe *sqe)
3726{
3727 struct io_unlink *un = &req->unlink;
3728 const char __user *fname;
3729
Jens Axboe22634bc2021-06-23 09:07:45 -06003730 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3731 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003732 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3733 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003734 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003735 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3736 return -EBADF;
3737
3738 un->dfd = READ_ONCE(sqe->fd);
3739
3740 un->flags = READ_ONCE(sqe->unlink_flags);
3741 if (un->flags & ~AT_REMOVEDIR)
3742 return -EINVAL;
3743
3744 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3745 un->filename = getname(fname);
3746 if (IS_ERR(un->filename))
3747 return PTR_ERR(un->filename);
3748
3749 req->flags |= REQ_F_NEED_CLEANUP;
3750 return 0;
3751}
3752
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003753static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003754{
3755 struct io_unlink *un = &req->unlink;
3756 int ret;
3757
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003758 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003759 return -EAGAIN;
3760
3761 if (un->flags & AT_REMOVEDIR)
3762 ret = do_rmdir(un->dfd, un->filename);
3763 else
3764 ret = do_unlinkat(un->dfd, un->filename);
3765
3766 req->flags &= ~REQ_F_NEED_CLEANUP;
3767 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003768 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003769 io_req_complete(req, ret);
3770 return 0;
3771}
3772
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07003773static int io_mkdirat_prep(struct io_kiocb *req,
3774 const struct io_uring_sqe *sqe)
3775{
3776 struct io_mkdir *mkd = &req->mkdir;
3777 const char __user *fname;
3778
3779 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3780 return -EINVAL;
3781 if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
3782 sqe->splice_fd_in)
3783 return -EINVAL;
3784 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3785 return -EBADF;
3786
3787 mkd->dfd = READ_ONCE(sqe->fd);
3788 mkd->mode = READ_ONCE(sqe->len);
3789
3790 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3791 mkd->filename = getname(fname);
3792 if (IS_ERR(mkd->filename))
3793 return PTR_ERR(mkd->filename);
3794
3795 req->flags |= REQ_F_NEED_CLEANUP;
3796 return 0;
3797}
3798
3799static int io_mkdirat(struct io_kiocb *req, int issue_flags)
3800{
3801 struct io_mkdir *mkd = &req->mkdir;
3802 int ret;
3803
3804 if (issue_flags & IO_URING_F_NONBLOCK)
3805 return -EAGAIN;
3806
3807 ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);
3808
3809 req->flags &= ~REQ_F_NEED_CLEANUP;
3810 if (ret < 0)
3811 req_set_fail(req);
3812 io_req_complete(req, ret);
3813 return 0;
3814}
3815
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07003816static int io_symlinkat_prep(struct io_kiocb *req,
3817 const struct io_uring_sqe *sqe)
3818{
3819 struct io_symlink *sl = &req->symlink;
3820 const char __user *oldpath, *newpath;
3821
3822 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3823 return -EINVAL;
3824 if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index ||
3825 sqe->splice_fd_in)
3826 return -EINVAL;
3827 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3828 return -EBADF;
3829
3830 sl->new_dfd = READ_ONCE(sqe->fd);
3831 oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr));
3832 newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3833
3834 sl->oldpath = getname(oldpath);
3835 if (IS_ERR(sl->oldpath))
3836 return PTR_ERR(sl->oldpath);
3837
3838 sl->newpath = getname(newpath);
3839 if (IS_ERR(sl->newpath)) {
3840 putname(sl->oldpath);
3841 return PTR_ERR(sl->newpath);
3842 }
3843
3844 req->flags |= REQ_F_NEED_CLEANUP;
3845 return 0;
3846}
3847
3848static int io_symlinkat(struct io_kiocb *req, int issue_flags)
3849{
3850 struct io_symlink *sl = &req->symlink;
3851 int ret;
3852
3853 if (issue_flags & IO_URING_F_NONBLOCK)
3854 return -EAGAIN;
3855
3856 ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath);
3857
3858 req->flags &= ~REQ_F_NEED_CLEANUP;
3859 if (ret < 0)
3860 req_set_fail(req);
3861 io_req_complete(req, ret);
3862 return 0;
3863}
3864
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07003865static int io_linkat_prep(struct io_kiocb *req,
3866 const struct io_uring_sqe *sqe)
3867{
3868 struct io_hardlink *lnk = &req->hardlink;
3869 const char __user *oldf, *newf;
3870
3871 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3872 return -EINVAL;
3873 if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
3874 return -EINVAL;
3875 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3876 return -EBADF;
3877
3878 lnk->old_dfd = READ_ONCE(sqe->fd);
3879 lnk->new_dfd = READ_ONCE(sqe->len);
3880 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3881 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3882 lnk->flags = READ_ONCE(sqe->hardlink_flags);
3883
3884 lnk->oldpath = getname(oldf);
3885 if (IS_ERR(lnk->oldpath))
3886 return PTR_ERR(lnk->oldpath);
3887
3888 lnk->newpath = getname(newf);
3889 if (IS_ERR(lnk->newpath)) {
3890 putname(lnk->oldpath);
3891 return PTR_ERR(lnk->newpath);
3892 }
3893
3894 req->flags |= REQ_F_NEED_CLEANUP;
3895 return 0;
3896}
3897
3898static int io_linkat(struct io_kiocb *req, int issue_flags)
3899{
3900 struct io_hardlink *lnk = &req->hardlink;
3901 int ret;
3902
3903 if (issue_flags & IO_URING_F_NONBLOCK)
3904 return -EAGAIN;
3905
3906 ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd,
3907 lnk->newpath, lnk->flags);
3908
3909 req->flags &= ~REQ_F_NEED_CLEANUP;
3910 if (ret < 0)
3911 req_set_fail(req);
3912 io_req_complete(req, ret);
3913 return 0;
3914}
3915
Jens Axboe36f4fa62020-09-05 11:14:22 -06003916static int io_shutdown_prep(struct io_kiocb *req,
3917 const struct io_uring_sqe *sqe)
3918{
3919#if defined(CONFIG_NET)
3920 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3921 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003922 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3923 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003924 return -EINVAL;
3925
3926 req->shutdown.how = READ_ONCE(sqe->len);
3927 return 0;
3928#else
3929 return -EOPNOTSUPP;
3930#endif
3931}
3932
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003933static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003934{
3935#if defined(CONFIG_NET)
3936 struct socket *sock;
3937 int ret;
3938
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003939 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003940 return -EAGAIN;
3941
Linus Torvalds48aba792020-12-16 12:44:05 -08003942 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003943 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003944 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003945
3946 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003947 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003948 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003949 io_req_complete(req, ret);
3950 return 0;
3951#else
3952 return -EOPNOTSUPP;
3953#endif
3954}
3955
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003956static int __io_splice_prep(struct io_kiocb *req,
3957 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003958{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003959 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003960 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003961
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003962 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3963 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003964
3965 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003966 sp->len = READ_ONCE(sqe->len);
3967 sp->flags = READ_ONCE(sqe->splice_flags);
3968
3969 if (unlikely(sp->flags & ~valid_flags))
3970 return -EINVAL;
3971
Pavel Begunkov62906e82021-08-10 14:52:47 +01003972 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003973 (sp->flags & SPLICE_F_FD_IN_FIXED));
3974 if (!sp->file_in)
3975 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003976 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003977 return 0;
3978}
3979
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003980static int io_tee_prep(struct io_kiocb *req,
3981 const struct io_uring_sqe *sqe)
3982{
3983 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3984 return -EINVAL;
3985 return __io_splice_prep(req, sqe);
3986}
3987
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003988static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003989{
3990 struct io_splice *sp = &req->splice;
3991 struct file *in = sp->file_in;
3992 struct file *out = sp->file_out;
3993 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3994 long ret = 0;
3995
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003996 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003997 return -EAGAIN;
3998 if (sp->len)
3999 ret = do_tee(in, out, sp->len, flags);
4000
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004001 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4002 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004003 req->flags &= ~REQ_F_NEED_CLEANUP;
4004
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004005 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004006 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004007 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004008 return 0;
4009}
4010
4011static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4012{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01004013 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03004014
4015 sp->off_in = READ_ONCE(sqe->splice_off_in);
4016 sp->off_out = READ_ONCE(sqe->off);
4017 return __io_splice_prep(req, sqe);
4018}
4019
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004020static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004021{
4022 struct io_splice *sp = &req->splice;
4023 struct file *in = sp->file_in;
4024 struct file *out = sp->file_out;
4025 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4026 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004027 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004028
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004029 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004030 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004031
4032 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4033 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004034
Jens Axboe948a7742020-05-17 14:21:38 -06004035 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004036 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004037
Pavel Begunkove1d767f2021-03-19 17:22:43 +00004038 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
4039 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004040 req->flags &= ~REQ_F_NEED_CLEANUP;
4041
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004042 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004043 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004044 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004045 return 0;
4046}
4047
Jens Axboe2b188cc2019-01-07 10:46:33 -07004048/*
4049 * IORING_OP_NOP just posts a completion event, nothing else.
4050 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004051static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004052{
4053 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004054
Jens Axboedef596e2019-01-09 08:59:42 -07004055 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4056 return -EINVAL;
4057
Pavel Begunkov889fca72021-02-10 00:03:09 +00004058 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004059 return 0;
4060}
4061
Pavel Begunkov1155c762021-02-18 18:29:38 +00004062static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004063{
Jens Axboe6b063142019-01-10 22:13:58 -07004064 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004065
Jens Axboe09bb8392019-03-13 12:39:28 -06004066 if (!req->file)
4067 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004068
Jens Axboe6b063142019-01-10 22:13:58 -07004069 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004070 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004071 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4072 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004073 return -EINVAL;
4074
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004075 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4076 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4077 return -EINVAL;
4078
4079 req->sync.off = READ_ONCE(sqe->off);
4080 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004081 return 0;
4082}
4083
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004084static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004085{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004086 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004087 int ret;
4088
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004089 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004090 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004091 return -EAGAIN;
4092
Jens Axboe9adbd452019-12-20 08:45:55 -07004093 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004094 end > 0 ? end : LLONG_MAX,
4095 req->sync.flags & IORING_FSYNC_DATASYNC);
4096 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);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004099 return 0;
4100}
4101
Jens Axboed63d1b52019-12-10 10:38:56 -07004102static int io_fallocate_prep(struct io_kiocb *req,
4103 const struct io_uring_sqe *sqe)
4104{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004105 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
4106 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07004107 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004108 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4109 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004110
4111 req->sync.off = READ_ONCE(sqe->off);
4112 req->sync.len = READ_ONCE(sqe->addr);
4113 req->sync.mode = READ_ONCE(sqe->len);
4114 return 0;
4115}
4116
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004117static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004118{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004119 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004120
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004121 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004122 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004123 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004124 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4125 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004126 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004127 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004128 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004129 return 0;
4130}
4131
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004132static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004133{
Jens Axboef8748882020-01-08 17:47:02 -07004134 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004135 int ret;
4136
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004137 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4138 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004139 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004140 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004141 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004142 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004143
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004144 /* open.how should be already initialised */
4145 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004146 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004147
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004148 req->open.dfd = READ_ONCE(sqe->fd);
4149 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004150 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004151 if (IS_ERR(req->open.filename)) {
4152 ret = PTR_ERR(req->open.filename);
4153 req->open.filename = NULL;
4154 return ret;
4155 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01004156
4157 req->open.file_slot = READ_ONCE(sqe->file_index);
4158 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
4159 return -EINVAL;
4160
Jens Axboe4022e7a2020-03-19 19:23:18 -06004161 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004162 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004163 return 0;
4164}
4165
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004166static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4167{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01004168 u64 mode = READ_ONCE(sqe->len);
4169 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004170
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004171 req->open.how = build_open_how(flags, mode);
4172 return __io_openat_prep(req, sqe);
4173}
4174
Jens Axboecebdb982020-01-08 17:59:24 -07004175static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4176{
4177 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004178 size_t len;
4179 int ret;
4180
Jens Axboecebdb982020-01-08 17:59:24 -07004181 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4182 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004183 if (len < OPEN_HOW_SIZE_VER0)
4184 return -EINVAL;
4185
4186 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4187 len);
4188 if (ret)
4189 return ret;
4190
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004191 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004192}
4193
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004194static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004195{
4196 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004197 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01004198 bool resolve_nonblock, nonblock_set;
4199 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004200 int ret;
4201
Jens Axboecebdb982020-01-08 17:59:24 -07004202 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004203 if (ret)
4204 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004205 nonblock_set = op.open_flag & O_NONBLOCK;
4206 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004207 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004208 /*
4209 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4210 * it'll always -EAGAIN
4211 */
4212 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4213 return -EAGAIN;
4214 op.lookup_flags |= LOOKUP_CACHED;
4215 op.open_flag |= O_NONBLOCK;
4216 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004217
Pavel Begunkovb9445592021-08-25 12:25:45 +01004218 if (!fixed) {
4219 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
4220 if (ret < 0)
4221 goto err;
4222 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004223
4224 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004225 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004226 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004227 * We could hang on to this 'fd' on retrying, but seems like
4228 * marginal gain for something that is now known to be a slower
4229 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07004230 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01004231 if (!fixed)
4232 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004233
4234 ret = PTR_ERR(file);
4235 /* only retry if RESOLVE_CACHED wasn't already set by application */
4236 if (ret == -EAGAIN &&
4237 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4238 return -EAGAIN;
4239 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004240 }
4241
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004242 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4243 file->f_flags &= ~O_NONBLOCK;
4244 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004245
4246 if (!fixed)
4247 fd_install(ret, file);
4248 else
4249 ret = io_install_fixed_file(req, file, issue_flags,
4250 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004251err:
4252 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004253 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004254 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004255 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004256 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004257 return 0;
4258}
4259
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004260static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004261{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004262 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004263}
4264
Jens Axboe067524e2020-03-02 16:32:28 -07004265static int io_remove_buffers_prep(struct io_kiocb *req,
4266 const struct io_uring_sqe *sqe)
4267{
4268 struct io_provide_buf *p = &req->pbuf;
4269 u64 tmp;
4270
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004271 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4272 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004273 return -EINVAL;
4274
4275 tmp = READ_ONCE(sqe->fd);
4276 if (!tmp || tmp > USHRT_MAX)
4277 return -EINVAL;
4278
4279 memset(p, 0, sizeof(*p));
4280 p->nbufs = tmp;
4281 p->bgid = READ_ONCE(sqe->buf_group);
4282 return 0;
4283}
4284
4285static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4286 int bgid, unsigned nbufs)
4287{
4288 unsigned i = 0;
4289
4290 /* shouldn't happen */
4291 if (!nbufs)
4292 return 0;
4293
4294 /* the head kbuf is the list itself */
4295 while (!list_empty(&buf->list)) {
4296 struct io_buffer *nxt;
4297
4298 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4299 list_del(&nxt->list);
4300 kfree(nxt);
4301 if (++i == nbufs)
4302 return i;
4303 }
4304 i++;
4305 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004306 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004307
4308 return i;
4309}
4310
Pavel Begunkov889fca72021-02-10 00:03:09 +00004311static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004312{
4313 struct io_provide_buf *p = &req->pbuf;
4314 struct io_ring_ctx *ctx = req->ctx;
4315 struct io_buffer *head;
4316 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004317 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004318
4319 io_ring_submit_lock(ctx, !force_nonblock);
4320
4321 lockdep_assert_held(&ctx->uring_lock);
4322
4323 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004324 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004325 if (head)
4326 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004327 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004328 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004329
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004330 /* complete before unlock, IOPOLL may need the lock */
4331 __io_req_complete(req, issue_flags, ret, 0);
4332 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004333 return 0;
4334}
4335
Jens Axboeddf0322d2020-02-23 16:41:33 -07004336static int io_provide_buffers_prep(struct io_kiocb *req,
4337 const struct io_uring_sqe *sqe)
4338{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004339 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004340 struct io_provide_buf *p = &req->pbuf;
4341 u64 tmp;
4342
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004343 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004344 return -EINVAL;
4345
4346 tmp = READ_ONCE(sqe->fd);
4347 if (!tmp || tmp > USHRT_MAX)
4348 return -E2BIG;
4349 p->nbufs = tmp;
4350 p->addr = READ_ONCE(sqe->addr);
4351 p->len = READ_ONCE(sqe->len);
4352
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004353 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4354 &size))
4355 return -EOVERFLOW;
4356 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4357 return -EOVERFLOW;
4358
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004359 size = (unsigned long)p->len * p->nbufs;
4360 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004361 return -EFAULT;
4362
4363 p->bgid = READ_ONCE(sqe->buf_group);
4364 tmp = READ_ONCE(sqe->off);
4365 if (tmp > USHRT_MAX)
4366 return -E2BIG;
4367 p->bid = tmp;
4368 return 0;
4369}
4370
4371static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4372{
4373 struct io_buffer *buf;
4374 u64 addr = pbuf->addr;
4375 int i, bid = pbuf->bid;
4376
4377 for (i = 0; i < pbuf->nbufs; i++) {
4378 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4379 if (!buf)
4380 break;
4381
4382 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004383 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004384 buf->bid = bid;
4385 addr += pbuf->len;
4386 bid++;
4387 if (!*head) {
4388 INIT_LIST_HEAD(&buf->list);
4389 *head = buf;
4390 } else {
4391 list_add_tail(&buf->list, &(*head)->list);
4392 }
4393 }
4394
4395 return i ? i : -ENOMEM;
4396}
4397
Pavel Begunkov889fca72021-02-10 00:03:09 +00004398static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004399{
4400 struct io_provide_buf *p = &req->pbuf;
4401 struct io_ring_ctx *ctx = req->ctx;
4402 struct io_buffer *head, *list;
4403 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004404 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004405
4406 io_ring_submit_lock(ctx, !force_nonblock);
4407
4408 lockdep_assert_held(&ctx->uring_lock);
4409
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004410 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004411
4412 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004413 if (ret >= 0 && !list) {
4414 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4415 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004416 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004417 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004418 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004419 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004420 /* complete before unlock, IOPOLL may need the lock */
4421 __io_req_complete(req, issue_flags, ret, 0);
4422 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004423 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004424}
4425
Jens Axboe3e4827b2020-01-08 15:18:09 -07004426static int io_epoll_ctl_prep(struct io_kiocb *req,
4427 const struct io_uring_sqe *sqe)
4428{
4429#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004430 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004431 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004432 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004433 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004434
4435 req->epoll.epfd = READ_ONCE(sqe->fd);
4436 req->epoll.op = READ_ONCE(sqe->len);
4437 req->epoll.fd = READ_ONCE(sqe->off);
4438
4439 if (ep_op_has_event(req->epoll.op)) {
4440 struct epoll_event __user *ev;
4441
4442 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4443 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4444 return -EFAULT;
4445 }
4446
4447 return 0;
4448#else
4449 return -EOPNOTSUPP;
4450#endif
4451}
4452
Pavel Begunkov889fca72021-02-10 00:03:09 +00004453static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004454{
4455#if defined(CONFIG_EPOLL)
4456 struct io_epoll *ie = &req->epoll;
4457 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004458 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004459
4460 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4461 if (force_nonblock && ret == -EAGAIN)
4462 return -EAGAIN;
4463
4464 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004465 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004466 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004467 return 0;
4468#else
4469 return -EOPNOTSUPP;
4470#endif
4471}
4472
Jens Axboec1ca7572019-12-25 22:18:28 -07004473static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4474{
4475#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004476 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004477 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004478 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4479 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004480
4481 req->madvise.addr = READ_ONCE(sqe->addr);
4482 req->madvise.len = READ_ONCE(sqe->len);
4483 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4484 return 0;
4485#else
4486 return -EOPNOTSUPP;
4487#endif
4488}
4489
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004490static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004491{
4492#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4493 struct io_madvise *ma = &req->madvise;
4494 int ret;
4495
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004496 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004497 return -EAGAIN;
4498
Minchan Kim0726b012020-10-17 16:14:50 -07004499 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004500 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004501 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004502 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004503 return 0;
4504#else
4505 return -EOPNOTSUPP;
4506#endif
4507}
4508
Jens Axboe4840e412019-12-25 22:03:45 -07004509static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4510{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004511 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004512 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004513 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4514 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004515
4516 req->fadvise.offset = READ_ONCE(sqe->off);
4517 req->fadvise.len = READ_ONCE(sqe->len);
4518 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4519 return 0;
4520}
4521
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004522static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004523{
4524 struct io_fadvise *fa = &req->fadvise;
4525 int ret;
4526
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004527 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004528 switch (fa->advice) {
4529 case POSIX_FADV_NORMAL:
4530 case POSIX_FADV_RANDOM:
4531 case POSIX_FADV_SEQUENTIAL:
4532 break;
4533 default:
4534 return -EAGAIN;
4535 }
4536 }
Jens Axboe4840e412019-12-25 22:03:45 -07004537
4538 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4539 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004540 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004541 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004542 return 0;
4543}
4544
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004545static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4546{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004547 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004548 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004549 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004550 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004551 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004552 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004553
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004554 req->statx.dfd = READ_ONCE(sqe->fd);
4555 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004556 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004557 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4558 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004559
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004560 return 0;
4561}
4562
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004563static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004564{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004565 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004566 int ret;
4567
Pavel Begunkov59d70012021-03-22 01:58:30 +00004568 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004569 return -EAGAIN;
4570
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004571 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4572 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004573
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004574 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004575 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004576 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004577 return 0;
4578}
4579
Jens Axboeb5dba592019-12-11 14:02:38 -07004580static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4581{
Jens Axboe14587a462020-09-05 11:36:08 -06004582 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004583 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004584 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004585 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004586 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004587 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004588 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004589
4590 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004591 return 0;
4592}
4593
Pavel Begunkov889fca72021-02-10 00:03:09 +00004594static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004595{
Jens Axboe9eac1902021-01-19 15:50:37 -07004596 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004597 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004598 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004599 struct file *file = NULL;
4600 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004601
Jens Axboe9eac1902021-01-19 15:50:37 -07004602 spin_lock(&files->file_lock);
4603 fdt = files_fdtable(files);
4604 if (close->fd >= fdt->max_fds) {
4605 spin_unlock(&files->file_lock);
4606 goto err;
4607 }
4608 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004609 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004610 spin_unlock(&files->file_lock);
4611 file = NULL;
4612 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004613 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004614
4615 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004616 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004617 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004618 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004619 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004620
Jens Axboe9eac1902021-01-19 15:50:37 -07004621 ret = __close_fd_get_file(close->fd, &file);
4622 spin_unlock(&files->file_lock);
4623 if (ret < 0) {
4624 if (ret == -ENOENT)
4625 ret = -EBADF;
4626 goto err;
4627 }
4628
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004629 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004630 ret = filp_close(file, current->files);
4631err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004632 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004633 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004634 if (file)
4635 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004636 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004637 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004638}
4639
Pavel Begunkov1155c762021-02-18 18:29:38 +00004640static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004641{
4642 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004643
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004644 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4645 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004646 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4647 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004648 return -EINVAL;
4649
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004650 req->sync.off = READ_ONCE(sqe->off);
4651 req->sync.len = READ_ONCE(sqe->len);
4652 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004653 return 0;
4654}
4655
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004656static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004657{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004658 int ret;
4659
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004660 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004661 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004662 return -EAGAIN;
4663
Jens Axboe9adbd452019-12-20 08:45:55 -07004664 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004665 req->sync.flags);
4666 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004667 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004668 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004669 return 0;
4670}
4671
YueHaibing469956e2020-03-04 15:53:52 +08004672#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004673static int io_setup_async_msg(struct io_kiocb *req,
4674 struct io_async_msghdr *kmsg)
4675{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004676 struct io_async_msghdr *async_msg = req->async_data;
4677
4678 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004679 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004680 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004681 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004682 return -ENOMEM;
4683 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004684 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004685 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004686 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004687 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004688 /* if were using fast_iov, set it to the new one */
4689 if (!async_msg->free_iov)
4690 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4691
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004692 return -EAGAIN;
4693}
4694
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004695static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4696 struct io_async_msghdr *iomsg)
4697{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004698 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004699 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004700 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004701 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004702}
4703
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004704static int io_sendmsg_prep_async(struct io_kiocb *req)
4705{
4706 int ret;
4707
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004708 ret = io_sendmsg_copy_hdr(req, req->async_data);
4709 if (!ret)
4710 req->flags |= REQ_F_NEED_CLEANUP;
4711 return ret;
4712}
4713
Jens Axboe3529d8c2019-12-19 18:24:38 -07004714static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004715{
Jens Axboee47293f2019-12-20 08:58:21 -07004716 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004717
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004718 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4719 return -EINVAL;
4720
Pavel Begunkov270a5942020-07-12 20:41:04 +03004721 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004722 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004723 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4724 if (sr->msg_flags & MSG_DONTWAIT)
4725 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004726
Jens Axboed8768362020-02-27 14:17:49 -07004727#ifdef CONFIG_COMPAT
4728 if (req->ctx->compat)
4729 sr->msg_flags |= MSG_CMSG_COMPAT;
4730#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004731 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004732}
4733
Pavel Begunkov889fca72021-02-10 00:03:09 +00004734static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004735{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004736 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004737 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004738 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004739 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004740 int ret;
4741
Florent Revestdba4a922020-12-04 12:36:04 +01004742 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004743 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004744 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004745
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004746 kmsg = req->async_data;
4747 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004748 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004749 if (ret)
4750 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004751 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004752 }
4753
Pavel Begunkov04411802021-04-01 15:44:00 +01004754 flags = req->sr_msg.msg_flags;
4755 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004756 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004757 if (flags & MSG_WAITALL)
4758 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4759
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004760 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004761 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004762 return io_setup_async_msg(req, kmsg);
4763 if (ret == -ERESTARTSYS)
4764 ret = -EINTR;
4765
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004766 /* fast path, check for non-NULL to avoid function call */
4767 if (kmsg->free_iov)
4768 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004769 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004770 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004771 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004772 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004773 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004774}
4775
Pavel Begunkov889fca72021-02-10 00:03:09 +00004776static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004777{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004778 struct io_sr_msg *sr = &req->sr_msg;
4779 struct msghdr msg;
4780 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004781 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004782 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004783 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004784 int ret;
4785
Florent Revestdba4a922020-12-04 12:36:04 +01004786 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004787 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004788 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004789
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004790 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4791 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004792 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004793
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004794 msg.msg_name = NULL;
4795 msg.msg_control = NULL;
4796 msg.msg_controllen = 0;
4797 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004798
Pavel Begunkov04411802021-04-01 15:44:00 +01004799 flags = req->sr_msg.msg_flags;
4800 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004801 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004802 if (flags & MSG_WAITALL)
4803 min_ret = iov_iter_count(&msg.msg_iter);
4804
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004805 msg.msg_flags = flags;
4806 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004807 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004808 return -EAGAIN;
4809 if (ret == -ERESTARTSYS)
4810 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004811
Stefan Metzmacher00312752021-03-20 20:33:36 +01004812 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004813 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004814 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004815 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004816}
4817
Pavel Begunkov1400e692020-07-12 20:41:05 +03004818static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4819 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004820{
4821 struct io_sr_msg *sr = &req->sr_msg;
4822 struct iovec __user *uiov;
4823 size_t iov_len;
4824 int ret;
4825
Pavel Begunkov1400e692020-07-12 20:41:05 +03004826 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4827 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004828 if (ret)
4829 return ret;
4830
4831 if (req->flags & REQ_F_BUFFER_SELECT) {
4832 if (iov_len > 1)
4833 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004834 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004835 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004836 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004837 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004838 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004839 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004840 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004841 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004842 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004843 if (ret > 0)
4844 ret = 0;
4845 }
4846
4847 return ret;
4848}
4849
4850#ifdef CONFIG_COMPAT
4851static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004852 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004853{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004854 struct io_sr_msg *sr = &req->sr_msg;
4855 struct compat_iovec __user *uiov;
4856 compat_uptr_t ptr;
4857 compat_size_t len;
4858 int ret;
4859
Pavel Begunkov4af34172021-04-11 01:46:30 +01004860 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4861 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004862 if (ret)
4863 return ret;
4864
4865 uiov = compat_ptr(ptr);
4866 if (req->flags & REQ_F_BUFFER_SELECT) {
4867 compat_ssize_t clen;
4868
4869 if (len > 1)
4870 return -EINVAL;
4871 if (!access_ok(uiov, sizeof(*uiov)))
4872 return -EFAULT;
4873 if (__get_user(clen, &uiov->iov_len))
4874 return -EFAULT;
4875 if (clen < 0)
4876 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004877 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004878 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004879 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004880 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004881 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004882 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004883 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004884 if (ret < 0)
4885 return ret;
4886 }
4887
4888 return 0;
4889}
Jens Axboe03b12302019-12-02 18:50:25 -07004890#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004891
Pavel Begunkov1400e692020-07-12 20:41:05 +03004892static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4893 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004894{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004895 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004896
4897#ifdef CONFIG_COMPAT
4898 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004899 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004900#endif
4901
Pavel Begunkov1400e692020-07-12 20:41:05 +03004902 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004903}
4904
Jens Axboebcda7ba2020-02-23 16:42:51 -07004905static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004906 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004907{
4908 struct io_sr_msg *sr = &req->sr_msg;
4909 struct io_buffer *kbuf;
4910
Jens Axboebcda7ba2020-02-23 16:42:51 -07004911 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4912 if (IS_ERR(kbuf))
4913 return kbuf;
4914
4915 sr->kbuf = kbuf;
4916 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004917 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004918}
4919
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004920static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4921{
4922 return io_put_kbuf(req, req->sr_msg.kbuf);
4923}
4924
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004925static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004926{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004927 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004928
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004929 ret = io_recvmsg_copy_hdr(req, req->async_data);
4930 if (!ret)
4931 req->flags |= REQ_F_NEED_CLEANUP;
4932 return ret;
4933}
4934
4935static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4936{
4937 struct io_sr_msg *sr = &req->sr_msg;
4938
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004939 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4940 return -EINVAL;
4941
Pavel Begunkov270a5942020-07-12 20:41:04 +03004942 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004943 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004944 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004945 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4946 if (sr->msg_flags & MSG_DONTWAIT)
4947 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004948
Jens Axboed8768362020-02-27 14:17:49 -07004949#ifdef CONFIG_COMPAT
4950 if (req->ctx->compat)
4951 sr->msg_flags |= MSG_CMSG_COMPAT;
4952#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004953 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004954}
4955
Pavel Begunkov889fca72021-02-10 00:03:09 +00004956static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004957{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004958 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004959 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004960 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004961 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004962 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004963 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004964 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004965
Florent Revestdba4a922020-12-04 12:36:04 +01004966 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004967 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004968 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004969
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004970 kmsg = req->async_data;
4971 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004972 ret = io_recvmsg_copy_hdr(req, &iomsg);
4973 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004974 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004975 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004976 }
4977
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004978 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004979 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004980 if (IS_ERR(kbuf))
4981 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004982 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004983 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4984 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004985 1, req->sr_msg.len);
4986 }
4987
Pavel Begunkov04411802021-04-01 15:44:00 +01004988 flags = req->sr_msg.msg_flags;
4989 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004990 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004991 if (flags & MSG_WAITALL)
4992 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4993
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004994 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4995 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004996 if (force_nonblock && ret == -EAGAIN)
4997 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004998 if (ret == -ERESTARTSYS)
4999 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03005000
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005001 if (req->flags & REQ_F_BUFFER_SELECTED)
5002 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00005003 /* fast path, check for non-NULL to avoid function call */
5004 if (kmsg->free_iov)
5005 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03005006 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005007 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005008 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005009 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06005010 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06005011}
5012
Pavel Begunkov889fca72021-02-10 00:03:09 +00005013static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005014{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005015 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005016 struct io_sr_msg *sr = &req->sr_msg;
5017 struct msghdr msg;
5018 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005019 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005020 struct iovec iov;
5021 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005022 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005023 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005024 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005025
Florent Revestdba4a922020-12-04 12:36:04 +01005026 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005027 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005028 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005029
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005030 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005031 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005032 if (IS_ERR(kbuf))
5033 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005034 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005035 }
5036
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005037 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005038 if (unlikely(ret))
5039 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005040
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005041 msg.msg_name = NULL;
5042 msg.msg_control = NULL;
5043 msg.msg_controllen = 0;
5044 msg.msg_namelen = 0;
5045 msg.msg_iocb = NULL;
5046 msg.msg_flags = 0;
5047
Pavel Begunkov04411802021-04-01 15:44:00 +01005048 flags = req->sr_msg.msg_flags;
5049 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005050 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01005051 if (flags & MSG_WAITALL)
5052 min_ret = iov_iter_count(&msg.msg_iter);
5053
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005054 ret = sock_recvmsg(sock, &msg, flags);
5055 if (force_nonblock && ret == -EAGAIN)
5056 return -EAGAIN;
5057 if (ret == -ERESTARTSYS)
5058 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005059out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005060 if (req->flags & REQ_F_BUFFER_SELECTED)
5061 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01005062 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005063 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005064 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005065 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005066}
5067
Jens Axboe3529d8c2019-12-19 18:24:38 -07005068static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005069{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005070 struct io_accept *accept = &req->accept;
5071
Jens Axboe14587a462020-09-05 11:36:08 -06005072 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005073 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005074 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005075 return -EINVAL;
5076
Jens Axboed55e5f52019-12-11 16:12:15 -07005077 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5078 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005079 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005080 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005081
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005082 accept->file_slot = READ_ONCE(sqe->file_index);
5083 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
5084 (accept->flags & SOCK_CLOEXEC)))
5085 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005086 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
5087 return -EINVAL;
5088 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
5089 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005090 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005091}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005092
Pavel Begunkov889fca72021-02-10 00:03:09 +00005093static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005094{
5095 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005096 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005097 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005098 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005099 struct file *file;
5100 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005101
Jiufei Xuee697dee2020-06-10 13:41:59 +08005102 if (req->file->f_flags & O_NONBLOCK)
5103 req->flags |= REQ_F_NOWAIT;
5104
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005105 if (!fixed) {
5106 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
5107 if (unlikely(fd < 0))
5108 return fd;
5109 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005110 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
5111 accept->flags);
5112 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005113 if (!fixed)
5114 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005115 ret = PTR_ERR(file);
5116 if (ret == -EAGAIN && force_nonblock)
5117 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005118 if (ret == -ERESTARTSYS)
5119 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005120 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005121 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01005122 fd_install(fd, file);
5123 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01005124 } else {
5125 ret = io_install_fixed_file(req, file, issue_flags,
5126 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005127 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005128 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005129 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005130}
5131
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005132static int io_connect_prep_async(struct io_kiocb *req)
5133{
5134 struct io_async_connect *io = req->async_data;
5135 struct io_connect *conn = &req->connect;
5136
5137 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5138}
5139
Jens Axboe3529d8c2019-12-19 18:24:38 -07005140static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005141{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005142 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005143
Jens Axboe14587a462020-09-05 11:36:08 -06005144 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005145 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005146 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
5147 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005148 return -EINVAL;
5149
Jens Axboe3529d8c2019-12-19 18:24:38 -07005150 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5151 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005152 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005153}
5154
Pavel Begunkov889fca72021-02-10 00:03:09 +00005155static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005156{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005157 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005158 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005159 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005160 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005161
Jens Axboee8c2bc12020-08-15 18:44:09 -07005162 if (req->async_data) {
5163 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005164 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005165 ret = move_addr_to_kernel(req->connect.addr,
5166 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005167 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005168 if (ret)
5169 goto out;
5170 io = &__io;
5171 }
5172
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005173 file_flags = force_nonblock ? O_NONBLOCK : 0;
5174
Jens Axboee8c2bc12020-08-15 18:44:09 -07005175 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005176 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005177 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005178 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005179 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005180 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005181 ret = -ENOMEM;
5182 goto out;
5183 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005184 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005185 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005186 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005187 if (ret == -ERESTARTSYS)
5188 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005189out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005190 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005191 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005192 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005193 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005194}
YueHaibing469956e2020-03-04 15:53:52 +08005195#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005196#define IO_NETOP_FN(op) \
5197static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5198{ \
5199 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005200}
5201
Jens Axboe99a10082021-02-19 09:35:19 -07005202#define IO_NETOP_PREP(op) \
5203IO_NETOP_FN(op) \
5204static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5205{ \
5206 return -EOPNOTSUPP; \
5207} \
5208
5209#define IO_NETOP_PREP_ASYNC(op) \
5210IO_NETOP_PREP(op) \
5211static int io_##op##_prep_async(struct io_kiocb *req) \
5212{ \
5213 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005214}
5215
Jens Axboe99a10082021-02-19 09:35:19 -07005216IO_NETOP_PREP_ASYNC(sendmsg);
5217IO_NETOP_PREP_ASYNC(recvmsg);
5218IO_NETOP_PREP_ASYNC(connect);
5219IO_NETOP_PREP(accept);
5220IO_NETOP_FN(send);
5221IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005222#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005223
Jens Axboed7718a92020-02-14 22:23:12 -07005224struct io_poll_table {
5225 struct poll_table_struct pt;
5226 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005227 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07005228 int error;
5229};
5230
Jens Axboed7718a92020-02-14 22:23:12 -07005231static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005232 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07005233{
Jens Axboed7718a92020-02-14 22:23:12 -07005234 /* for instances that support it check for an event match first: */
5235 if (mask && !(mask & poll->events))
5236 return 0;
5237
5238 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5239
5240 list_del_init(&poll->wait.entry);
5241
Jens Axboed7718a92020-02-14 22:23:12 -07005242 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005243 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005244
Jens Axboed7718a92020-02-14 22:23:12 -07005245 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005246 * If this fails, then the task is exiting. When a task exits, the
5247 * work gets canceled, so just cancel this request as well instead
5248 * of executing it. We can't safely execute it anyway, as we may not
5249 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005250 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005251 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005252 return 1;
5253}
5254
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005255static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5256 __acquires(&req->ctx->completion_lock)
5257{
5258 struct io_ring_ctx *ctx = req->ctx;
5259
Jens Axboe316319e2021-08-19 09:41:42 -06005260 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005261 if (unlikely(req->task->flags & PF_EXITING))
5262 WRITE_ONCE(poll->canceled, true);
5263
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005264 if (!req->result && !READ_ONCE(poll->canceled)) {
5265 struct poll_table_struct pt = { ._key = poll->events };
5266
5267 req->result = vfs_poll(req->file, &pt) & poll->events;
5268 }
5269
Jens Axboe79ebeae2021-08-10 15:18:27 -06005270 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005271 if (!req->result && !READ_ONCE(poll->canceled)) {
5272 add_wait_queue(poll->head, &poll->wait);
5273 return true;
5274 }
5275
5276 return false;
5277}
5278
Jens Axboed4e7cd32020-08-15 11:44:50 -07005279static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005280{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005281 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005282 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005283 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005284 return req->apoll->double_poll;
5285}
5286
5287static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5288{
5289 if (req->opcode == IORING_OP_POLL_ADD)
5290 return &req->poll;
5291 return &req->apoll->poll;
5292}
5293
5294static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005295 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005296{
5297 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005298
5299 lockdep_assert_held(&req->ctx->completion_lock);
5300
5301 if (poll && poll->head) {
5302 struct wait_queue_head *head = poll->head;
5303
Jens Axboe79ebeae2021-08-10 15:18:27 -06005304 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005305 list_del_init(&poll->wait.entry);
5306 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005307 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005308 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005309 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005310 }
5311}
5312
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005313static bool __io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005314 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005315{
5316 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005317 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005318 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005319
Pavel Begunkove27414b2021-04-09 09:13:20 +01005320 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005321 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005322 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005323 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005324 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005325 }
Jens Axboeb69de282021-03-17 08:37:41 -06005326 if (req->poll.events & EPOLLONESHOT)
5327 flags = 0;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005328 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005329 req->poll.done = true;
5330 flags = 0;
5331 }
Hao Xu7b289c32021-04-13 15:20:39 +08005332 if (flags & IORING_CQE_F_MORE)
5333 ctx->cq_extra++;
5334
Jens Axboe88e41cf2021-02-22 22:08:01 -07005335 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005336}
5337
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005338static inline bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
5339 __must_hold(&req->ctx->completion_lock)
5340{
5341 bool done;
5342
5343 done = __io_poll_complete(req, mask);
5344 io_commit_cqring(req->ctx);
5345 return done;
5346}
5347
Pavel Begunkovf237c302021-08-18 12:42:46 +01005348static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005349{
Jens Axboe6d816e02020-08-11 08:04:14 -06005350 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005351 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005352
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005353 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005354 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005355 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005356 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005357
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005358 done = __io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005359 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005360 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005361 hash_del(&req->hash_node);
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005362 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005363 req->result = 0;
5364 add_wait_queue(req->poll.head, &req->poll.wait);
5365 }
Xiaoguang Wang31efe482021-09-03 22:24:36 +08005366 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005367 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005368 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005369
Jens Axboe88e41cf2021-02-22 22:08:01 -07005370 if (done) {
5371 nxt = io_put_req_find_next(req);
5372 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005373 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005374 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005375 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005376}
5377
5378static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5379 int sync, void *key)
5380{
5381 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005382 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005383 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005384 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005385
5386 /* for instances that support it check for an event match first: */
5387 if (mask && !(mask & poll->events))
5388 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005389 if (!(poll->events & EPOLLONESHOT))
5390 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005391
Jens Axboe8706e042020-09-28 08:38:54 -06005392 list_del_init(&wait->entry);
5393
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005394 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005395 bool done;
5396
Jens Axboe79ebeae2021-08-10 15:18:27 -06005397 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005398 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005399 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005400 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005401 /* make sure double remove sees this as being gone */
5402 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005403 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005404 if (!done) {
5405 /* use wait func handler, so it matches the rq type */
5406 poll->wait.func(&poll->wait, mode, sync, key);
5407 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005408 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005409 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005410 return 1;
5411}
5412
5413static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5414 wait_queue_func_t wake_func)
5415{
5416 poll->head = NULL;
5417 poll->done = false;
5418 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005419#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5420 /* mask in events that we always want/need */
5421 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005422 INIT_LIST_HEAD(&poll->wait.entry);
5423 init_waitqueue_func_entry(&poll->wait, wake_func);
5424}
5425
5426static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005427 struct wait_queue_head *head,
5428 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005429{
5430 struct io_kiocb *req = pt->req;
5431
5432 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005433 * The file being polled uses multiple waitqueues for poll handling
5434 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5435 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005436 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005437 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005438 struct io_poll_iocb *poll_one = poll;
5439
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005440 /* double add on the same waitqueue head, ignore */
5441 if (poll_one->head == head)
5442 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005443 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005444 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005445 if ((*poll_ptr)->head == head)
5446 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005447 pt->error = -EINVAL;
5448 return;
5449 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005450 /*
5451 * Can't handle multishot for double wait for now, turn it
5452 * into one-shot mode.
5453 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005454 if (!(poll_one->events & EPOLLONESHOT))
5455 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005456 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5457 if (!poll) {
5458 pt->error = -ENOMEM;
5459 return;
5460 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005461 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005462 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005463 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005464 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005465 }
5466
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005467 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005468 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005469
5470 if (poll->events & EPOLLEXCLUSIVE)
5471 add_wait_queue_exclusive(head, &poll->wait);
5472 else
5473 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005474}
5475
5476static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5477 struct poll_table_struct *p)
5478{
5479 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005480 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005481
Jens Axboe807abcb2020-07-17 17:09:27 -06005482 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005483}
5484
Pavel Begunkovf237c302021-08-18 12:42:46 +01005485static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005486{
Jens Axboed7718a92020-02-14 22:23:12 -07005487 struct async_poll *apoll = req->apoll;
5488 struct io_ring_ctx *ctx = req->ctx;
5489
Olivier Langlois236daeae2021-05-31 02:36:37 -04005490 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005491
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005492 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005493 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005494 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005495 }
5496
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005497 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005498 io_poll_remove_double(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005499 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005500
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005501 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005502 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005503 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005504 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005505}
5506
5507static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5508 void *key)
5509{
5510 struct io_kiocb *req = wait->private;
5511 struct io_poll_iocb *poll = &req->apoll->poll;
5512
5513 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5514 key_to_poll(key));
5515
5516 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5517}
5518
5519static void io_poll_req_insert(struct io_kiocb *req)
5520{
5521 struct io_ring_ctx *ctx = req->ctx;
5522 struct hlist_head *list;
5523
5524 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5525 hlist_add_head(&req->hash_node, list);
5526}
5527
5528static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5529 struct io_poll_iocb *poll,
5530 struct io_poll_table *ipt, __poll_t mask,
5531 wait_queue_func_t wake_func)
5532 __acquires(&ctx->completion_lock)
5533{
5534 struct io_ring_ctx *ctx = req->ctx;
5535 bool cancel = false;
5536
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005537 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005538 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005539 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005540 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005541
5542 ipt->pt._key = mask;
5543 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005544 ipt->error = 0;
5545 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005546
Jens Axboed7718a92020-02-14 22:23:12 -07005547 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005548 if (unlikely(!ipt->nr_entries) && !ipt->error)
5549 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005550
Jens Axboe79ebeae2021-08-10 15:18:27 -06005551 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005552 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005553 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005554 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005555 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005556 if (unlikely(list_empty(&poll->wait.entry))) {
5557 if (ipt->error)
5558 cancel = true;
5559 ipt->error = 0;
5560 mask = 0;
5561 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005562 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005563 list_del_init(&poll->wait.entry);
5564 else if (cancel)
5565 WRITE_ONCE(poll->canceled, true);
5566 else if (!poll->done) /* actually waiting for an event */
5567 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005568 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005569 }
5570
5571 return mask;
5572}
5573
Olivier Langlois59b735a2021-06-22 05:17:39 -07005574enum {
5575 IO_APOLL_OK,
5576 IO_APOLL_ABORTED,
5577 IO_APOLL_READY
5578};
5579
5580static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005581{
5582 const struct io_op_def *def = &io_op_defs[req->opcode];
5583 struct io_ring_ctx *ctx = req->ctx;
5584 struct async_poll *apoll;
5585 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005586 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005587 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005588
5589 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005590 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005591 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005592 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005593 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005594 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005595
5596 if (def->pollin) {
5597 rw = READ;
5598 mask |= POLLIN | POLLRDNORM;
5599
5600 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5601 if ((req->opcode == IORING_OP_RECVMSG) &&
5602 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5603 mask &= ~POLLIN;
5604 } else {
5605 rw = WRITE;
5606 mask |= POLLOUT | POLLWRNORM;
5607 }
5608
Jens Axboe9dab14b2020-08-25 12:27:50 -06005609 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005610 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005611 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005612
5613 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5614 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005615 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005616 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005617 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005618 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005619 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005620 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005621
5622 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5623 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005624 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005625 if (ret || ipt.error)
5626 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5627
Olivier Langlois236daeae2021-05-31 02:36:37 -04005628 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5629 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005630 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005631}
5632
5633static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005634 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005635 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005636{
Jens Axboeb41e9852020-02-17 09:52:41 -07005637 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005638
Jens Axboe50826202021-02-23 09:02:26 -07005639 if (!poll->head)
5640 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005641 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005642 if (do_cancel)
5643 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005644 if (!list_empty(&poll->wait.entry)) {
5645 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005646 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005647 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005648 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005649 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005650 return do_complete;
5651}
5652
Pavel Begunkov5d709042021-08-09 20:18:13 +01005653static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005654 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005655{
5656 bool do_complete;
5657
Jens Axboed4e7cd32020-08-15 11:44:50 -07005658 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005659 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005660
Jens Axboeb41e9852020-02-17 09:52:41 -07005661 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005662 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005663 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005664 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005665 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005666 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005667 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005668}
5669
Jens Axboe76e1b642020-09-26 15:05:03 -06005670/*
5671 * Returns true if we found and killed one or more poll requests
5672 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005673static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005674 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005675{
Jens Axboe78076bb2019-12-04 19:56:40 -07005676 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005677 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005678 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005679
Jens Axboe79ebeae2021-08-10 15:18:27 -06005680 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005681 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5682 struct hlist_head *list;
5683
5684 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005685 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005686 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005687 posted += io_poll_remove_one(req);
5688 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005689 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005690 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005691
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005692 if (posted)
5693 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005694
5695 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005696}
5697
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005698static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5699 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005700 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005701{
Jens Axboe78076bb2019-12-04 19:56:40 -07005702 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005703 struct io_kiocb *req;
5704
Jens Axboe78076bb2019-12-04 19:56:40 -07005705 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5706 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005707 if (sqe_addr != req->user_data)
5708 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005709 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5710 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005711 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005712 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005713 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005714}
5715
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005716static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5717 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005718 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005719{
5720 struct io_kiocb *req;
5721
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005722 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005723 if (!req)
5724 return -ENOENT;
5725 if (io_poll_remove_one(req))
5726 return 0;
5727
5728 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005729}
5730
Pavel Begunkov9096af32021-04-14 13:38:36 +01005731static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5732 unsigned int flags)
5733{
5734 u32 events;
5735
5736 events = READ_ONCE(sqe->poll32_events);
5737#ifdef __BIG_ENDIAN
5738 events = swahw32(events);
5739#endif
5740 if (!(flags & IORING_POLL_ADD_MULTI))
5741 events |= EPOLLONESHOT;
5742 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5743}
5744
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005745static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005746 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005747{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005748 struct io_poll_update *upd = &req->poll_update;
5749 u32 flags;
5750
Jens Axboe221c5eb2019-01-17 09:41:58 -07005751 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5752 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005753 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005754 return -EINVAL;
5755 flags = READ_ONCE(sqe->len);
5756 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5757 IORING_POLL_ADD_MULTI))
5758 return -EINVAL;
5759 /* meaningless without update */
5760 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005761 return -EINVAL;
5762
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005763 upd->old_user_data = READ_ONCE(sqe->addr);
5764 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5765 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005766
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005767 upd->new_user_data = READ_ONCE(sqe->off);
5768 if (!upd->update_user_data && upd->new_user_data)
5769 return -EINVAL;
5770 if (upd->update_events)
5771 upd->events = io_poll_parse_events(sqe, flags);
5772 else if (sqe->poll32_events)
5773 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005774
Jens Axboe221c5eb2019-01-17 09:41:58 -07005775 return 0;
5776}
5777
Jens Axboe221c5eb2019-01-17 09:41:58 -07005778static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5779 void *key)
5780{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005781 struct io_kiocb *req = wait->private;
5782 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005783
Jens Axboed7718a92020-02-14 22:23:12 -07005784 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005785}
5786
Jens Axboe221c5eb2019-01-17 09:41:58 -07005787static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5788 struct poll_table_struct *p)
5789{
5790 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5791
Jens Axboee8c2bc12020-08-15 18:44:09 -07005792 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c62019-11-14 12:09:58 -07005793}
5794
Jens Axboe3529d8c2019-12-19 18:24:38 -07005795static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005796{
5797 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005798 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005799
5800 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5801 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005802 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005803 return -EINVAL;
5804 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005805 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005806 return -EINVAL;
5807
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005808 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005809 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005810 return 0;
5811}
5812
Pavel Begunkov61e98202021-02-10 00:03:08 +00005813static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005814{
5815 struct io_poll_iocb *poll = &req->poll;
5816 struct io_ring_ctx *ctx = req->ctx;
5817 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005818 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005819
Jens Axboed7718a92020-02-14 22:23:12 -07005820 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005821
Jens Axboed7718a92020-02-14 22:23:12 -07005822 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5823 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005824
Jens Axboe8c838782019-03-12 15:48:16 -06005825 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005826 ipt.error = 0;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005827 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005828 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005829 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005830
Jens Axboe8c838782019-03-12 15:48:16 -06005831 if (mask) {
5832 io_cqring_ev_posted(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005833 if (poll->events & EPOLLONESHOT)
5834 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005835 }
Jens Axboe8c838782019-03-12 15:48:16 -06005836 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005837}
5838
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005839static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005840{
5841 struct io_ring_ctx *ctx = req->ctx;
5842 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005843 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005844 int ret;
5845
Jens Axboe79ebeae2021-08-10 15:18:27 -06005846 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005847 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005848 if (!preq) {
5849 ret = -ENOENT;
5850 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005851 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005852
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005853 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5854 completing = true;
5855 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5856 goto err;
5857 }
5858
Jens Axboecb3b200e2021-04-06 09:49:31 -06005859 /*
5860 * Don't allow racy completion with singleshot, as we cannot safely
5861 * update those. For multishot, if we're racing with completion, just
5862 * let completion re-add it.
5863 */
5864 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5865 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5866 ret = -EALREADY;
5867 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005868 }
5869 /* we now have a detached poll request. reissue. */
5870 ret = 0;
5871err:
Jens Axboeb69de282021-03-17 08:37:41 -06005872 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005873 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005874 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005875 io_req_complete(req, ret);
5876 return 0;
5877 }
5878 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005879 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005880 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005881 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005882 preq->poll.events |= IO_POLL_UNMASK;
5883 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005884 if (req->poll_update.update_user_data)
5885 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005886 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005887
Jens Axboeb69de282021-03-17 08:37:41 -06005888 /* complete update request, we're done with it */
5889 io_req_complete(req, ret);
5890
Jens Axboecb3b200e2021-04-06 09:49:31 -06005891 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005892 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005893 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005894 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005895 io_req_complete(preq, ret);
5896 }
Jens Axboeb69de282021-03-17 08:37:41 -06005897 }
5898 return 0;
5899}
5900
Pavel Begunkovf237c302021-08-18 12:42:46 +01005901static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005902{
Jens Axboe89850fc2021-08-10 15:11:51 -06005903 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005904 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005905}
5906
Jens Axboe5262f562019-09-17 12:26:57 -06005907static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5908{
Jens Axboead8a48a2019-11-15 08:49:11 -07005909 struct io_timeout_data *data = container_of(timer,
5910 struct io_timeout_data, timer);
5911 struct io_kiocb *req = data->req;
5912 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005913 unsigned long flags;
5914
Jens Axboe89850fc2021-08-10 15:11:51 -06005915 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005916 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005917 atomic_set(&req->ctx->cq_timeouts,
5918 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005919 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005920
Jens Axboe89850fc2021-08-10 15:11:51 -06005921 req->io_task_work.func = io_req_task_timeout;
5922 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005923 return HRTIMER_NORESTART;
5924}
5925
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005926static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5927 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005928 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005929{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005930 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005931 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005932 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005933
5934 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005935 found = user_data == req->user_data;
5936 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005937 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005938 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005939 if (!found)
5940 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005941
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005942 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005943 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005944 return ERR_PTR(-EALREADY);
5945 list_del_init(&req->timeout.list);
5946 return req;
5947}
5948
5949static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005950 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005951 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005952{
5953 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5954
5955 if (IS_ERR(req))
5956 return PTR_ERR(req);
5957
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005958 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005959 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005960 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005961 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005962}
5963
Jens Axboe50c1df22021-08-27 17:11:06 -06005964static clockid_t io_timeout_get_clock(struct io_timeout_data *data)
5965{
5966 switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) {
5967 case IORING_TIMEOUT_BOOTTIME:
5968 return CLOCK_BOOTTIME;
5969 case IORING_TIMEOUT_REALTIME:
5970 return CLOCK_REALTIME;
5971 default:
5972 /* can't happen, vetted at prep time */
5973 WARN_ON_ONCE(1);
5974 fallthrough;
5975 case 0:
5976 return CLOCK_MONOTONIC;
5977 }
5978}
5979
Pavel Begunkovf1042b62021-08-28 19:54:39 -06005980static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5981 struct timespec64 *ts, enum hrtimer_mode mode)
5982 __must_hold(&ctx->timeout_lock)
5983{
5984 struct io_timeout_data *io;
5985 struct io_kiocb *req;
5986 bool found = false;
5987
5988 list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) {
5989 found = user_data == req->user_data;
5990 if (found)
5991 break;
5992 }
5993 if (!found)
5994 return -ENOENT;
5995
5996 io = req->async_data;
5997 if (hrtimer_try_to_cancel(&io->timer) == -1)
5998 return -EALREADY;
5999 hrtimer_init(&io->timer, io_timeout_get_clock(io), mode);
6000 io->timer.function = io_link_timeout_fn;
6001 hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode);
6002 return 0;
6003}
6004
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006005static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
6006 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06006007 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006008{
6009 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
6010 struct io_timeout_data *data;
6011
6012 if (IS_ERR(req))
6013 return PTR_ERR(req);
6014
6015 req->timeout.off = 0; /* noseq */
6016 data = req->async_data;
6017 list_add_tail(&req->timeout.list, &ctx->timeout_list);
Jens Axboe50c1df22021-08-27 17:11:06 -06006018 hrtimer_init(&data->timer, io_timeout_get_clock(data), mode);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006019 data->timer.function = io_timeout_fn;
6020 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
6021 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07006022}
6023
Jens Axboe3529d8c2019-12-19 18:24:38 -07006024static int io_timeout_remove_prep(struct io_kiocb *req,
6025 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07006026{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006027 struct io_timeout_rem *tr = &req->timeout_rem;
6028
Jens Axboeb29472e2019-12-17 18:50:29 -07006029 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
6030 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006031 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6032 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006033 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07006034 return -EINVAL;
6035
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006036 tr->ltimeout = false;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006037 tr->addr = READ_ONCE(sqe->addr);
6038 tr->flags = READ_ONCE(sqe->timeout_flags);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006039 if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) {
6040 if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
6041 return -EINVAL;
6042 if (tr->flags & IORING_LINK_TIMEOUT_UPDATE)
6043 tr->ltimeout = true;
6044 if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006045 return -EINVAL;
6046 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
6047 return -EFAULT;
6048 } else if (tr->flags) {
6049 /* timeout removal doesn't support flags */
6050 return -EINVAL;
6051 }
6052
Jens Axboeb29472e2019-12-17 18:50:29 -07006053 return 0;
6054}
6055
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006056static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
6057{
6058 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
6059 : HRTIMER_MODE_REL;
6060}
6061
Jens Axboe11365042019-10-16 09:08:32 -06006062/*
6063 * Remove or update an existing timeout command
6064 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00006065static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06006066{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006067 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06006068 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006069 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06006070
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006071 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
6072 spin_lock(&ctx->completion_lock);
6073 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00006074 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006075 spin_unlock_irq(&ctx->timeout_lock);
6076 spin_unlock(&ctx->completion_lock);
6077 } else {
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006078 enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags);
6079
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006080 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf1042b62021-08-28 19:54:39 -06006081 if (tr->ltimeout)
6082 ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode);
6083 else
6084 ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01006085 spin_unlock_irq(&ctx->timeout_lock);
6086 }
Jens Axboe11365042019-10-16 09:08:32 -06006087
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006088 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006089 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006090 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06006091 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06006092}
6093
Jens Axboe3529d8c2019-12-19 18:24:38 -07006094static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07006095 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06006096{
Jens Axboead8a48a2019-11-15 08:49:11 -07006097 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06006098 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006099 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06006100
Jens Axboead8a48a2019-11-15 08:49:11 -07006101 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06006102 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006103 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
6104 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06006105 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03006106 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07006107 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06006108 flags = READ_ONCE(sqe->timeout_flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006109 if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK))
6110 return -EINVAL;
6111 /* more than one clock specified is invalid, obviously */
6112 if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1)
Jens Axboe5262f562019-09-17 12:26:57 -06006113 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06006114
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006115 INIT_LIST_HEAD(&req->timeout.list);
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006116 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01006117 if (unlikely(off && !req->ctx->off_timeout_used))
6118 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07006119
Jens Axboee8c2bc12020-08-15 18:44:09 -07006120 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07006121 return -ENOMEM;
6122
Jens Axboee8c2bc12020-08-15 18:44:09 -07006123 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006124 data->req = req;
Jens Axboe50c1df22021-08-27 17:11:06 -06006125 data->flags = flags;
Jens Axboead8a48a2019-11-15 08:49:11 -07006126
6127 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06006128 return -EFAULT;
6129
Pavel Begunkov8662dae2021-01-19 13:32:44 +00006130 data->mode = io_translate_timeout_mode(flags);
Jens Axboe50c1df22021-08-27 17:11:06 -06006131 hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006132
6133 if (is_timeout_link) {
6134 struct io_submit_link *link = &req->ctx->submit_state.link;
6135
6136 if (!link->head)
6137 return -EINVAL;
6138 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
6139 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01006140 req->timeout.head = link->last;
6141 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01006142 }
Jens Axboead8a48a2019-11-15 08:49:11 -07006143 return 0;
6144}
6145
Pavel Begunkov61e98202021-02-10 00:03:08 +00006146static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07006147{
Jens Axboead8a48a2019-11-15 08:49:11 -07006148 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006149 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07006150 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006151 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07006152
Jens Axboe89850fc2021-08-10 15:11:51 -06006153 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07006154
Jens Axboe5262f562019-09-17 12:26:57 -06006155 /*
6156 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07006157 * timeout event to be satisfied. If it isn't set, then this is
6158 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06006159 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006160 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07006161 entry = ctx->timeout_list.prev;
6162 goto add;
6163 }
Jens Axboe5262f562019-09-17 12:26:57 -06006164
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006165 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
6166 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06006167
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05006168 /* Update the last seq here in case io_flush_timeouts() hasn't.
6169 * This is safe because ->completion_lock is held, and submissions
6170 * and completions are never mixed in the same ->completion_lock section.
6171 */
6172 ctx->cq_last_tm_flush = tail;
6173
Jens Axboe5262f562019-09-17 12:26:57 -06006174 /*
6175 * Insertion sort, ensuring the first entry in the list is always
6176 * the one we need first.
6177 */
Jens Axboe5262f562019-09-17 12:26:57 -06006178 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006179 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
6180 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06006181
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03006182 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07006183 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03006184 /* nxt.seq is behind @tail, otherwise would've been completed */
6185 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06006186 break;
6187 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07006188add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03006189 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07006190 data->timer.function = io_timeout_fn;
6191 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06006192 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06006193 return 0;
6194}
6195
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006196struct io_cancel_data {
6197 struct io_ring_ctx *ctx;
6198 u64 user_data;
6199};
6200
Jens Axboe62755e32019-10-28 21:49:21 -06006201static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06006202{
Jens Axboe62755e32019-10-28 21:49:21 -06006203 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006204 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06006205
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006206 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06006207}
6208
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006209static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
6210 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06006211{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006212 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06006213 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06006214 int ret = 0;
6215
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006216 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07006217 return -ENOENT;
6218
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006219 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06006220 switch (cancel_ret) {
6221 case IO_WQ_CANCEL_OK:
6222 ret = 0;
6223 break;
6224 case IO_WQ_CANCEL_RUNNING:
6225 ret = -EALREADY;
6226 break;
6227 case IO_WQ_CANCEL_NOTFOUND:
6228 ret = -ENOENT;
6229 break;
6230 }
6231
Jens Axboee977d6d2019-11-05 12:39:45 -07006232 return ret;
6233}
6234
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006235static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07006236{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006237 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07006238 int ret;
6239
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006240 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006241
Pavel Begunkovf458dd842021-03-08 12:14:14 +00006242 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01006243 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006244 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01006245
6246 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006247 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006248 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006249 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07006250 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01006251 goto out;
6252 ret = io_poll_cancel(ctx, sqe_addr, false);
6253out:
6254 spin_unlock(&ctx->completion_lock);
6255 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006256}
6257
Jens Axboe3529d8c2019-12-19 18:24:38 -07006258static int io_async_cancel_prep(struct io_kiocb *req,
6259 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006260{
Jens Axboefbf23842019-12-17 18:45:56 -07006261 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006262 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006263 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6264 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006265 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
6266 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07006267 return -EINVAL;
6268
Jens Axboefbf23842019-12-17 18:45:56 -07006269 req->cancel.addr = READ_ONCE(sqe->addr);
6270 return 0;
6271}
6272
Pavel Begunkov61e98202021-02-10 00:03:08 +00006273static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006274{
6275 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006276 u64 sqe_addr = req->cancel.addr;
6277 struct io_tctx_node *node;
6278 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07006279
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006280 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00006281 if (ret != -ENOENT)
6282 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00006283
6284 /* slow path, try all io-wq's */
6285 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
6286 ret = -ENOENT;
6287 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
6288 struct io_uring_task *tctx = node->task->io_uring;
6289
Pavel Begunkov58f99372021-03-12 16:25:55 +00006290 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
6291 if (ret != -ENOENT)
6292 break;
6293 }
6294 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00006295done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00006296 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006297 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006298 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006299 return 0;
6300}
6301
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006302static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006303 const struct io_uring_sqe *sqe)
6304{
Daniele Albano61710e42020-07-18 14:15:16 -06006305 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6306 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006307 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006308 return -EINVAL;
6309
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006310 req->rsrc_update.offset = READ_ONCE(sqe->off);
6311 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6312 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006313 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006314 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006315 return 0;
6316}
6317
Pavel Begunkov889fca72021-02-10 00:03:09 +00006318static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006319{
6320 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006321 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006322 int ret;
6323
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006324 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006325 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006326
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006327 up.offset = req->rsrc_update.offset;
6328 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006329 up.nr = 0;
6330 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006331 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006332
6333 mutex_lock(&ctx->uring_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006334 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006335 &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006336 mutex_unlock(&ctx->uring_lock);
6337
6338 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006339 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006340 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006341 return 0;
6342}
6343
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006344static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006345{
Jens Axboed625c6e2019-12-17 19:53:05 -07006346 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006347 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006348 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006349 case IORING_OP_READV:
6350 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006351 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006352 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006353 case IORING_OP_WRITEV:
6354 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006355 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006356 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006357 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006358 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006359 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006360 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006361 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006362 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006363 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006364 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006365 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006366 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006367 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006368 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006369 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006370 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006371 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006372 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006373 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006374 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006375 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006376 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006377 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006378 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006379 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006380 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006381 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006382 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006383 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006384 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006385 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006386 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006387 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006388 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006389 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006390 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006391 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006392 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006393 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006394 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006395 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006396 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006397 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006398 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006399 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006400 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006401 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006402 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006403 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006404 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006405 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006406 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006407 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006408 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006409 case IORING_OP_SHUTDOWN:
6410 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006411 case IORING_OP_RENAMEAT:
6412 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006413 case IORING_OP_UNLINKAT:
6414 return io_unlinkat_prep(req, sqe);
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006415 case IORING_OP_MKDIRAT:
6416 return io_mkdirat_prep(req, sqe);
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006417 case IORING_OP_SYMLINKAT:
6418 return io_symlinkat_prep(req, sqe);
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006419 case IORING_OP_LINKAT:
6420 return io_linkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006421 }
6422
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006423 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6424 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006425 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006426}
6427
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006428static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006429{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006430 if (!io_op_defs[req->opcode].needs_async_setup)
6431 return 0;
6432 if (WARN_ON_ONCE(req->async_data))
6433 return -EFAULT;
6434 if (io_alloc_async_data(req))
6435 return -EAGAIN;
6436
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006437 switch (req->opcode) {
6438 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006439 return io_rw_prep_async(req, READ);
6440 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006441 return io_rw_prep_async(req, WRITE);
6442 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006443 return io_sendmsg_prep_async(req);
6444 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006445 return io_recvmsg_prep_async(req);
6446 case IORING_OP_CONNECT:
6447 return io_connect_prep_async(req);
6448 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006449 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6450 req->opcode);
6451 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006452}
6453
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006454static u32 io_get_sequence(struct io_kiocb *req)
6455{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006456 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006457
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006458 /* need original cached_sq_head, but it was increased for each req */
6459 io_for_each_link(req, req)
6460 seq--;
6461 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006462}
6463
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006464static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006465{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006466 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006467 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006468 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006469 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006470 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006471
Pavel Begunkovb8ce1b92021-08-31 14:13:11 +01006472 if (req->flags & REQ_F_FAIL) {
6473 io_req_complete_fail_submit(req);
6474 return true;
6475 }
6476
Pavel Begunkov3c199662021-06-15 16:47:57 +01006477 /*
6478 * If we need to drain a request in the middle of a link, drain the
6479 * head request and the next request/link after the current link.
6480 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6481 * maintained for every request of our link.
6482 */
6483 if (ctx->drain_next) {
6484 req->flags |= REQ_F_IO_DRAIN;
6485 ctx->drain_next = false;
6486 }
6487 /* not interested in head, start from the first linked */
6488 io_for_each_link(pos, req->link) {
6489 if (pos->flags & REQ_F_IO_DRAIN) {
6490 ctx->drain_next = true;
6491 req->flags |= REQ_F_IO_DRAIN;
6492 break;
6493 }
6494 }
6495
Jens Axboedef596e2019-01-09 08:59:42 -07006496 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006497 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006498 !(req->flags & REQ_F_IO_DRAIN))) {
6499 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006500 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006501 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006502
6503 seq = io_get_sequence(req);
6504 /* Still a chance to pass the sequence check */
6505 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006506 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006507
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006508 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006509 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006510 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006511 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006512 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006513 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006514 ret = -ENOMEM;
6515fail:
6516 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006517 return true;
6518 }
Jens Axboe31b51512019-01-18 22:56:34 -07006519
Jens Axboe79ebeae2021-08-10 15:18:27 -06006520 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006521 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006522 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006523 kfree(de);
Pavel Begunkovf237c302021-08-18 12:42:46 +01006524 io_queue_async_work(req, NULL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006525 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006526 }
6527
6528 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006529 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006530 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006531 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006532 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006533 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006534}
6535
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006536static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006537{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006538 if (req->flags & REQ_F_BUFFER_SELECTED) {
6539 switch (req->opcode) {
6540 case IORING_OP_READV:
6541 case IORING_OP_READ_FIXED:
6542 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006543 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006544 break;
6545 case IORING_OP_RECVMSG:
6546 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006547 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006548 break;
6549 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006550 }
6551
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006552 if (req->flags & REQ_F_NEED_CLEANUP) {
6553 switch (req->opcode) {
6554 case IORING_OP_READV:
6555 case IORING_OP_READ_FIXED:
6556 case IORING_OP_READ:
6557 case IORING_OP_WRITEV:
6558 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006559 case IORING_OP_WRITE: {
6560 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006561
6562 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006563 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006564 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006565 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006566 case IORING_OP_SENDMSG: {
6567 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006568
6569 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006570 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006571 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006572 case IORING_OP_SPLICE:
6573 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006574 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6575 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006576 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006577 case IORING_OP_OPENAT:
6578 case IORING_OP_OPENAT2:
6579 if (req->open.filename)
6580 putname(req->open.filename);
6581 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006582 case IORING_OP_RENAMEAT:
6583 putname(req->rename.oldpath);
6584 putname(req->rename.newpath);
6585 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006586 case IORING_OP_UNLINKAT:
6587 putname(req->unlink.filename);
6588 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006589 case IORING_OP_MKDIRAT:
6590 putname(req->mkdir.filename);
6591 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006592 case IORING_OP_SYMLINKAT:
6593 putname(req->symlink.oldpath);
6594 putname(req->symlink.newpath);
6595 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006596 case IORING_OP_LINKAT:
6597 putname(req->hardlink.oldpath);
6598 putname(req->hardlink.newpath);
6599 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006600 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006601 }
Jens Axboe75652a302021-04-15 09:52:40 -06006602 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6603 kfree(req->apoll->double_poll);
6604 kfree(req->apoll);
6605 req->apoll = NULL;
6606 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006607 if (req->flags & REQ_F_INFLIGHT) {
6608 struct io_uring_task *tctx = req->task->io_uring;
6609
6610 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006611 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006612 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006613 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006614
6615 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006616}
6617
Pavel Begunkov889fca72021-02-10 00:03:09 +00006618static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006619{
Jens Axboeedafcce2019-01-09 09:16:05 -07006620 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006621 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006622 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006623
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006624 if ((req->flags & REQ_F_CREDS) && req->creds != current_cred())
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006625 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006626
Paul Moore5bd21822021-02-16 19:46:48 -05006627 if (!io_op_defs[req->opcode].audit_skip)
6628 audit_uring_entry(req->opcode);
6629
Jens Axboed625c6e2019-12-17 19:53:05 -07006630 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006631 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006632 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006633 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006634 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006635 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006636 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006637 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006638 break;
6639 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006640 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006641 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006642 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006643 break;
6644 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006645 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006646 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006648 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006649 break;
6650 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006651 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006652 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006653 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006654 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006655 break;
6656 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006657 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006658 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006659 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006660 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006661 break;
6662 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006663 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006664 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006665 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006666 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006667 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006668 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006669 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006670 break;
6671 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006672 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006673 break;
6674 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006675 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006676 break;
6677 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006678 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006679 break;
6680 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006681 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006682 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006683 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006684 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006685 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006686 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006687 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006688 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006689 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006690 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006691 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006692 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006693 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006694 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006695 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006696 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006697 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006698 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006699 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006700 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006701 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006702 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006703 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006704 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006705 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006706 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006707 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006708 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006709 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006710 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006711 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006712 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006713 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006714 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006715 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006716 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006717 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006718 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006719 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006720 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006721 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006722 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006723 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006724 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006725 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006726 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006727 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006728 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006729 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006730 break;
Dmitry Kadasheve34a02d2021-07-08 13:34:45 +07006731 case IORING_OP_MKDIRAT:
6732 ret = io_mkdirat(req, issue_flags);
6733 break;
Dmitry Kadashev7a8721f2021-07-08 13:34:46 +07006734 case IORING_OP_SYMLINKAT:
6735 ret = io_symlinkat(req, issue_flags);
6736 break;
Dmitry Kadashevcf30da92021-07-08 13:34:47 +07006737 case IORING_OP_LINKAT:
6738 ret = io_linkat(req, issue_flags);
6739 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740 default:
6741 ret = -EINVAL;
6742 break;
6743 }
Jens Axboe31b51512019-01-18 22:56:34 -07006744
Paul Moore5bd21822021-02-16 19:46:48 -05006745 if (!io_op_defs[req->opcode].audit_skip)
6746 audit_uring_exit(!ret, ret);
6747
Jens Axboe5730b272021-02-27 15:57:30 -07006748 if (creds)
6749 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006750 if (ret)
6751 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006752 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006753 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6754 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006755
6756 return 0;
6757}
6758
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006759static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6760{
6761 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6762
6763 req = io_put_req_find_next(req);
6764 return req ? &req->work : NULL;
6765}
6766
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006767static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006768{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006769 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006770 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006771 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006772
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006773 /* one will be dropped by ->io_free_work() after returning to io-wq */
6774 if (!(req->flags & REQ_F_REFCOUNT))
6775 __io_req_set_refcount(req, 2);
6776 else
6777 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006778
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006779 timeout = io_prep_linked_timeout(req);
6780 if (timeout)
6781 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006782
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006783 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006784 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006785 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006786
Jens Axboe561fb042019-10-24 07:25:42 -06006787 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006788 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006789 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006790 /*
6791 * We can get EAGAIN for polled IO even though we're
6792 * forcing a sync submission from here, since we can't
6793 * wait for request slots on the block side.
6794 */
6795 if (ret != -EAGAIN)
6796 break;
6797 cond_resched();
6798 } while (1);
6799 }
Jens Axboe31b51512019-01-18 22:56:34 -07006800
Pavel Begunkova3df76982021-02-18 22:32:52 +00006801 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006802 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006803 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006804}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006805
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006806static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006807 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006808{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006809 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006810}
6811
Jens Axboe09bb8392019-03-13 12:39:28 -06006812static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6813 int index)
6814{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006815 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006816
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006817 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006818}
6819
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006820static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006821{
6822 unsigned long file_ptr = (unsigned long) file;
6823
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006824 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006825 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006826 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006827 file_ptr |= FFS_ASYNC_WRITE;
6828 if (S_ISREG(file_inode(file)->i_mode))
6829 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006830 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006831}
6832
Pavel Begunkovac177052021-08-09 13:04:02 +01006833static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6834 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006835{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006836 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006837 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006838
Pavel Begunkovac177052021-08-09 13:04:02 +01006839 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6840 return NULL;
6841 fd = array_index_nospec(fd, ctx->nr_user_files);
6842 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6843 file = (struct file *) (file_ptr & FFS_MASK);
6844 file_ptr &= ~FFS_MASK;
6845 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006846 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006847 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006848 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006849}
6850
Pavel Begunkovac177052021-08-09 13:04:02 +01006851static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006852 struct io_kiocb *req, int fd)
6853{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006854 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006855
6856 trace_io_uring_file_get(ctx, fd);
6857
6858 /* we don't allow fixed io_uring files */
6859 if (file && unlikely(file->f_op == &io_uring_fops))
6860 io_req_track_inflight(req);
6861 return file;
6862}
6863
6864static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006865 struct io_kiocb *req, int fd, bool fixed)
6866{
6867 if (fixed)
6868 return io_file_get_fixed(ctx, req, fd);
6869 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006870 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006871}
6872
Pavel Begunkovf237c302021-08-18 12:42:46 +01006873static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006874{
6875 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006876 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006877
6878 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006879 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006880 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006881 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006882 } else {
6883 io_req_complete_post(req, -ETIME, 0);
6884 }
6885}
6886
Jens Axboe2665abf2019-11-05 12:40:47 -07006887static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6888{
Jens Axboead8a48a2019-11-15 08:49:11 -07006889 struct io_timeout_data *data = container_of(timer,
6890 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006891 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006892 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006893 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006894
Jens Axboe89b263f2021-08-10 15:14:18 -06006895 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006896 prev = req->timeout.head;
6897 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006898
6899 /*
6900 * We don't expect the list to be empty, that will only happen if we
6901 * race with the completion of the linked work.
6902 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006903 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006904 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006905 if (!req_ref_inc_not_zero(prev))
6906 prev = NULL;
6907 }
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006908 list_del(&req->timeout.list);
Jens Axboe89b263f2021-08-10 15:14:18 -06006909 req->timeout.prev = prev;
6910 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006911
Jens Axboe89b263f2021-08-10 15:14:18 -06006912 req->io_task_work.func = io_req_task_link_timeout;
6913 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006914 return HRTIMER_NORESTART;
6915}
6916
Pavel Begunkovde968c12021-03-19 17:22:33 +00006917static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006918{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006919 struct io_ring_ctx *ctx = req->ctx;
6920
Jens Axboe89b263f2021-08-10 15:14:18 -06006921 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006922 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006923 * If the back reference is NULL, then our linked request finished
6924 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006925 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006926 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006927 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006928
Jens Axboead8a48a2019-11-15 08:49:11 -07006929 data->timer.function = io_link_timeout_fn;
6930 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6931 data->mode);
Pavel Begunkovef9dd632021-08-28 19:54:38 -06006932 list_add_tail(&req->timeout.list, &ctx->ltimeout_list);
Jens Axboe2665abf2019-11-05 12:40:47 -07006933 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006934 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006935 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006936 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006937}
6938
Pavel Begunkovc5eef2b942021-02-10 00:03:22 +00006939static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006940 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006941{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006942 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006943 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006944
Olivier Langlois59b735a2021-06-22 05:17:39 -07006945issue_sqe:
Pavel Begunkovc5eef2b942021-02-10 00:03:22 +00006946 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006947
6948 /*
6949 * We async punt it if the file wasn't marked NOWAIT, or if the file
6950 * doesn't support non-blocking read/write attempts
6951 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006952 if (likely(!ret)) {
Pavel Begunkove342c802021-01-19 13:32:47 +00006953 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b942021-02-10 00:03:22 +00006954 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006955 struct io_submit_state *state = &ctx->submit_state;
Jens Axboee65ef562019-03-12 10:16:44 -06006956
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006957 state->compl_reqs[state->compl_nr++] = req;
6958 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006959 io_submit_flush_completions(ctx);
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006960 return;
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006961 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006962
6963 linked_timeout = io_prep_linked_timeout(req);
6964 if (linked_timeout)
6965 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006966 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006967 linked_timeout = io_prep_linked_timeout(req);
6968
Olivier Langlois59b735a2021-06-22 05:17:39 -07006969 switch (io_arm_poll_handler(req)) {
6970 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006971 if (linked_timeout)
6972 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006973 goto issue_sqe;
6974 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006975 /*
6976 * Queued up for async execution, worker will release
6977 * submit reference when the iocb is actually submitted.
6978 */
Pavel Begunkovf237c302021-08-18 12:42:46 +01006979 io_queue_async_work(req, NULL);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006980 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006981 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006982
6983 if (linked_timeout)
6984 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006985 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006986 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006987 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006988}
6989
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006990static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006991 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006992{
Pavel Begunkov10c66902021-06-15 16:47:56 +01006993 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006994 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08006995
Hao Xua8295b92021-08-27 17:46:09 +08006996 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) {
Pavel Begunkovc5eef2b942021-02-10 00:03:22 +00006997 __io_queue_sqe(req);
Hao Xua8295b92021-08-27 17:46:09 +08006998 } else if (req->flags & REQ_F_FAIL) {
Pavel Begunkovc6d3d9c2021-08-31 14:13:10 +01006999 io_req_complete_fail_submit(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01007000 } else {
7001 int ret = io_req_prep_async(req);
7002
7003 if (unlikely(ret))
7004 io_req_complete_failed(req, ret);
7005 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01007006 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07007007 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007008}
7009
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007010/*
7011 * Check SQE restrictions (opcode and flags).
7012 *
7013 * Returns 'true' if SQE is allowed, 'false' otherwise.
7014 */
7015static inline bool io_check_restriction(struct io_ring_ctx *ctx,
7016 struct io_kiocb *req,
7017 unsigned int sqe_flags)
7018{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01007019 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007020 return true;
7021
7022 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
7023 return false;
7024
7025 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
7026 ctx->restrictions.sqe_flags_required)
7027 return false;
7028
7029 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
7030 ctx->restrictions.sqe_flags_required))
7031 return false;
7032
7033 return true;
7034}
7035
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007036static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007037 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007038 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007039{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007040 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007041 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007042 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007043
Pavel Begunkov864ea922021-08-09 13:04:08 +01007044 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007045 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007046 /* same numerical values with corresponding REQ_F_*, safe to copy */
7047 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007048 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007049 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007050 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03007051 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007052
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007053 /* enforce forwards compatibility on users */
Pavel Begunkovdddca222021-04-27 16:13:52 +01007054 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00007055 return -EINVAL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007056 if (unlikely(req->opcode >= IORING_OP_LAST))
7057 return -EINVAL;
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01007058 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02007059 return -EACCES;
7060
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007061 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
7062 !io_op_defs[req->opcode].buffer_select)
7063 return -EOPNOTSUPP;
Pavel Begunkov3c199662021-06-15 16:47:57 +01007064 if (unlikely(sqe_flags & IOSQE_IO_DRAIN))
7065 ctx->drain_active = true;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007066
Jens Axboe003e8dc2021-03-06 09:22:27 -07007067 personality = READ_ONCE(sqe->personality);
7068 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007069 req->creds = xa_load(&ctx->personalities, personality);
7070 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07007071 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01007072 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01007073 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07007074 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007075 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03007076
Jens Axboe27926b62020-10-28 09:33:23 -06007077 /*
7078 * Plug now if we have more than 1 IO left after this, and the target
7079 * is potentially a read/write to block based storage.
7080 */
7081 if (!state->plug_started && state->ios_left > 1 &&
7082 io_op_defs[req->opcode].plug) {
7083 blk_start_plug(&state->plug);
7084 state->plug_started = true;
7085 }
Jens Axboe63ff8222020-05-07 14:56:15 -06007086
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007087 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01007088 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01007089 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00007090 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00007091 ret = -EBADF;
7092 }
7093
Pavel Begunkov71b547c2020-10-10 18:34:09 +01007094 state->ios_left--;
7095 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03007096}
7097
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007098static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007099 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007100 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007101{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007102 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007103 int ret;
7104
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007105 ret = io_init_req(ctx, req, sqe);
7106 if (unlikely(ret)) {
7107fail_req:
Hao Xua8295b92021-08-27 17:46:09 +08007108 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007109 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08007110 /*
7111 * we can judge a link req is failed or cancelled by if
7112 * REQ_F_FAIL is set, but the head is an exception since
7113 * it may be set REQ_F_FAIL because of other req's failure
7114 * so let's leverage req->result to distinguish if a head
7115 * is set REQ_F_FAIL because of its failure or other req's
7116 * failure so that we can set the correct ret code for it.
7117 * init result here to avoid affecting the normal path.
7118 */
7119 if (!(link->head->flags & REQ_F_FAIL))
7120 req_fail_link_node(link->head, -ECANCELED);
7121 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7122 /*
7123 * the current req is a normal req, we should return
7124 * error and thus break the submittion loop.
7125 */
7126 io_req_complete_failed(req, ret);
7127 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007128 }
Hao Xua8295b92021-08-27 17:46:09 +08007129 req_fail_link_node(req, ret);
7130 } else {
7131 ret = io_req_prep(req, sqe);
7132 if (unlikely(ret))
7133 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007134 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01007135
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007136 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04007137 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
7138 req->flags, true,
7139 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00007140
Jens Axboe6c271ce2019-01-10 11:22:30 -07007141 /*
7142 * If we already have a head request, queue this one for async
7143 * submittal once the head completes. If we don't have a head but
7144 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
7145 * submitted sync once the chain is complete. If none of those
7146 * conditions are true (normal request), then just queue it.
7147 */
7148 if (link->head) {
7149 struct io_kiocb *head = link->head;
7150
Hao Xua8295b92021-08-27 17:46:09 +08007151 if (!(req->flags & REQ_F_FAIL)) {
7152 ret = io_req_prep_async(req);
7153 if (unlikely(ret)) {
7154 req_fail_link_node(req, ret);
7155 if (!(head->flags & REQ_F_FAIL))
7156 req_fail_link_node(head, -ECANCELED);
7157 }
7158 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007159 trace_io_uring_link(ctx, req, head);
7160 link->last->link = req;
7161 link->last = req;
7162
7163 /* last request of a link, enqueue the link */
7164 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
7165 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01007166 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007167 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08007168 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07007169 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08007170 link->head = req;
7171 link->last = req;
7172 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00007173 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08007174 }
7175 }
7176
7177 return 0;
7178}
7179
7180/*
7181 * Batched submission is done, ensure local IO is flushed out.
7182 */
7183static void io_submit_state_end(struct io_submit_state *state,
7184 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03007185{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007186 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00007187 io_queue_sqe(state->link.head);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01007188 if (state->compl_nr)
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01007189 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07007190 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007191 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06007192}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007193
Jens Axboe9e645e112019-05-10 16:07:28 -06007194/*
7195 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03007196 */
Jens Axboe9e645e112019-05-10 16:07:28 -06007197static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03007198 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06007199{
7200 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07007201 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007202 /* set only head, no need to init link_last in advance */
7203 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07007204}
7205
Jens Axboe193155c2020-02-22 23:22:19 -07007206static void io_commit_sqring(struct io_ring_ctx *ctx)
7207{
Jens Axboe75c6a032020-01-28 10:15:23 -07007208 struct io_rings *rings = ctx->rings;
7209
7210 /*
Jens Axboe193155c2020-02-22 23:22:19 -07007211 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07007212 * since once we write the new head, the application could
7213 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03007214 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03007215 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07007216}
7217
Jens Axboe9e645e112019-05-10 16:07:28 -06007218/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01007219 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06007220 * that is mapped by userspace. This means that care needs to be taken to
7221 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07007222 * being a good citizen. If members of the sqe are validated and then later
7223 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03007224 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06007225 */
7226static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06007227{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01007228 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007229 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06007230
7231 /*
7232 * The cached sq head (or cq tail) serves two purposes:
7233 *
7234 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03007235 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06007236 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007237 * though the application is the one updating it.
7238 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01007239 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03007240 if (likely(head < ctx->sq_entries))
7241 return &ctx->sq_sqes[head];
7242
7243 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01007244 ctx->cq_extra--;
7245 WRITE_ONCE(ctx->rings->sq_dropped,
7246 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03007247 return NULL;
7248}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07007249
Jens Axboe0f212202020-09-13 13:09:39 -06007250static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01007251 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007252{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007253 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007254
Pavel Begunkovee7d46d92019-12-30 21:24:45 +03007255 /* make sure SQ entry isn't read before tail */
7256 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03007257 if (!percpu_ref_tryget_many(&ctx->refs, nr))
7258 return -EAGAIN;
Pavel Begunkov9a108672021-08-27 11:55:01 +01007259 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007260
Pavel Begunkovba88ff12021-02-10 00:03:11 +00007261 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00007262 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07007263 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03007264 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007265
Pavel Begunkov258b29a2021-02-10 00:03:10 +00007266 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03007267 if (unlikely(!req)) {
7268 if (!submitted)
7269 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03007270 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06007271 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007272 sqe = io_get_sqe(ctx);
7273 if (unlikely(!sqe)) {
Hao Xu0c6e1d72021-08-26 01:58:56 +08007274 list_add(&req->inflight_entry, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00007275 break;
7276 }
Jens Axboed3656342019-12-18 09:50:26 -07007277 /* will complete beyond this point, count as submitted */
7278 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007279 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07007280 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007281 }
7282
Pavel Begunkov9466f432020-01-25 22:34:01 +03007283 if (unlikely(submitted != nr)) {
7284 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06007285 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03007286
Pavel Begunkov09899b12021-06-14 02:36:22 +01007287 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06007288 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03007289 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007290
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00007291 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03007292 /* Commit SQ ring head once we've consumed and submitted all SQEs */
7293 io_commit_sqring(ctx);
7294
Jens Axboe6c271ce2019-01-10 11:22:30 -07007295 return submitted;
7296}
7297
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007298static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
7299{
7300 return READ_ONCE(sqd->state);
7301}
7302
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007303static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
7304{
7305 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06007306 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007307 WRITE_ONCE(ctx->rings->sq_flags,
7308 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007309 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007310}
7311
7312static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
7313{
Jens Axboe79ebeae2021-08-10 15:18:27 -06007314 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07007315 WRITE_ONCE(ctx->rings->sq_flags,
7316 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007317 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08007318}
7319
Xiaoguang Wang08369242020-11-03 14:15:59 +08007320static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007321{
Jens Axboec8d1ba52020-09-14 11:07:26 -06007322 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08007323 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007324
Jens Axboec8d1ba52020-09-14 11:07:26 -06007325 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06007326 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07007327 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
7328 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06007329
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007330 if (!list_empty(&ctx->iopoll_list) || to_submit) {
7331 unsigned nr_events = 0;
Pavel Begunkov948e1942021-06-24 15:09:55 +01007332 const struct cred *creds = NULL;
7333
7334 if (ctx->sq_creds != current_cred())
7335 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007336
Xiaoguang Wang08369242020-11-03 14:15:59 +08007337 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007338 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkova8576af2021-08-15 10:40:21 +01007339 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08007340
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01007341 /*
7342 * Don't submit if refs are dying, good for io_uring_register(),
7343 * but also it is relied upon by io_ring_exit_work()
7344 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007345 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7346 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007347 ret = io_submit_sqes(ctx, to_submit);
7348 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007349
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007350 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7351 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007352 if (creds)
7353 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007354 }
Jens Axboe90554202020-09-03 12:12:41 -06007355
Xiaoguang Wang08369242020-11-03 14:15:59 +08007356 return ret;
7357}
7358
7359static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7360{
7361 struct io_ring_ctx *ctx;
7362 unsigned sq_thread_idle = 0;
7363
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007364 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7365 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007366 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007367}
7368
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007369static bool io_sqd_handle_event(struct io_sq_data *sqd)
7370{
7371 bool did_sig = false;
7372 struct ksignal ksig;
7373
7374 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7375 signal_pending(current)) {
7376 mutex_unlock(&sqd->lock);
7377 if (signal_pending(current))
7378 did_sig = get_signal(&ksig);
7379 cond_resched();
7380 mutex_lock(&sqd->lock);
7381 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007382 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7383}
7384
Jens Axboe6c271ce2019-01-10 11:22:30 -07007385static int io_sq_thread(void *data)
7386{
Jens Axboe69fb2132020-09-14 11:16:23 -06007387 struct io_sq_data *sqd = data;
7388 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d9205f2020-11-12 14:55:59 +08007389 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007390 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007391 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007392
Pavel Begunkov696ee882021-04-01 09:55:04 +01007393 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007394 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007395
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007396 if (sqd->sq_cpu != -1)
7397 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7398 else
7399 set_cpus_allowed_ptr(current, cpu_online_mask);
7400 current->flags |= PF_NO_SETAFFINITY;
7401
Paul Moore5bd21822021-02-16 19:46:48 -05007402 audit_alloc_kernel(current);
7403
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007404 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007405 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007406 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007407
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007408 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7409 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007410 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007411 timeout = jiffies + sqd->sq_thread_idle;
7412 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007413
Jens Axboee95eee22020-09-08 09:11:32 -06007414 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007415 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007416 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007417
Xiaoguang Wang08369242020-11-03 14:15:59 +08007418 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7419 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007420 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007421 if (io_run_task_work())
7422 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007423
Xiaoguang Wang08369242020-11-03 14:15:59 +08007424 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007425 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007426 if (sqt_spin)
7427 timeout = jiffies + sqd->sq_thread_idle;
7428 continue;
7429 }
7430
Xiaoguang Wang08369242020-11-03 14:15:59 +08007431 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007432 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007433 bool needs_sched = true;
7434
Hao Xu724cb4f2021-04-21 23:19:11 +08007435 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007436 io_ring_set_wakeup_flag(ctx);
7437
Hao Xu724cb4f2021-04-21 23:19:11 +08007438 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7439 !list_empty_careful(&ctx->iopoll_list)) {
7440 needs_sched = false;
7441 break;
7442 }
7443 if (io_sqring_entries(ctx)) {
7444 needs_sched = false;
7445 break;
7446 }
7447 }
7448
7449 if (needs_sched) {
7450 mutex_unlock(&sqd->lock);
7451 schedule();
7452 mutex_lock(&sqd->lock);
7453 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007454 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7455 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007456 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007457
7458 finish_wait(&sqd->wait, &wait);
7459 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007460 }
7461
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007462 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007463 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007464 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007465 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007466 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007467 mutex_unlock(&sqd->lock);
7468
Paul Moore5bd21822021-02-16 19:46:48 -05007469 audit_free(current);
7470
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007471 complete(&sqd->exited);
7472 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007473}
7474
Jens Axboebda52162019-09-24 13:47:15 -06007475struct io_wait_queue {
7476 struct wait_queue_entry wq;
7477 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007478 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007479 unsigned nr_timeouts;
7480};
7481
Pavel Begunkov6c503152021-01-04 20:36:36 +00007482static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007483{
7484 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007485 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007486
7487 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007488 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007489 * started waiting. For timeouts, we always want to return to userspace,
7490 * regardless of event count.
7491 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007492 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007493}
7494
7495static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7496 int wake_flags, void *key)
7497{
7498 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7499 wq);
7500
Pavel Begunkov6c503152021-01-04 20:36:36 +00007501 /*
7502 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7503 * the task, and the next invocation will do it.
7504 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007505 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007506 return autoremove_wake_function(curr, mode, wake_flags, key);
7507 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007508}
7509
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007510static int io_run_task_work_sig(void)
7511{
7512 if (io_run_task_work())
7513 return 1;
7514 if (!signal_pending(current))
7515 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007516 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007517 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007518 return -EINTR;
7519}
7520
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007521/* when returns >0, the caller should retry */
7522static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7523 struct io_wait_queue *iowq,
7524 signed long *timeout)
7525{
7526 int ret;
7527
7528 /* make sure we run task_work before checking for signals */
7529 ret = io_run_task_work_sig();
7530 if (ret || io_should_wake(iowq))
7531 return ret;
7532 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007533 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007534 return 1;
7535
7536 *timeout = schedule_timeout(*timeout);
7537 return !*timeout ? -ETIME : 1;
7538}
7539
Jens Axboe2b188cc2019-01-07 10:46:33 -07007540/*
7541 * Wait until events become available, if we don't already have some. The
7542 * application must reap them itself, as they reside on the shared cq ring.
7543 */
7544static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007545 const sigset_t __user *sig, size_t sigsz,
7546 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007547{
Pavel Begunkov902910992021-08-09 09:07:32 -06007548 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007549 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007550 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7551 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007552
Jens Axboeb41e9852020-02-17 09:52:41 -07007553 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007554 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007555 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007556 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007557 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007558 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007559 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007560
7561 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007562#ifdef CONFIG_COMPAT
7563 if (in_compat_syscall())
7564 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007565 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007566 else
7567#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007568 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007569
Jens Axboe2b188cc2019-01-07 10:46:33 -07007570 if (ret)
7571 return ret;
7572 }
7573
Hao Xuc73ebb62020-11-03 10:54:37 +08007574 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007575 struct timespec64 ts;
7576
Hao Xuc73ebb62020-11-03 10:54:37 +08007577 if (get_timespec64(&ts, uts))
7578 return -EFAULT;
7579 timeout = timespec64_to_jiffies(&ts);
7580 }
7581
Pavel Begunkov902910992021-08-09 09:07:32 -06007582 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7583 iowq.wq.private = current;
7584 INIT_LIST_HEAD(&iowq.wq.entry);
7585 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007586 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007587 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007588
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007589 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007590 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007591 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007592 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007593 ret = -EBUSY;
7594 break;
7595 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007596 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007597 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007598 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007599 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007600 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007601 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007602
Jens Axboeb7db41c2020-07-04 08:55:50 -06007603 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007604
Hristo Venev75b28af2019-08-26 17:23:46 +00007605 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007606}
7607
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007608static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007609{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007610 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007611
7612 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007613 kfree(table[i]);
7614 kfree(table);
7615}
7616
7617static void **io_alloc_page_table(size_t size)
7618{
7619 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7620 size_t init_size = size;
7621 void **table;
7622
Pavel Begunkov0bea96f52021-08-20 10:36:36 +01007623 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007624 if (!table)
7625 return NULL;
7626
7627 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007628 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007629
Pavel Begunkov0bea96f52021-08-20 10:36:36 +01007630 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007631 if (!table[i]) {
7632 io_free_page_table(table, init_size);
7633 return NULL;
7634 }
7635 size -= this_size;
7636 }
7637 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007638}
7639
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007640static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7641{
7642 percpu_ref_exit(&ref_node->refs);
7643 kfree(ref_node);
7644}
7645
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007646static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7647{
7648 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7649 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7650 unsigned long flags;
7651 bool first_add = false;
7652
7653 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7654 node->done = true;
7655
7656 while (!list_empty(&ctx->rsrc_ref_list)) {
7657 node = list_first_entry(&ctx->rsrc_ref_list,
7658 struct io_rsrc_node, node);
7659 /* recycle ref nodes in order */
7660 if (!node->done)
7661 break;
7662 list_del(&node->node);
7663 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7664 }
7665 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7666
7667 if (first_add)
7668 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7669}
7670
7671static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7672{
7673 struct io_rsrc_node *ref_node;
7674
7675 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7676 if (!ref_node)
7677 return NULL;
7678
7679 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7680 0, GFP_KERNEL)) {
7681 kfree(ref_node);
7682 return NULL;
7683 }
7684 INIT_LIST_HEAD(&ref_node->node);
7685 INIT_LIST_HEAD(&ref_node->rsrc_list);
7686 ref_node->done = false;
7687 return ref_node;
7688}
7689
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007690static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7691 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007692{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007693 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7694 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007695
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007696 if (data_to_kill) {
7697 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007698
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007699 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007700 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007701 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007702 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007703
Pavel Begunkov3e942492021-04-11 01:46:34 +01007704 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007705 percpu_ref_kill(&rsrc_node->refs);
7706 ctx->rsrc_node = NULL;
7707 }
7708
7709 if (!ctx->rsrc_node) {
7710 ctx->rsrc_node = ctx->rsrc_backup_node;
7711 ctx->rsrc_backup_node = NULL;
7712 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007713}
7714
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007715static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007716{
7717 if (ctx->rsrc_backup_node)
7718 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007719 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007720 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7721}
7722
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007723static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007724{
7725 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007726
Pavel Begunkov215c3902021-04-01 15:43:48 +01007727 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007728 if (data->quiesce)
7729 return -ENXIO;
7730
7731 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007732 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007733 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007734 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007735 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007736 io_rsrc_node_switch(ctx, data);
7737
Pavel Begunkov3e942492021-04-11 01:46:34 +01007738 /* kill initial ref, already quiesced if zero */
7739 if (atomic_dec_and_test(&data->refs))
7740 break;
Jens Axboec018db42021-08-09 08:15:50 -06007741 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007742 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007743 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007744 if (!ret) {
7745 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007746 break;
Jens Axboec018db42021-08-09 08:15:50 -06007747 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007748
Pavel Begunkov3e942492021-04-11 01:46:34 +01007749 atomic_inc(&data->refs);
7750 /* wait for all works potentially completing data->done */
7751 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007752 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007753
Hao Xu8bad28d2021-02-19 17:19:36 +08007754 ret = io_run_task_work_sig();
7755 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007756 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007757 data->quiesce = false;
7758
Hao Xu8bad28d2021-02-19 17:19:36 +08007759 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007760}
7761
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007762static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7763{
7764 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7765 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7766
7767 return &data->tags[table_idx][off];
7768}
7769
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007770static void io_rsrc_data_free(struct io_rsrc_data *data)
7771{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007772 size_t size = data->nr * sizeof(data->tags[0][0]);
7773
7774 if (data->tags)
7775 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007776 kfree(data);
7777}
7778
Pavel Begunkovd878c812021-06-14 02:36:18 +01007779static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7780 u64 __user *utags, unsigned nr,
7781 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007782{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007783 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007784 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007785 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007786
7787 data = kzalloc(sizeof(*data), GFP_KERNEL);
7788 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007789 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007790 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007791 if (!data->tags) {
7792 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007793 return -ENOMEM;
7794 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007795
7796 data->nr = nr;
7797 data->ctx = ctx;
7798 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007799 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007800 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007801 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007802 u64 *tag_slot = io_get_tag_slot(data, i);
7803
7804 if (copy_from_user(tag_slot, &utags[i],
7805 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007806 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007807 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007808 }
7809
Pavel Begunkov3e942492021-04-11 01:46:34 +01007810 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007811 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007812 *pdata = data;
7813 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007814fail:
7815 io_rsrc_data_free(data);
7816 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007817}
7818
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007819static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7820{
Pavel Begunkov0bea96f52021-08-20 10:36:36 +01007821 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7822 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007823 return !!table->files;
7824}
7825
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007826static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007827{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007828 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007829 table->files = NULL;
7830}
7831
Jens Axboe2b188cc2019-01-07 10:46:33 -07007832static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7833{
7834#if defined(CONFIG_UNIX)
7835 if (ctx->ring_sock) {
7836 struct sock *sock = ctx->ring_sock->sk;
7837 struct sk_buff *skb;
7838
7839 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7840 kfree_skb(skb);
7841 }
7842#else
7843 int i;
7844
7845 for (i = 0; i < ctx->nr_user_files; i++) {
7846 struct file *file;
7847
7848 file = io_file_from_index(ctx, i);
7849 if (file)
7850 fput(file);
7851 }
7852#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007853 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007854 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007855 ctx->file_data = NULL;
7856 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007857}
7858
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007859static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7860{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007861 int ret;
7862
Pavel Begunkov08480402021-04-13 02:58:38 +01007863 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007864 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007865 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7866 if (!ret)
7867 __io_sqe_files_unregister(ctx);
7868 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007869}
7870
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007871static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007872 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007873{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007874 WARN_ON_ONCE(sqd->thread == current);
7875
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007876 /*
7877 * Do the dance but not conditional clear_bit() because it'd race with
7878 * other threads incrementing park_pending and setting the bit.
7879 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007880 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007881 if (atomic_dec_return(&sqd->park_pending))
7882 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007883 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007884}
7885
Jens Axboe86e0d672021-03-05 08:44:39 -07007886static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007887 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007888{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007889 WARN_ON_ONCE(sqd->thread == current);
7890
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007891 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007892 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007893 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007894 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007895 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007896}
7897
7898static void io_sq_thread_stop(struct io_sq_data *sqd)
7899{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007900 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007901 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007902
Jens Axboe05962f92021-03-06 13:58:48 -07007903 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007904 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007905 if (sqd->thread)
7906 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007907 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007908 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007909}
7910
Jens Axboe534ca6d2020-09-02 13:52:19 -06007911static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007912{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007913 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007914 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7915
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007916 io_sq_thread_stop(sqd);
7917 kfree(sqd);
7918 }
7919}
7920
7921static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7922{
7923 struct io_sq_data *sqd = ctx->sq_data;
7924
7925 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007926 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007927 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007928 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007929 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007930
7931 io_put_sq_data(sqd);
7932 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007933 }
7934}
7935
Jens Axboeaa061652020-09-02 14:50:27 -06007936static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7937{
7938 struct io_ring_ctx *ctx_attach;
7939 struct io_sq_data *sqd;
7940 struct fd f;
7941
7942 f = fdget(p->wq_fd);
7943 if (!f.file)
7944 return ERR_PTR(-ENXIO);
7945 if (f.file->f_op != &io_uring_fops) {
7946 fdput(f);
7947 return ERR_PTR(-EINVAL);
7948 }
7949
7950 ctx_attach = f.file->private_data;
7951 sqd = ctx_attach->sq_data;
7952 if (!sqd) {
7953 fdput(f);
7954 return ERR_PTR(-EINVAL);
7955 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007956 if (sqd->task_tgid != current->tgid) {
7957 fdput(f);
7958 return ERR_PTR(-EPERM);
7959 }
Jens Axboeaa061652020-09-02 14:50:27 -06007960
7961 refcount_inc(&sqd->refs);
7962 fdput(f);
7963 return sqd;
7964}
7965
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007966static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7967 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007968{
7969 struct io_sq_data *sqd;
7970
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007971 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007972 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7973 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007974 if (!IS_ERR(sqd)) {
7975 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007976 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007977 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007978 /* fall through for EPERM case, setup new sqd/task */
7979 if (PTR_ERR(sqd) != -EPERM)
7980 return sqd;
7981 }
Jens Axboeaa061652020-09-02 14:50:27 -06007982
Jens Axboe534ca6d2020-09-02 13:52:19 -06007983 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7984 if (!sqd)
7985 return ERR_PTR(-ENOMEM);
7986
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007987 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007988 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007989 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007990 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007991 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007992 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007993 return sqd;
7994}
7995
Jens Axboe6b063142019-01-10 22:13:58 -07007996#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007997/*
7998 * Ensure the UNIX gc is aware of our file set, so we are certain that
7999 * the io_uring can be safely unregistered on process exit, even if we have
8000 * loops in the file referencing.
8001 */
8002static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
8003{
8004 struct sock *sk = ctx->ring_sock->sk;
8005 struct scm_fp_list *fpl;
8006 struct sk_buff *skb;
Jens Axboe08a451732019-10-03 08:11:03 -06008007 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07008008
Jens Axboe6b063142019-01-10 22:13:58 -07008009 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
8010 if (!fpl)
8011 return -ENOMEM;
8012
8013 skb = alloc_skb(0, GFP_KERNEL);
8014 if (!skb) {
8015 kfree(fpl);
8016 return -ENOMEM;
8017 }
8018
8019 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07008020
Jens Axboe08a451732019-10-03 08:11:03 -06008021 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07008022 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07008023 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008024 struct file *file = io_file_from_index(ctx, i + offset);
8025
8026 if (!file)
Jens Axboe08a451732019-10-03 08:11:03 -06008027 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06008028 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a451732019-10-03 08:11:03 -06008029 unix_inflight(fpl->user, fpl->fp[nr_files]);
8030 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07008031 }
8032
Jens Axboe08a451732019-10-03 08:11:03 -06008033 if (nr_files) {
8034 fpl->max = SCM_MAX_FD;
8035 fpl->count = nr_files;
8036 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008037 skb->destructor = unix_destruct_scm;
Jens Axboe08a451732019-10-03 08:11:03 -06008038 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
8039 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07008040
Jens Axboe08a451732019-10-03 08:11:03 -06008041 for (i = 0; i < nr_files; i++)
8042 fput(fpl->fp[i]);
8043 } else {
8044 kfree_skb(skb);
8045 kfree(fpl);
8046 }
Jens Axboe6b063142019-01-10 22:13:58 -07008047
8048 return 0;
8049}
8050
8051/*
8052 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
8053 * causes regular reference counting to break down. We rely on the UNIX
8054 * garbage collection to take care of this problem for us.
8055 */
8056static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8057{
8058 unsigned left, total;
8059 int ret = 0;
8060
8061 total = 0;
8062 left = ctx->nr_user_files;
8063 while (left) {
8064 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07008065
8066 ret = __io_sqe_files_scm(ctx, this_files, total);
8067 if (ret)
8068 break;
8069 left -= this_files;
8070 total += this_files;
8071 }
8072
8073 if (!ret)
8074 return 0;
8075
8076 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06008077 struct file *file = io_file_from_index(ctx, total);
8078
8079 if (file)
8080 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07008081 total++;
8082 }
8083
8084 return ret;
8085}
8086#else
8087static int io_sqe_files_scm(struct io_ring_ctx *ctx)
8088{
8089 return 0;
8090}
8091#endif
8092
Pavel Begunkov47e90392021-04-01 15:43:56 +01008093static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06008094{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008095 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008096#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06008097 struct sock *sock = ctx->ring_sock->sk;
8098 struct sk_buff_head list, *head = &sock->sk_receive_queue;
8099 struct sk_buff *skb;
8100 int i;
8101
8102 __skb_queue_head_init(&list);
8103
8104 /*
8105 * Find the skb that holds this file in its SCM_RIGHTS. When found,
8106 * remove this entry and rearrange the file array.
8107 */
8108 skb = skb_dequeue(head);
8109 while (skb) {
8110 struct scm_fp_list *fp;
8111
8112 fp = UNIXCB(skb).fp;
8113 for (i = 0; i < fp->count; i++) {
8114 int left;
8115
8116 if (fp->fp[i] != file)
8117 continue;
8118
8119 unix_notinflight(fp->user, fp->fp[i]);
8120 left = fp->count - 1 - i;
8121 if (left) {
8122 memmove(&fp->fp[i], &fp->fp[i + 1],
8123 left * sizeof(struct file *));
8124 }
8125 fp->count--;
8126 if (!fp->count) {
8127 kfree_skb(skb);
8128 skb = NULL;
8129 } else {
8130 __skb_queue_tail(&list, skb);
8131 }
8132 fput(file);
8133 file = NULL;
8134 break;
8135 }
8136
8137 if (!file)
8138 break;
8139
8140 __skb_queue_tail(&list, skb);
8141
8142 skb = skb_dequeue(head);
8143 }
8144
8145 if (skb_peek(&list)) {
8146 spin_lock_irq(&head->lock);
8147 while ((skb = __skb_dequeue(&list)) != NULL)
8148 __skb_queue_tail(head, skb);
8149 spin_unlock_irq(&head->lock);
8150 }
8151#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07008152 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008153#endif
8154}
8155
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008156static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008157{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008158 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008159 struct io_ring_ctx *ctx = rsrc_data->ctx;
8160 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008161
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008162 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
8163 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008164
8165 if (prsrc->tag) {
8166 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008167
8168 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008169 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008170 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01008171 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008172 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008173 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008174 io_cqring_ev_posted(ctx);
8175 io_ring_submit_unlock(ctx, lock_ring);
8176 }
8177
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01008178 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008179 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008180 }
8181
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01008182 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01008183 if (atomic_dec_and_test(&rsrc_data->refs))
8184 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008185}
8186
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008187static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06008188{
8189 struct io_ring_ctx *ctx;
8190 struct llist_node *node;
8191
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008192 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
8193 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008194
8195 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008196 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06008197 struct llist_node *next = node->next;
8198
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008199 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008200 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06008201 node = next;
8202 }
8203}
8204
Jens Axboe05f3fb32019-12-09 11:22:50 -07008205static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01008206 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008207{
8208 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008209 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008210 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01008211 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008212
8213 if (ctx->file_data)
8214 return -EBUSY;
8215 if (!nr_args)
8216 return -EINVAL;
8217 if (nr_args > IORING_MAX_FIXED_FILES)
8218 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01008219 if (nr_args > rlimit(RLIMIT_NOFILE))
8220 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008221 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008222 if (ret)
8223 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008224 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
8225 &ctx->file_data);
8226 if (ret)
8227 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008228
Pavel Begunkovf3baed32021-04-01 15:43:42 +01008229 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008230 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008231 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008232
Jens Axboe05f3fb32019-12-09 11:22:50 -07008233 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01008234 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008235 ret = -EFAULT;
8236 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008237 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008238 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01008239 if (fd == -1) {
8240 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008241 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01008242 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008243 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008244 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008245
Jens Axboe05f3fb32019-12-09 11:22:50 -07008246 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008247 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01008248 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008249 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008250
8251 /*
8252 * Don't allow io_uring instances to be registered. If UNIX
8253 * isn't enabled, then this causes a reference cycle and this
8254 * instance can never get freed. If UNIX is enabled we'll
8255 * handle it just fine, but there's still no point in allowing
8256 * a ring fd as it doesn't support regular read/write anyway.
8257 */
8258 if (file->f_op == &io_uring_fops) {
8259 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008260 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008261 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008262 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008263 }
8264
Jens Axboe05f3fb32019-12-09 11:22:50 -07008265 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008266 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01008267 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008268 return ret;
8269 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008270
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008271 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07008272 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008273out_fput:
8274 for (i = 0; i < ctx->nr_user_files; i++) {
8275 file = io_file_from_index(ctx, i);
8276 if (file)
8277 fput(file);
8278 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01008279 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008280 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01008281out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01008282 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06008283 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008284 return ret;
8285}
8286
Jens Axboec3a31e62019-10-03 13:59:56 -06008287static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
8288 int index)
8289{
8290#if defined(CONFIG_UNIX)
8291 struct sock *sock = ctx->ring_sock->sk;
8292 struct sk_buff_head *head = &sock->sk_receive_queue;
8293 struct sk_buff *skb;
8294
8295 /*
8296 * See if we can merge this file into an existing skb SCM_RIGHTS
8297 * file set. If there's no room, fall back to allocating a new skb
8298 * and filling it in.
8299 */
8300 spin_lock_irq(&head->lock);
8301 skb = skb_peek(head);
8302 if (skb) {
8303 struct scm_fp_list *fpl = UNIXCB(skb).fp;
8304
8305 if (fpl->count < SCM_MAX_FD) {
8306 __skb_unlink(skb, head);
8307 spin_unlock_irq(&head->lock);
8308 fpl->fp[fpl->count] = get_file(file);
8309 unix_inflight(fpl->user, fpl->fp[fpl->count]);
8310 fpl->count++;
8311 spin_lock_irq(&head->lock);
8312 __skb_queue_head(head, skb);
8313 } else {
8314 skb = NULL;
8315 }
8316 }
8317 spin_unlock_irq(&head->lock);
8318
8319 if (skb) {
8320 fput(file);
8321 return 0;
8322 }
8323
8324 return __io_sqe_files_scm(ctx, 1, index);
8325#else
8326 return 0;
8327#endif
8328}
8329
Pavel Begunkovb9445592021-08-25 12:25:45 +01008330static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
8331 unsigned int issue_flags, u32 slot_index)
8332{
8333 struct io_ring_ctx *ctx = req->ctx;
8334 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
8335 struct io_fixed_file *file_slot;
8336 int ret = -EBADF;
8337
8338 io_ring_submit_lock(ctx, !force_nonblock);
8339 if (file->f_op == &io_uring_fops)
8340 goto err;
8341 ret = -ENXIO;
8342 if (!ctx->file_data)
8343 goto err;
8344 ret = -EINVAL;
8345 if (slot_index >= ctx->nr_user_files)
8346 goto err;
8347
8348 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8349 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
8350 ret = -EBADF;
8351 if (file_slot->file_ptr)
8352 goto err;
8353
8354 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8355 io_fixed_file_set(file_slot, file);
8356 ret = io_sqe_file_register(ctx, file, slot_index);
8357 if (ret) {
8358 file_slot->file_ptr = 0;
8359 goto err;
8360 }
8361
8362 ret = 0;
8363err:
8364 io_ring_submit_unlock(ctx, !force_nonblock);
8365 if (ret)
8366 fput(file);
8367 return ret;
8368}
8369
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008370static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
Pavel Begunkove7c78372021-04-01 15:43:45 +01008371 struct io_rsrc_node *node, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008372{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008373 struct io_rsrc_put *prsrc;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008374
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008375 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8376 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08008377 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008378
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008379 prsrc->tag = *io_get_tag_slot(data, idx);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008380 prsrc->rsrc = rsrc;
Pavel Begunkove7c78372021-04-01 15:43:45 +01008381 list_add(&prsrc->list, &node->rsrc_list);
Hillf Dantona5318d32020-03-23 17:47:15 +08008382 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008383}
8384
8385static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008386 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008387 unsigned nr_args)
8388{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008389 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008390 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008391 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008392 struct io_fixed_file *file_slot;
8393 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008394 int fd, i, err = 0;
8395 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008396 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008397
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008398 if (!ctx->file_data)
8399 return -ENXIO;
8400 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008401 return -EINVAL;
8402
Pavel Begunkov67973b92021-01-26 13:51:09 +00008403 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008404 u64 tag = 0;
8405
8406 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8407 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008408 err = -EFAULT;
8409 break;
8410 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008411 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8412 err = -EINVAL;
8413 break;
8414 }
noah4e0377a2021-01-26 15:23:28 -05008415 if (fd == IORING_REGISTER_FILES_SKIP)
8416 continue;
8417
Pavel Begunkov67973b92021-01-26 13:51:09 +00008418 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008419 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008420
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008421 if (file_slot->file_ptr) {
8422 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008423 err = io_queue_rsrc_removal(data, up->offset + done,
8424 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008425 if (err)
8426 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008427 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008428 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008429 }
8430 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008431 file = fget(fd);
8432 if (!file) {
8433 err = -EBADF;
8434 break;
8435 }
8436 /*
8437 * Don't allow io_uring instances to be registered. If
8438 * UNIX isn't enabled, then this causes a reference
8439 * cycle and this instance can never get freed. If UNIX
8440 * is enabled we'll handle it just fine, but there's
8441 * still no point in allowing a ring fd as it doesn't
8442 * support regular read/write anyway.
8443 */
8444 if (file->f_op == &io_uring_fops) {
8445 fput(file);
8446 err = -EBADF;
8447 break;
8448 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008449 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008450 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008451 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008452 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008453 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008454 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008455 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008456 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008457 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008458 }
8459
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008460 if (needs_switch)
8461 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008462 return done ? done : err;
8463}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008464
Jens Axboe685fe7f2021-03-08 09:37:51 -07008465static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8466 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008467{
Jens Axboee9418942021-02-19 12:33:30 -07008468 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008469 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008470 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008471
Yang Yingliang362a9e62021-07-20 16:38:05 +08008472 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008473 hash = ctx->hash_map;
8474 if (!hash) {
8475 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008476 if (!hash) {
8477 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008478 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008479 }
Jens Axboee9418942021-02-19 12:33:30 -07008480 refcount_set(&hash->refs, 1);
8481 init_waitqueue_head(&hash->wait);
8482 ctx->hash_map = hash;
8483 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008484 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008485
8486 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008487 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008488 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008489 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008490
Jens Axboed25e3a32021-02-16 11:41:41 -07008491 /* Do QD, or 4 * CPUS, whatever is smallest */
8492 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008493
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008494 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008495}
8496
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008497static int io_uring_alloc_task_context(struct task_struct *task,
8498 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008499{
8500 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008501 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008502
Pavel Begunkov09899b12021-06-14 02:36:22 +01008503 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008504 if (unlikely(!tctx))
8505 return -ENOMEM;
8506
Jens Axboed8a6df12020-10-15 16:24:45 -06008507 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8508 if (unlikely(ret)) {
8509 kfree(tctx);
8510 return ret;
8511 }
8512
Jens Axboe685fe7f2021-03-08 09:37:51 -07008513 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008514 if (IS_ERR(tctx->io_wq)) {
8515 ret = PTR_ERR(tctx->io_wq);
8516 percpu_counter_destroy(&tctx->inflight);
8517 kfree(tctx);
8518 return ret;
8519 }
8520
Jens Axboe0f212202020-09-13 13:09:39 -06008521 xa_init(&tctx->xa);
8522 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008523 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008524 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008525 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008526 spin_lock_init(&tctx->task_lock);
8527 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008528 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008529 return 0;
8530}
8531
8532void __io_uring_free(struct task_struct *tsk)
8533{
8534 struct io_uring_task *tctx = tsk->io_uring;
8535
8536 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008537 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008538 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008539
Jens Axboed8a6df12020-10-15 16:24:45 -06008540 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008541 kfree(tctx);
8542 tsk->io_uring = NULL;
8543}
8544
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008545static int io_sq_offload_create(struct io_ring_ctx *ctx,
8546 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008547{
8548 int ret;
8549
Jens Axboed25e3a32021-02-16 11:41:41 -07008550 /* Retain compatibility with failing for an invalid attach attempt */
8551 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8552 IORING_SETUP_ATTACH_WQ) {
8553 struct fd f;
8554
8555 f = fdget(p->wq_fd);
8556 if (!f.file)
8557 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008558 if (f.file->f_op != &io_uring_fops) {
8559 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008560 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008561 }
8562 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008563 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008564 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008565 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008566 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008567 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008568
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008569 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008570 if (IS_ERR(sqd)) {
8571 ret = PTR_ERR(sqd);
8572 goto err;
8573 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008574
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008575 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008576 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008577 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8578 if (!ctx->sq_thread_idle)
8579 ctx->sq_thread_idle = HZ;
8580
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008581 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008582 list_add(&ctx->sqd_list, &sqd->ctx_list);
8583 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008584 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008585 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008586 io_sq_thread_unpark(sqd);
8587
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008588 if (ret < 0)
8589 goto err;
8590 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008591 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008592
Jens Axboe6c271ce2019-01-10 11:22:30 -07008593 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008594 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008595
Jens Axboe917257d2019-04-13 09:28:55 -06008596 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008597 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008598 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008599 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008600 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008601 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008602 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008603
8604 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008605 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008606 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8607 if (IS_ERR(tsk)) {
8608 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008609 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008610 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008611
Jens Axboe46fe18b2021-03-04 12:39:36 -07008612 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008613 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008614 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008615 if (ret)
8616 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008617 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8618 /* Can't have SQ_AFF without SQPOLL */
8619 ret = -EINVAL;
8620 goto err;
8621 }
8622
Jens Axboe2b188cc2019-01-07 10:46:33 -07008623 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008624err_sqpoll:
8625 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008626err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008627 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008628 return ret;
8629}
8630
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008631static inline void __io_unaccount_mem(struct user_struct *user,
8632 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008633{
8634 atomic_long_sub(nr_pages, &user->locked_vm);
8635}
8636
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008637static inline int __io_account_mem(struct user_struct *user,
8638 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008639{
8640 unsigned long page_limit, cur_pages, new_pages;
8641
8642 /* Don't allow more pages than we can safely lock */
8643 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8644
8645 do {
8646 cur_pages = atomic_long_read(&user->locked_vm);
8647 new_pages = cur_pages + nr_pages;
8648 if (new_pages > page_limit)
8649 return -ENOMEM;
8650 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8651 new_pages) != cur_pages);
8652
8653 return 0;
8654}
8655
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008656static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008657{
Jens Axboe62e398b2021-02-21 16:19:37 -07008658 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008659 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008660
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008661 if (ctx->mm_account)
8662 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008663}
8664
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008665static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008666{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008667 int ret;
8668
Jens Axboe62e398b2021-02-21 16:19:37 -07008669 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008670 ret = __io_account_mem(ctx->user, nr_pages);
8671 if (ret)
8672 return ret;
8673 }
8674
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008675 if (ctx->mm_account)
8676 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008677
8678 return 0;
8679}
8680
Jens Axboe2b188cc2019-01-07 10:46:33 -07008681static void io_mem_free(void *ptr)
8682{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008683 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008684
Mark Rutland52e04ef2019-04-30 17:30:21 +01008685 if (!ptr)
8686 return;
8687
8688 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008689 if (put_page_testzero(page))
8690 free_compound_page(page);
8691}
8692
8693static void *io_mem_alloc(size_t size)
8694{
8695 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008696 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008697
8698 return (void *) __get_free_pages(gfp_flags, get_order(size));
8699}
8700
Hristo Venev75b28af2019-08-26 17:23:46 +00008701static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8702 size_t *sq_offset)
8703{
8704 struct io_rings *rings;
8705 size_t off, sq_array_size;
8706
8707 off = struct_size(rings, cqes, cq_entries);
8708 if (off == SIZE_MAX)
8709 return SIZE_MAX;
8710
8711#ifdef CONFIG_SMP
8712 off = ALIGN(off, SMP_CACHE_BYTES);
8713 if (off == 0)
8714 return SIZE_MAX;
8715#endif
8716
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008717 if (sq_offset)
8718 *sq_offset = off;
8719
Hristo Venev75b28af2019-08-26 17:23:46 +00008720 sq_array_size = array_size(sizeof(u32), sq_entries);
8721 if (sq_array_size == SIZE_MAX)
8722 return SIZE_MAX;
8723
8724 if (check_add_overflow(off, sq_array_size, &off))
8725 return SIZE_MAX;
8726
Hristo Venev75b28af2019-08-26 17:23:46 +00008727 return off;
8728}
8729
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008730static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e92021-04-11 01:46:35 +01008731{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008732 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e92021-04-11 01:46:35 +01008733 unsigned int i;
8734
Pavel Begunkov62248432021-04-28 13:11:29 +01008735 if (imu != ctx->dummy_ubuf) {
8736 for (i = 0; i < imu->nr_bvecs; i++)
8737 unpin_user_page(imu->bvec[i].bv_page);
8738 if (imu->acct_pages)
8739 io_unaccount_mem(ctx, imu->acct_pages);
8740 kvfree(imu);
8741 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008742 *slot = NULL;
Pavel Begunkov7f61a1e92021-04-11 01:46:35 +01008743}
8744
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008745static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8746{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008747 io_buffer_unmap(ctx, &prsrc->buf);
8748 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008749}
8750
8751static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008752{
Pavel Begunkov7f61a1e92021-04-11 01:46:35 +01008753 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008754
Pavel Begunkov7f61a1e92021-04-11 01:46:35 +01008755 for (i = 0; i < ctx->nr_user_bufs; i++)
8756 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008757 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008758 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008759 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008760 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008761 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008762}
8763
Jens Axboeedafcce2019-01-09 09:16:05 -07008764static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8765{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008766 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008767
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008768 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008769 return -ENXIO;
8770
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008771 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8772 if (!ret)
8773 __io_sqe_buffers_unregister(ctx);
8774 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008775}
8776
8777static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8778 void __user *arg, unsigned index)
8779{
8780 struct iovec __user *src;
8781
8782#ifdef CONFIG_COMPAT
8783 if (ctx->compat) {
8784 struct compat_iovec __user *ciovs;
8785 struct compat_iovec ciov;
8786
8787 ciovs = (struct compat_iovec __user *) arg;
8788 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8789 return -EFAULT;
8790
Jens Axboed55e5f52019-12-11 16:12:15 -07008791 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008792 dst->iov_len = ciov.iov_len;
8793 return 0;
8794 }
8795#endif
8796 src = (struct iovec __user *) arg;
8797 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8798 return -EFAULT;
8799 return 0;
8800}
8801
Jens Axboede293932020-09-17 16:19:16 -06008802/*
8803 * Not super efficient, but this is just a registration time. And we do cache
8804 * the last compound head, so generally we'll only do a full search if we don't
8805 * match that one.
8806 *
8807 * We check if the given compound head page has already been accounted, to
8808 * avoid double accounting it. This allows us to account the full size of the
8809 * page, not just the constituent pages of a huge page.
8810 */
8811static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8812 int nr_pages, struct page *hpage)
8813{
8814 int i, j;
8815
8816 /* check current page array */
8817 for (i = 0; i < nr_pages; i++) {
8818 if (!PageCompound(pages[i]))
8819 continue;
8820 if (compound_head(pages[i]) == hpage)
8821 return true;
8822 }
8823
8824 /* check previously registered pages */
8825 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008826 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008827
8828 for (j = 0; j < imu->nr_bvecs; j++) {
8829 if (!PageCompound(imu->bvec[j].bv_page))
8830 continue;
8831 if (compound_head(imu->bvec[j].bv_page) == hpage)
8832 return true;
8833 }
8834 }
8835
8836 return false;
8837}
8838
8839static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8840 int nr_pages, struct io_mapped_ubuf *imu,
8841 struct page **last_hpage)
8842{
8843 int i, ret;
8844
Pavel Begunkov216e5832021-05-29 12:01:02 +01008845 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008846 for (i = 0; i < nr_pages; i++) {
8847 if (!PageCompound(pages[i])) {
8848 imu->acct_pages++;
8849 } else {
8850 struct page *hpage;
8851
8852 hpage = compound_head(pages[i]);
8853 if (hpage == *last_hpage)
8854 continue;
8855 *last_hpage = hpage;
8856 if (headpage_already_acct(ctx, pages, i, hpage))
8857 continue;
8858 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8859 }
8860 }
8861
8862 if (!imu->acct_pages)
8863 return 0;
8864
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008865 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008866 if (ret)
8867 imu->acct_pages = 0;
8868 return ret;
8869}
8870
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008871static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008872 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008873 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008874{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008875 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008876 struct vm_area_struct **vmas = NULL;
8877 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008878 unsigned long off, start, end, ubuf;
8879 size_t size;
8880 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008881
Pavel Begunkov62248432021-04-28 13:11:29 +01008882 if (!iov->iov_base) {
8883 *pimu = ctx->dummy_ubuf;
8884 return 0;
8885 }
8886
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008887 ubuf = (unsigned long) iov->iov_base;
8888 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8889 start = ubuf >> PAGE_SHIFT;
8890 nr_pages = end - start;
8891
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008892 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008893 ret = -ENOMEM;
8894
8895 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8896 if (!pages)
8897 goto done;
8898
8899 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8900 GFP_KERNEL);
8901 if (!vmas)
8902 goto done;
8903
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008904 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008905 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008906 goto done;
8907
8908 ret = 0;
8909 mmap_read_lock(current->mm);
8910 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8911 pages, vmas);
8912 if (pret == nr_pages) {
8913 /* don't support file backed memory */
8914 for (i = 0; i < nr_pages; i++) {
8915 struct vm_area_struct *vma = vmas[i];
8916
Pavel Begunkov40dad762021-06-09 15:26:54 +01008917 if (vma_is_shmem(vma))
8918 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008919 if (vma->vm_file &&
8920 !is_file_hugepages(vma->vm_file)) {
8921 ret = -EOPNOTSUPP;
8922 break;
8923 }
8924 }
8925 } else {
8926 ret = pret < 0 ? pret : -EFAULT;
8927 }
8928 mmap_read_unlock(current->mm);
8929 if (ret) {
8930 /*
8931 * if we did partial map, or found file backed vmas,
8932 * release any pages we did get
8933 */
8934 if (pret > 0)
8935 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008936 goto done;
8937 }
8938
8939 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8940 if (ret) {
8941 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008942 goto done;
8943 }
8944
8945 off = ubuf & ~PAGE_MASK;
8946 size = iov->iov_len;
8947 for (i = 0; i < nr_pages; i++) {
8948 size_t vec_len;
8949
8950 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8951 imu->bvec[i].bv_page = pages[i];
8952 imu->bvec[i].bv_len = vec_len;
8953 imu->bvec[i].bv_offset = off;
8954 off = 0;
8955 size -= vec_len;
8956 }
8957 /* store original address for later verification */
8958 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008959 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008960 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008961 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008962 ret = 0;
8963done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008964 if (ret)
8965 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008966 kvfree(pages);
8967 kvfree(vmas);
8968 return ret;
8969}
8970
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008971static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008972{
Pavel Begunkov87094462021-04-11 01:46:36 +01008973 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8974 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008975}
8976
8977static int io_buffer_validate(struct iovec *iov)
8978{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008979 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8980
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008981 /*
8982 * Don't impose further limits on the size and buffer
8983 * constraints here, we'll -EINVAL later when IO is
8984 * submitted if they are wrong.
8985 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008986 if (!iov->iov_base)
8987 return iov->iov_len ? -EFAULT : 0;
8988 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008989 return -EFAULT;
8990
8991 /* arbitrary limit, but we need something */
8992 if (iov->iov_len > SZ_1G)
8993 return -EFAULT;
8994
Pavel Begunkov50e96982021-03-24 22:59:01 +00008995 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8996 return -EOVERFLOW;
8997
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008998 return 0;
8999}
9000
9001static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009002 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009003{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009004 struct page *last_hpage = NULL;
9005 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009006 int i, ret;
9007 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009008
Pavel Begunkov87094462021-04-11 01:46:36 +01009009 if (ctx->user_bufs)
9010 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01009011 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01009012 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009013 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009014 if (ret)
9015 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01009016 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
9017 if (ret)
9018 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009019 ret = io_buffers_map_alloc(ctx, nr_args);
9020 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08009021 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009022 return ret;
9023 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009024
Pavel Begunkov87094462021-04-11 01:46:36 +01009025 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07009026 ret = io_copy_iov(ctx, &iov, arg, i);
9027 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009028 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08009029 ret = io_buffer_validate(&iov);
9030 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009031 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009032 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009033 ret = -EINVAL;
9034 break;
9035 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009036
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009037 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
9038 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009039 if (ret)
9040 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009041 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009042
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009043 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009044
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009045 ctx->buf_data = data;
9046 if (ret)
9047 __io_sqe_buffers_unregister(ctx);
9048 else
9049 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07009050 return ret;
9051}
9052
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009053static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
9054 struct io_uring_rsrc_update2 *up,
9055 unsigned int nr_args)
9056{
9057 u64 __user *tags = u64_to_user_ptr(up->tags);
9058 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009059 struct page *last_hpage = NULL;
9060 bool needs_switch = false;
9061 __u32 done;
9062 int i, err;
9063
9064 if (!ctx->buf_data)
9065 return -ENXIO;
9066 if (up->offset + nr_args > ctx->nr_user_bufs)
9067 return -EINVAL;
9068
9069 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009070 struct io_mapped_ubuf *imu;
9071 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009072 u64 tag = 0;
9073
9074 err = io_copy_iov(ctx, &iov, iovs, done);
9075 if (err)
9076 break;
9077 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
9078 err = -EFAULT;
9079 break;
9080 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009081 err = io_buffer_validate(&iov);
9082 if (err)
9083 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01009084 if (!iov.iov_base && tag) {
9085 err = -EINVAL;
9086 break;
9087 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009088 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
9089 if (err)
9090 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009091
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009092 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01009093 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009094 err = io_queue_rsrc_removal(ctx->buf_data, offset,
9095 ctx->rsrc_node, ctx->user_bufs[i]);
9096 if (unlikely(err)) {
9097 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009098 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009099 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009100 ctx->user_bufs[i] = NULL;
9101 needs_switch = true;
9102 }
9103
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01009104 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01009105 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009106 }
9107
9108 if (needs_switch)
9109 io_rsrc_node_switch(ctx, ctx->buf_data);
9110 return done ? done : err;
9111}
9112
Jens Axboe9b402842019-04-11 11:45:41 -06009113static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
9114{
9115 __s32 __user *fds = arg;
9116 int fd;
9117
9118 if (ctx->cq_ev_fd)
9119 return -EBUSY;
9120
9121 if (copy_from_user(&fd, fds, sizeof(*fds)))
9122 return -EFAULT;
9123
9124 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
9125 if (IS_ERR(ctx->cq_ev_fd)) {
9126 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01009127
Jens Axboe9b402842019-04-11 11:45:41 -06009128 ctx->cq_ev_fd = NULL;
9129 return ret;
9130 }
9131
9132 return 0;
9133}
9134
9135static int io_eventfd_unregister(struct io_ring_ctx *ctx)
9136{
9137 if (ctx->cq_ev_fd) {
9138 eventfd_ctx_put(ctx->cq_ev_fd);
9139 ctx->cq_ev_fd = NULL;
9140 return 0;
9141 }
9142
9143 return -ENXIO;
9144}
9145
Jens Axboe5a2e7452020-02-23 16:23:11 -07009146static void io_destroy_buffers(struct io_ring_ctx *ctx)
9147{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07009148 struct io_buffer *buf;
9149 unsigned long index;
9150
9151 xa_for_each(&ctx->io_buffers, index, buf)
9152 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009153}
9154
Pavel Begunkov72558342021-08-09 20:18:09 +01009155static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00009156{
Jens Axboe68e68ee2021-02-13 09:00:02 -07009157 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00009158
Pavel Begunkovbb943b82021-08-09 20:18:10 +01009159 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
9160 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00009161 kmem_cache_free(req_cachep, req);
9162 }
9163}
9164
Jens Axboe4010fec2021-02-27 15:04:18 -07009165static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009166{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009167 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00009168
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009169 mutex_lock(&ctx->uring_lock);
9170
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009171 if (state->free_reqs) {
9172 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
9173 state->free_reqs = 0;
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00009174 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009175
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01009176 io_flush_cached_locked_reqs(ctx, state);
9177 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07009178 mutex_unlock(&ctx->uring_lock);
9179}
9180
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009181static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009182{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009183 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009184 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009185}
9186
Jens Axboe2b188cc2019-01-07 10:46:33 -07009187static void io_ring_ctx_free(struct io_ring_ctx *ctx)
9188{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009189 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009190
Jens Axboe37d1e2e2021-02-17 21:03:43 -07009191 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06009192 mmdrop(ctx->mm_account);
9193 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07009194 }
Jens Axboedef596e2019-01-09 08:59:42 -07009195
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009196 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
9197 io_wait_rsrc_data(ctx->buf_data);
9198 io_wait_rsrc_data(ctx->file_data);
9199
Hao Xu8bad28d2021-02-19 17:19:36 +08009200 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009201 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009202 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01009203 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01009204 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01009205 if (ctx->rings)
9206 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08009207 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06009208 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07009209 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01009210 if (ctx->sq_creds)
9211 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07009212
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009213 /* there are no registered resources left, nobody uses it */
9214 if (ctx->rsrc_node)
9215 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00009216 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01009217 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01009218 flush_delayed_work(&ctx->rsrc_put_work);
9219
9220 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
9221 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009222
9223#if defined(CONFIG_UNIX)
Eric Biggers355e8d262019-06-12 14:58:43 -07009224 if (ctx->ring_sock) {
9225 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009226 sock_release(ctx->ring_sock);
Eric Biggers355e8d262019-06-12 14:58:43 -07009227 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009228#endif
Pavel Begunkovef9dd632021-08-28 19:54:38 -06009229 WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list));
Jens Axboe2b188cc2019-01-07 10:46:33 -07009230
Hristo Venev75b28af2019-08-26 17:23:46 +00009231 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009232 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009233
9234 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009235 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07009236 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07009237 if (ctx->hash_map)
9238 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07009239 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01009240 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009241 kfree(ctx);
9242}
9243
9244static __poll_t io_uring_poll(struct file *file, poll_table *wait)
9245{
9246 struct io_ring_ctx *ctx = file->private_data;
9247 __poll_t mask = 0;
9248
Pavel Begunkov311997b2021-06-14 23:37:28 +01009249 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02009250 /*
9251 * synchronizes with barrier from wq_has_sleeper call in
9252 * io_commit_cqring
9253 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07009254 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06009255 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009256 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08009257
9258 /*
9259 * Don't flush cqring overflow list here, just do a simple check.
9260 * Otherwise there could possible be ABBA deadlock:
9261 * CPU0 CPU1
9262 * ---- ----
9263 * lock(&ctx->uring_lock);
9264 * lock(&ep->mtx);
9265 * lock(&ctx->uring_lock);
9266 * lock(&ep->mtx);
9267 *
9268 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
9269 * pushs them to do the flush.
9270 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01009271 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009272 mask |= EPOLLIN | EPOLLRDNORM;
9273
9274 return mask;
9275}
9276
9277static int io_uring_fasync(int fd, struct file *file, int on)
9278{
9279 struct io_ring_ctx *ctx = file->private_data;
9280
9281 return fasync_helper(fd, file, on, &ctx->cq_fasync);
9282}
9283
Yejune Deng0bead8c2020-12-24 11:02:20 +08009284static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07009285{
Jens Axboe4379bf82021-02-15 13:40:22 -07009286 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07009287
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009288 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07009289 if (creds) {
9290 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08009291 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009292 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08009293
9294 return -EINVAL;
9295}
9296
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009297struct io_tctx_exit {
9298 struct callback_head task_work;
9299 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009300 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009301};
9302
9303static void io_tctx_exit_cb(struct callback_head *cb)
9304{
9305 struct io_uring_task *tctx = current->io_uring;
9306 struct io_tctx_exit *work;
9307
9308 work = container_of(cb, struct io_tctx_exit, task_work);
9309 /*
9310 * When @in_idle, we're in cancellation and it's racy to remove the
9311 * node. It'll be removed by the end of cancellation, just ignore it.
9312 */
9313 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009314 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009315 complete(&work->completion);
9316}
9317
Pavel Begunkov28090c12021-04-25 23:34:45 +01009318static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
9319{
9320 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
9321
9322 return req->ctx == data;
9323}
9324
Jens Axboe85faa7b2020-04-09 18:14:00 -06009325static void io_ring_exit_work(struct work_struct *work)
9326{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009327 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009328 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009329 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009330 struct io_tctx_exit exit;
9331 struct io_tctx_node *node;
9332 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06009333
Jens Axboe56952e92020-06-17 15:00:04 -06009334 /*
9335 * If we're doing polled IO and end up having requests being
9336 * submitted async (out-of-line), then completions can come in while
9337 * we're waiting for refs to drop. We need to reap these manually,
9338 * as nobody else will be looking for them.
9339 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009340 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009341 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01009342 if (ctx->sq_data) {
9343 struct io_sq_data *sqd = ctx->sq_data;
9344 struct task_struct *tsk;
9345
9346 io_sq_thread_park(sqd);
9347 tsk = sqd->thread;
9348 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9349 io_wq_cancel_cb(tsk->io_uring->io_wq,
9350 io_cancel_ctx_cb, ctx, true);
9351 io_sq_thread_unpark(sqd);
9352 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009353
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009354 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9355 /* there is little hope left, don't run it too often */
9356 interval = HZ * 60;
9357 }
9358 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009359
Pavel Begunkov7f006512021-04-14 13:38:34 +01009360 init_completion(&exit.completion);
9361 init_task_work(&exit.task_work, io_tctx_exit_cb);
9362 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009363 /*
9364 * Some may use context even when all refs and requests have been put,
9365 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009366 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009367 * this lock/unlock section also waits them to finish.
9368 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009369 mutex_lock(&ctx->uring_lock);
9370 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009371 WARN_ON_ONCE(time_after(jiffies, timeout));
9372
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009373 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9374 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009375 /* don't spin on a single task if cancellation failed */
9376 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009377 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9378 if (WARN_ON_ONCE(ret))
9379 continue;
9380 wake_up_process(node->task);
9381
9382 mutex_unlock(&ctx->uring_lock);
9383 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009384 mutex_lock(&ctx->uring_lock);
9385 }
9386 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009387 spin_lock(&ctx->completion_lock);
9388 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009389
Jens Axboe85faa7b2020-04-09 18:14:00 -06009390 io_ring_ctx_free(ctx);
9391}
9392
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009393/* Returns true if we found and killed one or more timeouts */
9394static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009395 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009396{
9397 struct io_kiocb *req, *tmp;
9398 int canceled = 0;
9399
Jens Axboe79ebeae2021-08-10 15:18:27 -06009400 spin_lock(&ctx->completion_lock);
9401 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009402 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009403 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009404 io_kill_timeout(req, -ECANCELED);
9405 canceled++;
9406 }
9407 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009408 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009409 if (canceled != 0)
9410 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009411 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009412 if (canceled != 0)
9413 io_cqring_ev_posted(ctx);
9414 return canceled != 0;
9415}
9416
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9418{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009419 unsigned long index;
9420 struct creds *creds;
9421
Jens Axboe2b188cc2019-01-07 10:46:33 -07009422 mutex_lock(&ctx->uring_lock);
9423 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009424 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009425 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009426 xa_for_each(&ctx->personalities, index, creds)
9427 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009428 mutex_unlock(&ctx->uring_lock);
9429
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009430 io_kill_timeouts(ctx, NULL, true);
9431 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009432
Jens Axboe15dff282019-11-13 09:09:23 -07009433 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009434 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009435
Jens Axboe85faa7b2020-04-09 18:14:00 -06009436 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009437 /*
9438 * Use system_unbound_wq to avoid spawning tons of event kworkers
9439 * if we're exiting a ton of rings at the same time. It just adds
9440 * noise and overhead, there's no discernable change in runtime
9441 * over using system_wq.
9442 */
9443 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009444}
9445
9446static int io_uring_release(struct inode *inode, struct file *file)
9447{
9448 struct io_ring_ctx *ctx = file->private_data;
9449
9450 file->private_data = NULL;
9451 io_ring_ctx_wait_and_kill(ctx);
9452 return 0;
9453}
9454
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009455struct io_task_cancel {
9456 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009457 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009458};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009459
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009460static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009461{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009462 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009463 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009464 bool ret;
9465
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009466 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009467 struct io_ring_ctx *ctx = req->ctx;
9468
9469 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009470 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009471 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009472 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009473 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009474 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009475 }
9476 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009477}
9478
Pavel Begunkove1915f72021-03-11 23:29:35 +00009479static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009480 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009481{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009482 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009483 LIST_HEAD(list);
9484
Jens Axboe79ebeae2021-08-10 15:18:27 -06009485 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009486 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009487 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009488 list_cut_position(&list, &ctx->defer_list, &de->list);
9489 break;
9490 }
9491 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009492 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009493 if (list_empty(&list))
9494 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009495
9496 while (!list_empty(&list)) {
9497 de = list_first_entry(&list, struct io_defer_entry, list);
9498 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009499 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009500 kfree(de);
9501 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009502 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009503}
9504
Pavel Begunkov1b007642021-03-06 11:02:17 +00009505static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9506{
9507 struct io_tctx_node *node;
9508 enum io_wq_cancel cret;
9509 bool ret = false;
9510
9511 mutex_lock(&ctx->uring_lock);
9512 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9513 struct io_uring_task *tctx = node->task->io_uring;
9514
9515 /*
9516 * io_wq will stay alive while we hold uring_lock, because it's
9517 * killed after ctx nodes, which requires to take the lock.
9518 */
9519 if (!tctx || !tctx->io_wq)
9520 continue;
9521 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9522 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9523 }
9524 mutex_unlock(&ctx->uring_lock);
9525
9526 return ret;
9527}
9528
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009529static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9530 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009531 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009532{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009533 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009534 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009535
9536 while (1) {
9537 enum io_wq_cancel cret;
9538 bool ret = false;
9539
Pavel Begunkov1b007642021-03-06 11:02:17 +00009540 if (!task) {
9541 ret |= io_uring_try_cancel_iowq(ctx);
9542 } else if (tctx && tctx->io_wq) {
9543 /*
9544 * Cancels requests of all rings, not only @ctx, but
9545 * it's fine as the task is in exit/exec.
9546 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009547 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009548 &cancel, true);
9549 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9550 }
9551
9552 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009553 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009554 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009555 while (!list_empty_careful(&ctx->iopoll_list)) {
9556 io_iopoll_try_reap_events(ctx);
9557 ret = true;
9558 }
9559 }
9560
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009561 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9562 ret |= io_poll_remove_all(ctx, task, cancel_all);
9563 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009564 if (task)
9565 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009566 if (!ret)
9567 break;
9568 cond_resched();
9569 }
9570}
9571
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009572static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009573{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009574 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009575 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009576 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009577
9578 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009579 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009580 if (unlikely(ret))
9581 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009582 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009583 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009584 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9585 node = kmalloc(sizeof(*node), GFP_KERNEL);
9586 if (!node)
9587 return -ENOMEM;
9588 node->ctx = ctx;
9589 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009590
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009591 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9592 node, GFP_KERNEL));
9593 if (ret) {
9594 kfree(node);
9595 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009596 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009597
9598 mutex_lock(&ctx->uring_lock);
9599 list_add(&node->ctx_node, &ctx->tctx_list);
9600 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009601 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009602 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009603 return 0;
9604}
9605
9606/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009607 * Note that this task has used io_uring. We use it for cancelation purposes.
9608 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009609static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009610{
9611 struct io_uring_task *tctx = current->io_uring;
9612
9613 if (likely(tctx && tctx->last == ctx))
9614 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009615 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009616}
9617
9618/*
Jens Axboe0f212202020-09-13 13:09:39 -06009619 * Remove this io_uring_file -> task mapping.
9620 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009621static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009622{
9623 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009624 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009625
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009626 if (!tctx)
9627 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009628 node = xa_erase(&tctx->xa, index);
9629 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009630 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009631
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009632 WARN_ON_ONCE(current != node->task);
9633 WARN_ON_ONCE(list_empty(&node->ctx_node));
9634
9635 mutex_lock(&node->ctx->uring_lock);
9636 list_del(&node->ctx_node);
9637 mutex_unlock(&node->ctx->uring_lock);
9638
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009639 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009640 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009641 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009642}
9643
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009644static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009645{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009646 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009647 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009648 unsigned long index;
9649
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009650 xa_for_each(&tctx->xa, index, node)
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009651 io_uring_del_tctx_node(index);
Marco Elverb16ef422021-05-27 11:25:48 +02009652 if (wq) {
9653 /*
9654 * Must be after io_uring_del_task_file() (removes nodes under
9655 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9656 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009657 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009658 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009659 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009660}
9661
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009662static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009663{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009664 if (tracked)
9665 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009666 return percpu_counter_sum(&tctx->inflight);
9667}
9668
Pavel Begunkov09899b12021-06-14 02:36:22 +01009669static void io_uring_drop_tctx_refs(struct task_struct *task)
9670{
9671 struct io_uring_task *tctx = task->io_uring;
9672 unsigned int refs = tctx->cached_refs;
9673
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009674 if (refs) {
9675 tctx->cached_refs = 0;
9676 percpu_counter_sub(&tctx->inflight, refs);
9677 put_task_struct_many(task, refs);
9678 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009679}
9680
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009681/*
9682 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9683 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9684 */
9685static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009686{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009687 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009688 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009689 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009690 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009691
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009692 WARN_ON_ONCE(sqd && sqd->thread != current);
9693
Palash Oswal6d042ff2021-04-27 18:21:49 +05309694 if (!current->io_uring)
9695 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009696 if (tctx->io_wq)
9697 io_wq_exit_start(tctx->io_wq);
9698
Jens Axboefdaf0832020-10-30 09:37:30 -06009699 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009700 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009701 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009702 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009703 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009704 if (!inflight)
9705 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009706
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009707 if (!sqd) {
9708 struct io_tctx_node *node;
9709 unsigned long index;
9710
9711 xa_for_each(&tctx->xa, index, node) {
9712 /* sqpoll task will cancel all its requests */
9713 if (node->ctx->sq_data)
9714 continue;
9715 io_uring_try_cancel_requests(node->ctx, current,
9716 cancel_all);
9717 }
9718 } else {
9719 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9720 io_uring_try_cancel_requests(ctx, current,
9721 cancel_all);
9722 }
9723
9724 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009725 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009726 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009727 * If we've seen completions, retry without waiting. This
9728 * avoids a race where a completion comes in before we did
9729 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009730 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009731 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009732 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009733 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009734 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009735 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009736
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009737 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009738 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009739 /* for exec all current's requests should be gone, kill tctx */
9740 __io_uring_free(current);
9741 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009742}
9743
Hao Xuf552a272021-08-12 12:14:35 +08009744void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009745{
Hao Xuf552a272021-08-12 12:14:35 +08009746 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009747}
9748
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009749static void *io_uring_validate_mmap_request(struct file *file,
9750 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009751{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009752 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009753 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009754 struct page *page;
9755 void *ptr;
9756
9757 switch (offset) {
9758 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009759 case IORING_OFF_CQ_RING:
9760 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009761 break;
9762 case IORING_OFF_SQES:
9763 ptr = ctx->sq_sqes;
9764 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009765 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009766 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009767 }
9768
9769 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009770 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009771 return ERR_PTR(-EINVAL);
9772
9773 return ptr;
9774}
9775
9776#ifdef CONFIG_MMU
9777
9778static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9779{
9780 size_t sz = vma->vm_end - vma->vm_start;
9781 unsigned long pfn;
9782 void *ptr;
9783
9784 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9785 if (IS_ERR(ptr))
9786 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009787
9788 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9789 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9790}
9791
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009792#else /* !CONFIG_MMU */
9793
9794static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9795{
9796 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9797}
9798
9799static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9800{
9801 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9802}
9803
9804static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9805 unsigned long addr, unsigned long len,
9806 unsigned long pgoff, unsigned long flags)
9807{
9808 void *ptr;
9809
9810 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9811 if (IS_ERR(ptr))
9812 return PTR_ERR(ptr);
9813
9814 return (unsigned long) ptr;
9815}
9816
9817#endif /* !CONFIG_MMU */
9818
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009819static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009820{
9821 DEFINE_WAIT(wait);
9822
9823 do {
9824 if (!io_sqring_full(ctx))
9825 break;
Jens Axboe90554202020-09-03 12:12:41 -06009826 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9827
9828 if (!io_sqring_full(ctx))
9829 break;
Jens Axboe90554202020-09-03 12:12:41 -06009830 schedule();
9831 } while (!signal_pending(current));
9832
9833 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009834 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009835}
9836
Hao Xuc73ebb62020-11-03 10:54:37 +08009837static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9838 struct __kernel_timespec __user **ts,
9839 const sigset_t __user **sig)
9840{
9841 struct io_uring_getevents_arg arg;
9842
9843 /*
9844 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9845 * is just a pointer to the sigset_t.
9846 */
9847 if (!(flags & IORING_ENTER_EXT_ARG)) {
9848 *sig = (const sigset_t __user *) argp;
9849 *ts = NULL;
9850 return 0;
9851 }
9852
9853 /*
9854 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9855 * timespec and sigset_t pointers if good.
9856 */
9857 if (*argsz != sizeof(arg))
9858 return -EINVAL;
9859 if (copy_from_user(&arg, argp, sizeof(arg)))
9860 return -EFAULT;
9861 *sig = u64_to_user_ptr(arg.sigmask);
9862 *argsz = arg.sigmask_sz;
9863 *ts = u64_to_user_ptr(arg.ts);
9864 return 0;
9865}
9866
Jens Axboe2b188cc2019-01-07 10:46:33 -07009867SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009868 u32, min_complete, u32, flags, const void __user *, argp,
9869 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009870{
9871 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009872 int submitted = 0;
9873 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009874 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009875
Jens Axboe4c6e2772020-07-01 11:29:10 -06009876 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009877
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009878 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9879 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009880 return -EINVAL;
9881
9882 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009883 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009884 return -EBADF;
9885
9886 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009887 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009888 goto out_fput;
9889
9890 ret = -ENXIO;
9891 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009892 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009893 goto out_fput;
9894
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009895 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009896 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009897 goto out;
9898
Jens Axboe6c271ce2019-01-10 11:22:30 -07009899 /*
9900 * For SQ polling, the thread will do all submissions and completions.
9901 * Just return the requested submit count, and wake the thread if
9902 * we were asked to.
9903 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009904 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009905 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009906 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009907
Jens Axboe21f96522021-08-14 09:04:40 -06009908 if (unlikely(ctx->sq_data->thread == NULL)) {
9909 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009910 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009911 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009912 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009913 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009914 if (flags & IORING_ENTER_SQ_WAIT) {
9915 ret = io_sqpoll_wait_sq(ctx);
9916 if (ret)
9917 goto out;
9918 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009919 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009920 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009921 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009922 if (unlikely(ret))
9923 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009924 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009925 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009926 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009927
9928 if (submitted != to_submit)
9929 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009930 }
9931 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009932 const sigset_t __user *sig;
9933 struct __kernel_timespec __user *ts;
9934
9935 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9936 if (unlikely(ret))
9937 goto out;
9938
Jens Axboe2b188cc2019-01-07 10:46:33 -07009939 min_complete = min(min_complete, ctx->cq_entries);
9940
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009941 /*
9942 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9943 * space applications don't need to do io completion events
9944 * polling again, they can rely on io_sq_thread to do polling
9945 * work, which can reduce cpu usage and uring_lock contention.
9946 */
9947 if (ctx->flags & IORING_SETUP_IOPOLL &&
9948 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009949 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009950 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009951 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009952 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009953 }
9954
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009955out:
Pavel Begunkov6805b32e2019-10-08 02:18:42 +03009956 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009957out_fput:
9958 fdput(f);
9959 return submitted ? submitted : ret;
9960}
9961
Tobias Klauserbebdb652020-02-26 18:38:32 +01009962#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009963static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9964 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009965{
Jens Axboe87ce9552020-01-30 08:25:34 -07009966 struct user_namespace *uns = seq_user_ns(m);
9967 struct group_info *gi;
9968 kernel_cap_t cap;
9969 unsigned __capi;
9970 int g;
9971
9972 seq_printf(m, "%5d\n", id);
9973 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9974 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9975 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9976 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9977 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9978 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9979 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9980 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9981 seq_puts(m, "\n\tGroups:\t");
9982 gi = cred->group_info;
9983 for (g = 0; g < gi->ngroups; g++) {
9984 seq_put_decimal_ull(m, g ? " " : "",
9985 from_kgid_munged(uns, gi->gid[g]));
9986 }
9987 seq_puts(m, "\n\tCapEff:\t");
9988 cap = cred->cap_effective;
9989 CAP_FOR_EACH_U32(__capi)
9990 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9991 seq_putc(m, '\n');
9992 return 0;
9993}
9994
9995static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9996{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009997 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009998 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009999 int i;
10000
Jens Axboefad8e0d2020-09-28 08:57:48 -060010001 /*
10002 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
10003 * since fdinfo case grabs it in the opposite direction of normal use
10004 * cases. If we fail to get the lock, we just don't iterate any
10005 * structures that could be going away outside the io_uring mutex.
10006 */
10007 has_lock = mutex_trylock(&ctx->uring_lock);
10008
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010009 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -060010010 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -070010011 if (!sq->thread)
10012 sq = NULL;
10013 }
Joseph Qidbbe9c62020-09-29 09:01:22 -060010014
10015 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10016 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -070010017 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010018 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -070010019 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -070010020
Jens Axboe87ce9552020-01-30 08:25:34 -070010021 if (f)
10022 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
10023 else
10024 seq_printf(m, "%5u: <none>\n", i);
10025 }
10026 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010027 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +010010028 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +010010029 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -070010030
Pavel Begunkov4751f532021-04-01 15:43:55 +010010031 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -070010032 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010033 if (has_lock && !xa_empty(&ctx->personalities)) {
10034 unsigned long index;
10035 const struct cred *cred;
10036
Jens Axboe87ce9552020-01-30 08:25:34 -070010037 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010038 xa_for_each(&ctx->personalities, index, cred)
10039 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -070010040 }
Jens Axboed7718a92020-02-14 22:23:12 -070010041 seq_printf(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -060010042 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -070010043 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
10044 struct hlist_head *list = &ctx->cancel_hash[i];
10045 struct io_kiocb *req;
10046
10047 hlist_for_each_entry(req, list, hash_node)
10048 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
10049 req->task->task_works != NULL);
10050 }
Jens Axboe79ebeae2021-08-10 15:18:27 -060010051 spin_unlock(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -060010052 if (has_lock)
10053 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -070010054}
10055
10056static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
10057{
10058 struct io_ring_ctx *ctx = f->private_data;
10059
10060 if (percpu_ref_tryget(&ctx->refs)) {
10061 __io_uring_show_fdinfo(ctx, m);
10062 percpu_ref_put(&ctx->refs);
10063 }
10064}
Tobias Klauserbebdb652020-02-26 18:38:32 +010010065#endif
Jens Axboe87ce9552020-01-30 08:25:34 -070010066
Jens Axboe2b188cc2019-01-07 10:46:33 -070010067static const struct file_operations io_uring_fops = {
10068 .release = io_uring_release,
10069 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +010010070#ifndef CONFIG_MMU
10071 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
10072 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
10073#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010074 .poll = io_uring_poll,
10075 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010076#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -070010077 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +010010078#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -070010079};
10080
10081static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
10082 struct io_uring_params *p)
10083{
Hristo Venev75b28af2019-08-26 17:23:46 +000010084 struct io_rings *rings;
10085 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010086
Jens Axboebd740482020-08-05 12:58:23 -060010087 /* make sure these are sane, as we already accounted them */
10088 ctx->sq_entries = p->sq_entries;
10089 ctx->cq_entries = p->cq_entries;
10090
Hristo Venev75b28af2019-08-26 17:23:46 +000010091 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
10092 if (size == SIZE_MAX)
10093 return -EOVERFLOW;
10094
10095 rings = io_mem_alloc(size);
10096 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010097 return -ENOMEM;
10098
Hristo Venev75b28af2019-08-26 17:23:46 +000010099 ctx->rings = rings;
10100 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
10101 rings->sq_ring_mask = p->sq_entries - 1;
10102 rings->cq_ring_mask = p->cq_entries - 1;
10103 rings->sq_ring_entries = p->sq_entries;
10104 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010105
10106 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -070010107 if (size == SIZE_MAX) {
10108 io_mem_free(ctx->rings);
10109 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010110 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -070010111 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010112
10113 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -070010114 if (!ctx->sq_sqes) {
10115 io_mem_free(ctx->rings);
10116 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010117 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -070010118 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010119
Jens Axboe2b188cc2019-01-07 10:46:33 -070010120 return 0;
10121}
10122
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010123static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
10124{
10125 int ret, fd;
10126
10127 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
10128 if (fd < 0)
10129 return fd;
10130
Pavel Begunkoveef51da2021-06-14 02:36:15 +010010131 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010132 if (ret) {
10133 put_unused_fd(fd);
10134 return ret;
10135 }
10136 fd_install(fd, file);
10137 return fd;
10138}
10139
Jens Axboe2b188cc2019-01-07 10:46:33 -070010140/*
10141 * Allocate an anonymous fd, this is what constitutes the application
10142 * visible backing of an io_uring instance. The application mmaps this
10143 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
10144 * we have to tie this fd to a socket for file garbage collection purposes.
10145 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010146static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010147{
10148 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010149#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010150 int ret;
10151
Jens Axboe2b188cc2019-01-07 10:46:33 -070010152 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
10153 &ctx->ring_sock);
10154 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010155 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010156#endif
10157
Jens Axboe2b188cc2019-01-07 10:46:33 -070010158 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
10159 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010160#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010161 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010162 sock_release(ctx->ring_sock);
10163 ctx->ring_sock = NULL;
10164 } else {
10165 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010166 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010167#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010168 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010169}
10170
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010171static int io_uring_create(unsigned entries, struct io_uring_params *p,
10172 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010173{
Jens Axboe2b188cc2019-01-07 10:46:33 -070010174 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010175 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010176 int ret;
10177
Jens Axboe8110c1a2019-12-28 15:39:54 -070010178 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010179 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010180 if (entries > IORING_MAX_ENTRIES) {
10181 if (!(p->flags & IORING_SETUP_CLAMP))
10182 return -EINVAL;
10183 entries = IORING_MAX_ENTRIES;
10184 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010185
10186 /*
10187 * Use twice as many entries for the CQ ring. It's possible for the
10188 * application to drive a higher depth than the size of the SQ ring,
10189 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -060010190 * some flexibility in overcommitting a bit. If the application has
10191 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
10192 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -070010193 */
10194 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -060010195 if (p->flags & IORING_SETUP_CQSIZE) {
10196 /*
10197 * If IORING_SETUP_CQSIZE is set, we do the same roundup
10198 * to a power-of-two, if it isn't already. We do NOT impose
10199 * any cq vs sq ring sizing.
10200 */
Joseph Qieb2667b32020-11-24 15:03:03 +080010201 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -060010202 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -070010203 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
10204 if (!(p->flags & IORING_SETUP_CLAMP))
10205 return -EINVAL;
10206 p->cq_entries = IORING_MAX_CQ_ENTRIES;
10207 }
Joseph Qieb2667b32020-11-24 15:03:03 +080010208 p->cq_entries = roundup_pow_of_two(p->cq_entries);
10209 if (p->cq_entries < p->sq_entries)
10210 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -060010211 } else {
10212 p->cq_entries = 2 * p->sq_entries;
10213 }
Jens Axboe2b188cc2019-01-07 10:46:33 -070010214
Jens Axboe2b188cc2019-01-07 10:46:33 -070010215 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -070010216 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -070010217 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010218 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -070010219 if (!capable(CAP_IPC_LOCK))
10220 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -060010221
10222 /*
10223 * This is just grabbed for accounting purposes. When a process exits,
10224 * the mm is exited and dropped before the files, hence we need to hang
10225 * on to this mm purely for the purposes of being able to unaccount
10226 * memory (locked/pinned vm). It's not used for anything else.
10227 */
Jens Axboe6b7898e2020-08-25 07:58:00 -060010228 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -060010229 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -060010230
Jens Axboe2b188cc2019-01-07 10:46:33 -070010231 ret = io_allocate_scq_urings(ctx, p);
10232 if (ret)
10233 goto err;
10234
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010235 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010236 if (ret)
10237 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010238 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +010010239 ret = io_rsrc_node_switch_start(ctx);
10240 if (ret)
10241 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +010010242 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010243
Jens Axboe2b188cc2019-01-07 10:46:33 -070010244 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010245 p->sq_off.head = offsetof(struct io_rings, sq.head);
10246 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
10247 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
10248 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
10249 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
10250 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
10251 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010252
10253 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +000010254 p->cq_off.head = offsetof(struct io_rings, cq.head);
10255 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
10256 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
10257 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
10258 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
10259 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +020010260 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -060010261
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010262 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
10263 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +080010264 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +080010265 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +010010266 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
10267 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010268
10269 if (copy_to_user(params, p, sizeof(*p))) {
10270 ret = -EFAULT;
10271 goto err;
10272 }
Jens Axboed1719f72020-07-30 13:43:53 -060010273
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010274 file = io_uring_get_file(ctx);
10275 if (IS_ERR(file)) {
10276 ret = PTR_ERR(file);
10277 goto err;
10278 }
10279
Jens Axboed1719f72020-07-30 13:43:53 -060010280 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -060010281 * Install ring fd as the very last thing, so we don't risk someone
10282 * having closed it before we finish setup
10283 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +000010284 ret = io_uring_install_fd(ctx, file);
10285 if (ret < 0) {
10286 /* fput will clean it up */
10287 fput(file);
10288 return ret;
10289 }
Jens Axboe044c1ab2019-10-28 09:15:33 -060010290
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010291 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010292 return ret;
10293err:
10294 io_ring_ctx_wait_and_kill(ctx);
10295 return ret;
10296}
10297
10298/*
10299 * Sets up an aio uring context, and returns the fd. Applications asks for a
10300 * ring size, we return the actual sq/cq ring sizes (among other things) in the
10301 * params structure passed in.
10302 */
10303static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
10304{
10305 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -070010306 int i;
10307
10308 if (copy_from_user(&p, params, sizeof(p)))
10309 return -EFAULT;
10310 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
10311 if (p.resv[i])
10312 return -EINVAL;
10313 }
10314
Jens Axboe6c271ce2019-01-10 11:22:30 -070010315 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -070010316 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010317 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
10318 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -070010319 return -EINVAL;
10320
Xiaoguang Wang7f136572020-05-05 16:28:53 +080010321 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010322}
10323
10324SYSCALL_DEFINE2(io_uring_setup, u32, entries,
10325 struct io_uring_params __user *, params)
10326{
10327 return io_uring_setup(entries, params);
10328}
10329
Jens Axboe66f4af92020-01-16 15:36:52 -070010330static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
10331{
10332 struct io_uring_probe *p;
10333 size_t size;
10334 int i, ret;
10335
10336 size = struct_size(p, ops, nr_args);
10337 if (size == SIZE_MAX)
10338 return -EOVERFLOW;
10339 p = kzalloc(size, GFP_KERNEL);
10340 if (!p)
10341 return -ENOMEM;
10342
10343 ret = -EFAULT;
10344 if (copy_from_user(p, arg, size))
10345 goto out;
10346 ret = -EINVAL;
10347 if (memchr_inv(p, 0, size))
10348 goto out;
10349
10350 p->last_op = IORING_OP_LAST - 1;
10351 if (nr_args > IORING_OP_LAST)
10352 nr_args = IORING_OP_LAST;
10353
10354 for (i = 0; i < nr_args; i++) {
10355 p->ops[i].op = i;
10356 if (!io_op_defs[i].not_supported)
10357 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10358 }
10359 p->ops_len = i;
10360
10361 ret = 0;
10362 if (copy_to_user(arg, p, size))
10363 ret = -EFAULT;
10364out:
10365 kfree(p);
10366 return ret;
10367}
10368
Jens Axboe071698e2020-01-28 10:04:42 -070010369static int io_register_personality(struct io_ring_ctx *ctx)
10370{
Jens Axboe4379bf82021-02-15 13:40:22 -070010371 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010372 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010373 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010374
Jens Axboe4379bf82021-02-15 13:40:22 -070010375 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010376
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010377 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10378 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010379 if (ret < 0) {
10380 put_cred(creds);
10381 return ret;
10382 }
10383 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010384}
10385
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010386static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10387 unsigned int nr_args)
10388{
10389 struct io_uring_restriction *res;
10390 size_t size;
10391 int i, ret;
10392
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010393 /* Restrictions allowed only if rings started disabled */
10394 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10395 return -EBADFD;
10396
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010397 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010398 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010399 return -EBUSY;
10400
10401 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10402 return -EINVAL;
10403
10404 size = array_size(nr_args, sizeof(*res));
10405 if (size == SIZE_MAX)
10406 return -EOVERFLOW;
10407
10408 res = memdup_user(arg, size);
10409 if (IS_ERR(res))
10410 return PTR_ERR(res);
10411
10412 ret = 0;
10413
10414 for (i = 0; i < nr_args; i++) {
10415 switch (res[i].opcode) {
10416 case IORING_RESTRICTION_REGISTER_OP:
10417 if (res[i].register_op >= IORING_REGISTER_LAST) {
10418 ret = -EINVAL;
10419 goto out;
10420 }
10421
10422 __set_bit(res[i].register_op,
10423 ctx->restrictions.register_op);
10424 break;
10425 case IORING_RESTRICTION_SQE_OP:
10426 if (res[i].sqe_op >= IORING_OP_LAST) {
10427 ret = -EINVAL;
10428 goto out;
10429 }
10430
10431 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10432 break;
10433 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10434 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10435 break;
10436 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10437 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10438 break;
10439 default:
10440 ret = -EINVAL;
10441 goto out;
10442 }
10443 }
10444
10445out:
10446 /* Reset all restrictions if an error happened */
10447 if (ret != 0)
10448 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10449 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010450 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010451
10452 kfree(res);
10453 return ret;
10454}
10455
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010456static int io_register_enable_rings(struct io_ring_ctx *ctx)
10457{
10458 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10459 return -EBADFD;
10460
10461 if (ctx->restrictions.registered)
10462 ctx->restricted = 1;
10463
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010464 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10465 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10466 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010467 return 0;
10468}
10469
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010470static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010471 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010472 unsigned nr_args)
10473{
10474 __u32 tmp;
10475 int err;
10476
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010477 if (up->resv)
10478 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010479 if (check_add_overflow(up->offset, nr_args, &tmp))
10480 return -EOVERFLOW;
10481 err = io_rsrc_node_switch_start(ctx);
10482 if (err)
10483 return err;
10484
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010485 switch (type) {
10486 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010487 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010488 case IORING_RSRC_BUFFER:
10489 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010490 }
10491 return -EINVAL;
10492}
10493
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010494static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10495 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010496{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010497 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010498
10499 if (!nr_args)
10500 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010501 memset(&up, 0, sizeof(up));
10502 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10503 return -EFAULT;
10504 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10505}
10506
10507static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010508 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010509{
10510 struct io_uring_rsrc_update2 up;
10511
10512 if (size != sizeof(up))
10513 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010514 if (copy_from_user(&up, arg, sizeof(up)))
10515 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010516 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010517 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010518 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010519}
10520
Pavel Begunkov792e3582021-04-25 14:32:21 +010010521static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010522 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010523{
10524 struct io_uring_rsrc_register rr;
10525
10526 /* keep it extendible */
10527 if (size != sizeof(rr))
10528 return -EINVAL;
10529
10530 memset(&rr, 0, sizeof(rr));
10531 if (copy_from_user(&rr, arg, size))
10532 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010533 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010534 return -EINVAL;
10535
Pavel Begunkov992da012021-06-10 16:37:37 +010010536 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010537 case IORING_RSRC_FILE:
10538 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10539 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010540 case IORING_RSRC_BUFFER:
10541 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10542 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010543 }
10544 return -EINVAL;
10545}
10546
Jens Axboefe764212021-06-17 10:19:54 -060010547static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10548 unsigned len)
10549{
10550 struct io_uring_task *tctx = current->io_uring;
10551 cpumask_var_t new_mask;
10552 int ret;
10553
10554 if (!tctx || !tctx->io_wq)
10555 return -EINVAL;
10556
10557 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10558 return -ENOMEM;
10559
10560 cpumask_clear(new_mask);
10561 if (len > cpumask_size())
10562 len = cpumask_size();
10563
10564 if (copy_from_user(new_mask, arg, len)) {
10565 free_cpumask_var(new_mask);
10566 return -EFAULT;
10567 }
10568
10569 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10570 free_cpumask_var(new_mask);
10571 return ret;
10572}
10573
10574static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10575{
10576 struct io_uring_task *tctx = current->io_uring;
10577
10578 if (!tctx || !tctx->io_wq)
10579 return -EINVAL;
10580
10581 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10582}
10583
Jens Axboe2e480052021-08-27 11:33:19 -060010584static int io_register_iowq_max_workers(struct io_ring_ctx *ctx,
10585 void __user *arg)
10586{
Jens Axboefa846932021-09-01 14:15:59 -060010587 struct io_uring_task *tctx = NULL;
10588 struct io_sq_data *sqd = NULL;
Jens Axboe2e480052021-08-27 11:33:19 -060010589 __u32 new_count[2];
10590 int i, ret;
10591
Jens Axboe2e480052021-08-27 11:33:19 -060010592 if (copy_from_user(new_count, arg, sizeof(new_count)))
10593 return -EFAULT;
10594 for (i = 0; i < ARRAY_SIZE(new_count); i++)
10595 if (new_count[i] > INT_MAX)
10596 return -EINVAL;
10597
Jens Axboefa846932021-09-01 14:15:59 -060010598 if (ctx->flags & IORING_SETUP_SQPOLL) {
10599 sqd = ctx->sq_data;
10600 if (sqd) {
Jens Axboe009ad9f2021-09-08 19:07:26 -060010601 /*
10602 * Observe the correct sqd->lock -> ctx->uring_lock
10603 * ordering. Fine to drop uring_lock here, we hold
10604 * a ref to the ctx.
10605 */
10606 mutex_unlock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010607 mutex_lock(&sqd->lock);
Jens Axboe009ad9f2021-09-08 19:07:26 -060010608 mutex_lock(&ctx->uring_lock);
Jens Axboefa846932021-09-01 14:15:59 -060010609 tctx = sqd->thread->io_uring;
10610 }
10611 } else {
10612 tctx = current->io_uring;
10613 }
10614
10615 ret = -EINVAL;
10616 if (!tctx || !tctx->io_wq)
10617 goto err;
10618
Jens Axboe2e480052021-08-27 11:33:19 -060010619 ret = io_wq_max_workers(tctx->io_wq, new_count);
10620 if (ret)
Jens Axboefa846932021-09-01 14:15:59 -060010621 goto err;
10622
10623 if (sqd)
10624 mutex_unlock(&sqd->lock);
Jens Axboe2e480052021-08-27 11:33:19 -060010625
10626 if (copy_to_user(arg, new_count, sizeof(new_count)))
10627 return -EFAULT;
10628
10629 return 0;
Jens Axboefa846932021-09-01 14:15:59 -060010630err:
10631 if (sqd)
10632 mutex_unlock(&sqd->lock);
10633 return ret;
Jens Axboe2e480052021-08-27 11:33:19 -060010634}
10635
Jens Axboe071698e2020-01-28 10:04:42 -070010636static bool io_register_op_must_quiesce(int op)
10637{
10638 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010639 case IORING_REGISTER_BUFFERS:
10640 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010641 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010642 case IORING_UNREGISTER_FILES:
10643 case IORING_REGISTER_FILES_UPDATE:
10644 case IORING_REGISTER_PROBE:
10645 case IORING_REGISTER_PERSONALITY:
10646 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010647 case IORING_REGISTER_FILES2:
10648 case IORING_REGISTER_FILES_UPDATE2:
10649 case IORING_REGISTER_BUFFERS2:
10650 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010651 case IORING_REGISTER_IOWQ_AFF:
10652 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe2e480052021-08-27 11:33:19 -060010653 case IORING_REGISTER_IOWQ_MAX_WORKERS:
Jens Axboe071698e2020-01-28 10:04:42 -070010654 return false;
10655 default:
10656 return true;
10657 }
10658}
10659
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010660static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10661{
10662 long ret;
10663
10664 percpu_ref_kill(&ctx->refs);
10665
10666 /*
10667 * Drop uring mutex before waiting for references to exit. If another
10668 * thread is currently inside io_uring_enter() it might need to grab the
10669 * uring_lock to make progress. If we hold it here across the drain
10670 * wait, then we can deadlock. It's safe to drop the mutex here, since
10671 * no new references will come in after we've killed the percpu ref.
10672 */
10673 mutex_unlock(&ctx->uring_lock);
10674 do {
10675 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10676 if (!ret)
10677 break;
10678 ret = io_run_task_work_sig();
10679 } while (ret >= 0);
10680 mutex_lock(&ctx->uring_lock);
10681
10682 if (ret)
10683 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10684 return ret;
10685}
10686
Jens Axboeedafcce2019-01-09 09:16:05 -070010687static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10688 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010689 __releases(ctx->uring_lock)
10690 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010691{
10692 int ret;
10693
Jens Axboe35fa71a2019-04-22 10:23:23 -060010694 /*
10695 * We're inside the ring mutex, if the ref is already dying, then
10696 * someone else killed the ctx or is already going through
10697 * io_uring_register().
10698 */
10699 if (percpu_ref_is_dying(&ctx->refs))
10700 return -ENXIO;
10701
Pavel Begunkov75c40212021-04-15 13:07:40 +010010702 if (ctx->restricted) {
10703 if (opcode >= IORING_REGISTER_LAST)
10704 return -EINVAL;
10705 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10706 if (!test_bit(opcode, ctx->restrictions.register_op))
10707 return -EACCES;
10708 }
10709
Jens Axboe071698e2020-01-28 10:04:42 -070010710 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010711 ret = io_ctx_quiesce(ctx);
10712 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010713 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010714 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010715
10716 switch (opcode) {
10717 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010718 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010719 break;
10720 case IORING_UNREGISTER_BUFFERS:
10721 ret = -EINVAL;
10722 if (arg || nr_args)
10723 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010724 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010725 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010726 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010727 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010728 break;
10729 case IORING_UNREGISTER_FILES:
10730 ret = -EINVAL;
10731 if (arg || nr_args)
10732 break;
10733 ret = io_sqe_files_unregister(ctx);
10734 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010735 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010736 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010737 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010738 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010739 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010740 ret = -EINVAL;
10741 if (nr_args != 1)
10742 break;
10743 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010744 if (ret)
10745 break;
10746 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10747 ctx->eventfd_async = 1;
10748 else
10749 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010750 break;
10751 case IORING_UNREGISTER_EVENTFD:
10752 ret = -EINVAL;
10753 if (arg || nr_args)
10754 break;
10755 ret = io_eventfd_unregister(ctx);
10756 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010757 case IORING_REGISTER_PROBE:
10758 ret = -EINVAL;
10759 if (!arg || nr_args > 256)
10760 break;
10761 ret = io_probe(ctx, arg, nr_args);
10762 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010763 case IORING_REGISTER_PERSONALITY:
10764 ret = -EINVAL;
10765 if (arg || nr_args)
10766 break;
10767 ret = io_register_personality(ctx);
10768 break;
10769 case IORING_UNREGISTER_PERSONALITY:
10770 ret = -EINVAL;
10771 if (arg)
10772 break;
10773 ret = io_unregister_personality(ctx, nr_args);
10774 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010775 case IORING_REGISTER_ENABLE_RINGS:
10776 ret = -EINVAL;
10777 if (arg || nr_args)
10778 break;
10779 ret = io_register_enable_rings(ctx);
10780 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010781 case IORING_REGISTER_RESTRICTIONS:
10782 ret = io_register_restrictions(ctx, arg, nr_args);
10783 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010784 case IORING_REGISTER_FILES2:
10785 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010786 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010787 case IORING_REGISTER_FILES_UPDATE2:
10788 ret = io_register_rsrc_update(ctx, arg, nr_args,
10789 IORING_RSRC_FILE);
10790 break;
10791 case IORING_REGISTER_BUFFERS2:
10792 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10793 break;
10794 case IORING_REGISTER_BUFFERS_UPDATE:
10795 ret = io_register_rsrc_update(ctx, arg, nr_args,
10796 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010797 break;
Jens Axboefe764212021-06-17 10:19:54 -060010798 case IORING_REGISTER_IOWQ_AFF:
10799 ret = -EINVAL;
10800 if (!arg || !nr_args)
10801 break;
10802 ret = io_register_iowq_aff(ctx, arg, nr_args);
10803 break;
10804 case IORING_UNREGISTER_IOWQ_AFF:
10805 ret = -EINVAL;
10806 if (arg || nr_args)
10807 break;
10808 ret = io_unregister_iowq_aff(ctx);
10809 break;
Jens Axboe2e480052021-08-27 11:33:19 -060010810 case IORING_REGISTER_IOWQ_MAX_WORKERS:
10811 ret = -EINVAL;
10812 if (!arg || nr_args != 2)
10813 break;
10814 ret = io_register_iowq_max_workers(ctx, arg);
10815 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010816 default:
10817 ret = -EINVAL;
10818 break;
10819 }
10820
Jens Axboe071698e2020-01-28 10:04:42 -070010821 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010822 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010823 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010824 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010825 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010826 return ret;
10827}
10828
10829SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10830 void __user *, arg, unsigned int, nr_args)
10831{
10832 struct io_ring_ctx *ctx;
10833 long ret = -EBADF;
10834 struct fd f;
10835
10836 f = fdget(fd);
10837 if (!f.file)
10838 return -EBADF;
10839
10840 ret = -EOPNOTSUPP;
10841 if (f.file->f_op != &io_uring_fops)
10842 goto out_fput;
10843
10844 ctx = f.file->private_data;
10845
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010846 io_run_task_work();
10847
Jens Axboeedafcce2019-01-09 09:16:05 -070010848 mutex_lock(&ctx->uring_lock);
10849 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10850 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010851 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10852 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010853out_fput:
10854 fdput(f);
10855 return ret;
10856}
10857
Jens Axboe2b188cc2019-01-07 10:46:33 -070010858static int __init io_uring_init(void)
10859{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010860#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10861 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10862 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10863} while (0)
10864
10865#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10866 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10867 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10868 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10869 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10870 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10871 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10872 BUILD_BUG_SQE_ELEM(8, __u64, off);
10873 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10874 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010875 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010876 BUILD_BUG_SQE_ELEM(24, __u32, len);
10877 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10878 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10879 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10880 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010881 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10882 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010883 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10884 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10885 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10886 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10887 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10888 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10889 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10890 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010891 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010892 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10893 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010894 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010895 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010896 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010897 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010898
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010899 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10900 sizeof(struct io_uring_rsrc_update));
10901 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10902 sizeof(struct io_uring_rsrc_update2));
Pavel Begunkov90499ad2021-08-25 20:51:40 +010010903
10904 /* ->buf_index is u16 */
10905 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
10906
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010907 /* should fit into one byte */
10908 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
10909
Jens Axboed3656342019-12-18 09:50:26 -070010910 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Hao Xu32c2d332021-09-07 11:22:43 +080010911 BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010912
Jens Axboe91f245d2021-02-09 13:48:50 -070010913 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10914 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010915 return 0;
10916};
10917__initcall(io_uring_init);