blob: 62da427d614d785950aea0411184abdb35b958b1 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
Pavel Begunkovd068b502021-05-16 22:58:11 +010014 * through a control-dependency in io_get_cqe (smp_store_release to
Stefan Bühler1e84b972019-04-24 23:54:16 +020015 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070076#include <linux/eventpoll.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030077#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070078#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060079#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060080#include <linux/io_uring.h>
Nadav Amitef98eb02021-08-07 17:13:41 -070081#include <linux/tracehook.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070082
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020083#define CREATE_TRACE_POINTS
84#include <trace/events/io_uring.h>
85
Jens Axboe2b188cc2019-01-07 10:46:33 -070086#include <uapi/linux/io_uring.h>
87
88#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060089#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070090
Daniel Xu5277dea2019-09-14 14:23:45 -070091#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060092#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Olivier Langlois4ce8ad92021-06-23 11:50:18 -070093#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
Jens Axboe65e19f52019-10-26 07:20:21 -060094
wangyangbo187f08c2021-08-19 13:56:57 +080095/* only define max */
Pavel Begunkov042b0d82021-08-09 13:04:01 +010096#define IORING_MAX_FIXED_FILES (1U << 15)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020097#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
98 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -070099
wangyangbo187f08c2021-08-19 13:56:57 +0800100#define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100101#define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
102#define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
103
Pavel Begunkov489809e2021-05-14 12:06:44 +0100104#define IORING_MAX_REG_BUFFERS (1U << 14)
105
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000106#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
107 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
108 IOSQE_BUFFER_SELECT)
Pavel Begunkovc8543572021-06-17 18:14:04 +0100109#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \
110 REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS)
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000111
Pavel Begunkov09899b12021-06-14 02:36:22 +0100112#define IO_TCTX_REFS_CACHE_NR (1U << 10)
113
Jens Axboe2b188cc2019-01-07 10:46:33 -0700114struct io_uring {
115 u32 head ____cacheline_aligned_in_smp;
116 u32 tail ____cacheline_aligned_in_smp;
117};
118
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 * This data is shared with the application through the mmap at offsets
121 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 *
123 * The offsets to the member fields are published through struct
124 * io_sqring_offsets when calling io_uring_setup.
125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Head and tail offsets into the ring; the offsets need to be
129 * masked to get valid indices.
130 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000131 * The kernel controls head of the sq ring and the tail of the cq ring,
132 * and the application controls tail of the sq ring and the head of the
133 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000135 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200136 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 * ring_entries - 1)
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_ring_mask, cq_ring_mask;
141 /* Ring sizes (constant, power of 2) */
142 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200143 /*
144 * Number of invalid entries dropped by the kernel due to
145 * invalid index stored in array
146 *
147 * Written by the kernel, shouldn't be modified by the
148 * application (i.e. get number of "new events" by comparing to
149 * cached value).
150 *
151 * After a new SQ head value was read by the application this
152 * counter includes all submissions that were dropped reaching
153 * the new SQ head (and possibly more).
154 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000155 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200156 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200157 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 *
159 * Written by the kernel, shouldn't be modified by the
160 * application.
161 *
162 * The application needs a full memory barrier before checking
163 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200167 * Runtime CQ flags
168 *
169 * Written by the application, shouldn't be modified by the
170 * kernel.
171 */
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100172 u32 cq_flags;
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200173 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200174 * Number of completion events lost because the queue was full;
175 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800176 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200177 * the completion queue.
178 *
179 * Written by the kernel, shouldn't be modified by the
180 * application (i.e. get number of "new events" by comparing to
181 * cached value).
182 *
183 * As completion events come in out of order this counter is not
184 * ordered with any other data.
185 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000186 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200187 /*
188 * Ring buffer of completion events.
189 *
190 * The kernel writes completion events fresh every time they are
191 * produced, so the application is allowed to modify pending
192 * entries.
193 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000194 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700195};
196
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000197enum io_uring_cmd_flags {
198 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000199 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000200};
201
Jens Axboeedafcce2019-01-09 09:16:05 -0700202struct io_mapped_ubuf {
203 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100204 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700205 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600206 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100207 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700208};
209
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000210struct io_ring_ctx;
211
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000212struct io_overflow_cqe {
213 struct io_uring_cqe cqe;
214 struct list_head list;
215};
216
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100217struct io_fixed_file {
218 /* file * with additional FFS_* flags */
219 unsigned long file_ptr;
220};
221
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000222struct io_rsrc_put {
223 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100224 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000225 union {
226 void *rsrc;
227 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100228 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000229 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000230};
231
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100232struct io_file_table {
Pavel Begunkov042b0d82021-08-09 13:04:01 +0100233 struct io_fixed_file *files;
Jens Axboe31b51512019-01-18 22:56:34 -0700234};
235
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100236struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800237 struct percpu_ref refs;
238 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000239 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100240 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600241 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000242 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800243};
244
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100245typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
246
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100247struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700248 struct io_ring_ctx *ctx;
249
Pavel Begunkov2d091d62021-06-14 02:36:21 +0100250 u64 **tags;
251 unsigned int nr;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100252 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100253 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700254 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800255 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700256};
257
Jens Axboe5a2e7452020-02-23 16:23:11 -0700258struct io_buffer {
259 struct list_head list;
260 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300261 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700262 __u16 bid;
263};
264
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200265struct io_restriction {
266 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
267 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
268 u8 sqe_flags_allowed;
269 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200270 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200271};
272
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700273enum {
274 IO_SQ_THREAD_SHOULD_STOP = 0,
275 IO_SQ_THREAD_SHOULD_PARK,
276};
277
Jens Axboe534ca6d2020-09-02 13:52:19 -0600278struct io_sq_data {
279 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000280 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000281 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600282
283 /* ctx's that are using this sqd */
284 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600285
Jens Axboe534ca6d2020-09-02 13:52:19 -0600286 struct task_struct *thread;
287 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800288
289 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700290 int sq_cpu;
291 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700292 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700293
294 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700295 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600296};
297
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000298#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000299#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000300#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000301
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000302struct io_submit_link {
303 struct io_kiocb *head;
304 struct io_kiocb *last;
305};
306
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000307struct io_submit_state {
308 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000309 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000310
311 /*
312 * io_kiocb alloc cache
313 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000314 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000315 unsigned int free_reqs;
316
317 bool plug_started;
318
319 /*
320 * Batch completion logic
321 */
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +0100322 struct io_kiocb *compl_reqs[IO_COMPL_BATCH];
323 unsigned int compl_nr;
324 /* inline/task_work completion list, under ->uring_lock */
325 struct list_head free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000326
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000327 unsigned int ios_left;
328};
329
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330struct io_ring_ctx {
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100331 /* const or read-mostly hot data */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332 struct {
333 struct percpu_ref refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100335 struct io_rings *rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800337 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800338 unsigned int drain_next: 1;
339 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200340 unsigned int restricted: 1;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +0100341 unsigned int off_timeout_used: 1;
Pavel Begunkov10c66902021-06-15 16:47:56 +0100342 unsigned int drain_active: 1;
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100343 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100345 /* submission data */
Pavel Begunkovb52ecf82021-06-14 23:37:21 +0100346 struct {
Pavel Begunkov0499e582021-06-14 23:37:29 +0100347 struct mutex uring_lock;
348
Hristo Venev75b28af2019-08-26 17:23:46 +0000349 /*
350 * Ring buffer of indices into array of io_uring_sqe, which is
351 * mmapped by the application using the IORING_OFF_SQES offset.
352 *
353 * This indirection could e.g. be used to assign fixed
354 * io_uring_sqe entries to operations and only submit them to
355 * the queue when needed.
356 *
357 * The kernel modifies neither the indices array nor the entries
358 * array.
359 */
360 u32 *sq_array;
Pavel Begunkovc7af47c2021-06-14 23:37:20 +0100361 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362 unsigned cached_sq_head;
363 unsigned sq_entries;
Jens Axboede0617e2019-04-06 21:51:27 -0600364 struct list_head defer_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100365
366 /*
367 * Fixed resources fast path, should be accessed only under
368 * uring_lock, and updated through io_uring_register(2)
369 */
370 struct io_rsrc_node *rsrc_node;
371 struct io_file_table file_table;
372 unsigned nr_user_files;
373 unsigned nr_user_bufs;
374 struct io_mapped_ubuf **user_bufs;
375
376 struct io_submit_state submit_state;
Jens Axboe5262f562019-09-17 12:26:57 -0600377 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700378 struct list_head cq_overflow_list;
Pavel Begunkov7f1129d2021-06-14 23:37:22 +0100379 struct xarray io_buffers;
380 struct xarray personalities;
381 u32 pers_next;
382 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383 } ____cacheline_aligned_in_smp;
384
Pavel Begunkovd0acdee2021-05-16 22:58:12 +0100385 /* IRQ completion list, under ->completion_lock */
386 struct list_head locked_free_list;
387 unsigned int locked_free_nr;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700388
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100389 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600390 struct io_sq_data *sq_data; /* if using sq thread polling */
391
Jens Axboe90554202020-09-03 12:12:41 -0600392 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600393 struct list_head sqd_list;
Hristo Venev75b28af2019-08-26 17:23:46 +0000394
Pavel Begunkov5ed7a372021-06-14 23:37:27 +0100395 unsigned long check_cq_overflow;
396
Jens Axboe206aefd2019-11-07 18:27:42 -0700397 struct {
398 unsigned cached_cq_tail;
399 unsigned cq_entries;
Jens Axboe206aefd2019-11-07 18:27:42 -0700400 struct eventfd_ctx *cq_ev_fd;
Pavel Begunkov0499e582021-06-14 23:37:29 +0100401 struct wait_queue_head poll_wait;
402 struct wait_queue_head cq_wait;
403 unsigned cq_extra;
404 atomic_t cq_timeouts;
405 struct fasync_struct *cq_fasync;
406 unsigned cq_last_tm_flush;
Jens Axboe206aefd2019-11-07 18:27:42 -0700407 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408
409 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700411
Jens Axboe89850fc2021-08-10 15:11:51 -0600412 spinlock_t timeout_lock;
413
Jens Axboedef596e2019-01-09 08:59:42 -0700414 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300415 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700416 * io_uring instances that don't use IORING_SETUP_SQPOLL.
417 * For SQPOLL, only the single threaded io_sq_thread() will
418 * manipulate the list, hence no extra locking is needed there.
419 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300420 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700421 struct hlist_head *cancel_hash;
422 unsigned cancel_hash_bits;
Hao Xu915b3dd2021-06-28 05:37:30 +0800423 bool poll_multi_queue;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600425
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200426 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700427
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100428 /* slow path rsrc auxilary data, used by update/register */
429 struct {
430 struct io_rsrc_node *rsrc_backup_node;
431 struct io_mapped_ubuf *dummy_ubuf;
432 struct io_rsrc_data *file_data;
433 struct io_rsrc_data *buf_data;
434
435 struct delayed_work rsrc_put_work;
436 struct llist_head rsrc_put_llist;
437 struct list_head rsrc_ref_list;
438 spinlock_t rsrc_ref_lock;
439 };
440
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700441 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100442 struct {
443 #if defined(CONFIG_UNIX)
444 struct socket *ring_sock;
445 #endif
446 /* hashed buffered write serialization */
447 struct io_wq_hash *hash_map;
448
449 /* Only used for accounting purposes */
450 struct user_struct *user;
451 struct mm_struct *mm_account;
452
453 /* ctx exit and cancelation */
Pavel Begunkov9011bf92021-06-30 21:54:03 +0100454 struct llist_head fallback_llist;
455 struct delayed_work fallback_work;
Pavel Begunkovb986af72021-05-16 22:58:06 +0100456 struct work_struct exit_work;
457 struct list_head tctx_list;
458 struct completion ref_comp;
459 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460};
461
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100462struct io_uring_task {
463 /* submission side */
Pavel Begunkov09899b12021-06-14 02:36:22 +0100464 int cached_refs;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100465 struct xarray xa;
466 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100467 const struct io_ring_ctx *last;
468 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100469 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100470 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100472
473 spinlock_t task_lock;
474 struct io_wq_work_list task_list;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100475 struct callback_head task_work;
Pavel Begunkov6294f362021-08-10 17:53:55 +0100476 bool task_running;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100477};
478
Jens Axboe09bb8392019-03-13 12:39:28 -0600479/*
480 * First field must be the file pointer in all the
481 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
482 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700483struct io_poll_iocb {
484 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000485 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700486 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600487 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700488 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700489 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700490};
491
Pavel Begunkov9d805892021-04-13 02:58:40 +0100492struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000493 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100494 u64 old_user_data;
495 u64 new_user_data;
496 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600497 bool update_events;
498 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000499};
500
Jens Axboeb5dba592019-12-11 14:02:38 -0700501struct io_close {
502 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700503 int fd;
504};
505
Jens Axboead8a48a2019-11-15 08:49:11 -0700506struct io_timeout_data {
507 struct io_kiocb *req;
508 struct hrtimer timer;
509 struct timespec64 ts;
510 enum hrtimer_mode mode;
511};
512
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700513struct io_accept {
514 struct file *file;
515 struct sockaddr __user *addr;
516 int __user *addr_len;
517 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600518 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700519};
520
521struct io_sync {
522 struct file *file;
523 loff_t len;
524 loff_t off;
525 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700526 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700527};
528
Jens Axboefbf23842019-12-17 18:45:56 -0700529struct io_cancel {
530 struct file *file;
531 u64 addr;
532};
533
Jens Axboeb29472e2019-12-17 18:50:29 -0700534struct io_timeout {
535 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300536 u32 off;
537 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300538 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000539 /* head of the link, used by linked timeouts only */
540 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600541 /* for linked completions */
542 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700543};
544
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100545struct io_timeout_rem {
546 struct file *file;
547 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000548
549 /* timeout update */
550 struct timespec64 ts;
551 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100552};
553
Jens Axboe9adbd452019-12-20 08:45:55 -0700554struct io_rw {
555 /* NOTE: kiocb has the file as the first member, so don't do it here */
556 struct kiocb kiocb;
557 u64 addr;
558 u64 len;
559};
560
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700561struct io_connect {
562 struct file *file;
563 struct sockaddr __user *addr;
564 int addr_len;
565};
566
Jens Axboee47293f2019-12-20 08:58:21 -0700567struct io_sr_msg {
568 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700569 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100570 struct compat_msghdr __user *umsg_compat;
571 struct user_msghdr __user *umsg;
572 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700573 };
Jens Axboee47293f2019-12-20 08:58:21 -0700574 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700575 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700576 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700577 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700578};
579
Jens Axboe15b71ab2019-12-11 11:20:36 -0700580struct io_open {
581 struct file *file;
582 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100583 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700584 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700585 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600586 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700587};
588
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000589struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700590 struct file *file;
591 u64 arg;
592 u32 nr_args;
593 u32 offset;
594};
595
Jens Axboe4840e412019-12-25 22:03:45 -0700596struct io_fadvise {
597 struct file *file;
598 u64 offset;
599 u32 len;
600 u32 advice;
601};
602
Jens Axboec1ca7572019-12-25 22:18:28 -0700603struct io_madvise {
604 struct file *file;
605 u64 addr;
606 u32 len;
607 u32 advice;
608};
609
Jens Axboe3e4827b2020-01-08 15:18:09 -0700610struct io_epoll {
611 struct file *file;
612 int epfd;
613 int op;
614 int fd;
615 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700616};
617
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300618struct io_splice {
619 struct file *file_out;
620 struct file *file_in;
621 loff_t off_out;
622 loff_t off_in;
623 u64 len;
624 unsigned int flags;
625};
626
Jens Axboeddf0322d2020-02-23 16:41:33 -0700627struct io_provide_buf {
628 struct file *file;
629 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100630 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700631 __u32 bgid;
632 __u16 nbufs;
633 __u16 bid;
634};
635
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700636struct io_statx {
637 struct file *file;
638 int dfd;
639 unsigned int mask;
640 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700641 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700642 struct statx __user *buffer;
643};
644
Jens Axboe36f4fa62020-09-05 11:14:22 -0600645struct io_shutdown {
646 struct file *file;
647 int how;
648};
649
Jens Axboe80a261f2020-09-28 14:23:58 -0600650struct io_rename {
651 struct file *file;
652 int old_dfd;
653 int new_dfd;
654 struct filename *oldpath;
655 struct filename *newpath;
656 int flags;
657};
658
Jens Axboe14a11432020-09-28 14:27:37 -0600659struct io_unlink {
660 struct file *file;
661 int dfd;
662 int flags;
663 struct filename *filename;
664};
665
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300666struct io_completion {
667 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000668 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300669};
670
Jens Axboef499a022019-12-02 16:28:46 -0700671struct io_async_connect {
672 struct sockaddr_storage address;
673};
674
Jens Axboe03b12302019-12-02 18:50:25 -0700675struct io_async_msghdr {
676 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000677 /* points to an allocated iov, if NULL we use fast_iov instead */
678 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700679 struct sockaddr __user *uaddr;
680 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700681 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700682};
683
Jens Axboef67676d2019-12-02 11:03:47 -0700684struct io_async_rw {
685 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600686 const struct iovec *free_iovec;
687 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600688 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600689 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700690};
691
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300692enum {
693 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
694 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
695 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
696 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
697 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700698 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300699
Pavel Begunkovdddca222021-04-27 16:13:52 +0100700 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100701 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300702 REQ_F_INFLIGHT_BIT,
703 REQ_F_CUR_POS_BIT,
704 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300705 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300706 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700707 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700708 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000709 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600710 REQ_F_REISSUE_BIT,
Pavel Begunkov8c130822021-03-22 01:58:32 +0000711 REQ_F_DONT_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100712 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100713 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100714 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700715 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100716 REQ_F_NOWAIT_READ_BIT,
717 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700718 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700719
720 /* not a real bit, just to check we're not overflowing the space */
721 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300722};
723
724enum {
725 /* ctx owns file */
726 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
727 /* drain existing IO first */
728 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
729 /* linked sqes */
730 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
731 /* doesn't sever on completion < 0 */
732 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
733 /* IOSQE_ASYNC */
734 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700735 /* IOSQE_BUFFER_SELECT */
736 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300737
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300738 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100739 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000740 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300741 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
742 /* read/write uses file position */
743 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
744 /* must not punt to workers */
745 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100746 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300747 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300748 /* needs cleanup */
749 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700750 /* already went through poll handler */
751 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700752 /* buffer already selected */
753 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000754 /* completion is deferred through io_comp_state */
755 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600756 /* caller should reissue async */
757 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov8c130822021-03-22 01:58:32 +0000758 /* don't attempt request reissue, see io_rw_reissue() */
759 REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700760 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100761 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700762 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100763 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700764 /* regular file */
765 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100766 /* has creds assigned */
767 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100768 /* skip refcounting if not set */
769 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100770 /* there is a linked timeout that has to be armed */
771 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700772};
773
774struct async_poll {
775 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600776 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300777};
778
Pavel Begunkovf237c302021-08-18 12:42:46 +0100779typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100780
Jens Axboe7cbf1722021-02-10 00:03:20 +0000781struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100782 union {
783 struct io_wq_work_node node;
784 struct llist_node fallback_node;
785 };
786 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000787};
788
Pavel Begunkov992da012021-06-10 16:37:37 +0100789enum {
790 IORING_RSRC_FILE = 0,
791 IORING_RSRC_BUFFER = 1,
792};
793
Jens Axboe09bb8392019-03-13 12:39:28 -0600794/*
795 * NOTE! Each of the iocb union members has the file pointer
796 * as the first entry in their struct definition. So you can
797 * access the file pointer through any of the sub-structs,
798 * or directly as just 'ki_filp' in this struct.
799 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700800struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700801 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600802 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700803 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700804 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100805 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700806 struct io_accept accept;
807 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700808 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700809 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100810 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700811 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700812 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700813 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700814 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000815 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700816 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700817 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700818 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300819 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700820 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700821 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600822 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600823 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600824 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300825 /* use only after cleaning per-op data, see io_clean_op() */
826 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700827 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828
Jens Axboee8c2bc12020-08-15 18:44:09 -0700829 /* opcode allocated if it needs to store data for async defer */
830 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700831 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800832 /* polled IO has completed */
833 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700834
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700835 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300836 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700837
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300838 struct io_ring_ctx *ctx;
839 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700840 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300841 struct task_struct *task;
842 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700843
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000844 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000845 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700846
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100847 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300848 struct list_head inflight_entry;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100849 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300850 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
851 struct hlist_node hash_node;
852 struct async_poll *apoll;
853 struct io_wq_work work;
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100854 const struct cred *creds;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +0100855
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100856 /* store used ubuf, so we can prevent reloading */
857 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700858};
859
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000860struct io_tctx_node {
861 struct list_head ctx_node;
862 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000863 struct io_ring_ctx *ctx;
864};
865
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300866struct io_defer_entry {
867 struct list_head list;
868 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300869 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300870};
871
Jens Axboed3656342019-12-18 09:50:26 -0700872struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700873 /* needs req->file assigned */
874 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700875 /* hash wq insertion if file is a regular file */
876 unsigned hash_reg_file : 1;
877 /* unbound wq insertion if file is a non-regular file */
878 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700879 /* opcode is not supported by this kernel */
880 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700881 /* set if opcode supports polled "wait" */
882 unsigned pollin : 1;
883 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700884 /* op supports buffer selection */
885 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000886 /* do prep async if is going to be punted */
887 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600888 /* should block plug */
889 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700890 /* size of async data needed, if any */
891 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700892};
893
Jens Axboe09186822020-10-13 15:01:40 -0600894static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300895 [IORING_OP_NOP] = {},
896 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700897 .needs_file = 1,
898 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700899 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700900 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000901 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600902 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700903 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700904 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300905 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700906 .needs_file = 1,
907 .hash_reg_file = 1,
908 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700909 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000910 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600911 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700912 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700913 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300914 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700915 .needs_file = 1,
916 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300917 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700918 .needs_file = 1,
919 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700920 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600921 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700922 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700923 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300924 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700925 .needs_file = 1,
926 .hash_reg_file = 1,
927 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700928 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600929 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700930 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700931 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300932 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700933 .needs_file = 1,
934 .unbound_nonreg_file = 1,
935 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300936 [IORING_OP_POLL_REMOVE] = {},
937 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700938 .needs_file = 1,
939 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300940 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700941 .needs_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 Axboee8c2bc12020-08-15 18:44:09 -0700945 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700946 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300947 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700948 .needs_file = 1,
949 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700950 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700951 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000952 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700953 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700954 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300955 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700956 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700957 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000958 [IORING_OP_TIMEOUT_REMOVE] = {
959 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000960 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300961 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700962 .needs_file = 1,
963 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700964 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_ASYNC_CANCEL] = {},
967 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700968 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700969 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300970 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700971 .needs_file = 1,
972 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700973 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000974 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700975 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700976 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300977 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700978 .needs_file = 1,
979 },
Jens Axboe44526be2021-02-15 13:32:18 -0700980 [IORING_OP_OPENAT] = {},
981 [IORING_OP_CLOSE] = {},
982 [IORING_OP_FILES_UPDATE] = {},
983 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300984 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700985 .needs_file = 1,
986 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700987 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700988 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600989 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700990 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700991 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300992 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700993 .needs_file = 1,
994 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700995 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600996 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700997 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700998 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300999 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001000 .needs_file = 1,
1001 },
Jens Axboe44526be2021-02-15 13:32:18 -07001002 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001003 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001004 .needs_file = 1,
1005 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001006 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001007 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001008 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001009 .needs_file = 1,
1010 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001011 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001012 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001013 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001014 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001015 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001016 [IORING_OP_EPOLL_CTL] = {
1017 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001018 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001019 [IORING_OP_SPLICE] = {
1020 .needs_file = 1,
1021 .hash_reg_file = 1,
1022 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001023 },
1024 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001025 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001026 [IORING_OP_TEE] = {
1027 .needs_file = 1,
1028 .hash_reg_file = 1,
1029 .unbound_nonreg_file = 1,
1030 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001031 [IORING_OP_SHUTDOWN] = {
1032 .needs_file = 1,
1033 },
Jens Axboe44526be2021-02-15 13:32:18 -07001034 [IORING_OP_RENAMEAT] = {},
1035 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001036};
1037
Pavel Begunkov0756a862021-08-15 10:40:25 +01001038/* requests with any of those set should undergo io_disarm_next() */
1039#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1040
Pavel Begunkov7a612352021-03-09 00:37:59 +00001041static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001042static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001043static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1044 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001045 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001046static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001047
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001048static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1049 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001050static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001051static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001052static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001053static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001054static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001055 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001056 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001057static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001058static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001059 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001060static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001061static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001062
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001063static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01001064static void io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001065static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001066
Pavel Begunkovb9445592021-08-25 12:25:45 +01001067static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1068 unsigned int issue_flags, u32 slot_index);
1069
Jens Axboe2b188cc2019-01-07 10:46:33 -07001070static struct kmem_cache *req_cachep;
1071
Jens Axboe09186822020-10-13 15:01:40 -06001072static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073
1074struct sock *io_uring_get_socket(struct file *file)
1075{
1076#if defined(CONFIG_UNIX)
1077 if (file->f_op == &io_uring_fops) {
1078 struct io_ring_ctx *ctx = file->private_data;
1079
1080 return ctx->ring_sock->sk;
1081 }
1082#endif
1083 return NULL;
1084}
1085EXPORT_SYMBOL(io_uring_get_socket);
1086
Pavel Begunkovf237c302021-08-18 12:42:46 +01001087static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1088{
1089 if (!*locked) {
1090 mutex_lock(&ctx->uring_lock);
1091 *locked = true;
1092 }
1093}
1094
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001095#define io_for_each_link(pos, head) \
1096 for (pos = (head); pos; pos = pos->link)
1097
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001098/*
1099 * Shamelessly stolen from the mm implementation of page reference checking,
1100 * see commit f958d7b528b1 for details.
1101 */
1102#define req_ref_zero_or_close_to_overflow(req) \
1103 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1104
1105static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1106{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001107 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001108 return atomic_inc_not_zero(&req->refs);
1109}
1110
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001111static inline bool req_ref_put_and_test(struct io_kiocb *req)
1112{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001113 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1114 return true;
1115
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001116 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1117 return atomic_dec_and_test(&req->refs);
1118}
1119
1120static inline void req_ref_put(struct io_kiocb *req)
1121{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001122 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001123 WARN_ON_ONCE(req_ref_put_and_test(req));
1124}
1125
1126static inline void req_ref_get(struct io_kiocb *req)
1127{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001128 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001129 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1130 atomic_inc(&req->refs);
1131}
1132
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001133static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001134{
1135 if (!(req->flags & REQ_F_REFCOUNT)) {
1136 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001137 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001138 }
1139}
1140
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001141static inline void io_req_set_refcount(struct io_kiocb *req)
1142{
1143 __io_req_set_refcount(req, 1);
1144}
1145
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001146static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001147{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001148 struct io_ring_ctx *ctx = req->ctx;
1149
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001150 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001151 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001152 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001153 }
1154}
1155
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001156static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1157{
1158 bool got = percpu_ref_tryget(ref);
1159
1160 /* already at zero, wait for ->release() */
1161 if (!got)
1162 wait_for_completion(compl);
1163 percpu_ref_resurrect(ref);
1164 if (got)
1165 percpu_ref_put(ref);
1166}
1167
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001168static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1169 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001170{
1171 struct io_kiocb *req;
1172
Pavel Begunkov68207682021-03-22 01:58:25 +00001173 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001174 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001175 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001176 return true;
1177
1178 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001179 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001180 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001181 }
1182 return false;
1183}
1184
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001185static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001186{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001187 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001188}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001189
Jens Axboe2b188cc2019-01-07 10:46:33 -07001190static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1191{
1192 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1193
Jens Axboe0f158b42020-05-14 17:18:39 -06001194 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195}
1196
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001197static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1198{
1199 return !req->timeout.off;
1200}
1201
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001202static void io_fallback_req_func(struct work_struct *work)
1203{
1204 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1205 fallback_work.work);
1206 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1207 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001208 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001209
1210 percpu_ref_get(&ctx->refs);
1211 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001212 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001213
Pavel Begunkovf237c302021-08-18 12:42:46 +01001214 if (locked) {
1215 if (ctx->submit_state.compl_nr)
1216 io_submit_flush_completions(ctx);
1217 mutex_unlock(&ctx->uring_lock);
1218 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001219 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001220
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001221}
1222
Jens Axboe2b188cc2019-01-07 10:46:33 -07001223static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1224{
1225 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001226 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001227
1228 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1229 if (!ctx)
1230 return NULL;
1231
Jens Axboe78076bb2019-12-04 19:56:40 -07001232 /*
1233 * Use 5 bits less than the max cq entries, that should give us around
1234 * 32 entries per hash list if totally full and uniformly spread.
1235 */
1236 hash_bits = ilog2(p->cq_entries);
1237 hash_bits -= 5;
1238 if (hash_bits <= 0)
1239 hash_bits = 1;
1240 ctx->cancel_hash_bits = hash_bits;
1241 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1242 GFP_KERNEL);
1243 if (!ctx->cancel_hash)
1244 goto err;
1245 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1246
Pavel Begunkov62248432021-04-28 13:11:29 +01001247 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1248 if (!ctx->dummy_ubuf)
1249 goto err;
1250 /* set invalid range, so io_import_fixed() fails meeting it */
1251 ctx->dummy_ubuf->ubuf = -1UL;
1252
Roman Gushchin21482892019-05-07 10:01:48 -07001253 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001254 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1255 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256
1257 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001258 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001259 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001260 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001261 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001262 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001263 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001264 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001266 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001268 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001269 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001270 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001271 INIT_LIST_HEAD(&ctx->timeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001272 spin_lock_init(&ctx->rsrc_ref_lock);
1273 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001274 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1275 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001276 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001277 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001278 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001279 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001281err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001282 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001283 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001284 kfree(ctx);
1285 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286}
1287
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001288static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1289{
1290 struct io_rings *r = ctx->rings;
1291
1292 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1293 ctx->cq_extra--;
1294}
1295
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001296static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001297{
Jens Axboe2bc99302020-07-09 09:43:27 -06001298 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1299 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001300
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001301 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001302 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001303
Bob Liu9d858b22019-11-13 18:06:25 +08001304 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001305}
1306
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001307#define FFS_ASYNC_READ 0x1UL
1308#define FFS_ASYNC_WRITE 0x2UL
1309#ifdef CONFIG_64BIT
1310#define FFS_ISREG 0x4UL
1311#else
1312#define FFS_ISREG 0x0UL
1313#endif
1314#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1315
1316static inline bool io_req_ffs_set(struct io_kiocb *req)
1317{
1318 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1319}
1320
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001321static void io_req_track_inflight(struct io_kiocb *req)
1322{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001323 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001324 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001325 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001326 }
1327}
1328
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001329static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1330{
1331 req->flags &= ~REQ_F_LINK_TIMEOUT;
1332}
1333
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001334static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1335{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001336 if (WARN_ON_ONCE(!req->link))
1337 return NULL;
1338
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001339 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1340 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001341
1342 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001343 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001344 __io_req_set_refcount(req->link, 2);
1345 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001346}
1347
1348static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1349{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001350 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001351 return NULL;
1352 return __io_prep_linked_timeout(req);
1353}
1354
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001355static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001356{
Jens Axboed3656342019-12-18 09:50:26 -07001357 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001358 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001359
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001360 if (!(req->flags & REQ_F_CREDS)) {
1361 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001362 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001363 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001364
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001365 req->work.list.next = NULL;
1366 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001367 if (req->flags & REQ_F_FORCE_ASYNC)
1368 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1369
Jens Axboed3656342019-12-18 09:50:26 -07001370 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001371 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001372 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001373 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001374 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001375 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001376 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001377
1378 switch (req->opcode) {
1379 case IORING_OP_SPLICE:
1380 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001381 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1382 req->work.flags |= IO_WQ_WORK_UNBOUND;
1383 break;
1384 }
Jens Axboe561fb042019-10-24 07:25:42 -06001385}
1386
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001387static void io_prep_async_link(struct io_kiocb *req)
1388{
1389 struct io_kiocb *cur;
1390
Pavel Begunkov44eff402021-07-26 14:14:31 +01001391 if (req->flags & REQ_F_LINK_TIMEOUT) {
1392 struct io_ring_ctx *ctx = req->ctx;
1393
Jens Axboe79ebeae2021-08-10 15:18:27 -06001394 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001395 io_for_each_link(cur, req)
1396 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001397 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001398 } else {
1399 io_for_each_link(cur, req)
1400 io_prep_async_work(cur);
1401 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001402}
1403
Pavel Begunkovf237c302021-08-18 12:42:46 +01001404static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001405{
Jackie Liua197f662019-11-08 08:09:12 -07001406 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001407 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001408 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001409
Pavel Begunkovf237c302021-08-18 12:42:46 +01001410 /* must not take the lock, NULL it as a precaution */
1411 locked = NULL;
1412
Jens Axboe3bfe6102021-02-16 14:15:30 -07001413 BUG_ON(!tctx);
1414 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001415
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001416 /* init ->work of the whole link before punting */
1417 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001418
1419 /*
1420 * Not expected to happen, but if we do have a bug where this _can_
1421 * happen, catch it here and ensure the request is marked as
1422 * canceled. That will make io-wq go through the usual work cancel
1423 * procedure rather than attempt to run this request (or create a new
1424 * worker for it).
1425 */
1426 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1427 req->work.flags |= IO_WQ_WORK_CANCEL;
1428
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001429 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1430 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001431 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001432 if (link)
1433 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001434}
1435
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001436static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001437 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001438 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001439{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001440 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001441
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001442 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001443 atomic_set(&req->ctx->cq_timeouts,
1444 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001445 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001446 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001447 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001448 }
1449}
1450
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001451static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001452{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001453 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001454 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1455 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001456
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001457 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001458 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001459 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001460 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001461 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001462 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001463}
1464
Pavel Begunkov360428f2020-05-30 14:54:17 +03001465static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001466 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001467{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001468 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001469
Jens Axboe79ebeae2021-08-10 15:18:27 -06001470 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001471 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001472 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001473 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001474 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001475
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001476 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001477 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001478
1479 /*
1480 * Since seq can easily wrap around over time, subtract
1481 * the last seq at which timeouts were flushed before comparing.
1482 * Assuming not more than 2^31-1 events have happened since,
1483 * these subtractions won't have wrapped, so we can check if
1484 * target is in [last_seq, current_seq] by comparing the two.
1485 */
1486 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1487 events_got = seq - ctx->cq_last_tm_flush;
1488 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001489 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001490
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001491 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001492 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001493 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001494 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001495 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001496}
1497
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001498static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001499{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001500 if (ctx->off_timeout_used)
1501 io_flush_timeouts(ctx);
1502 if (ctx->drain_active)
1503 io_queue_deferred(ctx);
1504}
1505
1506static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1507{
1508 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1509 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001510 /* order cqe stores with ring update */
1511 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001512}
1513
Jens Axboe90554202020-09-03 12:12:41 -06001514static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1515{
1516 struct io_rings *r = ctx->rings;
1517
Pavel Begunkova566c552021-05-16 22:58:08 +01001518 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001519}
1520
Pavel Begunkov888aae22021-01-19 13:32:39 +00001521static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1522{
1523 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1524}
1525
Pavel Begunkovd068b502021-05-16 22:58:11 +01001526static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527{
Hristo Venev75b28af2019-08-26 17:23:46 +00001528 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001529 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001530
Stefan Bühler115e12e2019-04-24 23:54:18 +02001531 /*
1532 * writes to the cq entry need to come after reading head; the
1533 * control dependency is enough as we're using WRITE_ONCE to
1534 * fill the cq entry
1535 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001536 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001537 return NULL;
1538
Pavel Begunkov888aae22021-01-19 13:32:39 +00001539 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001540 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001541}
1542
Jens Axboef2842ab2020-01-08 11:04:00 -07001543static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1544{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001545 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001546 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001547 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1548 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001549 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001550}
1551
Jens Axboe2c5d7632021-08-21 07:21:19 -06001552/*
1553 * This should only get called when at least one event has been posted.
1554 * Some applications rely on the eventfd notification count only changing
1555 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1556 * 1:1 relationship between how many times this function is called (and
1557 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1558 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001559static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001560{
Jens Axboe5fd46172021-08-06 14:04:31 -06001561 /*
1562 * wake_up_all() may seem excessive, but io_wake_function() and
1563 * io_should_wake() handle the termination of the loop and only
1564 * wake as many waiters as we need to.
1565 */
1566 if (wq_has_sleeper(&ctx->cq_wait))
1567 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001568 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1569 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001570 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001571 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001572 if (waitqueue_active(&ctx->poll_wait)) {
1573 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001574 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1575 }
Jens Axboe8c838782019-03-12 15:48:16 -06001576}
1577
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001578static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1579{
1580 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe5fd46172021-08-06 14:04:31 -06001581 if (wq_has_sleeper(&ctx->cq_wait))
1582 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001583 }
1584 if (io_should_trigger_evfd(ctx))
1585 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001586 if (waitqueue_active(&ctx->poll_wait)) {
1587 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001588 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1589 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001590}
1591
Jens Axboec4a2ed72019-11-21 21:01:26 -07001592/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001593static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001594{
Jens Axboeb18032b2021-01-24 16:58:56 -07001595 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001596
Pavel Begunkova566c552021-05-16 22:58:08 +01001597 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001598 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001599
Jens Axboeb18032b2021-01-24 16:58:56 -07001600 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001601 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001602 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001603 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001604 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001605
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001606 if (!cqe && !force)
1607 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001608 ocqe = list_first_entry(&ctx->cq_overflow_list,
1609 struct io_overflow_cqe, list);
1610 if (cqe)
1611 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1612 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001613 io_account_cq_overflow(ctx);
1614
Jens Axboeb18032b2021-01-24 16:58:56 -07001615 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001616 list_del(&ocqe->list);
1617 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001618 }
1619
Pavel Begunkov09e88402020-12-17 00:24:38 +00001620 all_flushed = list_empty(&ctx->cq_overflow_list);
1621 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001622 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001623 WRITE_ONCE(ctx->rings->sq_flags,
1624 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001625 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001626
Jens Axboeb18032b2021-01-24 16:58:56 -07001627 if (posted)
1628 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001629 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001630 if (posted)
1631 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001632 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001633}
1634
Pavel Begunkov90f67362021-08-09 20:18:12 +01001635static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001636{
Jens Axboeca0a2652021-03-04 17:15:48 -07001637 bool ret = true;
1638
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001639 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001640 /* iopoll syncs against uring_lock, not completion_lock */
1641 if (ctx->flags & IORING_SETUP_IOPOLL)
1642 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001643 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001644 if (ctx->flags & IORING_SETUP_IOPOLL)
1645 mutex_unlock(&ctx->uring_lock);
1646 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001647
1648 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001649}
1650
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001651/* must to be called somewhat shortly after putting a request */
1652static inline void io_put_task(struct task_struct *task, int nr)
1653{
1654 struct io_uring_task *tctx = task->io_uring;
1655
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001656 if (likely(task == current)) {
1657 tctx->cached_refs += nr;
1658 } else {
1659 percpu_counter_sub(&tctx->inflight, nr);
1660 if (unlikely(atomic_read(&tctx->in_idle)))
1661 wake_up(&tctx->wait);
1662 put_task_struct_many(task, nr);
1663 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001664}
1665
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001666static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1667 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001669 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001671 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1672 if (!ocqe) {
1673 /*
1674 * If we're in ring overflow flush mode, or in task cancel mode,
1675 * or cannot allocate an overflow entry, then we need to drop it
1676 * on the floor.
1677 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001678 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001679 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001681 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001682 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001683 WRITE_ONCE(ctx->rings->sq_flags,
1684 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1685
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001686 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001687 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001688 ocqe->cqe.res = res;
1689 ocqe->cqe.flags = cflags;
1690 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1691 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001692}
1693
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001694static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1695 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001696{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697 struct io_uring_cqe *cqe;
1698
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001699 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001700
1701 /*
1702 * If we can't get a cq entry, userspace overflowed the
1703 * submission (by quite a lot). Increment the overflow count in
1704 * the ring.
1705 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001706 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001707 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001708 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001709 WRITE_ONCE(cqe->res, res);
1710 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001711 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001712 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001713 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001714}
1715
Pavel Begunkov8d133262021-04-11 01:46:33 +01001716/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001717static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1718 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001719{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001720 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001721}
1722
Pavel Begunkov7a612352021-03-09 00:37:59 +00001723static void io_req_complete_post(struct io_kiocb *req, long res,
1724 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001726 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727
Jens Axboe79ebeae2021-08-10 15:18:27 -06001728 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001729 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001730 /*
1731 * If we're the last reference to this request, add to our locked
1732 * free_list cache.
1733 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001734 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001735 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001736 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001737 io_disarm_next(req);
1738 if (req->link) {
1739 io_req_task_queue(req->link);
1740 req->link = NULL;
1741 }
1742 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001743 io_dismantle_req(req);
1744 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001745 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001746 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001747 } else {
1748 if (!percpu_ref_tryget(&ctx->refs))
1749 req = NULL;
1750 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001751 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001752 spin_unlock(&ctx->completion_lock);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001753
Pavel Begunkov180f8292021-03-14 20:57:09 +00001754 if (req) {
1755 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001756 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001757 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758}
1759
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001760static inline bool io_req_needs_clean(struct io_kiocb *req)
1761{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001762 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001763}
1764
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001765static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001766 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001767{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001768 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001769 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001770 req->result = res;
1771 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001772 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001773}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774
Pavel Begunkov889fca72021-02-10 00:03:09 +00001775static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1776 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001777{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001778 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1779 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001780 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001781 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001782}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001783
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001784static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001785{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001786 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001787}
1788
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001789static void io_req_complete_failed(struct io_kiocb *req, long res)
1790{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001791 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001792 io_req_complete_post(req, res, 0);
1793}
1794
Pavel Begunkov864ea922021-08-09 13:04:08 +01001795/*
1796 * Don't initialise the fields below on every allocation, but do that in
1797 * advance and keep them valid across allocations.
1798 */
1799static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1800{
1801 req->ctx = ctx;
1802 req->link = NULL;
1803 req->async_data = NULL;
1804 /* not necessary, but safer to zero */
1805 req->result = 0;
1806}
1807
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001808static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001809 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001810{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001811 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001812 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001813 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001814 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001815}
1816
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001817/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001818static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001819{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001820 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001821 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001822
Jens Axboec7dae4b2021-02-09 19:53:37 -07001823 /*
1824 * If we have more than a batch's worth of requests in our IRQ side
1825 * locked cache, grab the lock and move them over to our submission
1826 * side cache.
1827 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001828 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001829 io_flush_cached_locked_reqs(ctx, state);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001830
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001831 nr = state->free_reqs;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001832 while (!list_empty(&state->free_list)) {
1833 struct io_kiocb *req = list_first_entry(&state->free_list,
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001834 struct io_kiocb, inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001835
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001836 list_del(&req->inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001837 state->reqs[nr++] = req;
1838 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001839 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001840 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001841
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001842 state->free_reqs = nr;
1843 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001844}
1845
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001846/*
1847 * A request might get retired back into the request caches even before opcode
1848 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1849 * Because of that, io_alloc_req() should be called only under ->uring_lock
1850 * and with extra caution to not get a request that is still worked on.
1851 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001852static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001853 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001854{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001855 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001856 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1857 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01001859 BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001860
Pavel Begunkov864ea922021-08-09 13:04:08 +01001861 if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
1862 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001863
Pavel Begunkov864ea922021-08-09 13:04:08 +01001864 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1865 state->reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001866
Pavel Begunkov864ea922021-08-09 13:04:08 +01001867 /*
1868 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1869 * retry single alloc to be on the safe side.
1870 */
1871 if (unlikely(ret <= 0)) {
1872 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1873 if (!state->reqs[0])
1874 return NULL;
1875 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001877
1878 for (i = 0; i < ret; i++)
1879 io_preinit_req(state->reqs[i], ctx);
1880 state->free_reqs = ret;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001881got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001882 state->free_reqs--;
1883 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884}
1885
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001886static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001887{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001888 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001889 fput(file);
1890}
1891
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001892static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001894 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001895
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001896 if (io_req_needs_clean(req))
1897 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001898 if (!(flags & REQ_F_FIXED_FILE))
1899 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001900 if (req->fixed_rsrc_refs)
1901 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001902 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001903 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001904 req->async_data = NULL;
1905 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001906}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001907
Pavel Begunkov216578e2020-10-13 09:44:00 +01001908static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001909{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001910 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001911
Pavel Begunkov216578e2020-10-13 09:44:00 +01001912 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001913 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001914
Jens Axboe79ebeae2021-08-10 15:18:27 -06001915 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001916 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001917 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001918 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001919
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001920 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001921}
1922
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001923static inline void io_remove_next_linked(struct io_kiocb *req)
1924{
1925 struct io_kiocb *nxt = req->link;
1926
1927 req->link = nxt->link;
1928 nxt->link = NULL;
1929}
1930
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001931static bool io_kill_linked_timeout(struct io_kiocb *req)
1932 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06001933 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001934{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001935 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001936
Pavel Begunkovb97e7362021-08-15 10:40:23 +01001937 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001938 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001939
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001940 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001941 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001942 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001943 io_cqring_fill_event(link->ctx, link->user_data,
1944 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001945 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001946 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001947 }
1948 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001949 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001950}
1951
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001952static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001953 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001954{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001955 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06001956
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001957 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001958 while (link) {
1959 nxt = link->link;
1960 link->link = NULL;
1961
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001962 trace_io_uring_fail_link(req, link);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001963 io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001964 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001965 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001966 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001967}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001968
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001969static bool io_disarm_next(struct io_kiocb *req)
1970 __must_hold(&req->ctx->completion_lock)
1971{
1972 bool posted = false;
1973
Pavel Begunkov0756a862021-08-15 10:40:25 +01001974 if (req->flags & REQ_F_ARM_LTIMEOUT) {
1975 struct io_kiocb *link = req->link;
1976
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001977 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01001978 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
1979 io_remove_next_linked(req);
1980 io_cqring_fill_event(link->ctx, link->user_data,
1981 -ECANCELED, 0);
1982 io_put_req_deferred(link);
1983 posted = true;
1984 }
1985 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06001986 struct io_ring_ctx *ctx = req->ctx;
1987
1988 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001989 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06001990 spin_unlock_irq(&ctx->timeout_lock);
1991 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001992 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01001993 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001994 posted |= (req->link != NULL);
1995 io_fail_links(req);
1996 }
1997 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06001998}
1999
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002000static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002001{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002002 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002003
Jens Axboe9e645e112019-05-10 16:07:28 -06002004 /*
2005 * If LINK is set, we have dependent requests in this chain. If we
2006 * didn't fail this request, queue the first one up, moving any other
2007 * dependencies to the next request. In case of failure, fail the rest
2008 * of the chain.
2009 */
Pavel Begunkov0756a862021-08-15 10:40:25 +01002010 if (req->flags & IO_DISARM_MASK) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002011 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002012 bool posted;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002013
Jens Axboe79ebeae2021-08-10 15:18:27 -06002014 spin_lock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002015 posted = io_disarm_next(req);
2016 if (posted)
2017 io_commit_cqring(req->ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002018 spin_unlock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002019 if (posted)
2020 io_cqring_ev_posted(ctx);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002021 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002022 nxt = req->link;
2023 req->link = NULL;
2024 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002025}
Jens Axboe2665abf2019-11-05 12:40:47 -07002026
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002027static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002028{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002029 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002030 return NULL;
2031 return __io_req_find_next(req);
2032}
2033
Pavel Begunkovf237c302021-08-18 12:42:46 +01002034static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002035{
2036 if (!ctx)
2037 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002038 if (*locked) {
Hao Xu99c8bc52021-08-21 06:19:54 +08002039 if (ctx->submit_state.compl_nr)
2040 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002041 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002042 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002043 }
2044 percpu_ref_put(&ctx->refs);
2045}
2046
Jens Axboe7cbf1722021-02-10 00:03:20 +00002047static void tctx_task_work(struct callback_head *cb)
2048{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002049 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002050 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002051 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2052 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002053
Pavel Begunkov16f72072021-06-17 18:14:09 +01002054 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002055 struct io_wq_work_node *node;
2056
2057 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002058 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002059 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002060 if (!node)
2061 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002062 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002063 if (!node)
2064 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002065
Pavel Begunkov6294f362021-08-10 17:53:55 +01002066 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002067 struct io_wq_work_node *next = node->next;
2068 struct io_kiocb *req = container_of(node, struct io_kiocb,
2069 io_task_work.node);
2070
2071 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002072 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002073 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002074 /* if not contended, grab and improve batching */
2075 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002076 percpu_ref_get(&ctx->refs);
2077 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002078 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002079 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002080 } while (node);
2081
Jens Axboe7cbf1722021-02-10 00:03:20 +00002082 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002083 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002084
Pavel Begunkovf237c302021-08-18 12:42:46 +01002085 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002086}
2087
Pavel Begunkove09ee512021-07-01 13:26:05 +01002088static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002089{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002090 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002091 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002092 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002093 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002094 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002095 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002096
2097 WARN_ON_ONCE(!tctx);
2098
Jens Axboe0b81e802021-02-16 10:33:53 -07002099 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002100 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002101 running = tctx->task_running;
2102 if (!running)
2103 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002104 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002105
2106 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002107 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002108 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002109
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002110 /*
2111 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2112 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2113 * processing task_work. There's no reliable way to tell if TWA_RESUME
2114 * will do the job.
2115 */
2116 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002117 if (!task_work_add(tsk, &tctx->task_work, notify)) {
2118 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002119 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002120 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002121
Pavel Begunkove09ee512021-07-01 13:26:05 +01002122 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002123 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002124 node = tctx->task_list.first;
2125 INIT_WQ_LIST(&tctx->task_list);
2126 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002127
Pavel Begunkove09ee512021-07-01 13:26:05 +01002128 while (node) {
2129 req = container_of(node, struct io_kiocb, io_task_work.node);
2130 node = node->next;
2131 if (llist_add(&req->io_task_work.fallback_node,
2132 &req->ctx->fallback_llist))
2133 schedule_delayed_work(&req->ctx->fallback_work, 1);
2134 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002135}
2136
Pavel Begunkovf237c302021-08-18 12:42:46 +01002137static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002138{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002139 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002140
Pavel Begunkove83acd72021-02-28 22:35:09 +00002141 /* ctx is guaranteed to stay alive while we hold uring_lock */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002142 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002143 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002144}
2145
Pavel Begunkovf237c302021-08-18 12:42:46 +01002146static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002147{
2148 struct io_ring_ctx *ctx = req->ctx;
2149
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002150 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002151 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002152 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002153 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002154 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002155 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002156 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002157}
2158
Pavel Begunkova3df76982021-02-18 22:32:52 +00002159static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2160{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002161 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002162 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002163 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002164}
2165
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002166static void io_req_task_queue(struct io_kiocb *req)
2167{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002168 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002169 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002170}
2171
Jens Axboe773af692021-07-27 10:25:55 -06002172static void io_req_task_queue_reissue(struct io_kiocb *req)
2173{
2174 req->io_task_work.func = io_queue_async_work;
2175 io_req_task_work_add(req);
2176}
2177
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002178static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002179{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002180 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002181
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002182 if (nxt)
2183 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002184}
2185
Jens Axboe9e645e112019-05-10 16:07:28 -06002186static void io_free_req(struct io_kiocb *req)
2187{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002188 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002189 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002190}
2191
Pavel Begunkovf237c302021-08-18 12:42:46 +01002192static void io_free_req_work(struct io_kiocb *req, bool *locked)
2193{
2194 io_free_req(req);
2195}
2196
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002197struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002198 struct task_struct *task;
2199 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002200 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002201};
2202
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002203static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002204{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002205 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002206 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002207 rb->task = NULL;
2208}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002209
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002210static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2211 struct req_batch *rb)
2212{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002213 if (rb->ctx_refs)
2214 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002215 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002216 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002217}
2218
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002219static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2220 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002221{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002222 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002223 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002224
Jens Axboee3bc8e92020-09-24 08:45:57 -06002225 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002226 if (rb->task)
2227 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002228 rb->task = req->task;
2229 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002230 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002231 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002232 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002233
Pavel Begunkovbd759042021-02-12 03:23:50 +00002234 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002235 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002236 else
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002237 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002238}
2239
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01002240static void io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002241 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002242{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002243 struct io_submit_state *state = &ctx->submit_state;
2244 int i, nr = state->compl_nr;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002245 struct req_batch rb;
2246
Jens Axboe79ebeae2021-08-10 15:18:27 -06002247 spin_lock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002248 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002249 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002250
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002251 __io_cqring_fill_event(ctx, req->user_data, req->result,
2252 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002253 }
2254 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002255 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002256 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002257
2258 io_init_req_batch(&rb);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002259 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002260 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov905c1722021-02-10 00:03:14 +00002261
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002262 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002263 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002264 }
2265
2266 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002267 state->compl_nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002268}
2269
Jens Axboeba816ad2019-09-28 11:36:45 -06002270/*
2271 * Drop reference to request, return next in chain (if there is one) if this
2272 * was the last reference to this request.
2273 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002274static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002275{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002276 struct io_kiocb *nxt = NULL;
2277
Jens Axboede9b4cc2021-02-24 13:28:27 -07002278 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002279 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002280 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002281 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002282 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283}
2284
Pavel Begunkov0d850352021-03-19 17:22:37 +00002285static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002287 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002288 io_free_req(req);
2289}
2290
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002291static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002292{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002293 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002294 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002295 io_req_task_work_add(req);
2296 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002297}
2298
Pavel Begunkov6c503152021-01-04 20:36:36 +00002299static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002300{
2301 /* See comment at the top of this file */
2302 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002303 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002304}
2305
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002306static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2307{
2308 struct io_rings *rings = ctx->rings;
2309
2310 /* make sure SQ entry isn't read before tail */
2311 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2312}
2313
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002314static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002315{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002316 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002317
Jens Axboebcda7ba2020-02-23 16:42:51 -07002318 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2319 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002320 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002321 kfree(kbuf);
2322 return cflags;
2323}
2324
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002325static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2326{
2327 struct io_buffer *kbuf;
2328
Pavel Begunkovae421d92021-08-17 20:28:08 +01002329 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2330 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002331 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2332 return io_put_kbuf(req, kbuf);
2333}
2334
Jens Axboe4c6e2772020-07-01 11:29:10 -06002335static inline bool io_run_task_work(void)
2336{
Nadav Amitef98eb02021-08-07 17:13:41 -07002337 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002338 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002339 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002340 return true;
2341 }
2342
2343 return false;
2344}
2345
Jens Axboedef596e2019-01-09 08:59:42 -07002346/*
2347 * Find and free completed poll iocbs
2348 */
2349static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002350 struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002351{
Jens Axboe8237e042019-12-28 10:48:22 -07002352 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002353 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002354
2355 /* order with ->result store in io_complete_rw_iopoll() */
2356 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002357
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002358 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002359 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002360 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002361 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002362
Pavel Begunkova8576af2021-08-15 10:40:21 +01002363 if (READ_ONCE(req->result) == -EAGAIN &&
Pavel Begunkov8c130822021-03-22 01:58:32 +00002364 !(req->flags & REQ_F_DONT_REISSUE)) {
Pavel Begunkovf1613402021-02-11 18:28:21 +00002365 req->iopoll_completed = 0;
Jens Axboe773af692021-07-27 10:25:55 -06002366 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002367 continue;
Pavel Begunkovf1613402021-02-11 18:28:21 +00002368 }
2369
Pavel Begunkovae421d92021-08-17 20:28:08 +01002370 __io_cqring_fill_event(ctx, req->user_data, req->result,
2371 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002372 (*nr_events)++;
2373
Jens Axboede9b4cc2021-02-24 13:28:27 -07002374 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002375 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002376 }
Jens Axboedef596e2019-01-09 08:59:42 -07002377
Jens Axboe09bb8392019-03-13 12:39:28 -06002378 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002379 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002380 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002381}
2382
Jens Axboedef596e2019-01-09 08:59:42 -07002383static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002384 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002385{
2386 struct io_kiocb *req, *tmp;
2387 LIST_HEAD(done);
2388 bool spin;
Jens Axboedef596e2019-01-09 08:59:42 -07002389
2390 /*
2391 * Only spin for completions if we don't have multiple devices hanging
2392 * off our complete list, and we're under the requested amount.
2393 */
Hao Xu915b3dd2021-06-28 05:37:30 +08002394 spin = !ctx->poll_multi_queue && *nr_events < min;
Jens Axboedef596e2019-01-09 08:59:42 -07002395
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002396 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002397 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002398 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002399
2400 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002401 * Move completed and retryable entries to our local lists.
2402 * If we find a request that requires polling, break out
2403 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002404 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002405 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002406 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002407 continue;
2408 }
2409 if (!list_empty(&done))
2410 break;
2411
2412 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002413 if (unlikely(ret < 0))
2414 return ret;
2415 else if (ret)
2416 spin = false;
Jens Axboedef596e2019-01-09 08:59:42 -07002417
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002418 /* iopoll may have completed current req */
2419 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002420 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002421 }
2422
2423 if (!list_empty(&done))
Pavel Begunkova8576af2021-08-15 10:40:21 +01002424 io_iopoll_complete(ctx, nr_events, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002425
Pavel Begunkova2416e12021-08-09 13:04:09 +01002426 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002427}
2428
2429/*
Jens Axboedef596e2019-01-09 08:59:42 -07002430 * We can't just wait for polled events to come to us, we have to actively
2431 * find and complete them.
2432 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002433static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002434{
2435 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2436 return;
2437
2438 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002439 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002440 unsigned int nr_events = 0;
2441
Pavel Begunkova8576af2021-08-15 10:40:21 +01002442 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002443
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002444 /* let it sleep and repeat later if can't complete a request */
2445 if (nr_events == 0)
2446 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002447 /*
2448 * Ensure we allow local-to-the-cpu processing to take place,
2449 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002450 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002451 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002452 if (need_resched()) {
2453 mutex_unlock(&ctx->uring_lock);
2454 cond_resched();
2455 mutex_lock(&ctx->uring_lock);
2456 }
Jens Axboedef596e2019-01-09 08:59:42 -07002457 }
2458 mutex_unlock(&ctx->uring_lock);
2459}
2460
Pavel Begunkov7668b922020-07-07 16:36:21 +03002461static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002462{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002463 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002464 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002465
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002466 /*
2467 * We disallow the app entering submit/complete with polling, but we
2468 * still need to lock the ring to prevent racing with polled issue
2469 * that got punted to a workqueue.
2470 */
2471 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002472 /*
2473 * Don't enter poll loop if we already have events pending.
2474 * If we do, we can potentially be spinning for commands that
2475 * already triggered a CQE (eg in error).
2476 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002477 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002478 __io_cqring_overflow_flush(ctx, false);
2479 if (io_cqring_events(ctx))
2480 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002481 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002482 /*
2483 * If a submit got punted to a workqueue, we can have the
2484 * application entering polling for a command before it gets
2485 * issued. That app will hold the uring_lock for the duration
2486 * of the poll right here, so we need to take a breather every
2487 * now and then to ensure that the issue has a chance to add
2488 * the poll to the issued list. Otherwise we can spin here
2489 * forever, while the workqueue is stuck trying to acquire the
2490 * very same mutex.
2491 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002492 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002493 u32 tail = ctx->cached_cq_tail;
2494
Jens Axboe500f9fb2019-08-19 12:15:59 -06002495 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002496 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002497 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002498
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002499 /* some requests don't go through iopoll_list */
2500 if (tail != ctx->cached_cq_tail ||
2501 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002502 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002503 }
Pavel Begunkova8576af2021-08-15 10:40:21 +01002504 ret = io_do_iopoll(ctx, &nr_events, min);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002505 } while (!ret && nr_events < min && !need_resched());
2506out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002507 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002508 return ret;
2509}
2510
Jens Axboe491381ce2019-10-17 09:20:46 -06002511static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002512{
Jens Axboe491381ce2019-10-17 09:20:46 -06002513 /*
2514 * Tell lockdep we inherited freeze protection from submission
2515 * thread.
2516 */
2517 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002518 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002519
Pavel Begunkov1c986792021-03-22 01:58:31 +00002520 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2521 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002522 }
2523}
2524
Jens Axboeb63534c2020-06-04 11:28:00 -06002525#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002526static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002527{
Pavel Begunkovab454432021-03-22 01:58:33 +00002528 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002529
Pavel Begunkovab454432021-03-22 01:58:33 +00002530 if (!rw)
2531 return !io_req_prep_async(req);
2532 /* may have left rw->iter inconsistent on -EIOCBQUEUED */
2533 iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter));
2534 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002535}
Jens Axboeb63534c2020-06-04 11:28:00 -06002536
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002537static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002538{
Jens Axboe355afae2020-09-02 09:30:31 -06002539 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002540 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002541
Jens Axboe355afae2020-09-02 09:30:31 -06002542 if (!S_ISBLK(mode) && !S_ISREG(mode))
2543 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002544 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2545 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002546 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002547 /*
2548 * If ref is dying, we might be running poll reap from the exit work.
2549 * Don't attempt to reissue from that path, just let it fail with
2550 * -EAGAIN.
2551 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002552 if (percpu_ref_is_dying(&ctx->refs))
2553 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002554 /*
2555 * Play it safe and assume not safe to re-import and reissue if we're
2556 * not in the original thread group (or in task context).
2557 */
2558 if (!same_thread_group(req->task, current) || !in_task())
2559 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002560 return true;
2561}
Jens Axboee82ad482021-04-02 19:45:34 -06002562#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002563static bool io_resubmit_prep(struct io_kiocb *req)
2564{
2565 return false;
2566}
Jens Axboee82ad482021-04-02 19:45:34 -06002567static bool io_rw_should_reissue(struct io_kiocb *req)
2568{
2569 return false;
2570}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002571#endif
2572
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002573static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002574{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002575 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2576 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002577 if (res != req->result) {
2578 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2579 io_rw_should_reissue(req)) {
2580 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002581 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002582 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002583 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002584 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002585 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002586 return false;
2587}
2588
Pavel Begunkovf237c302021-08-18 12:42:46 +01002589static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002590{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002591 unsigned int cflags = io_put_rw_kbuf(req);
2592 long res = req->result;
2593
2594 if (*locked) {
2595 struct io_ring_ctx *ctx = req->ctx;
2596 struct io_submit_state *state = &ctx->submit_state;
2597
2598 io_req_complete_state(req, res, cflags);
2599 state->compl_reqs[state->compl_nr++] = req;
2600 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
2601 io_submit_flush_completions(ctx);
2602 } else {
2603 io_req_complete_post(req, res, cflags);
2604 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002605}
2606
2607static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2608 unsigned int issue_flags)
2609{
2610 if (__io_complete_rw_common(req, res))
2611 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002612 __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002613}
2614
2615static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2616{
Jens Axboe9adbd452019-12-20 08:45:55 -07002617 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002618
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002619 if (__io_complete_rw_common(req, res))
2620 return;
2621 req->result = res;
2622 req->io_task_work.func = io_req_task_complete;
2623 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624}
2625
Jens Axboedef596e2019-01-09 08:59:42 -07002626static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2627{
Jens Axboe9adbd452019-12-20 08:45:55 -07002628 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002629
Jens Axboe491381ce2019-10-17 09:20:46 -06002630 if (kiocb->ki_flags & IOCB_WRITE)
2631 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002632 if (unlikely(res != req->result)) {
Jens Axboea1ff1e32021-04-12 06:40:02 -06002633 if (!(res == -EAGAIN && io_rw_should_reissue(req) &&
2634 io_resubmit_prep(req))) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002635 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002636 req->flags |= REQ_F_DONT_REISSUE;
2637 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002638 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002639
2640 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002641 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002642 smp_wmb();
2643 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002644}
2645
2646/*
2647 * After the iocb has been issued, it's safe to be found on the poll list.
2648 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002649 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002650 * accessing the kiocb cookie.
2651 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002652static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002653{
2654 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002655 const bool in_async = io_wq_current_is_worker();
2656
2657 /* workqueue context doesn't hold uring_lock, grab it now */
2658 if (unlikely(in_async))
2659 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002660
2661 /*
2662 * Track whether we have multiple files in our lists. This will impact
2663 * how we do polling eventually, not spinning if we're on potentially
2664 * different devices.
2665 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002666 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002667 ctx->poll_multi_queue = false;
2668 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002669 struct io_kiocb *list_req;
Hao Xu915b3dd2021-06-28 05:37:30 +08002670 unsigned int queue_num0, queue_num1;
Jens Axboedef596e2019-01-09 08:59:42 -07002671
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002672 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002673 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002674
2675 if (list_req->file != req->file) {
2676 ctx->poll_multi_queue = true;
2677 } else {
2678 queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie);
2679 queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie);
2680 if (queue_num0 != queue_num1)
2681 ctx->poll_multi_queue = true;
2682 }
Jens Axboedef596e2019-01-09 08:59:42 -07002683 }
2684
2685 /*
2686 * For fast devices, IO may have already completed. If it has, add
2687 * it to the front so we find it first.
2688 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002689 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002690 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002691 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002692 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002693
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002694 if (unlikely(in_async)) {
2695 /*
2696 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2697 * in sq thread task context or in io worker task context. If
2698 * current task context is sq thread, we don't need to check
2699 * whether should wake up sq thread.
2700 */
2701 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2702 wq_has_sleeper(&ctx->sq_data->wait))
2703 wake_up(&ctx->sq_data->wait);
2704
2705 mutex_unlock(&ctx->uring_lock);
2706 }
Jens Axboedef596e2019-01-09 08:59:42 -07002707}
2708
Jens Axboe4503b762020-06-01 10:00:27 -06002709static bool io_bdev_nowait(struct block_device *bdev)
2710{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002711 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002712}
2713
Jens Axboe2b188cc2019-01-07 10:46:33 -07002714/*
2715 * If we tracked the file through the SCM inflight mechanism, we could support
2716 * any file. For now, just ensure that anything potentially problematic is done
2717 * inline.
2718 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002719static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002720{
2721 umode_t mode = file_inode(file)->i_mode;
2722
Jens Axboe4503b762020-06-01 10:00:27 -06002723 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002724 if (IS_ENABLED(CONFIG_BLOCK) &&
2725 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002726 return true;
2727 return false;
2728 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002729 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002731 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002732 if (IS_ENABLED(CONFIG_BLOCK) &&
2733 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002734 file->f_op != &io_uring_fops)
2735 return true;
2736 return false;
2737 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002738
Jens Axboec5b85622020-06-09 19:23:05 -06002739 /* any ->read/write should understand O_NONBLOCK */
2740 if (file->f_flags & O_NONBLOCK)
2741 return true;
2742
Jens Axboeaf197f52020-04-28 13:15:06 -06002743 if (!(file->f_mode & FMODE_NOWAIT))
2744 return false;
2745
2746 if (rw == READ)
2747 return file->f_op->read_iter != NULL;
2748
2749 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002750}
2751
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002752static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002753{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002754 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002755 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002756 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002757 return true;
2758
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002759 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002760}
2761
Pavel Begunkova88fc402020-09-30 22:57:53 +03002762static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002763{
Jens Axboedef596e2019-01-09 08:59:42 -07002764 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002765 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002766 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002767 unsigned ioprio;
2768 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002769
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002770 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002771 req->flags |= REQ_F_ISREG;
2772
Jens Axboe2b188cc2019-01-07 10:46:33 -07002773 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002774 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002775 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002776 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002777 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002778 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002779 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2780 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2781 if (unlikely(ret))
2782 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002783
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002784 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2785 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2786 req->flags |= REQ_F_NOWAIT;
2787
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788 ioprio = READ_ONCE(sqe->ioprio);
2789 if (ioprio) {
2790 ret = ioprio_check_cap(ioprio);
2791 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002792 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002793
2794 kiocb->ki_ioprio = ioprio;
2795 } else
2796 kiocb->ki_ioprio = get_current_ioprio();
2797
Jens Axboedef596e2019-01-09 08:59:42 -07002798 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002799 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2800 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002801 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002802
Jens Axboedef596e2019-01-09 08:59:42 -07002803 kiocb->ki_flags |= IOCB_HIPRI;
2804 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002805 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002806 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002807 if (kiocb->ki_flags & IOCB_HIPRI)
2808 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002809 kiocb->ki_complete = io_complete_rw;
2810 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002811
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002812 if (req->opcode == IORING_OP_READ_FIXED ||
2813 req->opcode == IORING_OP_WRITE_FIXED) {
2814 req->imu = NULL;
2815 io_req_set_rsrc_node(req);
2816 }
2817
Jens Axboe3529d8c2019-12-19 18:24:38 -07002818 req->rw.addr = READ_ONCE(sqe->addr);
2819 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002820 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002821 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002822}
2823
2824static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2825{
2826 switch (ret) {
2827 case -EIOCBQUEUED:
2828 break;
2829 case -ERESTARTSYS:
2830 case -ERESTARTNOINTR:
2831 case -ERESTARTNOHAND:
2832 case -ERESTART_RESTARTBLOCK:
2833 /*
2834 * We can't just restart the syscall, since previously
2835 * submitted sqes may already be in progress. Just fail this
2836 * IO with EINTR.
2837 */
2838 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002839 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002840 default:
2841 kiocb->ki_complete(kiocb, ret, 0);
2842 }
2843}
2844
Jens Axboea1d7c392020-06-22 11:09:46 -06002845static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002846 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002847{
Jens Axboeba042912019-12-25 16:33:42 -07002848 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002849 struct io_async_rw *io = req->async_data;
Pavel Begunkov97284632021-04-08 19:28:03 +01002850 bool check_reissue = kiocb->ki_complete == io_complete_rw;
Jens Axboeba042912019-12-25 16:33:42 -07002851
Jens Axboe227c0c92020-08-13 11:51:40 -06002852 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002853 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002854 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002855 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002856 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002857 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002858 }
2859
Jens Axboeba042912019-12-25 16:33:42 -07002860 if (req->flags & REQ_F_CUR_POS)
2861 req->file->f_pos = kiocb->ki_pos;
Hao Xue149bd742021-06-28 05:48:05 +08002862 if (ret >= 0 && check_reissue)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002863 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002864 else
2865 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002866
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01002867 if (check_reissue && (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002868 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002869 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002870 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002871 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002872 req_set_fail(req);
Pavel Begunkovae421d92021-08-17 20:28:08 +01002873 __io_req_complete(req, issue_flags, ret,
2874 io_put_rw_kbuf(req));
Pavel Begunkov97284632021-04-08 19:28:03 +01002875 }
2876 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002877}
2878
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002879static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2880 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002881{
Jens Axboe9adbd452019-12-20 08:45:55 -07002882 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002883 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002884 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002885
Pavel Begunkov75769e32021-04-01 15:43:54 +01002886 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002887 return -EFAULT;
2888 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002889 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002890 return -EFAULT;
2891
2892 /*
2893 * May not be a start of buffer, set size appropriately
2894 * and advance us to the beginning.
2895 */
2896 offset = buf_addr - imu->ubuf;
2897 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002898
2899 if (offset) {
2900 /*
2901 * Don't use iov_iter_advance() here, as it's really slow for
2902 * using the latter parts of a big fixed buffer - it iterates
2903 * over each segment manually. We can cheat a bit here, because
2904 * we know that:
2905 *
2906 * 1) it's a BVEC iter, we set it up
2907 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2908 * first and last bvec
2909 *
2910 * So just find our index, and adjust the iterator afterwards.
2911 * If the offset is within the first bvec (or the whole first
2912 * bvec, just use iov_iter_advance(). This makes it easier
2913 * since we can just skip the first segment, which may not
2914 * be PAGE_SIZE aligned.
2915 */
2916 const struct bio_vec *bvec = imu->bvec;
2917
2918 if (offset <= bvec->bv_len) {
2919 iov_iter_advance(iter, offset);
2920 } else {
2921 unsigned long seg_skip;
2922
2923 /* skip first vec */
2924 offset -= bvec->bv_len;
2925 seg_skip = 1 + (offset >> PAGE_SHIFT);
2926
2927 iter->bvec = bvec + seg_skip;
2928 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002929 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002930 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002931 }
2932 }
2933
Pavel Begunkov847595d2021-02-04 13:52:06 +00002934 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002935}
2936
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002937static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
2938{
2939 struct io_ring_ctx *ctx = req->ctx;
2940 struct io_mapped_ubuf *imu = req->imu;
2941 u16 index, buf_index = req->buf_index;
2942
2943 if (likely(!imu)) {
2944 if (unlikely(buf_index >= ctx->nr_user_bufs))
2945 return -EFAULT;
2946 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2947 imu = READ_ONCE(ctx->user_bufs[index]);
2948 req->imu = imu;
2949 }
2950 return __io_import_fixed(req, rw, iter, imu);
2951}
2952
Jens Axboebcda7ba2020-02-23 16:42:51 -07002953static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2954{
2955 if (needs_lock)
2956 mutex_unlock(&ctx->uring_lock);
2957}
2958
2959static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2960{
2961 /*
2962 * "Normal" inline submissions always hold the uring_lock, since we
2963 * grab it from the system call. Same is true for the SQPOLL offload.
2964 * The only exception is when we've detached the request and issue it
2965 * from an async worker thread, grab the lock for that case.
2966 */
2967 if (needs_lock)
2968 mutex_lock(&ctx->uring_lock);
2969}
2970
2971static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2972 int bgid, struct io_buffer *kbuf,
2973 bool needs_lock)
2974{
2975 struct io_buffer *head;
2976
2977 if (req->flags & REQ_F_BUFFER_SELECTED)
2978 return kbuf;
2979
2980 io_ring_submit_lock(req->ctx, needs_lock);
2981
2982 lockdep_assert_held(&req->ctx->uring_lock);
2983
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002984 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002985 if (head) {
2986 if (!list_empty(&head->list)) {
2987 kbuf = list_last_entry(&head->list, struct io_buffer,
2988 list);
2989 list_del(&kbuf->list);
2990 } else {
2991 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002992 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002993 }
2994 if (*len > kbuf->len)
2995 *len = kbuf->len;
2996 } else {
2997 kbuf = ERR_PTR(-ENOBUFS);
2998 }
2999
3000 io_ring_submit_unlock(req->ctx, needs_lock);
3001
3002 return kbuf;
3003}
3004
Jens Axboe4d954c22020-02-27 07:31:19 -07003005static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3006 bool needs_lock)
3007{
3008 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003009 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003010
3011 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003012 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003013 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3014 if (IS_ERR(kbuf))
3015 return kbuf;
3016 req->rw.addr = (u64) (unsigned long) kbuf;
3017 req->flags |= REQ_F_BUFFER_SELECTED;
3018 return u64_to_user_ptr(kbuf->addr);
3019}
3020
3021#ifdef CONFIG_COMPAT
3022static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3023 bool needs_lock)
3024{
3025 struct compat_iovec __user *uiov;
3026 compat_ssize_t clen;
3027 void __user *buf;
3028 ssize_t len;
3029
3030 uiov = u64_to_user_ptr(req->rw.addr);
3031 if (!access_ok(uiov, sizeof(*uiov)))
3032 return -EFAULT;
3033 if (__get_user(clen, &uiov->iov_len))
3034 return -EFAULT;
3035 if (clen < 0)
3036 return -EINVAL;
3037
3038 len = clen;
3039 buf = io_rw_buffer_select(req, &len, needs_lock);
3040 if (IS_ERR(buf))
3041 return PTR_ERR(buf);
3042 iov[0].iov_base = buf;
3043 iov[0].iov_len = (compat_size_t) len;
3044 return 0;
3045}
3046#endif
3047
3048static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3049 bool needs_lock)
3050{
3051 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3052 void __user *buf;
3053 ssize_t len;
3054
3055 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3056 return -EFAULT;
3057
3058 len = iov[0].iov_len;
3059 if (len < 0)
3060 return -EINVAL;
3061 buf = io_rw_buffer_select(req, &len, needs_lock);
3062 if (IS_ERR(buf))
3063 return PTR_ERR(buf);
3064 iov[0].iov_base = buf;
3065 iov[0].iov_len = len;
3066 return 0;
3067}
3068
3069static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3070 bool needs_lock)
3071{
Jens Axboedddb3e22020-06-04 11:27:01 -06003072 if (req->flags & REQ_F_BUFFER_SELECTED) {
3073 struct io_buffer *kbuf;
3074
3075 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3076 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3077 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003078 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003079 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003080 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003081 return -EINVAL;
3082
3083#ifdef CONFIG_COMPAT
3084 if (req->ctx->compat)
3085 return io_compat_import(req, iov, needs_lock);
3086#endif
3087
3088 return __io_iov_buffer_select(req, iov, needs_lock);
3089}
3090
Pavel Begunkov847595d2021-02-04 13:52:06 +00003091static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3092 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003093{
Jens Axboe9adbd452019-12-20 08:45:55 -07003094 void __user *buf = u64_to_user_ptr(req->rw.addr);
3095 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003096 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003097 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003098
Pavel Begunkov7d009162019-11-25 23:14:40 +03003099 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003100 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003101 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003102 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003103
Jens Axboebcda7ba2020-02-23 16:42:51 -07003104 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003105 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003106 return -EINVAL;
3107
Jens Axboe3a6820f2019-12-22 15:19:35 -07003108 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003109 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003110 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003111 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003112 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003113 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003114 }
3115
Jens Axboe3a6820f2019-12-22 15:19:35 -07003116 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3117 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003118 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003119 }
3120
Jens Axboe4d954c22020-02-27 07:31:19 -07003121 if (req->flags & REQ_F_BUFFER_SELECT) {
3122 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003123 if (!ret)
3124 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003125 *iovec = NULL;
3126 return ret;
3127 }
3128
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003129 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3130 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003131}
3132
Jens Axboe0fef9482020-08-26 10:36:20 -06003133static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3134{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003135 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003136}
3137
Jens Axboe32960612019-09-23 11:05:34 -06003138/*
3139 * For files that don't have ->read_iter() and ->write_iter(), handle them
3140 * by looping over ->read() or ->write() manually.
3141 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003142static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003143{
Jens Axboe4017eb92020-10-22 14:14:12 -06003144 struct kiocb *kiocb = &req->rw.kiocb;
3145 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003146 ssize_t ret = 0;
3147
3148 /*
3149 * Don't support polled IO through this interface, and we can't
3150 * support non-blocking either. For the latter, this just causes
3151 * the kiocb to be handled from an async context.
3152 */
3153 if (kiocb->ki_flags & IOCB_HIPRI)
3154 return -EOPNOTSUPP;
3155 if (kiocb->ki_flags & IOCB_NOWAIT)
3156 return -EAGAIN;
3157
3158 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003159 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003160 ssize_t nr;
3161
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003162 if (!iov_iter_is_bvec(iter)) {
3163 iovec = iov_iter_iovec(iter);
3164 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003165 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3166 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003167 }
3168
Jens Axboe32960612019-09-23 11:05:34 -06003169 if (rw == READ) {
3170 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003171 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003172 } else {
3173 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003174 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003175 }
3176
3177 if (nr < 0) {
3178 if (!ret)
3179 ret = nr;
3180 break;
3181 }
3182 ret += nr;
3183 if (nr != iovec.iov_len)
3184 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003185 req->rw.len -= nr;
3186 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003187 iov_iter_advance(iter, nr);
3188 }
3189
3190 return ret;
3191}
3192
Jens Axboeff6165b2020-08-13 09:47:43 -06003193static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3194 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003195{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003196 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003197
Jens Axboeff6165b2020-08-13 09:47:43 -06003198 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003199 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003200 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003201 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003202 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003203 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003204 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003205 unsigned iov_off = 0;
3206
3207 rw->iter.iov = rw->fast_iov;
3208 if (iter->iov != fast_iov) {
3209 iov_off = iter->iov - fast_iov;
3210 rw->iter.iov += iov_off;
3211 }
3212 if (rw->fast_iov != fast_iov)
3213 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003214 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003215 } else {
3216 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003217 }
3218}
3219
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003220static inline int io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003221{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003222 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3223 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3224 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003225}
3226
Jens Axboeff6165b2020-08-13 09:47:43 -06003227static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3228 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003229 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003230{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003231 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003232 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003233 if (!req->async_data) {
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003234 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003235 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003236 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003237 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003238
Jens Axboeff6165b2020-08-13 09:47:43 -06003239 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003240 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003241 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003242}
3243
Pavel Begunkov73debe62020-09-30 22:57:54 +03003244static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003245{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003246 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003247 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003248 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003249
Pavel Begunkov2846c482020-11-07 13:16:27 +00003250 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003251 if (unlikely(ret < 0))
3252 return ret;
3253
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003254 iorw->bytes_done = 0;
3255 iorw->free_iovec = iov;
3256 if (iov)
3257 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003258 return 0;
3259}
3260
Pavel Begunkov73debe62020-09-30 22:57:54 +03003261static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003262{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003263 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3264 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003265 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003266}
3267
Jens Axboec1dd91d2020-08-03 16:43:59 -06003268/*
3269 * This is our waitqueue callback handler, registered through lock_page_async()
3270 * when we initially tried to do the IO with the iocb armed our waitqueue.
3271 * This gets called when the page is unlocked, and we generally expect that to
3272 * happen when the page IO is completed and the page is now uptodate. This will
3273 * queue a task_work based retry of the operation, attempting to copy the data
3274 * again. If the latter fails because the page was NOT uptodate, then we will
3275 * do a thread based blocking retry of the operation. That's the unexpected
3276 * slow path.
3277 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003278static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3279 int sync, void *arg)
3280{
3281 struct wait_page_queue *wpq;
3282 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003283 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003284
3285 wpq = container_of(wait, struct wait_page_queue, wait);
3286
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003287 if (!wake_page_match(wpq, key))
3288 return 0;
3289
Hao Xuc8d317a2020-09-29 20:00:45 +08003290 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003291 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003292 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003293 return 1;
3294}
3295
Jens Axboec1dd91d2020-08-03 16:43:59 -06003296/*
3297 * This controls whether a given IO request should be armed for async page
3298 * based retry. If we return false here, the request is handed to the async
3299 * worker threads for retry. If we're doing buffered reads on a regular file,
3300 * we prepare a private wait_page_queue entry and retry the operation. This
3301 * will either succeed because the page is now uptodate and unlocked, or it
3302 * will register a callback when the page is unlocked at IO completion. Through
3303 * that callback, io_uring uses task_work to setup a retry of the operation.
3304 * That retry will attempt the buffered read again. The retry will generally
3305 * succeed, or in rare cases where it fails, we then fall back to using the
3306 * async worker threads for a blocking retry.
3307 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003308static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003309{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003310 struct io_async_rw *rw = req->async_data;
3311 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003312 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003313
3314 /* never retry for NOWAIT, we just complete with -EAGAIN */
3315 if (req->flags & REQ_F_NOWAIT)
3316 return false;
3317
Jens Axboe227c0c92020-08-13 11:51:40 -06003318 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003319 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003320 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003321
Jens Axboebcf5a062020-05-22 09:24:42 -06003322 /*
3323 * just use poll if we can, and don't attempt if the fs doesn't
3324 * support callback based unlocks
3325 */
3326 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3327 return false;
3328
Jens Axboe3b2a4432020-08-16 10:58:43 -07003329 wait->wait.func = io_async_buf_func;
3330 wait->wait.private = req;
3331 wait->wait.flags = 0;
3332 INIT_LIST_HEAD(&wait->wait.entry);
3333 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003334 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003335 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003336 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003337}
3338
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003339static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003340{
3341 if (req->file->f_op->read_iter)
3342 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003343 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003344 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003345 else
3346 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003347}
3348
Pavel Begunkov889fca72021-02-10 00:03:09 +00003349static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003350{
3351 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003352 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003353 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003354 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003355 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003356 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003357
Pavel Begunkov2846c482020-11-07 13:16:27 +00003358 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003359 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003360 iovec = NULL;
3361 } else {
3362 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3363 if (ret < 0)
3364 return ret;
3365 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003366 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003367 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003368
Jens Axboefd6c2e42019-12-18 12:19:41 -07003369 /* Ensure we clear previously set non-block flag */
3370 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003371 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003372 else
3373 kiocb->ki_flags |= IOCB_NOWAIT;
3374
Pavel Begunkov24c74672020-06-21 13:09:51 +03003375 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003376 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003377 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003378 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003379 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003380
Pavel Begunkov632546c2020-11-07 13:16:26 +00003381 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003382 if (unlikely(ret)) {
3383 kfree(iovec);
3384 return ret;
3385 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003386
Jens Axboe227c0c92020-08-13 11:51:40 -06003387 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003388
Jens Axboe230d50d2021-04-01 20:41:15 -06003389 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003390 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003391 /* IOPOLL retry should happen for io-wq threads */
3392 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003393 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003394 /* no retry on NONBLOCK nor RWF_NOWAIT */
3395 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003396 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003397 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003398 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003399 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003400 } else if (ret == -EIOCBQUEUED) {
3401 goto out_free;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003402 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003403 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003404 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003405 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003406 }
3407
Jens Axboe227c0c92020-08-13 11:51:40 -06003408 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003409 if (ret2)
3410 return ret2;
3411
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003412 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003413 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003414 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003415 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003416
Pavel Begunkovb23df912021-02-04 13:52:04 +00003417 do {
3418 io_size -= ret;
3419 rw->bytes_done += ret;
3420 /* if we can retry, do so with the callbacks armed */
3421 if (!io_rw_should_retry(req)) {
3422 kiocb->ki_flags &= ~IOCB_WAITQ;
3423 return -EAGAIN;
3424 }
3425
3426 /*
3427 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3428 * we get -EIOCBQUEUED, then we'll get a notification when the
3429 * desired page gets unlocked. We can also get a partial read
3430 * here, and if we do, then just retry at the new offset.
3431 */
3432 ret = io_iter_do_read(req, iter);
3433 if (ret == -EIOCBQUEUED)
3434 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003435 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003436 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003437 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003438done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003439 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003440out_free:
3441 /* it's faster to check here then delegate to kfree */
3442 if (iovec)
3443 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003444 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003445}
3446
Pavel Begunkov73debe62020-09-30 22:57:54 +03003447static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003448{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003449 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3450 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003451 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003452}
3453
Pavel Begunkov889fca72021-02-10 00:03:09 +00003454static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003455{
3456 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003457 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003458 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003459 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003460 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003461 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003462
Pavel Begunkov2846c482020-11-07 13:16:27 +00003463 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003464 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003465 iovec = NULL;
3466 } else {
3467 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3468 if (ret < 0)
3469 return ret;
3470 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003471 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003472 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003473
Jens Axboefd6c2e42019-12-18 12:19:41 -07003474 /* Ensure we clear previously set non-block flag */
3475 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003476 kiocb->ki_flags &= ~IOCB_NOWAIT;
3477 else
3478 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003479
Pavel Begunkov24c74672020-06-21 13:09:51 +03003480 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003481 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003482 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003483
Jens Axboe10d59342019-12-09 20:16:22 -07003484 /* file path doesn't support NOWAIT for non-direct_IO */
3485 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3486 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003487 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003488
Pavel Begunkov632546c2020-11-07 13:16:26 +00003489 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003490 if (unlikely(ret))
3491 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003492
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003493 /*
3494 * Open-code file_start_write here to grab freeze protection,
3495 * which will be released by another thread in
3496 * io_complete_rw(). Fool lockdep by telling it the lock got
3497 * released so that it doesn't complain about the held lock when
3498 * we return to userspace.
3499 */
3500 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003501 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003502 __sb_writers_release(file_inode(req->file)->i_sb,
3503 SB_FREEZE_WRITE);
3504 }
3505 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003506
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003507 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003508 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003509 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003510 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003511 else
3512 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003513
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003514 if (req->flags & REQ_F_REISSUE) {
3515 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003516 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003517 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003518
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003519 /*
3520 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3521 * retry them without IOCB_NOWAIT.
3522 */
3523 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3524 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003525 /* no retry on NONBLOCK nor RWF_NOWAIT */
3526 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003527 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003528 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003529 /* IOPOLL retry should happen for io-wq threads */
3530 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3531 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003532done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003533 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003534 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003535copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003536 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003537 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003538 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003539 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003540 }
Jens Axboe31b51512019-01-18 22:56:34 -07003541out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003542 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003543 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003544 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003545 return ret;
3546}
3547
Jens Axboe80a261f2020-09-28 14:23:58 -06003548static int io_renameat_prep(struct io_kiocb *req,
3549 const struct io_uring_sqe *sqe)
3550{
3551 struct io_rename *ren = &req->rename;
3552 const char __user *oldf, *newf;
3553
Jens Axboeed7eb252021-06-23 09:04:13 -06003554 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3555 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003556 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003557 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003558 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3559 return -EBADF;
3560
3561 ren->old_dfd = READ_ONCE(sqe->fd);
3562 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3563 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3564 ren->new_dfd = READ_ONCE(sqe->len);
3565 ren->flags = READ_ONCE(sqe->rename_flags);
3566
3567 ren->oldpath = getname(oldf);
3568 if (IS_ERR(ren->oldpath))
3569 return PTR_ERR(ren->oldpath);
3570
3571 ren->newpath = getname(newf);
3572 if (IS_ERR(ren->newpath)) {
3573 putname(ren->oldpath);
3574 return PTR_ERR(ren->newpath);
3575 }
3576
3577 req->flags |= REQ_F_NEED_CLEANUP;
3578 return 0;
3579}
3580
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003581static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003582{
3583 struct io_rename *ren = &req->rename;
3584 int ret;
3585
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003586 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003587 return -EAGAIN;
3588
3589 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3590 ren->newpath, ren->flags);
3591
3592 req->flags &= ~REQ_F_NEED_CLEANUP;
3593 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003594 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003595 io_req_complete(req, ret);
3596 return 0;
3597}
3598
Jens Axboe14a11432020-09-28 14:27:37 -06003599static int io_unlinkat_prep(struct io_kiocb *req,
3600 const struct io_uring_sqe *sqe)
3601{
3602 struct io_unlink *un = &req->unlink;
3603 const char __user *fname;
3604
Jens Axboe22634bc2021-06-23 09:07:45 -06003605 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3606 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003607 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3608 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003609 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003610 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3611 return -EBADF;
3612
3613 un->dfd = READ_ONCE(sqe->fd);
3614
3615 un->flags = READ_ONCE(sqe->unlink_flags);
3616 if (un->flags & ~AT_REMOVEDIR)
3617 return -EINVAL;
3618
3619 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3620 un->filename = getname(fname);
3621 if (IS_ERR(un->filename))
3622 return PTR_ERR(un->filename);
3623
3624 req->flags |= REQ_F_NEED_CLEANUP;
3625 return 0;
3626}
3627
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003628static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003629{
3630 struct io_unlink *un = &req->unlink;
3631 int ret;
3632
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003633 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003634 return -EAGAIN;
3635
3636 if (un->flags & AT_REMOVEDIR)
3637 ret = do_rmdir(un->dfd, un->filename);
3638 else
3639 ret = do_unlinkat(un->dfd, un->filename);
3640
3641 req->flags &= ~REQ_F_NEED_CLEANUP;
3642 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003643 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003644 io_req_complete(req, ret);
3645 return 0;
3646}
3647
Jens Axboe36f4fa62020-09-05 11:14:22 -06003648static int io_shutdown_prep(struct io_kiocb *req,
3649 const struct io_uring_sqe *sqe)
3650{
3651#if defined(CONFIG_NET)
3652 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3653 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003654 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3655 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003656 return -EINVAL;
3657
3658 req->shutdown.how = READ_ONCE(sqe->len);
3659 return 0;
3660#else
3661 return -EOPNOTSUPP;
3662#endif
3663}
3664
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003665static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003666{
3667#if defined(CONFIG_NET)
3668 struct socket *sock;
3669 int ret;
3670
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003671 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003672 return -EAGAIN;
3673
Linus Torvalds48aba792020-12-16 12:44:05 -08003674 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003675 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003676 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003677
3678 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003679 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003680 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003681 io_req_complete(req, ret);
3682 return 0;
3683#else
3684 return -EOPNOTSUPP;
3685#endif
3686}
3687
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003688static int __io_splice_prep(struct io_kiocb *req,
3689 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003690{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003691 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003692 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003693
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003694 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3695 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003696
3697 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003698 sp->len = READ_ONCE(sqe->len);
3699 sp->flags = READ_ONCE(sqe->splice_flags);
3700
3701 if (unlikely(sp->flags & ~valid_flags))
3702 return -EINVAL;
3703
Pavel Begunkov62906e82021-08-10 14:52:47 +01003704 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003705 (sp->flags & SPLICE_F_FD_IN_FIXED));
3706 if (!sp->file_in)
3707 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003708 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003709 return 0;
3710}
3711
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003712static int io_tee_prep(struct io_kiocb *req,
3713 const struct io_uring_sqe *sqe)
3714{
3715 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3716 return -EINVAL;
3717 return __io_splice_prep(req, sqe);
3718}
3719
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003720static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003721{
3722 struct io_splice *sp = &req->splice;
3723 struct file *in = sp->file_in;
3724 struct file *out = sp->file_out;
3725 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3726 long ret = 0;
3727
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003728 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003729 return -EAGAIN;
3730 if (sp->len)
3731 ret = do_tee(in, out, sp->len, flags);
3732
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003733 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3734 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003735 req->flags &= ~REQ_F_NEED_CLEANUP;
3736
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003737 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003738 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003739 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003740 return 0;
3741}
3742
3743static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3744{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003745 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003746
3747 sp->off_in = READ_ONCE(sqe->splice_off_in);
3748 sp->off_out = READ_ONCE(sqe->off);
3749 return __io_splice_prep(req, sqe);
3750}
3751
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003752static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003753{
3754 struct io_splice *sp = &req->splice;
3755 struct file *in = sp->file_in;
3756 struct file *out = sp->file_out;
3757 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3758 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003759 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003760
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003761 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003762 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003763
3764 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3765 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003766
Jens Axboe948a7742020-05-17 14:21:38 -06003767 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003768 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003769
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003770 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3771 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003772 req->flags &= ~REQ_F_NEED_CLEANUP;
3773
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003774 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003775 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003776 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003777 return 0;
3778}
3779
Jens Axboe2b188cc2019-01-07 10:46:33 -07003780/*
3781 * IORING_OP_NOP just posts a completion event, nothing else.
3782 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003783static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003784{
3785 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003786
Jens Axboedef596e2019-01-09 08:59:42 -07003787 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3788 return -EINVAL;
3789
Pavel Begunkov889fca72021-02-10 00:03:09 +00003790 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003791 return 0;
3792}
3793
Pavel Begunkov1155c762021-02-18 18:29:38 +00003794static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003795{
Jens Axboe6b063142019-01-10 22:13:58 -07003796 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003797
Jens Axboe09bb8392019-03-13 12:39:28 -06003798 if (!req->file)
3799 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003800
Jens Axboe6b063142019-01-10 22:13:58 -07003801 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003802 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003803 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
3804 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003805 return -EINVAL;
3806
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003807 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3808 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3809 return -EINVAL;
3810
3811 req->sync.off = READ_ONCE(sqe->off);
3812 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003813 return 0;
3814}
3815
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003816static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003817{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003818 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003819 int ret;
3820
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003821 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003822 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003823 return -EAGAIN;
3824
Jens Axboe9adbd452019-12-20 08:45:55 -07003825 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003826 end > 0 ? end : LLONG_MAX,
3827 req->sync.flags & IORING_FSYNC_DATASYNC);
3828 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003829 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003830 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003831 return 0;
3832}
3833
Jens Axboed63d1b52019-12-10 10:38:56 -07003834static int io_fallocate_prep(struct io_kiocb *req,
3835 const struct io_uring_sqe *sqe)
3836{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003837 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
3838 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07003839 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003840 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3841 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003842
3843 req->sync.off = READ_ONCE(sqe->off);
3844 req->sync.len = READ_ONCE(sqe->addr);
3845 req->sync.mode = READ_ONCE(sqe->len);
3846 return 0;
3847}
3848
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003849static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003850{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003851 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003852
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003853 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003854 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003855 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003856 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3857 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003858 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003859 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003860 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003861 return 0;
3862}
3863
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003864static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003865{
Jens Axboef8748882020-01-08 17:47:02 -07003866 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003867 int ret;
3868
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003869 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3870 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01003871 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003872 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003873 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003874 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003875
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003876 /* open.how should be already initialised */
3877 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003878 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003879
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003880 req->open.dfd = READ_ONCE(sqe->fd);
3881 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003882 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003883 if (IS_ERR(req->open.filename)) {
3884 ret = PTR_ERR(req->open.filename);
3885 req->open.filename = NULL;
3886 return ret;
3887 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01003888
3889 req->open.file_slot = READ_ONCE(sqe->file_index);
3890 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
3891 return -EINVAL;
3892
Jens Axboe4022e7a2020-03-19 19:23:18 -06003893 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003894 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003895 return 0;
3896}
3897
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003898static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3899{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003900 u64 mode = READ_ONCE(sqe->len);
3901 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003902
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003903 req->open.how = build_open_how(flags, mode);
3904 return __io_openat_prep(req, sqe);
3905}
3906
Jens Axboecebdb982020-01-08 17:59:24 -07003907static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3908{
3909 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003910 size_t len;
3911 int ret;
3912
Jens Axboecebdb982020-01-08 17:59:24 -07003913 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3914 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003915 if (len < OPEN_HOW_SIZE_VER0)
3916 return -EINVAL;
3917
3918 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3919 len);
3920 if (ret)
3921 return ret;
3922
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003923 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003924}
3925
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003926static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003927{
3928 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003929 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01003930 bool resolve_nonblock, nonblock_set;
3931 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003932 int ret;
3933
Jens Axboecebdb982020-01-08 17:59:24 -07003934 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003935 if (ret)
3936 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003937 nonblock_set = op.open_flag & O_NONBLOCK;
3938 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003939 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003940 /*
3941 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3942 * it'll always -EAGAIN
3943 */
3944 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3945 return -EAGAIN;
3946 op.lookup_flags |= LOOKUP_CACHED;
3947 op.open_flag |= O_NONBLOCK;
3948 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003949
Pavel Begunkovb9445592021-08-25 12:25:45 +01003950 if (!fixed) {
3951 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
3952 if (ret < 0)
3953 goto err;
3954 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003955
3956 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003957 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003958 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003959 * We could hang on to this 'fd' on retrying, but seems like
3960 * marginal gain for something that is now known to be a slower
3961 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07003962 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01003963 if (!fixed)
3964 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003965
3966 ret = PTR_ERR(file);
3967 /* only retry if RESOLVE_CACHED wasn't already set by application */
3968 if (ret == -EAGAIN &&
3969 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
3970 return -EAGAIN;
3971 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003972 }
3973
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003974 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
3975 file->f_flags &= ~O_NONBLOCK;
3976 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01003977
3978 if (!fixed)
3979 fd_install(ret, file);
3980 else
3981 ret = io_install_fixed_file(req, file, issue_flags,
3982 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003983err:
3984 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003985 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003986 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003987 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01003988 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003989 return 0;
3990}
3991
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003992static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003993{
Pavel Begunkove45cff52021-02-28 22:35:14 +00003994 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07003995}
3996
Jens Axboe067524e2020-03-02 16:32:28 -07003997static int io_remove_buffers_prep(struct io_kiocb *req,
3998 const struct io_uring_sqe *sqe)
3999{
4000 struct io_provide_buf *p = &req->pbuf;
4001 u64 tmp;
4002
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004003 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4004 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004005 return -EINVAL;
4006
4007 tmp = READ_ONCE(sqe->fd);
4008 if (!tmp || tmp > USHRT_MAX)
4009 return -EINVAL;
4010
4011 memset(p, 0, sizeof(*p));
4012 p->nbufs = tmp;
4013 p->bgid = READ_ONCE(sqe->buf_group);
4014 return 0;
4015}
4016
4017static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4018 int bgid, unsigned nbufs)
4019{
4020 unsigned i = 0;
4021
4022 /* shouldn't happen */
4023 if (!nbufs)
4024 return 0;
4025
4026 /* the head kbuf is the list itself */
4027 while (!list_empty(&buf->list)) {
4028 struct io_buffer *nxt;
4029
4030 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4031 list_del(&nxt->list);
4032 kfree(nxt);
4033 if (++i == nbufs)
4034 return i;
4035 }
4036 i++;
4037 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004038 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004039
4040 return i;
4041}
4042
Pavel Begunkov889fca72021-02-10 00:03:09 +00004043static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004044{
4045 struct io_provide_buf *p = &req->pbuf;
4046 struct io_ring_ctx *ctx = req->ctx;
4047 struct io_buffer *head;
4048 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004049 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004050
4051 io_ring_submit_lock(ctx, !force_nonblock);
4052
4053 lockdep_assert_held(&ctx->uring_lock);
4054
4055 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004056 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004057 if (head)
4058 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004059 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004060 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004061
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004062 /* complete before unlock, IOPOLL may need the lock */
4063 __io_req_complete(req, issue_flags, ret, 0);
4064 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004065 return 0;
4066}
4067
Jens Axboeddf0322d2020-02-23 16:41:33 -07004068static int io_provide_buffers_prep(struct io_kiocb *req,
4069 const struct io_uring_sqe *sqe)
4070{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004071 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004072 struct io_provide_buf *p = &req->pbuf;
4073 u64 tmp;
4074
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004075 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004076 return -EINVAL;
4077
4078 tmp = READ_ONCE(sqe->fd);
4079 if (!tmp || tmp > USHRT_MAX)
4080 return -E2BIG;
4081 p->nbufs = tmp;
4082 p->addr = READ_ONCE(sqe->addr);
4083 p->len = READ_ONCE(sqe->len);
4084
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004085 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4086 &size))
4087 return -EOVERFLOW;
4088 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4089 return -EOVERFLOW;
4090
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004091 size = (unsigned long)p->len * p->nbufs;
4092 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004093 return -EFAULT;
4094
4095 p->bgid = READ_ONCE(sqe->buf_group);
4096 tmp = READ_ONCE(sqe->off);
4097 if (tmp > USHRT_MAX)
4098 return -E2BIG;
4099 p->bid = tmp;
4100 return 0;
4101}
4102
4103static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4104{
4105 struct io_buffer *buf;
4106 u64 addr = pbuf->addr;
4107 int i, bid = pbuf->bid;
4108
4109 for (i = 0; i < pbuf->nbufs; i++) {
4110 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4111 if (!buf)
4112 break;
4113
4114 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004115 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004116 buf->bid = bid;
4117 addr += pbuf->len;
4118 bid++;
4119 if (!*head) {
4120 INIT_LIST_HEAD(&buf->list);
4121 *head = buf;
4122 } else {
4123 list_add_tail(&buf->list, &(*head)->list);
4124 }
4125 }
4126
4127 return i ? i : -ENOMEM;
4128}
4129
Pavel Begunkov889fca72021-02-10 00:03:09 +00004130static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004131{
4132 struct io_provide_buf *p = &req->pbuf;
4133 struct io_ring_ctx *ctx = req->ctx;
4134 struct io_buffer *head, *list;
4135 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004136 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004137
4138 io_ring_submit_lock(ctx, !force_nonblock);
4139
4140 lockdep_assert_held(&ctx->uring_lock);
4141
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004142 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004143
4144 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004145 if (ret >= 0 && !list) {
4146 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4147 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004148 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004149 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004150 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004151 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004152 /* complete before unlock, IOPOLL may need the lock */
4153 __io_req_complete(req, issue_flags, ret, 0);
4154 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004155 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004156}
4157
Jens Axboe3e4827b2020-01-08 15:18:09 -07004158static int io_epoll_ctl_prep(struct io_kiocb *req,
4159 const struct io_uring_sqe *sqe)
4160{
4161#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004162 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004163 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004164 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004165 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004166
4167 req->epoll.epfd = READ_ONCE(sqe->fd);
4168 req->epoll.op = READ_ONCE(sqe->len);
4169 req->epoll.fd = READ_ONCE(sqe->off);
4170
4171 if (ep_op_has_event(req->epoll.op)) {
4172 struct epoll_event __user *ev;
4173
4174 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4175 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4176 return -EFAULT;
4177 }
4178
4179 return 0;
4180#else
4181 return -EOPNOTSUPP;
4182#endif
4183}
4184
Pavel Begunkov889fca72021-02-10 00:03:09 +00004185static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004186{
4187#if defined(CONFIG_EPOLL)
4188 struct io_epoll *ie = &req->epoll;
4189 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004190 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004191
4192 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4193 if (force_nonblock && ret == -EAGAIN)
4194 return -EAGAIN;
4195
4196 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004197 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004198 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004199 return 0;
4200#else
4201 return -EOPNOTSUPP;
4202#endif
4203}
4204
Jens Axboec1ca7572019-12-25 22:18:28 -07004205static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4206{
4207#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004208 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004209 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004210 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4211 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004212
4213 req->madvise.addr = READ_ONCE(sqe->addr);
4214 req->madvise.len = READ_ONCE(sqe->len);
4215 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4216 return 0;
4217#else
4218 return -EOPNOTSUPP;
4219#endif
4220}
4221
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004222static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004223{
4224#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4225 struct io_madvise *ma = &req->madvise;
4226 int ret;
4227
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004228 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004229 return -EAGAIN;
4230
Minchan Kim0726b012020-10-17 16:14:50 -07004231 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004232 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004233 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004234 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004235 return 0;
4236#else
4237 return -EOPNOTSUPP;
4238#endif
4239}
4240
Jens Axboe4840e412019-12-25 22:03:45 -07004241static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4242{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004243 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004244 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004245 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4246 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004247
4248 req->fadvise.offset = READ_ONCE(sqe->off);
4249 req->fadvise.len = READ_ONCE(sqe->len);
4250 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4251 return 0;
4252}
4253
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004254static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004255{
4256 struct io_fadvise *fa = &req->fadvise;
4257 int ret;
4258
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004259 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004260 switch (fa->advice) {
4261 case POSIX_FADV_NORMAL:
4262 case POSIX_FADV_RANDOM:
4263 case POSIX_FADV_SEQUENTIAL:
4264 break;
4265 default:
4266 return -EAGAIN;
4267 }
4268 }
Jens Axboe4840e412019-12-25 22:03:45 -07004269
4270 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4271 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004272 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004273 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004274 return 0;
4275}
4276
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004277static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4278{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004279 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004280 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004281 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004282 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004283 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004284 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004285
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004286 req->statx.dfd = READ_ONCE(sqe->fd);
4287 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004288 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004289 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4290 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004291
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004292 return 0;
4293}
4294
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004295static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004296{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004297 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004298 int ret;
4299
Pavel Begunkov59d70012021-03-22 01:58:30 +00004300 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004301 return -EAGAIN;
4302
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004303 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4304 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004305
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004306 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004307 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004308 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004309 return 0;
4310}
4311
Jens Axboeb5dba592019-12-11 14:02:38 -07004312static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4313{
Jens Axboe14587a462020-09-05 11:36:08 -06004314 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004315 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004316 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004317 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004318 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004319 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004320 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004321
4322 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004323 return 0;
4324}
4325
Pavel Begunkov889fca72021-02-10 00:03:09 +00004326static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004327{
Jens Axboe9eac1902021-01-19 15:50:37 -07004328 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004329 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004330 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004331 struct file *file = NULL;
4332 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004333
Jens Axboe9eac1902021-01-19 15:50:37 -07004334 spin_lock(&files->file_lock);
4335 fdt = files_fdtable(files);
4336 if (close->fd >= fdt->max_fds) {
4337 spin_unlock(&files->file_lock);
4338 goto err;
4339 }
4340 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004341 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004342 spin_unlock(&files->file_lock);
4343 file = NULL;
4344 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004345 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004346
4347 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004348 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004349 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004350 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004351 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004352
Jens Axboe9eac1902021-01-19 15:50:37 -07004353 ret = __close_fd_get_file(close->fd, &file);
4354 spin_unlock(&files->file_lock);
4355 if (ret < 0) {
4356 if (ret == -ENOENT)
4357 ret = -EBADF;
4358 goto err;
4359 }
4360
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004361 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004362 ret = filp_close(file, current->files);
4363err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004364 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004365 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004366 if (file)
4367 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004368 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004369 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004370}
4371
Pavel Begunkov1155c762021-02-18 18:29:38 +00004372static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004373{
4374 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004375
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004376 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4377 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004378 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4379 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004380 return -EINVAL;
4381
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004382 req->sync.off = READ_ONCE(sqe->off);
4383 req->sync.len = READ_ONCE(sqe->len);
4384 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004385 return 0;
4386}
4387
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004388static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004389{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004390 int ret;
4391
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004392 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004393 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004394 return -EAGAIN;
4395
Jens Axboe9adbd452019-12-20 08:45:55 -07004396 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004397 req->sync.flags);
4398 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004399 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004400 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004401 return 0;
4402}
4403
YueHaibing469956e2020-03-04 15:53:52 +08004404#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004405static int io_setup_async_msg(struct io_kiocb *req,
4406 struct io_async_msghdr *kmsg)
4407{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004408 struct io_async_msghdr *async_msg = req->async_data;
4409
4410 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004411 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004412 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004413 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004414 return -ENOMEM;
4415 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004416 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004417 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004418 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004419 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004420 /* if were using fast_iov, set it to the new one */
4421 if (!async_msg->free_iov)
4422 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4423
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004424 return -EAGAIN;
4425}
4426
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004427static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4428 struct io_async_msghdr *iomsg)
4429{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004430 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004431 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004432 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004433 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004434}
4435
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004436static int io_sendmsg_prep_async(struct io_kiocb *req)
4437{
4438 int ret;
4439
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004440 ret = io_sendmsg_copy_hdr(req, req->async_data);
4441 if (!ret)
4442 req->flags |= REQ_F_NEED_CLEANUP;
4443 return ret;
4444}
4445
Jens Axboe3529d8c2019-12-19 18:24:38 -07004446static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004447{
Jens Axboee47293f2019-12-20 08:58:21 -07004448 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004449
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004450 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4451 return -EINVAL;
4452
Pavel Begunkov270a5942020-07-12 20:41:04 +03004453 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004454 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004455 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4456 if (sr->msg_flags & MSG_DONTWAIT)
4457 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004458
Jens Axboed8768362020-02-27 14:17:49 -07004459#ifdef CONFIG_COMPAT
4460 if (req->ctx->compat)
4461 sr->msg_flags |= MSG_CMSG_COMPAT;
4462#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004463 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004464}
4465
Pavel Begunkov889fca72021-02-10 00:03:09 +00004466static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004467{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004468 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004469 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004470 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004471 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004472 int ret;
4473
Florent Revestdba4a922020-12-04 12:36:04 +01004474 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004475 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004476 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004477
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004478 kmsg = req->async_data;
4479 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004480 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004481 if (ret)
4482 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004483 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004484 }
4485
Pavel Begunkov04411802021-04-01 15:44:00 +01004486 flags = req->sr_msg.msg_flags;
4487 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004488 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004489 if (flags & MSG_WAITALL)
4490 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4491
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004492 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004493 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004494 return io_setup_async_msg(req, kmsg);
4495 if (ret == -ERESTARTSYS)
4496 ret = -EINTR;
4497
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004498 /* fast path, check for non-NULL to avoid function call */
4499 if (kmsg->free_iov)
4500 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004501 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004502 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004503 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004504 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004505 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004506}
4507
Pavel Begunkov889fca72021-02-10 00:03:09 +00004508static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004509{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004510 struct io_sr_msg *sr = &req->sr_msg;
4511 struct msghdr msg;
4512 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004513 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004514 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004515 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004516 int ret;
4517
Florent Revestdba4a922020-12-04 12:36:04 +01004518 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004519 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004520 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004521
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004522 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4523 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004524 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004525
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004526 msg.msg_name = NULL;
4527 msg.msg_control = NULL;
4528 msg.msg_controllen = 0;
4529 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004530
Pavel Begunkov04411802021-04-01 15:44:00 +01004531 flags = req->sr_msg.msg_flags;
4532 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004533 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004534 if (flags & MSG_WAITALL)
4535 min_ret = iov_iter_count(&msg.msg_iter);
4536
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004537 msg.msg_flags = flags;
4538 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004539 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004540 return -EAGAIN;
4541 if (ret == -ERESTARTSYS)
4542 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004543
Stefan Metzmacher00312752021-03-20 20:33:36 +01004544 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004545 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004546 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004547 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004548}
4549
Pavel Begunkov1400e692020-07-12 20:41:05 +03004550static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4551 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004552{
4553 struct io_sr_msg *sr = &req->sr_msg;
4554 struct iovec __user *uiov;
4555 size_t iov_len;
4556 int ret;
4557
Pavel Begunkov1400e692020-07-12 20:41:05 +03004558 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4559 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004560 if (ret)
4561 return ret;
4562
4563 if (req->flags & REQ_F_BUFFER_SELECT) {
4564 if (iov_len > 1)
4565 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004566 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004567 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004568 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004569 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004570 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004571 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004572 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004573 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004574 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004575 if (ret > 0)
4576 ret = 0;
4577 }
4578
4579 return ret;
4580}
4581
4582#ifdef CONFIG_COMPAT
4583static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004584 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004585{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004586 struct io_sr_msg *sr = &req->sr_msg;
4587 struct compat_iovec __user *uiov;
4588 compat_uptr_t ptr;
4589 compat_size_t len;
4590 int ret;
4591
Pavel Begunkov4af34172021-04-11 01:46:30 +01004592 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4593 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004594 if (ret)
4595 return ret;
4596
4597 uiov = compat_ptr(ptr);
4598 if (req->flags & REQ_F_BUFFER_SELECT) {
4599 compat_ssize_t clen;
4600
4601 if (len > 1)
4602 return -EINVAL;
4603 if (!access_ok(uiov, sizeof(*uiov)))
4604 return -EFAULT;
4605 if (__get_user(clen, &uiov->iov_len))
4606 return -EFAULT;
4607 if (clen < 0)
4608 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004609 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004610 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004611 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004612 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004613 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004614 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004615 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004616 if (ret < 0)
4617 return ret;
4618 }
4619
4620 return 0;
4621}
Jens Axboe03b12302019-12-02 18:50:25 -07004622#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004623
Pavel Begunkov1400e692020-07-12 20:41:05 +03004624static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4625 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004626{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004627 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004628
4629#ifdef CONFIG_COMPAT
4630 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004631 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004632#endif
4633
Pavel Begunkov1400e692020-07-12 20:41:05 +03004634 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004635}
4636
Jens Axboebcda7ba2020-02-23 16:42:51 -07004637static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004638 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004639{
4640 struct io_sr_msg *sr = &req->sr_msg;
4641 struct io_buffer *kbuf;
4642
Jens Axboebcda7ba2020-02-23 16:42:51 -07004643 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4644 if (IS_ERR(kbuf))
4645 return kbuf;
4646
4647 sr->kbuf = kbuf;
4648 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004649 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004650}
4651
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004652static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4653{
4654 return io_put_kbuf(req, req->sr_msg.kbuf);
4655}
4656
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004657static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004658{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004659 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004660
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004661 ret = io_recvmsg_copy_hdr(req, req->async_data);
4662 if (!ret)
4663 req->flags |= REQ_F_NEED_CLEANUP;
4664 return ret;
4665}
4666
4667static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4668{
4669 struct io_sr_msg *sr = &req->sr_msg;
4670
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004671 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4672 return -EINVAL;
4673
Pavel Begunkov270a5942020-07-12 20:41:04 +03004674 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004675 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004676 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004677 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4678 if (sr->msg_flags & MSG_DONTWAIT)
4679 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004680
Jens Axboed8768362020-02-27 14:17:49 -07004681#ifdef CONFIG_COMPAT
4682 if (req->ctx->compat)
4683 sr->msg_flags |= MSG_CMSG_COMPAT;
4684#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004685 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004686}
4687
Pavel Begunkov889fca72021-02-10 00:03:09 +00004688static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004689{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004690 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004691 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004692 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004693 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004694 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004695 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004696 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004697
Florent Revestdba4a922020-12-04 12:36:04 +01004698 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004699 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004700 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004701
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004702 kmsg = req->async_data;
4703 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004704 ret = io_recvmsg_copy_hdr(req, &iomsg);
4705 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004706 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004707 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004708 }
4709
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004710 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004711 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004712 if (IS_ERR(kbuf))
4713 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004714 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004715 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4716 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004717 1, req->sr_msg.len);
4718 }
4719
Pavel Begunkov04411802021-04-01 15:44:00 +01004720 flags = req->sr_msg.msg_flags;
4721 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004722 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004723 if (flags & MSG_WAITALL)
4724 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4725
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004726 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4727 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004728 if (force_nonblock && ret == -EAGAIN)
4729 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004730 if (ret == -ERESTARTSYS)
4731 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004732
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004733 if (req->flags & REQ_F_BUFFER_SELECTED)
4734 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004735 /* fast path, check for non-NULL to avoid function call */
4736 if (kmsg->free_iov)
4737 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004738 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004739 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004740 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004741 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004742 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004743}
4744
Pavel Begunkov889fca72021-02-10 00:03:09 +00004745static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004746{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004747 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004748 struct io_sr_msg *sr = &req->sr_msg;
4749 struct msghdr msg;
4750 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004751 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004752 struct iovec iov;
4753 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004754 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004755 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004756 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004757
Florent Revestdba4a922020-12-04 12:36:04 +01004758 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004759 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004760 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004761
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004762 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004763 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004764 if (IS_ERR(kbuf))
4765 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004766 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004767 }
4768
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004769 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004770 if (unlikely(ret))
4771 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004772
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004773 msg.msg_name = NULL;
4774 msg.msg_control = NULL;
4775 msg.msg_controllen = 0;
4776 msg.msg_namelen = 0;
4777 msg.msg_iocb = NULL;
4778 msg.msg_flags = 0;
4779
Pavel Begunkov04411802021-04-01 15:44:00 +01004780 flags = req->sr_msg.msg_flags;
4781 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004782 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004783 if (flags & MSG_WAITALL)
4784 min_ret = iov_iter_count(&msg.msg_iter);
4785
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004786 ret = sock_recvmsg(sock, &msg, flags);
4787 if (force_nonblock && ret == -EAGAIN)
4788 return -EAGAIN;
4789 if (ret == -ERESTARTSYS)
4790 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004791out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004792 if (req->flags & REQ_F_BUFFER_SELECTED)
4793 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01004794 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004795 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004796 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004797 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004798}
4799
Jens Axboe3529d8c2019-12-19 18:24:38 -07004800static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004801{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004802 struct io_accept *accept = &req->accept;
4803
Jens Axboe14587a462020-09-05 11:36:08 -06004804 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004805 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004806 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004807 return -EINVAL;
4808
Jens Axboed55e5f52019-12-11 16:12:15 -07004809 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4810 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004811 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004812 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004813 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004814}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004815
Pavel Begunkov889fca72021-02-10 00:03:09 +00004816static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004817{
4818 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004819 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004820 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004821 int ret;
4822
Jiufei Xuee697dee2020-06-10 13:41:59 +08004823 if (req->file->f_flags & O_NONBLOCK)
4824 req->flags |= REQ_F_NOWAIT;
4825
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004826 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004827 accept->addr_len, accept->flags,
4828 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004829 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004830 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004831 if (ret < 0) {
4832 if (ret == -ERESTARTSYS)
4833 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004834 req_set_fail(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004835 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004836 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004837 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004838}
4839
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004840static int io_connect_prep_async(struct io_kiocb *req)
4841{
4842 struct io_async_connect *io = req->async_data;
4843 struct io_connect *conn = &req->connect;
4844
4845 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4846}
4847
Jens Axboe3529d8c2019-12-19 18:24:38 -07004848static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004849{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004850 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004851
Jens Axboe14587a462020-09-05 11:36:08 -06004852 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004853 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004854 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
4855 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004856 return -EINVAL;
4857
Jens Axboe3529d8c2019-12-19 18:24:38 -07004858 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4859 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004860 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004861}
4862
Pavel Begunkov889fca72021-02-10 00:03:09 +00004863static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004864{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004865 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004866 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004867 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004868 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004869
Jens Axboee8c2bc12020-08-15 18:44:09 -07004870 if (req->async_data) {
4871 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004872 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004873 ret = move_addr_to_kernel(req->connect.addr,
4874 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004875 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004876 if (ret)
4877 goto out;
4878 io = &__io;
4879 }
4880
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004881 file_flags = force_nonblock ? O_NONBLOCK : 0;
4882
Jens Axboee8c2bc12020-08-15 18:44:09 -07004883 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004884 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004885 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004886 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004887 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004888 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004889 ret = -ENOMEM;
4890 goto out;
4891 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004892 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004893 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004894 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004895 if (ret == -ERESTARTSYS)
4896 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004897out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004898 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004899 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004900 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004901 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004902}
YueHaibing469956e2020-03-04 15:53:52 +08004903#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004904#define IO_NETOP_FN(op) \
4905static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4906{ \
4907 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004908}
4909
Jens Axboe99a10082021-02-19 09:35:19 -07004910#define IO_NETOP_PREP(op) \
4911IO_NETOP_FN(op) \
4912static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4913{ \
4914 return -EOPNOTSUPP; \
4915} \
4916
4917#define IO_NETOP_PREP_ASYNC(op) \
4918IO_NETOP_PREP(op) \
4919static int io_##op##_prep_async(struct io_kiocb *req) \
4920{ \
4921 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004922}
4923
Jens Axboe99a10082021-02-19 09:35:19 -07004924IO_NETOP_PREP_ASYNC(sendmsg);
4925IO_NETOP_PREP_ASYNC(recvmsg);
4926IO_NETOP_PREP_ASYNC(connect);
4927IO_NETOP_PREP(accept);
4928IO_NETOP_FN(send);
4929IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004930#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004931
Jens Axboed7718a92020-02-14 22:23:12 -07004932struct io_poll_table {
4933 struct poll_table_struct pt;
4934 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01004935 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07004936 int error;
4937};
4938
Jens Axboed7718a92020-02-14 22:23:12 -07004939static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01004940 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07004941{
Jens Axboed7718a92020-02-14 22:23:12 -07004942 /* for instances that support it check for an event match first: */
4943 if (mask && !(mask & poll->events))
4944 return 0;
4945
4946 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4947
4948 list_del_init(&poll->wait.entry);
4949
Jens Axboed7718a92020-02-14 22:23:12 -07004950 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01004951 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004952
Jens Axboed7718a92020-02-14 22:23:12 -07004953 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004954 * If this fails, then the task is exiting. When a task exits, the
4955 * work gets canceled, so just cancel this request as well instead
4956 * of executing it. We can't safely execute it anyway, as we may not
4957 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004958 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01004959 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004960 return 1;
4961}
4962
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004963static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4964 __acquires(&req->ctx->completion_lock)
4965{
4966 struct io_ring_ctx *ctx = req->ctx;
4967
Jens Axboe316319e2021-08-19 09:41:42 -06004968 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01004969 if (unlikely(req->task->flags & PF_EXITING))
4970 WRITE_ONCE(poll->canceled, true);
4971
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004972 if (!req->result && !READ_ONCE(poll->canceled)) {
4973 struct poll_table_struct pt = { ._key = poll->events };
4974
4975 req->result = vfs_poll(req->file, &pt) & poll->events;
4976 }
4977
Jens Axboe79ebeae2021-08-10 15:18:27 -06004978 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004979 if (!req->result && !READ_ONCE(poll->canceled)) {
4980 add_wait_queue(poll->head, &poll->wait);
4981 return true;
4982 }
4983
4984 return false;
4985}
4986
Jens Axboed4e7cd32020-08-15 11:44:50 -07004987static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004988{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004989 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004990 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004991 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004992 return req->apoll->double_poll;
4993}
4994
4995static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4996{
4997 if (req->opcode == IORING_OP_POLL_ADD)
4998 return &req->poll;
4999 return &req->apoll->poll;
5000}
5001
5002static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005003 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005004{
5005 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005006
5007 lockdep_assert_held(&req->ctx->completion_lock);
5008
5009 if (poll && poll->head) {
5010 struct wait_queue_head *head = poll->head;
5011
Jens Axboe79ebeae2021-08-10 15:18:27 -06005012 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005013 list_del_init(&poll->wait.entry);
5014 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005015 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005016 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005017 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005018 }
5019}
5020
Pavel Begunkove27414b2021-04-09 09:13:20 +01005021static bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005022 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005023{
5024 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005025 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005026 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005027
Pavel Begunkove27414b2021-04-09 09:13:20 +01005028 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005029 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005030 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005031 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005032 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005033 }
Jens Axboeb69de282021-03-17 08:37:41 -06005034 if (req->poll.events & EPOLLONESHOT)
5035 flags = 0;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005036 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005037 req->poll.done = true;
5038 flags = 0;
5039 }
Hao Xu7b289c32021-04-13 15:20:39 +08005040 if (flags & IORING_CQE_F_MORE)
5041 ctx->cq_extra++;
5042
Jens Axboe18bceab2020-05-15 11:56:54 -06005043 io_commit_cqring(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005044 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005045}
5046
Pavel Begunkovf237c302021-08-18 12:42:46 +01005047static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005048{
Jens Axboe6d816e02020-08-11 08:04:14 -06005049 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005050 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005051
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005052 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005053 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005054 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005055 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005056
Pavel Begunkove27414b2021-04-09 09:13:20 +01005057 done = io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005058 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005059 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005060 hash_del(&req->hash_node);
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005061 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005062 req->result = 0;
5063 add_wait_queue(req->poll.head, &req->poll.wait);
5064 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005065 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005066 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005067
Jens Axboe88e41cf2021-02-22 22:08:01 -07005068 if (done) {
5069 nxt = io_put_req_find_next(req);
5070 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005071 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005072 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005073 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005074}
5075
5076static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5077 int sync, void *key)
5078{
5079 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005080 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005081 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005082 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005083
5084 /* for instances that support it check for an event match first: */
5085 if (mask && !(mask & poll->events))
5086 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005087 if (!(poll->events & EPOLLONESHOT))
5088 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005089
Jens Axboe8706e042020-09-28 08:38:54 -06005090 list_del_init(&wait->entry);
5091
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005092 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005093 bool done;
5094
Jens Axboe79ebeae2021-08-10 15:18:27 -06005095 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005096 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005097 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005098 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005099 /* make sure double remove sees this as being gone */
5100 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005101 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005102 if (!done) {
5103 /* use wait func handler, so it matches the rq type */
5104 poll->wait.func(&poll->wait, mode, sync, key);
5105 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005106 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005107 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005108 return 1;
5109}
5110
5111static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5112 wait_queue_func_t wake_func)
5113{
5114 poll->head = NULL;
5115 poll->done = false;
5116 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005117#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5118 /* mask in events that we always want/need */
5119 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005120 INIT_LIST_HEAD(&poll->wait.entry);
5121 init_waitqueue_func_entry(&poll->wait, wake_func);
5122}
5123
5124static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005125 struct wait_queue_head *head,
5126 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005127{
5128 struct io_kiocb *req = pt->req;
5129
5130 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005131 * The file being polled uses multiple waitqueues for poll handling
5132 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5133 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005134 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005135 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005136 struct io_poll_iocb *poll_one = poll;
5137
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005138 /* double add on the same waitqueue head, ignore */
5139 if (poll_one->head == head)
5140 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005141 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005142 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005143 if ((*poll_ptr)->head == head)
5144 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005145 pt->error = -EINVAL;
5146 return;
5147 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005148 /*
5149 * Can't handle multishot for double wait for now, turn it
5150 * into one-shot mode.
5151 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005152 if (!(poll_one->events & EPOLLONESHOT))
5153 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005154 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5155 if (!poll) {
5156 pt->error = -ENOMEM;
5157 return;
5158 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005159 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005160 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005161 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005162 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005163 }
5164
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005165 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005166 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005167
5168 if (poll->events & EPOLLEXCLUSIVE)
5169 add_wait_queue_exclusive(head, &poll->wait);
5170 else
5171 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005172}
5173
5174static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5175 struct poll_table_struct *p)
5176{
5177 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005178 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005179
Jens Axboe807abcb2020-07-17 17:09:27 -06005180 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005181}
5182
Pavel Begunkovf237c302021-08-18 12:42:46 +01005183static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005184{
Jens Axboed7718a92020-02-14 22:23:12 -07005185 struct async_poll *apoll = req->apoll;
5186 struct io_ring_ctx *ctx = req->ctx;
5187
Olivier Langlois236daeae2021-05-31 02:36:37 -04005188 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005189
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005190 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005191 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005192 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005193 }
5194
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005195 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005196 io_poll_remove_double(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005197 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005198
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005199 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005200 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005201 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005202 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005203}
5204
5205static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5206 void *key)
5207{
5208 struct io_kiocb *req = wait->private;
5209 struct io_poll_iocb *poll = &req->apoll->poll;
5210
5211 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5212 key_to_poll(key));
5213
5214 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5215}
5216
5217static void io_poll_req_insert(struct io_kiocb *req)
5218{
5219 struct io_ring_ctx *ctx = req->ctx;
5220 struct hlist_head *list;
5221
5222 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5223 hlist_add_head(&req->hash_node, list);
5224}
5225
5226static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5227 struct io_poll_iocb *poll,
5228 struct io_poll_table *ipt, __poll_t mask,
5229 wait_queue_func_t wake_func)
5230 __acquires(&ctx->completion_lock)
5231{
5232 struct io_ring_ctx *ctx = req->ctx;
5233 bool cancel = false;
5234
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005235 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005236 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005237 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005238 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005239
5240 ipt->pt._key = mask;
5241 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005242 ipt->error = 0;
5243 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005244
Jens Axboed7718a92020-02-14 22:23:12 -07005245 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005246 if (unlikely(!ipt->nr_entries) && !ipt->error)
5247 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005248
Jens Axboe79ebeae2021-08-10 15:18:27 -06005249 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005250 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005251 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005252 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005253 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005254 if (unlikely(list_empty(&poll->wait.entry))) {
5255 if (ipt->error)
5256 cancel = true;
5257 ipt->error = 0;
5258 mask = 0;
5259 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005260 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005261 list_del_init(&poll->wait.entry);
5262 else if (cancel)
5263 WRITE_ONCE(poll->canceled, true);
5264 else if (!poll->done) /* actually waiting for an event */
5265 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005266 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005267 }
5268
5269 return mask;
5270}
5271
Olivier Langlois59b735a2021-06-22 05:17:39 -07005272enum {
5273 IO_APOLL_OK,
5274 IO_APOLL_ABORTED,
5275 IO_APOLL_READY
5276};
5277
5278static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005279{
5280 const struct io_op_def *def = &io_op_defs[req->opcode];
5281 struct io_ring_ctx *ctx = req->ctx;
5282 struct async_poll *apoll;
5283 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005284 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005285 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005286
5287 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005288 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005289 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005290 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005291 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005292 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005293
5294 if (def->pollin) {
5295 rw = READ;
5296 mask |= POLLIN | POLLRDNORM;
5297
5298 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5299 if ((req->opcode == IORING_OP_RECVMSG) &&
5300 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5301 mask &= ~POLLIN;
5302 } else {
5303 rw = WRITE;
5304 mask |= POLLOUT | POLLWRNORM;
5305 }
5306
Jens Axboe9dab14b2020-08-25 12:27:50 -06005307 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005308 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005309 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005310
5311 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5312 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005313 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005314 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005315 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005316 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005317 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005318 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005319
5320 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5321 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005322 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005323 if (ret || ipt.error)
5324 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5325
Olivier Langlois236daeae2021-05-31 02:36:37 -04005326 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5327 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005328 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005329}
5330
5331static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005332 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005333 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005334{
Jens Axboeb41e9852020-02-17 09:52:41 -07005335 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005336
Jens Axboe50826202021-02-23 09:02:26 -07005337 if (!poll->head)
5338 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005339 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005340 if (do_cancel)
5341 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005342 if (!list_empty(&poll->wait.entry)) {
5343 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005344 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005345 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005346 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005347 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005348 return do_complete;
5349}
5350
Pavel Begunkov5d709042021-08-09 20:18:13 +01005351static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005352 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005353{
5354 bool do_complete;
5355
Jens Axboed4e7cd32020-08-15 11:44:50 -07005356 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005357 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005358
Jens Axboeb41e9852020-02-17 09:52:41 -07005359 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005360 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005361 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005362 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005363 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005364 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005365 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005366}
5367
Jens Axboe76e1b642020-09-26 15:05:03 -06005368/*
5369 * Returns true if we found and killed one or more poll requests
5370 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005371static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005372 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005373{
Jens Axboe78076bb2019-12-04 19:56:40 -07005374 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005375 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005376 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005377
Jens Axboe79ebeae2021-08-10 15:18:27 -06005378 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005379 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5380 struct hlist_head *list;
5381
5382 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005383 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005384 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005385 posted += io_poll_remove_one(req);
5386 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005387 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005388 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005389
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005390 if (posted)
5391 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005392
5393 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005394}
5395
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005396static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5397 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005398 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005399{
Jens Axboe78076bb2019-12-04 19:56:40 -07005400 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005401 struct io_kiocb *req;
5402
Jens Axboe78076bb2019-12-04 19:56:40 -07005403 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5404 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005405 if (sqe_addr != req->user_data)
5406 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005407 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5408 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005409 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005410 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005411 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005412}
5413
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005414static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5415 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005416 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005417{
5418 struct io_kiocb *req;
5419
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005420 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005421 if (!req)
5422 return -ENOENT;
5423 if (io_poll_remove_one(req))
5424 return 0;
5425
5426 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005427}
5428
Pavel Begunkov9096af32021-04-14 13:38:36 +01005429static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5430 unsigned int flags)
5431{
5432 u32 events;
5433
5434 events = READ_ONCE(sqe->poll32_events);
5435#ifdef __BIG_ENDIAN
5436 events = swahw32(events);
5437#endif
5438 if (!(flags & IORING_POLL_ADD_MULTI))
5439 events |= EPOLLONESHOT;
5440 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5441}
5442
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005443static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005444 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005445{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005446 struct io_poll_update *upd = &req->poll_update;
5447 u32 flags;
5448
Jens Axboe221c5eb2019-01-17 09:41:58 -07005449 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5450 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005451 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005452 return -EINVAL;
5453 flags = READ_ONCE(sqe->len);
5454 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5455 IORING_POLL_ADD_MULTI))
5456 return -EINVAL;
5457 /* meaningless without update */
5458 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005459 return -EINVAL;
5460
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005461 upd->old_user_data = READ_ONCE(sqe->addr);
5462 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5463 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005464
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005465 upd->new_user_data = READ_ONCE(sqe->off);
5466 if (!upd->update_user_data && upd->new_user_data)
5467 return -EINVAL;
5468 if (upd->update_events)
5469 upd->events = io_poll_parse_events(sqe, flags);
5470 else if (sqe->poll32_events)
5471 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005472
Jens Axboe221c5eb2019-01-17 09:41:58 -07005473 return 0;
5474}
5475
Jens Axboe221c5eb2019-01-17 09:41:58 -07005476static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5477 void *key)
5478{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005479 struct io_kiocb *req = wait->private;
5480 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005481
Jens Axboed7718a92020-02-14 22:23:12 -07005482 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005483}
5484
Jens Axboe221c5eb2019-01-17 09:41:58 -07005485static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5486 struct poll_table_struct *p)
5487{
5488 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5489
Jens Axboee8c2bc12020-08-15 18:44:09 -07005490 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005491}
5492
Jens Axboe3529d8c2019-12-19 18:24:38 -07005493static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005494{
5495 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005496 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005497
5498 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5499 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005500 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005501 return -EINVAL;
5502 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005503 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005504 return -EINVAL;
5505
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005506 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005507 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005508 return 0;
5509}
5510
Pavel Begunkov61e98202021-02-10 00:03:08 +00005511static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005512{
5513 struct io_poll_iocb *poll = &req->poll;
5514 struct io_ring_ctx *ctx = req->ctx;
5515 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005516 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005517
Jens Axboed7718a92020-02-14 22:23:12 -07005518 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005519
Jens Axboed7718a92020-02-14 22:23:12 -07005520 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5521 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005522
Jens Axboe8c838782019-03-12 15:48:16 -06005523 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005524 ipt.error = 0;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005525 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005526 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005527 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005528
Jens Axboe8c838782019-03-12 15:48:16 -06005529 if (mask) {
5530 io_cqring_ev_posted(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005531 if (poll->events & EPOLLONESHOT)
5532 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005533 }
Jens Axboe8c838782019-03-12 15:48:16 -06005534 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005535}
5536
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005537static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005538{
5539 struct io_ring_ctx *ctx = req->ctx;
5540 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005541 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005542 int ret;
5543
Jens Axboe79ebeae2021-08-10 15:18:27 -06005544 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005545 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005546 if (!preq) {
5547 ret = -ENOENT;
5548 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005549 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005550
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005551 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5552 completing = true;
5553 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5554 goto err;
5555 }
5556
Jens Axboecb3b200e2021-04-06 09:49:31 -06005557 /*
5558 * Don't allow racy completion with singleshot, as we cannot safely
5559 * update those. For multishot, if we're racing with completion, just
5560 * let completion re-add it.
5561 */
5562 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5563 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5564 ret = -EALREADY;
5565 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005566 }
5567 /* we now have a detached poll request. reissue. */
5568 ret = 0;
5569err:
Jens Axboeb69de282021-03-17 08:37:41 -06005570 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005571 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005572 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005573 io_req_complete(req, ret);
5574 return 0;
5575 }
5576 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005577 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005578 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005579 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005580 preq->poll.events |= IO_POLL_UNMASK;
5581 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005582 if (req->poll_update.update_user_data)
5583 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005584 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005585
Jens Axboeb69de282021-03-17 08:37:41 -06005586 /* complete update request, we're done with it */
5587 io_req_complete(req, ret);
5588
Jens Axboecb3b200e2021-04-06 09:49:31 -06005589 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005590 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005591 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005592 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005593 io_req_complete(preq, ret);
5594 }
Jens Axboeb69de282021-03-17 08:37:41 -06005595 }
5596 return 0;
5597}
5598
Pavel Begunkovf237c302021-08-18 12:42:46 +01005599static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005600{
Jens Axboe89850fc2021-08-10 15:11:51 -06005601 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005602 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005603}
5604
Jens Axboe5262f562019-09-17 12:26:57 -06005605static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5606{
Jens Axboead8a48a2019-11-15 08:49:11 -07005607 struct io_timeout_data *data = container_of(timer,
5608 struct io_timeout_data, timer);
5609 struct io_kiocb *req = data->req;
5610 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005611 unsigned long flags;
5612
Jens Axboe89850fc2021-08-10 15:11:51 -06005613 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005614 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005615 atomic_set(&req->ctx->cq_timeouts,
5616 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005617 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005618
Jens Axboe89850fc2021-08-10 15:11:51 -06005619 req->io_task_work.func = io_req_task_timeout;
5620 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005621 return HRTIMER_NORESTART;
5622}
5623
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005624static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5625 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005626 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005627{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005628 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005629 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005630 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005631
5632 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005633 found = user_data == req->user_data;
5634 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005635 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005636 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005637 if (!found)
5638 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005639
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005640 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005641 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005642 return ERR_PTR(-EALREADY);
5643 list_del_init(&req->timeout.list);
5644 return req;
5645}
5646
5647static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005648 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005649 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005650{
5651 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5652
5653 if (IS_ERR(req))
5654 return PTR_ERR(req);
5655
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005656 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005657 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005658 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005659 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005660}
5661
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005662static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5663 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06005664 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005665{
5666 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5667 struct io_timeout_data *data;
5668
5669 if (IS_ERR(req))
5670 return PTR_ERR(req);
5671
5672 req->timeout.off = 0; /* noseq */
5673 data = req->async_data;
5674 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5675 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5676 data->timer.function = io_timeout_fn;
5677 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5678 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005679}
5680
Jens Axboe3529d8c2019-12-19 18:24:38 -07005681static int io_timeout_remove_prep(struct io_kiocb *req,
5682 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005683{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005684 struct io_timeout_rem *tr = &req->timeout_rem;
5685
Jens Axboeb29472e2019-12-17 18:50:29 -07005686 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5687 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005688 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5689 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005690 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07005691 return -EINVAL;
5692
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005693 tr->addr = READ_ONCE(sqe->addr);
5694 tr->flags = READ_ONCE(sqe->timeout_flags);
5695 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5696 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5697 return -EINVAL;
5698 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5699 return -EFAULT;
5700 } else if (tr->flags) {
5701 /* timeout removal doesn't support flags */
5702 return -EINVAL;
5703 }
5704
Jens Axboeb29472e2019-12-17 18:50:29 -07005705 return 0;
5706}
5707
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005708static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5709{
5710 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5711 : HRTIMER_MODE_REL;
5712}
5713
Jens Axboe11365042019-10-16 09:08:32 -06005714/*
5715 * Remove or update an existing timeout command
5716 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005717static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005718{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005719 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005720 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005721 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005722
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005723 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
5724 spin_lock(&ctx->completion_lock);
5725 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005726 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005727 spin_unlock_irq(&ctx->timeout_lock);
5728 spin_unlock(&ctx->completion_lock);
5729 } else {
5730 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005731 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5732 io_translate_timeout_mode(tr->flags));
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005733 spin_unlock_irq(&ctx->timeout_lock);
5734 }
Jens Axboe11365042019-10-16 09:08:32 -06005735
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005736 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005737 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005738 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06005739 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005740}
5741
Jens Axboe3529d8c2019-12-19 18:24:38 -07005742static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005743 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005744{
Jens Axboead8a48a2019-11-15 08:49:11 -07005745 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005746 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005747 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005748
Jens Axboead8a48a2019-11-15 08:49:11 -07005749 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005750 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005751 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
5752 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06005753 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005754 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005755 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005756 flags = READ_ONCE(sqe->timeout_flags);
5757 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005758 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005759
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005760 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01005761 if (unlikely(off && !req->ctx->off_timeout_used))
5762 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07005763
Jens Axboee8c2bc12020-08-15 18:44:09 -07005764 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005765 return -ENOMEM;
5766
Jens Axboee8c2bc12020-08-15 18:44:09 -07005767 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005768 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005769
5770 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005771 return -EFAULT;
5772
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005773 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005774 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005775
5776 if (is_timeout_link) {
5777 struct io_submit_link *link = &req->ctx->submit_state.link;
5778
5779 if (!link->head)
5780 return -EINVAL;
5781 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
5782 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01005783 req->timeout.head = link->last;
5784 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005785 }
Jens Axboead8a48a2019-11-15 08:49:11 -07005786 return 0;
5787}
5788
Pavel Begunkov61e98202021-02-10 00:03:08 +00005789static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005790{
Jens Axboead8a48a2019-11-15 08:49:11 -07005791 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005792 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005793 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005794 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005795
Jens Axboe89850fc2021-08-10 15:11:51 -06005796 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005797
Jens Axboe5262f562019-09-17 12:26:57 -06005798 /*
5799 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005800 * timeout event to be satisfied. If it isn't set, then this is
5801 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005802 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005803 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005804 entry = ctx->timeout_list.prev;
5805 goto add;
5806 }
Jens Axboe5262f562019-09-17 12:26:57 -06005807
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005808 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5809 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005810
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005811 /* Update the last seq here in case io_flush_timeouts() hasn't.
5812 * This is safe because ->completion_lock is held, and submissions
5813 * and completions are never mixed in the same ->completion_lock section.
5814 */
5815 ctx->cq_last_tm_flush = tail;
5816
Jens Axboe5262f562019-09-17 12:26:57 -06005817 /*
5818 * Insertion sort, ensuring the first entry in the list is always
5819 * the one we need first.
5820 */
Jens Axboe5262f562019-09-17 12:26:57 -06005821 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005822 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5823 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005824
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005825 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005826 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005827 /* nxt.seq is behind @tail, otherwise would've been completed */
5828 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005829 break;
5830 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005831add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005832 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005833 data->timer.function = io_timeout_fn;
5834 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06005835 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005836 return 0;
5837}
5838
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005839struct io_cancel_data {
5840 struct io_ring_ctx *ctx;
5841 u64 user_data;
5842};
5843
Jens Axboe62755e32019-10-28 21:49:21 -06005844static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005845{
Jens Axboe62755e32019-10-28 21:49:21 -06005846 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005847 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06005848
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005849 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06005850}
5851
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005852static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
5853 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06005854{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005855 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06005856 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005857 int ret = 0;
5858
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005859 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005860 return -ENOENT;
5861
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005862 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005863 switch (cancel_ret) {
5864 case IO_WQ_CANCEL_OK:
5865 ret = 0;
5866 break;
5867 case IO_WQ_CANCEL_RUNNING:
5868 ret = -EALREADY;
5869 break;
5870 case IO_WQ_CANCEL_NOTFOUND:
5871 ret = -ENOENT;
5872 break;
5873 }
5874
Jens Axboee977d6d2019-11-05 12:39:45 -07005875 return ret;
5876}
5877
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005878static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07005879{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005881 int ret;
5882
Pavel Begunkovdadebc32021-08-23 13:30:44 +01005883 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005884
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005885 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01005886 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005887 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01005888
5889 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005890 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005891 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005892 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005893 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01005894 goto out;
5895 ret = io_poll_cancel(ctx, sqe_addr, false);
5896out:
5897 spin_unlock(&ctx->completion_lock);
5898 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005899}
5900
Jens Axboe3529d8c2019-12-19 18:24:38 -07005901static int io_async_cancel_prep(struct io_kiocb *req,
5902 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005903{
Jens Axboefbf23842019-12-17 18:45:56 -07005904 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005905 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005906 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5907 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005908 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
5909 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07005910 return -EINVAL;
5911
Jens Axboefbf23842019-12-17 18:45:56 -07005912 req->cancel.addr = READ_ONCE(sqe->addr);
5913 return 0;
5914}
5915
Pavel Begunkov61e98202021-02-10 00:03:08 +00005916static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005917{
5918 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005919 u64 sqe_addr = req->cancel.addr;
5920 struct io_tctx_node *node;
5921 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07005922
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005923 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00005924 if (ret != -ENOENT)
5925 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005926
5927 /* slow path, try all io-wq's */
5928 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
5929 ret = -ENOENT;
5930 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
5931 struct io_uring_task *tctx = node->task->io_uring;
5932
Pavel Begunkov58f99372021-03-12 16:25:55 +00005933 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
5934 if (ret != -ENOENT)
5935 break;
5936 }
5937 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00005938done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00005939 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005940 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005941 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005942 return 0;
5943}
5944
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005945static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005946 const struct io_uring_sqe *sqe)
5947{
Daniele Albano61710e42020-07-18 14:15:16 -06005948 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5949 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005950 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005951 return -EINVAL;
5952
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005953 req->rsrc_update.offset = READ_ONCE(sqe->off);
5954 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5955 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005956 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005957 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005958 return 0;
5959}
5960
Pavel Begunkov889fca72021-02-10 00:03:09 +00005961static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005962{
5963 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005964 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005965 int ret;
5966
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005967 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005968 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005969
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005970 up.offset = req->rsrc_update.offset;
5971 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005972 up.nr = 0;
5973 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01005974 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005975
5976 mutex_lock(&ctx->uring_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01005977 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01005978 &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005979 mutex_unlock(&ctx->uring_lock);
5980
5981 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005982 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005983 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005984 return 0;
5985}
5986
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005987static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005988{
Jens Axboed625c6e2019-12-17 19:53:05 -07005989 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005990 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005991 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005992 case IORING_OP_READV:
5993 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005994 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005995 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005996 case IORING_OP_WRITEV:
5997 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005998 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005999 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006000 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006001 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006002 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006003 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006004 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006005 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006006 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006007 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006008 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006009 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006010 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006011 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006012 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006013 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006014 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006015 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006016 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006017 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006018 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006019 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006020 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006021 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006022 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006023 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006024 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006025 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006026 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006027 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006028 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006029 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006030 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006031 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006032 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006033 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006034 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006035 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006036 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006037 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006038 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006039 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006040 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006041 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006042 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006043 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006044 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006045 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006046 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006047 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006048 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006049 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006050 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006051 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006052 case IORING_OP_SHUTDOWN:
6053 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006054 case IORING_OP_RENAMEAT:
6055 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006056 case IORING_OP_UNLINKAT:
6057 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006058 }
6059
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006060 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6061 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006062 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006063}
6064
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006065static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006066{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006067 if (!io_op_defs[req->opcode].needs_async_setup)
6068 return 0;
6069 if (WARN_ON_ONCE(req->async_data))
6070 return -EFAULT;
6071 if (io_alloc_async_data(req))
6072 return -EAGAIN;
6073
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006074 switch (req->opcode) {
6075 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006076 return io_rw_prep_async(req, READ);
6077 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006078 return io_rw_prep_async(req, WRITE);
6079 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006080 return io_sendmsg_prep_async(req);
6081 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006082 return io_recvmsg_prep_async(req);
6083 case IORING_OP_CONNECT:
6084 return io_connect_prep_async(req);
6085 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006086 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6087 req->opcode);
6088 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006089}
6090
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006091static u32 io_get_sequence(struct io_kiocb *req)
6092{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006093 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006094
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006095 /* need original cached_sq_head, but it was increased for each req */
6096 io_for_each_link(req, req)
6097 seq--;
6098 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006099}
6100
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006101static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006102{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006103 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006104 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006105 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006106 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006107 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006108
Pavel Begunkov3c199662021-06-15 16:47:57 +01006109 /*
6110 * If we need to drain a request in the middle of a link, drain the
6111 * head request and the next request/link after the current link.
6112 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6113 * maintained for every request of our link.
6114 */
6115 if (ctx->drain_next) {
6116 req->flags |= REQ_F_IO_DRAIN;
6117 ctx->drain_next = false;
6118 }
6119 /* not interested in head, start from the first linked */
6120 io_for_each_link(pos, req->link) {
6121 if (pos->flags & REQ_F_IO_DRAIN) {
6122 ctx->drain_next = true;
6123 req->flags |= REQ_F_IO_DRAIN;
6124 break;
6125 }
6126 }
6127
Jens Axboedef596e2019-01-09 08:59:42 -07006128 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006129 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006130 !(req->flags & REQ_F_IO_DRAIN))) {
6131 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006132 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006133 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006134
6135 seq = io_get_sequence(req);
6136 /* Still a chance to pass the sequence check */
6137 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006138 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006139
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006140 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006141 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006142 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006143 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006144 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006145 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006146 ret = -ENOMEM;
6147fail:
6148 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006149 return true;
6150 }
Jens Axboe31b51512019-01-18 22:56:34 -07006151
Jens Axboe79ebeae2021-08-10 15:18:27 -06006152 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006153 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006154 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006155 kfree(de);
Pavel Begunkovf237c302021-08-18 12:42:46 +01006156 io_queue_async_work(req, NULL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006157 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006158 }
6159
6160 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006161 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006162 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006163 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006164 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006165 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006166}
6167
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006168static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006169{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006170 if (req->flags & REQ_F_BUFFER_SELECTED) {
6171 switch (req->opcode) {
6172 case IORING_OP_READV:
6173 case IORING_OP_READ_FIXED:
6174 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006175 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006176 break;
6177 case IORING_OP_RECVMSG:
6178 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006179 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006180 break;
6181 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006182 }
6183
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006184 if (req->flags & REQ_F_NEED_CLEANUP) {
6185 switch (req->opcode) {
6186 case IORING_OP_READV:
6187 case IORING_OP_READ_FIXED:
6188 case IORING_OP_READ:
6189 case IORING_OP_WRITEV:
6190 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006191 case IORING_OP_WRITE: {
6192 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006193
6194 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006195 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006196 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006197 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006198 case IORING_OP_SENDMSG: {
6199 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006200
6201 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006202 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006203 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006204 case IORING_OP_SPLICE:
6205 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006206 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6207 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006208 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006209 case IORING_OP_OPENAT:
6210 case IORING_OP_OPENAT2:
6211 if (req->open.filename)
6212 putname(req->open.filename);
6213 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006214 case IORING_OP_RENAMEAT:
6215 putname(req->rename.oldpath);
6216 putname(req->rename.newpath);
6217 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006218 case IORING_OP_UNLINKAT:
6219 putname(req->unlink.filename);
6220 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006221 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006222 }
Jens Axboe75652a302021-04-15 09:52:40 -06006223 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6224 kfree(req->apoll->double_poll);
6225 kfree(req->apoll);
6226 req->apoll = NULL;
6227 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006228 if (req->flags & REQ_F_INFLIGHT) {
6229 struct io_uring_task *tctx = req->task->io_uring;
6230
6231 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006232 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006233 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006234 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006235
6236 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006237}
6238
Pavel Begunkov889fca72021-02-10 00:03:09 +00006239static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006240{
Jens Axboeedafcce2019-01-09 09:16:05 -07006241 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006242 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006243 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006244
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006245 if ((req->flags & REQ_F_CREDS) && req->creds != current_cred())
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006246 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006247
Jens Axboed625c6e2019-12-17 19:53:05 -07006248 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006250 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006251 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006253 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006254 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006255 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006256 break;
6257 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006258 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006259 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006260 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006261 break;
6262 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006263 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006264 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006266 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006267 break;
6268 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006269 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006270 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006271 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006272 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006273 break;
6274 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006275 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006276 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006277 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006278 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006279 break;
6280 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006281 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006282 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006283 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006284 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006285 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006286 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006287 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006288 break;
6289 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006290 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291 break;
6292 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006293 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006294 break;
6295 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006296 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006297 break;
6298 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006299 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006301 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006302 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006303 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006304 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006305 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006306 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006307 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006308 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006309 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006310 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006311 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006312 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006313 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006314 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006315 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006316 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006317 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006318 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006319 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006320 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006321 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006322 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006323 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006324 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006325 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006326 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006327 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006328 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006329 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006330 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006331 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006332 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006333 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006334 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006335 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006336 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006337 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006338 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006339 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006340 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006341 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006342 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006343 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006344 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006345 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006346 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006347 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006348 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006349 default:
6350 ret = -EINVAL;
6351 break;
6352 }
Jens Axboe31b51512019-01-18 22:56:34 -07006353
Jens Axboe5730b272021-02-27 15:57:30 -07006354 if (creds)
6355 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356 if (ret)
6357 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006358 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006359 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6360 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006361
6362 return 0;
6363}
6364
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006365static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6366{
6367 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6368
6369 req = io_put_req_find_next(req);
6370 return req ? &req->work : NULL;
6371}
6372
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006373static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006374{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006375 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006376 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006377 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006379 /* one will be dropped by ->io_free_work() after returning to io-wq */
6380 if (!(req->flags & REQ_F_REFCOUNT))
6381 __io_req_set_refcount(req, 2);
6382 else
6383 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006384
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006385 timeout = io_prep_linked_timeout(req);
6386 if (timeout)
6387 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006388
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006389 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006390 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006391 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006392
Jens Axboe561fb042019-10-24 07:25:42 -06006393 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006394 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006395 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006396 /*
6397 * We can get EAGAIN for polled IO even though we're
6398 * forcing a sync submission from here, since we can't
6399 * wait for request slots on the block side.
6400 */
6401 if (ret != -EAGAIN)
6402 break;
6403 cond_resched();
6404 } while (1);
6405 }
Jens Axboe31b51512019-01-18 22:56:34 -07006406
Pavel Begunkova3df76982021-02-18 22:32:52 +00006407 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006408 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006409 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006410}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006411
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006412static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006413 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006414{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006415 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006416}
6417
Jens Axboe09bb8392019-03-13 12:39:28 -06006418static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6419 int index)
6420{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006421 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006422
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006423 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006424}
6425
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006426static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006427{
6428 unsigned long file_ptr = (unsigned long) file;
6429
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006430 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006431 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006432 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006433 file_ptr |= FFS_ASYNC_WRITE;
6434 if (S_ISREG(file_inode(file)->i_mode))
6435 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006436 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006437}
6438
Pavel Begunkovac177052021-08-09 13:04:02 +01006439static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6440 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006441{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006442 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006443 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006444
Pavel Begunkovac177052021-08-09 13:04:02 +01006445 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6446 return NULL;
6447 fd = array_index_nospec(fd, ctx->nr_user_files);
6448 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6449 file = (struct file *) (file_ptr & FFS_MASK);
6450 file_ptr &= ~FFS_MASK;
6451 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006452 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006453 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006454 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006455}
6456
Pavel Begunkovac177052021-08-09 13:04:02 +01006457static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006458 struct io_kiocb *req, int fd)
6459{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006460 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006461
6462 trace_io_uring_file_get(ctx, fd);
6463
6464 /* we don't allow fixed io_uring files */
6465 if (file && unlikely(file->f_op == &io_uring_fops))
6466 io_req_track_inflight(req);
6467 return file;
6468}
6469
6470static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006471 struct io_kiocb *req, int fd, bool fixed)
6472{
6473 if (fixed)
6474 return io_file_get_fixed(ctx, req, fd);
6475 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006476 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006477}
6478
Pavel Begunkovf237c302021-08-18 12:42:46 +01006479static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006480{
6481 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006482 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006483
6484 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006485 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006486 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006487 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006488 } else {
6489 io_req_complete_post(req, -ETIME, 0);
6490 }
6491}
6492
Jens Axboe2665abf2019-11-05 12:40:47 -07006493static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6494{
Jens Axboead8a48a2019-11-15 08:49:11 -07006495 struct io_timeout_data *data = container_of(timer,
6496 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006497 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006498 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006499 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006500
Jens Axboe89b263f2021-08-10 15:14:18 -06006501 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006502 prev = req->timeout.head;
6503 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006504
6505 /*
6506 * We don't expect the list to be empty, that will only happen if we
6507 * race with the completion of the linked work.
6508 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006509 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006510 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006511 if (!req_ref_inc_not_zero(prev))
6512 prev = NULL;
6513 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006514 req->timeout.prev = prev;
6515 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006516
Jens Axboe89b263f2021-08-10 15:14:18 -06006517 req->io_task_work.func = io_req_task_link_timeout;
6518 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006519 return HRTIMER_NORESTART;
6520}
6521
Pavel Begunkovde968c12021-03-19 17:22:33 +00006522static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006523{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006524 struct io_ring_ctx *ctx = req->ctx;
6525
Jens Axboe89b263f2021-08-10 15:14:18 -06006526 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006527 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006528 * If the back reference is NULL, then our linked request finished
6529 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006530 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006531 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006532 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006533
Jens Axboead8a48a2019-11-15 08:49:11 -07006534 data->timer.function = io_link_timeout_fn;
6535 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6536 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006537 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006538 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006539 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006540 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006541}
6542
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006543static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006544 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006545{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006546 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006547 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006548
Olivier Langlois59b735a2021-06-22 05:17:39 -07006549issue_sqe:
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006550 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006551
6552 /*
6553 * We async punt it if the file wasn't marked NOWAIT, or if the file
6554 * doesn't support non-blocking read/write attempts
6555 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006556 if (likely(!ret)) {
Pavel Begunkove342c802021-01-19 13:32:47 +00006557 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006558 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006559 struct io_submit_state *state = &ctx->submit_state;
Jens Axboee65ef562019-03-12 10:16:44 -06006560
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006561 state->compl_reqs[state->compl_nr++] = req;
6562 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006563 io_submit_flush_completions(ctx);
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006564 return;
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006565 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006566
6567 linked_timeout = io_prep_linked_timeout(req);
6568 if (linked_timeout)
6569 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006570 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006571 linked_timeout = io_prep_linked_timeout(req);
6572
Olivier Langlois59b735a2021-06-22 05:17:39 -07006573 switch (io_arm_poll_handler(req)) {
6574 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006575 if (linked_timeout)
6576 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006577 goto issue_sqe;
6578 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006579 /*
6580 * Queued up for async execution, worker will release
6581 * submit reference when the iocb is actually submitted.
6582 */
Pavel Begunkovf237c302021-08-18 12:42:46 +01006583 io_queue_async_work(req, NULL);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006584 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006585 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006586
6587 if (linked_timeout)
6588 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006589 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006590 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006591 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006592}
6593
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006594static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006595 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006596{
Pavel Begunkov10c66902021-06-15 16:47:56 +01006597 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006598 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08006599
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006600 if (likely(!(req->flags & REQ_F_FORCE_ASYNC))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006601 __io_queue_sqe(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006602 } else {
6603 int ret = io_req_prep_async(req);
6604
6605 if (unlikely(ret))
6606 io_req_complete_failed(req, ret);
6607 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01006608 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07006609 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006610}
6611
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006612/*
6613 * Check SQE restrictions (opcode and flags).
6614 *
6615 * Returns 'true' if SQE is allowed, 'false' otherwise.
6616 */
6617static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6618 struct io_kiocb *req,
6619 unsigned int sqe_flags)
6620{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006621 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006622 return true;
6623
6624 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6625 return false;
6626
6627 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6628 ctx->restrictions.sqe_flags_required)
6629 return false;
6630
6631 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6632 ctx->restrictions.sqe_flags_required))
6633 return false;
6634
6635 return true;
6636}
6637
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006638static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006639 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006640 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006641{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006642 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006643 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006644 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006645
Pavel Begunkov864ea922021-08-09 13:04:08 +01006646 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006647 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006648 /* same numerical values with corresponding REQ_F_*, safe to copy */
6649 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006650 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006651 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006652 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006653 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006654
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006655 /* enforce forwards compatibility on users */
Pavel Begunkovdddca222021-04-27 16:13:52 +01006656 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006657 return -EINVAL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006658 if (unlikely(req->opcode >= IORING_OP_LAST))
6659 return -EINVAL;
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006660 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006661 return -EACCES;
6662
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006663 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6664 !io_op_defs[req->opcode].buffer_select)
6665 return -EOPNOTSUPP;
Pavel Begunkov3c199662021-06-15 16:47:57 +01006666 if (unlikely(sqe_flags & IOSQE_IO_DRAIN))
6667 ctx->drain_active = true;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006668
Jens Axboe003e8dc2021-03-06 09:22:27 -07006669 personality = READ_ONCE(sqe->personality);
6670 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006671 req->creds = xa_load(&ctx->personalities, personality);
6672 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07006673 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006674 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006675 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006676 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006677 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006678
Jens Axboe27926b62020-10-28 09:33:23 -06006679 /*
6680 * Plug now if we have more than 1 IO left after this, and the target
6681 * is potentially a read/write to block based storage.
6682 */
6683 if (!state->plug_started && state->ios_left > 1 &&
6684 io_op_defs[req->opcode].plug) {
6685 blk_start_plug(&state->plug);
6686 state->plug_started = true;
6687 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006688
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006689 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01006690 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01006691 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00006692 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006693 ret = -EBADF;
6694 }
6695
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006696 state->ios_left--;
6697 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006698}
6699
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006700static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006701 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006702 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006703{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006704 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006705 int ret;
6706
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006707 ret = io_init_req(ctx, req, sqe);
6708 if (unlikely(ret)) {
6709fail_req:
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006710 if (link->head) {
6711 /* fail even hard links since we don't submit */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006712 req_set_fail(link->head);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006713 io_req_complete_failed(link->head, -ECANCELED);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006714 link->head = NULL;
6715 }
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006716 io_req_complete_failed(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006717 return ret;
6718 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006719
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006720 ret = io_req_prep(req, sqe);
6721 if (unlikely(ret))
6722 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006723
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006724 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04006725 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
6726 req->flags, true,
6727 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006728
Jens Axboe6c271ce2019-01-10 11:22:30 -07006729 /*
6730 * If we already have a head request, queue this one for async
6731 * submittal once the head completes. If we don't have a head but
6732 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6733 * submitted sync once the chain is complete. If none of those
6734 * conditions are true (normal request), then just queue it.
6735 */
6736 if (link->head) {
6737 struct io_kiocb *head = link->head;
6738
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006739 ret = io_req_prep_async(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006740 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006741 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006742 trace_io_uring_link(ctx, req, head);
6743 link->last->link = req;
6744 link->last = req;
6745
6746 /* last request of a link, enqueue the link */
6747 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
6748 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01006749 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006750 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006751 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006752 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006753 link->head = req;
6754 link->last = req;
6755 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006756 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006757 }
6758 }
6759
6760 return 0;
6761}
6762
6763/*
6764 * Batched submission is done, ensure local IO is flushed out.
6765 */
6766static void io_submit_state_end(struct io_submit_state *state,
6767 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006768{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006769 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006770 io_queue_sqe(state->link.head);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006771 if (state->compl_nr)
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006772 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006773 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006774 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06006775}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006776
Jens Axboe9e645e112019-05-10 16:07:28 -06006777/*
6778 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006779 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006780static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006781 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006782{
6783 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006784 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006785 /* set only head, no need to init link_last in advance */
6786 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006787}
6788
Jens Axboe193155c2020-02-22 23:22:19 -07006789static void io_commit_sqring(struct io_ring_ctx *ctx)
6790{
Jens Axboe75c6a032020-01-28 10:15:23 -07006791 struct io_rings *rings = ctx->rings;
6792
6793 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006794 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006795 * since once we write the new head, the application could
6796 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006797 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006798 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006799}
6800
Jens Axboe9e645e112019-05-10 16:07:28 -06006801/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01006802 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006803 * that is mapped by userspace. This means that care needs to be taken to
6804 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006805 * being a good citizen. If members of the sqe are validated and then later
6806 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006807 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006808 */
6809static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006810{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01006811 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006812 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06006813
6814 /*
6815 * The cached sq head (or cq tail) serves two purposes:
6816 *
6817 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006818 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006819 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006820 * though the application is the one updating it.
6821 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006822 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006823 if (likely(head < ctx->sq_entries))
6824 return &ctx->sq_sqes[head];
6825
6826 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01006827 ctx->cq_extra--;
6828 WRITE_ONCE(ctx->rings->sq_dropped,
6829 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03006830 return NULL;
6831}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006832
Jens Axboe0f212202020-09-13 13:09:39 -06006833static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006834 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006835{
Pavel Begunkov09899b12021-06-14 02:36:22 +01006836 struct io_uring_task *tctx;
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006837 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006838
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006839 /* make sure SQ entry isn't read before tail */
6840 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006841 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6842 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006843
Pavel Begunkov09899b12021-06-14 02:36:22 +01006844 tctx = current->io_uring;
6845 tctx->cached_refs -= nr;
6846 if (unlikely(tctx->cached_refs < 0)) {
6847 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
6848
6849 percpu_counter_add(&tctx->inflight, refill);
6850 refcount_add(refill, &current->usage);
6851 tctx->cached_refs += refill;
6852 }
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006853 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006854
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006855 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006856 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006857 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006858
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006859 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006860 if (unlikely(!req)) {
6861 if (!submitted)
6862 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006863 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006864 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006865 sqe = io_get_sqe(ctx);
6866 if (unlikely(!sqe)) {
6867 kmem_cache_free(req_cachep, req);
6868 break;
6869 }
Jens Axboed3656342019-12-18 09:50:26 -07006870 /* will complete beyond this point, count as submitted */
6871 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006872 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006873 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006874 }
6875
Pavel Begunkov9466f432020-01-25 22:34:01 +03006876 if (unlikely(submitted != nr)) {
6877 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006878 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006879
Pavel Begunkov09899b12021-06-14 02:36:22 +01006880 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06006881 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006882 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006883
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006884 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006885 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6886 io_commit_sqring(ctx);
6887
Jens Axboe6c271ce2019-01-10 11:22:30 -07006888 return submitted;
6889}
6890
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006891static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
6892{
6893 return READ_ONCE(sqd->state);
6894}
6895
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006896static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6897{
6898 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06006899 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006900 WRITE_ONCE(ctx->rings->sq_flags,
6901 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006902 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006903}
6904
6905static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6906{
Jens Axboe79ebeae2021-08-10 15:18:27 -06006907 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006908 WRITE_ONCE(ctx->rings->sq_flags,
6909 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006910 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006911}
6912
Xiaoguang Wang08369242020-11-03 14:15:59 +08006913static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006914{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006915 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006916 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006917
Jens Axboec8d1ba52020-09-14 11:07:26 -06006918 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006919 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07006920 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
6921 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06006922
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006923 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6924 unsigned nr_events = 0;
Pavel Begunkov948e1942021-06-24 15:09:55 +01006925 const struct cred *creds = NULL;
6926
6927 if (ctx->sq_creds != current_cred())
6928 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006929
Xiaoguang Wang08369242020-11-03 14:15:59 +08006930 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006931 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkova8576af2021-08-15 10:40:21 +01006932 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006933
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01006934 /*
6935 * Don't submit if refs are dying, good for io_uring_register(),
6936 * but also it is relied upon by io_ring_exit_work()
6937 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00006938 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
6939 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006940 ret = io_submit_sqes(ctx, to_submit);
6941 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006942
Pavel Begunkovacfb3812021-05-16 22:58:03 +01006943 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
6944 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01006945 if (creds)
6946 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01006947 }
Jens Axboe90554202020-09-03 12:12:41 -06006948
Xiaoguang Wang08369242020-11-03 14:15:59 +08006949 return ret;
6950}
6951
6952static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6953{
6954 struct io_ring_ctx *ctx;
6955 unsigned sq_thread_idle = 0;
6956
Pavel Begunkovc9dca272021-03-10 13:13:55 +00006957 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6958 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006959 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006960}
6961
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006962static bool io_sqd_handle_event(struct io_sq_data *sqd)
6963{
6964 bool did_sig = false;
6965 struct ksignal ksig;
6966
6967 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
6968 signal_pending(current)) {
6969 mutex_unlock(&sqd->lock);
6970 if (signal_pending(current))
6971 did_sig = get_signal(&ksig);
6972 cond_resched();
6973 mutex_lock(&sqd->lock);
6974 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006975 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6976}
6977
Jens Axboe6c271ce2019-01-10 11:22:30 -07006978static int io_sq_thread(void *data)
6979{
Jens Axboe69fb2132020-09-14 11:16:23 -06006980 struct io_sq_data *sqd = data;
6981 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006982 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006983 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006984 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006985
Pavel Begunkov696ee882021-04-01 09:55:04 +01006986 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006987 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06006988
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006989 if (sqd->sq_cpu != -1)
6990 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6991 else
6992 set_cpus_allowed_ptr(current, cpu_online_mask);
6993 current->flags |= PF_NO_SETAFFINITY;
6994
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00006995 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006996 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01006997 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07006998
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006999 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7000 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007001 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007002 timeout = jiffies + sqd->sq_thread_idle;
7003 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007004
Jens Axboee95eee22020-09-08 09:11:32 -06007005 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007006 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007007 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007008
Xiaoguang Wang08369242020-11-03 14:15:59 +08007009 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7010 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007011 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007012 if (io_run_task_work())
7013 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007014
Xiaoguang Wang08369242020-11-03 14:15:59 +08007015 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007016 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007017 if (sqt_spin)
7018 timeout = jiffies + sqd->sq_thread_idle;
7019 continue;
7020 }
7021
Xiaoguang Wang08369242020-11-03 14:15:59 +08007022 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007023 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007024 bool needs_sched = true;
7025
Hao Xu724cb4f2021-04-21 23:19:11 +08007026 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007027 io_ring_set_wakeup_flag(ctx);
7028
Hao Xu724cb4f2021-04-21 23:19:11 +08007029 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7030 !list_empty_careful(&ctx->iopoll_list)) {
7031 needs_sched = false;
7032 break;
7033 }
7034 if (io_sqring_entries(ctx)) {
7035 needs_sched = false;
7036 break;
7037 }
7038 }
7039
7040 if (needs_sched) {
7041 mutex_unlock(&sqd->lock);
7042 schedule();
7043 mutex_lock(&sqd->lock);
7044 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007045 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7046 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007047 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007048
7049 finish_wait(&sqd->wait, &wait);
7050 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007051 }
7052
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007053 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007054 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007055 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007056 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007057 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007058 mutex_unlock(&sqd->lock);
7059
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007060 complete(&sqd->exited);
7061 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007062}
7063
Jens Axboebda52162019-09-24 13:47:15 -06007064struct io_wait_queue {
7065 struct wait_queue_entry wq;
7066 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007067 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007068 unsigned nr_timeouts;
7069};
7070
Pavel Begunkov6c503152021-01-04 20:36:36 +00007071static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007072{
7073 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007074 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007075
7076 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007077 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007078 * started waiting. For timeouts, we always want to return to userspace,
7079 * regardless of event count.
7080 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007081 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007082}
7083
7084static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7085 int wake_flags, void *key)
7086{
7087 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7088 wq);
7089
Pavel Begunkov6c503152021-01-04 20:36:36 +00007090 /*
7091 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7092 * the task, and the next invocation will do it.
7093 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007094 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007095 return autoremove_wake_function(curr, mode, wake_flags, key);
7096 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007097}
7098
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007099static int io_run_task_work_sig(void)
7100{
7101 if (io_run_task_work())
7102 return 1;
7103 if (!signal_pending(current))
7104 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007105 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007106 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007107 return -EINTR;
7108}
7109
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007110/* when returns >0, the caller should retry */
7111static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7112 struct io_wait_queue *iowq,
7113 signed long *timeout)
7114{
7115 int ret;
7116
7117 /* make sure we run task_work before checking for signals */
7118 ret = io_run_task_work_sig();
7119 if (ret || io_should_wake(iowq))
7120 return ret;
7121 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007122 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007123 return 1;
7124
7125 *timeout = schedule_timeout(*timeout);
7126 return !*timeout ? -ETIME : 1;
7127}
7128
Jens Axboe2b188cc2019-01-07 10:46:33 -07007129/*
7130 * Wait until events become available, if we don't already have some. The
7131 * application must reap them itself, as they reside on the shared cq ring.
7132 */
7133static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007134 const sigset_t __user *sig, size_t sigsz,
7135 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007136{
Pavel Begunkov902910992021-08-09 09:07:32 -06007137 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007138 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007139 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7140 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007141
Jens Axboeb41e9852020-02-17 09:52:41 -07007142 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007143 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007144 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007145 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007146 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007147 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007148 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007149
7150 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007151#ifdef CONFIG_COMPAT
7152 if (in_compat_syscall())
7153 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007154 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007155 else
7156#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007157 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007158
Jens Axboe2b188cc2019-01-07 10:46:33 -07007159 if (ret)
7160 return ret;
7161 }
7162
Hao Xuc73ebb62020-11-03 10:54:37 +08007163 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007164 struct timespec64 ts;
7165
Hao Xuc73ebb62020-11-03 10:54:37 +08007166 if (get_timespec64(&ts, uts))
7167 return -EFAULT;
7168 timeout = timespec64_to_jiffies(&ts);
7169 }
7170
Pavel Begunkov902910992021-08-09 09:07:32 -06007171 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7172 iowq.wq.private = current;
7173 INIT_LIST_HEAD(&iowq.wq.entry);
7174 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007175 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007176 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007177
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007178 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007179 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007180 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007181 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007182 ret = -EBUSY;
7183 break;
7184 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007185 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007186 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007187 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007188 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007189 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007190 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007191
Jens Axboeb7db41c2020-07-04 08:55:50 -06007192 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007193
Hristo Venev75b28af2019-08-26 17:23:46 +00007194 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007195}
7196
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007197static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007198{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007199 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007200
7201 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007202 kfree(table[i]);
7203 kfree(table);
7204}
7205
7206static void **io_alloc_page_table(size_t size)
7207{
7208 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7209 size_t init_size = size;
7210 void **table;
7211
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007212 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007213 if (!table)
7214 return NULL;
7215
7216 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007217 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007218
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007219 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007220 if (!table[i]) {
7221 io_free_page_table(table, init_size);
7222 return NULL;
7223 }
7224 size -= this_size;
7225 }
7226 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007227}
7228
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007229static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7230{
7231 percpu_ref_exit(&ref_node->refs);
7232 kfree(ref_node);
7233}
7234
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007235static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7236{
7237 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7238 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7239 unsigned long flags;
7240 bool first_add = false;
7241
7242 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7243 node->done = true;
7244
7245 while (!list_empty(&ctx->rsrc_ref_list)) {
7246 node = list_first_entry(&ctx->rsrc_ref_list,
7247 struct io_rsrc_node, node);
7248 /* recycle ref nodes in order */
7249 if (!node->done)
7250 break;
7251 list_del(&node->node);
7252 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7253 }
7254 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7255
7256 if (first_add)
7257 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7258}
7259
7260static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7261{
7262 struct io_rsrc_node *ref_node;
7263
7264 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7265 if (!ref_node)
7266 return NULL;
7267
7268 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7269 0, GFP_KERNEL)) {
7270 kfree(ref_node);
7271 return NULL;
7272 }
7273 INIT_LIST_HEAD(&ref_node->node);
7274 INIT_LIST_HEAD(&ref_node->rsrc_list);
7275 ref_node->done = false;
7276 return ref_node;
7277}
7278
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007279static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7280 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007281{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007282 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7283 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007284
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007285 if (data_to_kill) {
7286 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007287
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007288 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007289 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007290 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007291 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007292
Pavel Begunkov3e942492021-04-11 01:46:34 +01007293 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007294 percpu_ref_kill(&rsrc_node->refs);
7295 ctx->rsrc_node = NULL;
7296 }
7297
7298 if (!ctx->rsrc_node) {
7299 ctx->rsrc_node = ctx->rsrc_backup_node;
7300 ctx->rsrc_backup_node = NULL;
7301 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007302}
7303
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007304static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007305{
7306 if (ctx->rsrc_backup_node)
7307 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007308 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007309 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7310}
7311
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007312static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007313{
7314 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007315
Pavel Begunkov215c3902021-04-01 15:43:48 +01007316 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007317 if (data->quiesce)
7318 return -ENXIO;
7319
7320 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007321 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007322 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007323 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007324 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007325 io_rsrc_node_switch(ctx, data);
7326
Pavel Begunkov3e942492021-04-11 01:46:34 +01007327 /* kill initial ref, already quiesced if zero */
7328 if (atomic_dec_and_test(&data->refs))
7329 break;
Jens Axboec018db42021-08-09 08:15:50 -06007330 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007331 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007332 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007333 if (!ret) {
7334 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007335 break;
Jens Axboec018db42021-08-09 08:15:50 -06007336 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007337
Pavel Begunkov3e942492021-04-11 01:46:34 +01007338 atomic_inc(&data->refs);
7339 /* wait for all works potentially completing data->done */
7340 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007341 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007342
Hao Xu8bad28d2021-02-19 17:19:36 +08007343 ret = io_run_task_work_sig();
7344 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007345 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007346 data->quiesce = false;
7347
Hao Xu8bad28d2021-02-19 17:19:36 +08007348 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007349}
7350
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007351static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7352{
7353 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7354 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7355
7356 return &data->tags[table_idx][off];
7357}
7358
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007359static void io_rsrc_data_free(struct io_rsrc_data *data)
7360{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007361 size_t size = data->nr * sizeof(data->tags[0][0]);
7362
7363 if (data->tags)
7364 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007365 kfree(data);
7366}
7367
Pavel Begunkovd878c812021-06-14 02:36:18 +01007368static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7369 u64 __user *utags, unsigned nr,
7370 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007371{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007372 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007373 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007374 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007375
7376 data = kzalloc(sizeof(*data), GFP_KERNEL);
7377 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007378 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007379 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007380 if (!data->tags) {
7381 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007382 return -ENOMEM;
7383 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007384
7385 data->nr = nr;
7386 data->ctx = ctx;
7387 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007388 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007389 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007390 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007391 u64 *tag_slot = io_get_tag_slot(data, i);
7392
7393 if (copy_from_user(tag_slot, &utags[i],
7394 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007395 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007396 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007397 }
7398
Pavel Begunkov3e942492021-04-11 01:46:34 +01007399 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007400 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007401 *pdata = data;
7402 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007403fail:
7404 io_rsrc_data_free(data);
7405 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007406}
7407
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007408static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7409{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007410 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7411 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007412 return !!table->files;
7413}
7414
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007415static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007416{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007417 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007418 table->files = NULL;
7419}
7420
Jens Axboe2b188cc2019-01-07 10:46:33 -07007421static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7422{
7423#if defined(CONFIG_UNIX)
7424 if (ctx->ring_sock) {
7425 struct sock *sock = ctx->ring_sock->sk;
7426 struct sk_buff *skb;
7427
7428 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7429 kfree_skb(skb);
7430 }
7431#else
7432 int i;
7433
7434 for (i = 0; i < ctx->nr_user_files; i++) {
7435 struct file *file;
7436
7437 file = io_file_from_index(ctx, i);
7438 if (file)
7439 fput(file);
7440 }
7441#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007442 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007443 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007444 ctx->file_data = NULL;
7445 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007446}
7447
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007448static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7449{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007450 int ret;
7451
Pavel Begunkov08480402021-04-13 02:58:38 +01007452 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007453 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007454 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7455 if (!ret)
7456 __io_sqe_files_unregister(ctx);
7457 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007458}
7459
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007460static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007461 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007462{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007463 WARN_ON_ONCE(sqd->thread == current);
7464
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007465 /*
7466 * Do the dance but not conditional clear_bit() because it'd race with
7467 * other threads incrementing park_pending and setting the bit.
7468 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007469 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007470 if (atomic_dec_return(&sqd->park_pending))
7471 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007472 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007473}
7474
Jens Axboe86e0d672021-03-05 08:44:39 -07007475static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007476 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007477{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007478 WARN_ON_ONCE(sqd->thread == current);
7479
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007480 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007481 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007482 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007483 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007484 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007485}
7486
7487static void io_sq_thread_stop(struct io_sq_data *sqd)
7488{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007489 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007490 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007491
Jens Axboe05962f92021-03-06 13:58:48 -07007492 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007493 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007494 if (sqd->thread)
7495 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007496 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007497 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007498}
7499
Jens Axboe534ca6d2020-09-02 13:52:19 -06007500static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007501{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007502 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007503 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7504
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007505 io_sq_thread_stop(sqd);
7506 kfree(sqd);
7507 }
7508}
7509
7510static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7511{
7512 struct io_sq_data *sqd = ctx->sq_data;
7513
7514 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007515 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007516 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007517 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007518 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007519
7520 io_put_sq_data(sqd);
7521 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007522 }
7523}
7524
Jens Axboeaa061652020-09-02 14:50:27 -06007525static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7526{
7527 struct io_ring_ctx *ctx_attach;
7528 struct io_sq_data *sqd;
7529 struct fd f;
7530
7531 f = fdget(p->wq_fd);
7532 if (!f.file)
7533 return ERR_PTR(-ENXIO);
7534 if (f.file->f_op != &io_uring_fops) {
7535 fdput(f);
7536 return ERR_PTR(-EINVAL);
7537 }
7538
7539 ctx_attach = f.file->private_data;
7540 sqd = ctx_attach->sq_data;
7541 if (!sqd) {
7542 fdput(f);
7543 return ERR_PTR(-EINVAL);
7544 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007545 if (sqd->task_tgid != current->tgid) {
7546 fdput(f);
7547 return ERR_PTR(-EPERM);
7548 }
Jens Axboeaa061652020-09-02 14:50:27 -06007549
7550 refcount_inc(&sqd->refs);
7551 fdput(f);
7552 return sqd;
7553}
7554
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007555static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7556 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007557{
7558 struct io_sq_data *sqd;
7559
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007560 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007561 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7562 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007563 if (!IS_ERR(sqd)) {
7564 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007565 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007566 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007567 /* fall through for EPERM case, setup new sqd/task */
7568 if (PTR_ERR(sqd) != -EPERM)
7569 return sqd;
7570 }
Jens Axboeaa061652020-09-02 14:50:27 -06007571
Jens Axboe534ca6d2020-09-02 13:52:19 -06007572 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7573 if (!sqd)
7574 return ERR_PTR(-ENOMEM);
7575
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007576 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007577 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007578 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007579 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007580 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007581 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007582 return sqd;
7583}
7584
Jens Axboe6b063142019-01-10 22:13:58 -07007585#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007586/*
7587 * Ensure the UNIX gc is aware of our file set, so we are certain that
7588 * the io_uring can be safely unregistered on process exit, even if we have
7589 * loops in the file referencing.
7590 */
7591static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7592{
7593 struct sock *sk = ctx->ring_sock->sk;
7594 struct scm_fp_list *fpl;
7595 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007596 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007597
Jens Axboe6b063142019-01-10 22:13:58 -07007598 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7599 if (!fpl)
7600 return -ENOMEM;
7601
7602 skb = alloc_skb(0, GFP_KERNEL);
7603 if (!skb) {
7604 kfree(fpl);
7605 return -ENOMEM;
7606 }
7607
7608 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007609
Jens Axboe08a45172019-10-03 08:11:03 -06007610 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007611 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007612 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007613 struct file *file = io_file_from_index(ctx, i + offset);
7614
7615 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007616 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007617 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007618 unix_inflight(fpl->user, fpl->fp[nr_files]);
7619 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007620 }
7621
Jens Axboe08a45172019-10-03 08:11:03 -06007622 if (nr_files) {
7623 fpl->max = SCM_MAX_FD;
7624 fpl->count = nr_files;
7625 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007626 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007627 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7628 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007629
Jens Axboe08a45172019-10-03 08:11:03 -06007630 for (i = 0; i < nr_files; i++)
7631 fput(fpl->fp[i]);
7632 } else {
7633 kfree_skb(skb);
7634 kfree(fpl);
7635 }
Jens Axboe6b063142019-01-10 22:13:58 -07007636
7637 return 0;
7638}
7639
7640/*
7641 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7642 * causes regular reference counting to break down. We rely on the UNIX
7643 * garbage collection to take care of this problem for us.
7644 */
7645static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7646{
7647 unsigned left, total;
7648 int ret = 0;
7649
7650 total = 0;
7651 left = ctx->nr_user_files;
7652 while (left) {
7653 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007654
7655 ret = __io_sqe_files_scm(ctx, this_files, total);
7656 if (ret)
7657 break;
7658 left -= this_files;
7659 total += this_files;
7660 }
7661
7662 if (!ret)
7663 return 0;
7664
7665 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007666 struct file *file = io_file_from_index(ctx, total);
7667
7668 if (file)
7669 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007670 total++;
7671 }
7672
7673 return ret;
7674}
7675#else
7676static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7677{
7678 return 0;
7679}
7680#endif
7681
Pavel Begunkov47e90392021-04-01 15:43:56 +01007682static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007683{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007684 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007685#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007686 struct sock *sock = ctx->ring_sock->sk;
7687 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7688 struct sk_buff *skb;
7689 int i;
7690
7691 __skb_queue_head_init(&list);
7692
7693 /*
7694 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7695 * remove this entry and rearrange the file array.
7696 */
7697 skb = skb_dequeue(head);
7698 while (skb) {
7699 struct scm_fp_list *fp;
7700
7701 fp = UNIXCB(skb).fp;
7702 for (i = 0; i < fp->count; i++) {
7703 int left;
7704
7705 if (fp->fp[i] != file)
7706 continue;
7707
7708 unix_notinflight(fp->user, fp->fp[i]);
7709 left = fp->count - 1 - i;
7710 if (left) {
7711 memmove(&fp->fp[i], &fp->fp[i + 1],
7712 left * sizeof(struct file *));
7713 }
7714 fp->count--;
7715 if (!fp->count) {
7716 kfree_skb(skb);
7717 skb = NULL;
7718 } else {
7719 __skb_queue_tail(&list, skb);
7720 }
7721 fput(file);
7722 file = NULL;
7723 break;
7724 }
7725
7726 if (!file)
7727 break;
7728
7729 __skb_queue_tail(&list, skb);
7730
7731 skb = skb_dequeue(head);
7732 }
7733
7734 if (skb_peek(&list)) {
7735 spin_lock_irq(&head->lock);
7736 while ((skb = __skb_dequeue(&list)) != NULL)
7737 __skb_queue_tail(head, skb);
7738 spin_unlock_irq(&head->lock);
7739 }
7740#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007741 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007742#endif
7743}
7744
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007745static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007746{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007747 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007748 struct io_ring_ctx *ctx = rsrc_data->ctx;
7749 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007750
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007751 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7752 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007753
7754 if (prsrc->tag) {
7755 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007756
7757 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007758 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007759 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01007760 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007761 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007762 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007763 io_cqring_ev_posted(ctx);
7764 io_ring_submit_unlock(ctx, lock_ring);
7765 }
7766
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007767 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007768 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007769 }
7770
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007771 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01007772 if (atomic_dec_and_test(&rsrc_data->refs))
7773 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007774}
7775
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007776static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007777{
7778 struct io_ring_ctx *ctx;
7779 struct llist_node *node;
7780
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007781 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7782 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007783
7784 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007785 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007786 struct llist_node *next = node->next;
7787
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007788 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007789 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007790 node = next;
7791 }
7792}
7793
Jens Axboe05f3fb32019-12-09 11:22:50 -07007794static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01007795 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007796{
7797 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007798 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007799 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007800 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007801
7802 if (ctx->file_data)
7803 return -EBUSY;
7804 if (!nr_args)
7805 return -EINVAL;
7806 if (nr_args > IORING_MAX_FIXED_FILES)
7807 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01007808 if (nr_args > rlimit(RLIMIT_NOFILE))
7809 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007810 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007811 if (ret)
7812 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007813 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
7814 &ctx->file_data);
7815 if (ret)
7816 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007817
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007818 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007819 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007820 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821
Jens Axboe05f3fb32019-12-09 11:22:50 -07007822 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01007823 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007824 ret = -EFAULT;
7825 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007826 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007827 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01007828 if (fd == -1) {
7829 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007830 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01007831 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007832 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007833 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007834
Jens Axboe05f3fb32019-12-09 11:22:50 -07007835 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007836 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007837 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007838 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007839
7840 /*
7841 * Don't allow io_uring instances to be registered. If UNIX
7842 * isn't enabled, then this causes a reference cycle and this
7843 * instance can never get freed. If UNIX is enabled we'll
7844 * handle it just fine, but there's still no point in allowing
7845 * a ring fd as it doesn't support regular read/write anyway.
7846 */
7847 if (file->f_op == &io_uring_fops) {
7848 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007849 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007850 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007851 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007852 }
7853
Jens Axboe05f3fb32019-12-09 11:22:50 -07007854 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007855 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01007856 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007857 return ret;
7858 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007859
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007860 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007861 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007862out_fput:
7863 for (i = 0; i < ctx->nr_user_files; i++) {
7864 file = io_file_from_index(ctx, i);
7865 if (file)
7866 fput(file);
7867 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007868 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007869 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007870out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007871 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007872 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007873 return ret;
7874}
7875
Jens Axboec3a31e62019-10-03 13:59:56 -06007876static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7877 int index)
7878{
7879#if defined(CONFIG_UNIX)
7880 struct sock *sock = ctx->ring_sock->sk;
7881 struct sk_buff_head *head = &sock->sk_receive_queue;
7882 struct sk_buff *skb;
7883
7884 /*
7885 * See if we can merge this file into an existing skb SCM_RIGHTS
7886 * file set. If there's no room, fall back to allocating a new skb
7887 * and filling it in.
7888 */
7889 spin_lock_irq(&head->lock);
7890 skb = skb_peek(head);
7891 if (skb) {
7892 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7893
7894 if (fpl->count < SCM_MAX_FD) {
7895 __skb_unlink(skb, head);
7896 spin_unlock_irq(&head->lock);
7897 fpl->fp[fpl->count] = get_file(file);
7898 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7899 fpl->count++;
7900 spin_lock_irq(&head->lock);
7901 __skb_queue_head(head, skb);
7902 } else {
7903 skb = NULL;
7904 }
7905 }
7906 spin_unlock_irq(&head->lock);
7907
7908 if (skb) {
7909 fput(file);
7910 return 0;
7911 }
7912
7913 return __io_sqe_files_scm(ctx, 1, index);
7914#else
7915 return 0;
7916#endif
7917}
7918
Pavel Begunkovb9445592021-08-25 12:25:45 +01007919static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
7920 unsigned int issue_flags, u32 slot_index)
7921{
7922 struct io_ring_ctx *ctx = req->ctx;
7923 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
7924 struct io_fixed_file *file_slot;
7925 int ret = -EBADF;
7926
7927 io_ring_submit_lock(ctx, !force_nonblock);
7928 if (file->f_op == &io_uring_fops)
7929 goto err;
7930 ret = -ENXIO;
7931 if (!ctx->file_data)
7932 goto err;
7933 ret = -EINVAL;
7934 if (slot_index >= ctx->nr_user_files)
7935 goto err;
7936
7937 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
7938 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
7939 ret = -EBADF;
7940 if (file_slot->file_ptr)
7941 goto err;
7942
7943 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
7944 io_fixed_file_set(file_slot, file);
7945 ret = io_sqe_file_register(ctx, file, slot_index);
7946 if (ret) {
7947 file_slot->file_ptr = 0;
7948 goto err;
7949 }
7950
7951 ret = 0;
7952err:
7953 io_ring_submit_unlock(ctx, !force_nonblock);
7954 if (ret)
7955 fput(file);
7956 return ret;
7957}
7958
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007959static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
Pavel Begunkove7c78372021-04-01 15:43:45 +01007960 struct io_rsrc_node *node, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007961{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007962 struct io_rsrc_put *prsrc;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007963
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007964 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7965 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007966 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007967
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007968 prsrc->tag = *io_get_tag_slot(data, idx);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007969 prsrc->rsrc = rsrc;
Pavel Begunkove7c78372021-04-01 15:43:45 +01007970 list_add(&prsrc->list, &node->rsrc_list);
Hillf Dantona5318d32020-03-23 17:47:15 +08007971 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007972}
7973
7974static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007975 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007976 unsigned nr_args)
7977{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007978 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007979 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007980 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007981 struct io_fixed_file *file_slot;
7982 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007983 int fd, i, err = 0;
7984 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007985 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007986
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007987 if (!ctx->file_data)
7988 return -ENXIO;
7989 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06007990 return -EINVAL;
7991
Pavel Begunkov67973b92021-01-26 13:51:09 +00007992 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007993 u64 tag = 0;
7994
7995 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
7996 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007997 err = -EFAULT;
7998 break;
7999 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008000 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8001 err = -EINVAL;
8002 break;
8003 }
noah4e0377a2021-01-26 15:23:28 -05008004 if (fd == IORING_REGISTER_FILES_SKIP)
8005 continue;
8006
Pavel Begunkov67973b92021-01-26 13:51:09 +00008007 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008008 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008009
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008010 if (file_slot->file_ptr) {
8011 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008012 err = io_queue_rsrc_removal(data, up->offset + done,
8013 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008014 if (err)
8015 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008016 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008017 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008018 }
8019 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008020 file = fget(fd);
8021 if (!file) {
8022 err = -EBADF;
8023 break;
8024 }
8025 /*
8026 * Don't allow io_uring instances to be registered. If
8027 * UNIX isn't enabled, then this causes a reference
8028 * cycle and this instance can never get freed. If UNIX
8029 * is enabled we'll handle it just fine, but there's
8030 * still no point in allowing a ring fd as it doesn't
8031 * support regular read/write anyway.
8032 */
8033 if (file->f_op == &io_uring_fops) {
8034 fput(file);
8035 err = -EBADF;
8036 break;
8037 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008038 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008039 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008040 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008041 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008042 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008043 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008044 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008045 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008046 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008047 }
8048
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008049 if (needs_switch)
8050 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008051 return done ? done : err;
8052}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008053
Jens Axboe685fe7f2021-03-08 09:37:51 -07008054static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8055 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008056{
Jens Axboee9418942021-02-19 12:33:30 -07008057 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008058 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008059 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008060
Yang Yingliang362a9e62021-07-20 16:38:05 +08008061 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008062 hash = ctx->hash_map;
8063 if (!hash) {
8064 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008065 if (!hash) {
8066 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008067 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008068 }
Jens Axboee9418942021-02-19 12:33:30 -07008069 refcount_set(&hash->refs, 1);
8070 init_waitqueue_head(&hash->wait);
8071 ctx->hash_map = hash;
8072 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008073 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008074
8075 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008076 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008077 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008078 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008079
Jens Axboed25e3a32021-02-16 11:41:41 -07008080 /* Do QD, or 4 * CPUS, whatever is smallest */
8081 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008082
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008083 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008084}
8085
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008086static int io_uring_alloc_task_context(struct task_struct *task,
8087 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008088{
8089 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008090 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008091
Pavel Begunkov09899b12021-06-14 02:36:22 +01008092 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008093 if (unlikely(!tctx))
8094 return -ENOMEM;
8095
Jens Axboed8a6df12020-10-15 16:24:45 -06008096 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8097 if (unlikely(ret)) {
8098 kfree(tctx);
8099 return ret;
8100 }
8101
Jens Axboe685fe7f2021-03-08 09:37:51 -07008102 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008103 if (IS_ERR(tctx->io_wq)) {
8104 ret = PTR_ERR(tctx->io_wq);
8105 percpu_counter_destroy(&tctx->inflight);
8106 kfree(tctx);
8107 return ret;
8108 }
8109
Jens Axboe0f212202020-09-13 13:09:39 -06008110 xa_init(&tctx->xa);
8111 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008112 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008113 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008114 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008115 spin_lock_init(&tctx->task_lock);
8116 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008117 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008118 return 0;
8119}
8120
8121void __io_uring_free(struct task_struct *tsk)
8122{
8123 struct io_uring_task *tctx = tsk->io_uring;
8124
8125 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008126 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008127 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008128
Jens Axboed8a6df12020-10-15 16:24:45 -06008129 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008130 kfree(tctx);
8131 tsk->io_uring = NULL;
8132}
8133
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008134static int io_sq_offload_create(struct io_ring_ctx *ctx,
8135 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008136{
8137 int ret;
8138
Jens Axboed25e3a32021-02-16 11:41:41 -07008139 /* Retain compatibility with failing for an invalid attach attempt */
8140 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8141 IORING_SETUP_ATTACH_WQ) {
8142 struct fd f;
8143
8144 f = fdget(p->wq_fd);
8145 if (!f.file)
8146 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008147 if (f.file->f_op != &io_uring_fops) {
8148 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008149 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008150 }
8151 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008152 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008153 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008154 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008155 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008156 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008157
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008158 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008159 if (IS_ERR(sqd)) {
8160 ret = PTR_ERR(sqd);
8161 goto err;
8162 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008163
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008164 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008165 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008166 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8167 if (!ctx->sq_thread_idle)
8168 ctx->sq_thread_idle = HZ;
8169
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008170 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008171 list_add(&ctx->sqd_list, &sqd->ctx_list);
8172 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008173 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008174 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008175 io_sq_thread_unpark(sqd);
8176
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008177 if (ret < 0)
8178 goto err;
8179 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008180 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008181
Jens Axboe6c271ce2019-01-10 11:22:30 -07008182 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008183 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008184
Jens Axboe917257d2019-04-13 09:28:55 -06008185 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008186 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008187 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008188 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008189 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008190 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008191 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008192
8193 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008194 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008195 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8196 if (IS_ERR(tsk)) {
8197 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008198 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008199 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008200
Jens Axboe46fe18b2021-03-04 12:39:36 -07008201 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008202 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008203 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008204 if (ret)
8205 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008206 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8207 /* Can't have SQ_AFF without SQPOLL */
8208 ret = -EINVAL;
8209 goto err;
8210 }
8211
Jens Axboe2b188cc2019-01-07 10:46:33 -07008212 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008213err_sqpoll:
8214 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008215err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008216 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008217 return ret;
8218}
8219
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008220static inline void __io_unaccount_mem(struct user_struct *user,
8221 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008222{
8223 atomic_long_sub(nr_pages, &user->locked_vm);
8224}
8225
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008226static inline int __io_account_mem(struct user_struct *user,
8227 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008228{
8229 unsigned long page_limit, cur_pages, new_pages;
8230
8231 /* Don't allow more pages than we can safely lock */
8232 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8233
8234 do {
8235 cur_pages = atomic_long_read(&user->locked_vm);
8236 new_pages = cur_pages + nr_pages;
8237 if (new_pages > page_limit)
8238 return -ENOMEM;
8239 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8240 new_pages) != cur_pages);
8241
8242 return 0;
8243}
8244
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008245static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008246{
Jens Axboe62e398b2021-02-21 16:19:37 -07008247 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008248 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008249
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008250 if (ctx->mm_account)
8251 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008252}
8253
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008254static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008255{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008256 int ret;
8257
Jens Axboe62e398b2021-02-21 16:19:37 -07008258 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008259 ret = __io_account_mem(ctx->user, nr_pages);
8260 if (ret)
8261 return ret;
8262 }
8263
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008264 if (ctx->mm_account)
8265 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008266
8267 return 0;
8268}
8269
Jens Axboe2b188cc2019-01-07 10:46:33 -07008270static void io_mem_free(void *ptr)
8271{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008272 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008273
Mark Rutland52e04ef2019-04-30 17:30:21 +01008274 if (!ptr)
8275 return;
8276
8277 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008278 if (put_page_testzero(page))
8279 free_compound_page(page);
8280}
8281
8282static void *io_mem_alloc(size_t size)
8283{
8284 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008285 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008286
8287 return (void *) __get_free_pages(gfp_flags, get_order(size));
8288}
8289
Hristo Venev75b28af2019-08-26 17:23:46 +00008290static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8291 size_t *sq_offset)
8292{
8293 struct io_rings *rings;
8294 size_t off, sq_array_size;
8295
8296 off = struct_size(rings, cqes, cq_entries);
8297 if (off == SIZE_MAX)
8298 return SIZE_MAX;
8299
8300#ifdef CONFIG_SMP
8301 off = ALIGN(off, SMP_CACHE_BYTES);
8302 if (off == 0)
8303 return SIZE_MAX;
8304#endif
8305
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008306 if (sq_offset)
8307 *sq_offset = off;
8308
Hristo Venev75b28af2019-08-26 17:23:46 +00008309 sq_array_size = array_size(sizeof(u32), sq_entries);
8310 if (sq_array_size == SIZE_MAX)
8311 return SIZE_MAX;
8312
8313 if (check_add_overflow(off, sq_array_size, &off))
8314 return SIZE_MAX;
8315
Hristo Venev75b28af2019-08-26 17:23:46 +00008316 return off;
8317}
8318
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008319static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008320{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008321 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008322 unsigned int i;
8323
Pavel Begunkov62248432021-04-28 13:11:29 +01008324 if (imu != ctx->dummy_ubuf) {
8325 for (i = 0; i < imu->nr_bvecs; i++)
8326 unpin_user_page(imu->bvec[i].bv_page);
8327 if (imu->acct_pages)
8328 io_unaccount_mem(ctx, imu->acct_pages);
8329 kvfree(imu);
8330 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008331 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008332}
8333
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008334static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8335{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008336 io_buffer_unmap(ctx, &prsrc->buf);
8337 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008338}
8339
8340static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008341{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008342 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008343
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008344 for (i = 0; i < ctx->nr_user_bufs; i++)
8345 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008346 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008347 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008348 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008349 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008350 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008351}
8352
Jens Axboeedafcce2019-01-09 09:16:05 -07008353static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8354{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008355 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008356
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008357 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008358 return -ENXIO;
8359
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008360 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8361 if (!ret)
8362 __io_sqe_buffers_unregister(ctx);
8363 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008364}
8365
8366static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8367 void __user *arg, unsigned index)
8368{
8369 struct iovec __user *src;
8370
8371#ifdef CONFIG_COMPAT
8372 if (ctx->compat) {
8373 struct compat_iovec __user *ciovs;
8374 struct compat_iovec ciov;
8375
8376 ciovs = (struct compat_iovec __user *) arg;
8377 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8378 return -EFAULT;
8379
Jens Axboed55e5f52019-12-11 16:12:15 -07008380 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008381 dst->iov_len = ciov.iov_len;
8382 return 0;
8383 }
8384#endif
8385 src = (struct iovec __user *) arg;
8386 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8387 return -EFAULT;
8388 return 0;
8389}
8390
Jens Axboede293932020-09-17 16:19:16 -06008391/*
8392 * Not super efficient, but this is just a registration time. And we do cache
8393 * the last compound head, so generally we'll only do a full search if we don't
8394 * match that one.
8395 *
8396 * We check if the given compound head page has already been accounted, to
8397 * avoid double accounting it. This allows us to account the full size of the
8398 * page, not just the constituent pages of a huge page.
8399 */
8400static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8401 int nr_pages, struct page *hpage)
8402{
8403 int i, j;
8404
8405 /* check current page array */
8406 for (i = 0; i < nr_pages; i++) {
8407 if (!PageCompound(pages[i]))
8408 continue;
8409 if (compound_head(pages[i]) == hpage)
8410 return true;
8411 }
8412
8413 /* check previously registered pages */
8414 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008415 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008416
8417 for (j = 0; j < imu->nr_bvecs; j++) {
8418 if (!PageCompound(imu->bvec[j].bv_page))
8419 continue;
8420 if (compound_head(imu->bvec[j].bv_page) == hpage)
8421 return true;
8422 }
8423 }
8424
8425 return false;
8426}
8427
8428static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8429 int nr_pages, struct io_mapped_ubuf *imu,
8430 struct page **last_hpage)
8431{
8432 int i, ret;
8433
Pavel Begunkov216e5832021-05-29 12:01:02 +01008434 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008435 for (i = 0; i < nr_pages; i++) {
8436 if (!PageCompound(pages[i])) {
8437 imu->acct_pages++;
8438 } else {
8439 struct page *hpage;
8440
8441 hpage = compound_head(pages[i]);
8442 if (hpage == *last_hpage)
8443 continue;
8444 *last_hpage = hpage;
8445 if (headpage_already_acct(ctx, pages, i, hpage))
8446 continue;
8447 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8448 }
8449 }
8450
8451 if (!imu->acct_pages)
8452 return 0;
8453
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008454 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008455 if (ret)
8456 imu->acct_pages = 0;
8457 return ret;
8458}
8459
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008460static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008461 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008462 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008463{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008464 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008465 struct vm_area_struct **vmas = NULL;
8466 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008467 unsigned long off, start, end, ubuf;
8468 size_t size;
8469 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008470
Pavel Begunkov62248432021-04-28 13:11:29 +01008471 if (!iov->iov_base) {
8472 *pimu = ctx->dummy_ubuf;
8473 return 0;
8474 }
8475
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008476 ubuf = (unsigned long) iov->iov_base;
8477 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8478 start = ubuf >> PAGE_SHIFT;
8479 nr_pages = end - start;
8480
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008481 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008482 ret = -ENOMEM;
8483
8484 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8485 if (!pages)
8486 goto done;
8487
8488 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8489 GFP_KERNEL);
8490 if (!vmas)
8491 goto done;
8492
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008493 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008494 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008495 goto done;
8496
8497 ret = 0;
8498 mmap_read_lock(current->mm);
8499 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8500 pages, vmas);
8501 if (pret == nr_pages) {
8502 /* don't support file backed memory */
8503 for (i = 0; i < nr_pages; i++) {
8504 struct vm_area_struct *vma = vmas[i];
8505
Pavel Begunkov40dad762021-06-09 15:26:54 +01008506 if (vma_is_shmem(vma))
8507 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008508 if (vma->vm_file &&
8509 !is_file_hugepages(vma->vm_file)) {
8510 ret = -EOPNOTSUPP;
8511 break;
8512 }
8513 }
8514 } else {
8515 ret = pret < 0 ? pret : -EFAULT;
8516 }
8517 mmap_read_unlock(current->mm);
8518 if (ret) {
8519 /*
8520 * if we did partial map, or found file backed vmas,
8521 * release any pages we did get
8522 */
8523 if (pret > 0)
8524 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008525 goto done;
8526 }
8527
8528 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8529 if (ret) {
8530 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008531 goto done;
8532 }
8533
8534 off = ubuf & ~PAGE_MASK;
8535 size = iov->iov_len;
8536 for (i = 0; i < nr_pages; i++) {
8537 size_t vec_len;
8538
8539 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8540 imu->bvec[i].bv_page = pages[i];
8541 imu->bvec[i].bv_len = vec_len;
8542 imu->bvec[i].bv_offset = off;
8543 off = 0;
8544 size -= vec_len;
8545 }
8546 /* store original address for later verification */
8547 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008548 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008549 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008550 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008551 ret = 0;
8552done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008553 if (ret)
8554 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008555 kvfree(pages);
8556 kvfree(vmas);
8557 return ret;
8558}
8559
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008560static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008561{
Pavel Begunkov87094462021-04-11 01:46:36 +01008562 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8563 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008564}
8565
8566static int io_buffer_validate(struct iovec *iov)
8567{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008568 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8569
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008570 /*
8571 * Don't impose further limits on the size and buffer
8572 * constraints here, we'll -EINVAL later when IO is
8573 * submitted if they are wrong.
8574 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008575 if (!iov->iov_base)
8576 return iov->iov_len ? -EFAULT : 0;
8577 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008578 return -EFAULT;
8579
8580 /* arbitrary limit, but we need something */
8581 if (iov->iov_len > SZ_1G)
8582 return -EFAULT;
8583
Pavel Begunkov50e96982021-03-24 22:59:01 +00008584 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8585 return -EOVERFLOW;
8586
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008587 return 0;
8588}
8589
8590static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008591 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008592{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008593 struct page *last_hpage = NULL;
8594 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008595 int i, ret;
8596 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008597
Pavel Begunkov87094462021-04-11 01:46:36 +01008598 if (ctx->user_bufs)
8599 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01008600 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01008601 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008602 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008603 if (ret)
8604 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008605 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
8606 if (ret)
8607 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008608 ret = io_buffers_map_alloc(ctx, nr_args);
8609 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08008610 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008611 return ret;
8612 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008613
Pavel Begunkov87094462021-04-11 01:46:36 +01008614 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07008615 ret = io_copy_iov(ctx, &iov, arg, i);
8616 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008617 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008618 ret = io_buffer_validate(&iov);
8619 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008620 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008621 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008622 ret = -EINVAL;
8623 break;
8624 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008625
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008626 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
8627 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008628 if (ret)
8629 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008630 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008631
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008632 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008633
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008634 ctx->buf_data = data;
8635 if (ret)
8636 __io_sqe_buffers_unregister(ctx);
8637 else
8638 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07008639 return ret;
8640}
8641
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008642static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
8643 struct io_uring_rsrc_update2 *up,
8644 unsigned int nr_args)
8645{
8646 u64 __user *tags = u64_to_user_ptr(up->tags);
8647 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008648 struct page *last_hpage = NULL;
8649 bool needs_switch = false;
8650 __u32 done;
8651 int i, err;
8652
8653 if (!ctx->buf_data)
8654 return -ENXIO;
8655 if (up->offset + nr_args > ctx->nr_user_bufs)
8656 return -EINVAL;
8657
8658 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008659 struct io_mapped_ubuf *imu;
8660 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008661 u64 tag = 0;
8662
8663 err = io_copy_iov(ctx, &iov, iovs, done);
8664 if (err)
8665 break;
8666 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
8667 err = -EFAULT;
8668 break;
8669 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008670 err = io_buffer_validate(&iov);
8671 if (err)
8672 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008673 if (!iov.iov_base && tag) {
8674 err = -EINVAL;
8675 break;
8676 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008677 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
8678 if (err)
8679 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008680
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008681 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01008682 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008683 err = io_queue_rsrc_removal(ctx->buf_data, offset,
8684 ctx->rsrc_node, ctx->user_bufs[i]);
8685 if (unlikely(err)) {
8686 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008687 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008688 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008689 ctx->user_bufs[i] = NULL;
8690 needs_switch = true;
8691 }
8692
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008693 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008694 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008695 }
8696
8697 if (needs_switch)
8698 io_rsrc_node_switch(ctx, ctx->buf_data);
8699 return done ? done : err;
8700}
8701
Jens Axboe9b402842019-04-11 11:45:41 -06008702static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8703{
8704 __s32 __user *fds = arg;
8705 int fd;
8706
8707 if (ctx->cq_ev_fd)
8708 return -EBUSY;
8709
8710 if (copy_from_user(&fd, fds, sizeof(*fds)))
8711 return -EFAULT;
8712
8713 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8714 if (IS_ERR(ctx->cq_ev_fd)) {
8715 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01008716
Jens Axboe9b402842019-04-11 11:45:41 -06008717 ctx->cq_ev_fd = NULL;
8718 return ret;
8719 }
8720
8721 return 0;
8722}
8723
8724static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8725{
8726 if (ctx->cq_ev_fd) {
8727 eventfd_ctx_put(ctx->cq_ev_fd);
8728 ctx->cq_ev_fd = NULL;
8729 return 0;
8730 }
8731
8732 return -ENXIO;
8733}
8734
Jens Axboe5a2e7452020-02-23 16:23:11 -07008735static void io_destroy_buffers(struct io_ring_ctx *ctx)
8736{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07008737 struct io_buffer *buf;
8738 unsigned long index;
8739
8740 xa_for_each(&ctx->io_buffers, index, buf)
8741 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008742}
8743
Pavel Begunkov72558342021-08-09 20:18:09 +01008744static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008745{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008746 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008747
Pavel Begunkovbb943b82021-08-09 20:18:10 +01008748 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
8749 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00008750 kmem_cache_free(req_cachep, req);
8751 }
8752}
8753
Jens Axboe4010fec2021-02-27 15:04:18 -07008754static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008755{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008756 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008757
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008758 mutex_lock(&ctx->uring_lock);
8759
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008760 if (state->free_reqs) {
8761 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
8762 state->free_reqs = 0;
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008763 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008764
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008765 io_flush_cached_locked_reqs(ctx, state);
8766 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008767 mutex_unlock(&ctx->uring_lock);
8768}
8769
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008770static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008771{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008772 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008773 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008774}
8775
Jens Axboe2b188cc2019-01-07 10:46:33 -07008776static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8777{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008778 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008779
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008780 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008781 mmdrop(ctx->mm_account);
8782 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008783 }
Jens Axboedef596e2019-01-09 08:59:42 -07008784
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008785 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
8786 io_wait_rsrc_data(ctx->buf_data);
8787 io_wait_rsrc_data(ctx->file_data);
8788
Hao Xu8bad28d2021-02-19 17:19:36 +08008789 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008790 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008791 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008792 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01008793 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01008794 if (ctx->rings)
8795 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08008796 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008797 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008798 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01008799 if (ctx->sq_creds)
8800 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07008801
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008802 /* there are no registered resources left, nobody uses it */
8803 if (ctx->rsrc_node)
8804 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00008805 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008806 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008807 flush_delayed_work(&ctx->rsrc_put_work);
8808
8809 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
8810 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008811
8812#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008813 if (ctx->ring_sock) {
8814 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008815 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008816 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008817#endif
8818
Hristo Venev75b28af2019-08-26 17:23:46 +00008819 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008820 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008821
8822 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008823 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008824 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008825 if (ctx->hash_map)
8826 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008827 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01008828 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008829 kfree(ctx);
8830}
8831
8832static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8833{
8834 struct io_ring_ctx *ctx = file->private_data;
8835 __poll_t mask = 0;
8836
Pavel Begunkov311997b2021-06-14 23:37:28 +01008837 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008838 /*
8839 * synchronizes with barrier from wq_has_sleeper call in
8840 * io_commit_cqring
8841 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008842 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008843 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008844 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008845
8846 /*
8847 * Don't flush cqring overflow list here, just do a simple check.
8848 * Otherwise there could possible be ABBA deadlock:
8849 * CPU0 CPU1
8850 * ---- ----
8851 * lock(&ctx->uring_lock);
8852 * lock(&ep->mtx);
8853 * lock(&ctx->uring_lock);
8854 * lock(&ep->mtx);
8855 *
8856 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8857 * pushs them to do the flush.
8858 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01008859 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008860 mask |= EPOLLIN | EPOLLRDNORM;
8861
8862 return mask;
8863}
8864
8865static int io_uring_fasync(int fd, struct file *file, int on)
8866{
8867 struct io_ring_ctx *ctx = file->private_data;
8868
8869 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8870}
8871
Yejune Deng0bead8c2020-12-24 11:02:20 +08008872static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008873{
Jens Axboe4379bf82021-02-15 13:40:22 -07008874 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008875
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008876 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07008877 if (creds) {
8878 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008879 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008880 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008881
8882 return -EINVAL;
8883}
8884
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008885struct io_tctx_exit {
8886 struct callback_head task_work;
8887 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00008888 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008889};
8890
8891static void io_tctx_exit_cb(struct callback_head *cb)
8892{
8893 struct io_uring_task *tctx = current->io_uring;
8894 struct io_tctx_exit *work;
8895
8896 work = container_of(cb, struct io_tctx_exit, task_work);
8897 /*
8898 * When @in_idle, we're in cancellation and it's racy to remove the
8899 * node. It'll be removed by the end of cancellation, just ignore it.
8900 */
8901 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01008902 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008903 complete(&work->completion);
8904}
8905
Pavel Begunkov28090c12021-04-25 23:34:45 +01008906static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8907{
8908 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8909
8910 return req->ctx == data;
8911}
8912
Jens Axboe85faa7b2020-04-09 18:14:00 -06008913static void io_ring_exit_work(struct work_struct *work)
8914{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008915 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008916 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01008917 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008918 struct io_tctx_exit exit;
8919 struct io_tctx_node *node;
8920 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06008921
Jens Axboe56952e92020-06-17 15:00:04 -06008922 /*
8923 * If we're doing polled IO and end up having requests being
8924 * submitted async (out-of-line), then completions can come in while
8925 * we're waiting for refs to drop. We need to reap these manually,
8926 * as nobody else will be looking for them.
8927 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008928 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008929 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01008930 if (ctx->sq_data) {
8931 struct io_sq_data *sqd = ctx->sq_data;
8932 struct task_struct *tsk;
8933
8934 io_sq_thread_park(sqd);
8935 tsk = sqd->thread;
8936 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
8937 io_wq_cancel_cb(tsk->io_uring->io_wq,
8938 io_cancel_ctx_cb, ctx, true);
8939 io_sq_thread_unpark(sqd);
8940 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008941
Pavel Begunkov58d3be22021-08-09 13:04:17 +01008942 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
8943 /* there is little hope left, don't run it too often */
8944 interval = HZ * 60;
8945 }
8946 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008947
Pavel Begunkov7f006512021-04-14 13:38:34 +01008948 init_completion(&exit.completion);
8949 init_task_work(&exit.task_work, io_tctx_exit_cb);
8950 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01008951 /*
8952 * Some may use context even when all refs and requests have been put,
8953 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01008954 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01008955 * this lock/unlock section also waits them to finish.
8956 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008957 mutex_lock(&ctx->uring_lock);
8958 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008959 WARN_ON_ONCE(time_after(jiffies, timeout));
8960
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008961 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
8962 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01008963 /* don't spin on a single task if cancellation failed */
8964 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008965 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
8966 if (WARN_ON_ONCE(ret))
8967 continue;
8968 wake_up_process(node->task);
8969
8970 mutex_unlock(&ctx->uring_lock);
8971 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008972 mutex_lock(&ctx->uring_lock);
8973 }
8974 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008975 spin_lock(&ctx->completion_lock);
8976 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008977
Jens Axboe85faa7b2020-04-09 18:14:00 -06008978 io_ring_ctx_free(ctx);
8979}
8980
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008981/* Returns true if we found and killed one or more timeouts */
8982static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008983 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008984{
8985 struct io_kiocb *req, *tmp;
8986 int canceled = 0;
8987
Jens Axboe79ebeae2021-08-10 15:18:27 -06008988 spin_lock(&ctx->completion_lock);
8989 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008990 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008991 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008992 io_kill_timeout(req, -ECANCELED);
8993 canceled++;
8994 }
8995 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06008996 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01008997 if (canceled != 0)
8998 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06008999 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009000 if (canceled != 0)
9001 io_cqring_ev_posted(ctx);
9002 return canceled != 0;
9003}
9004
Jens Axboe2b188cc2019-01-07 10:46:33 -07009005static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9006{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009007 unsigned long index;
9008 struct creds *creds;
9009
Jens Axboe2b188cc2019-01-07 10:46:33 -07009010 mutex_lock(&ctx->uring_lock);
9011 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009012 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009013 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009014 xa_for_each(&ctx->personalities, index, creds)
9015 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009016 mutex_unlock(&ctx->uring_lock);
9017
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009018 io_kill_timeouts(ctx, NULL, true);
9019 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009020
Jens Axboe15dff282019-11-13 09:09:23 -07009021 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009022 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009023
Jens Axboe85faa7b2020-04-09 18:14:00 -06009024 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009025 /*
9026 * Use system_unbound_wq to avoid spawning tons of event kworkers
9027 * if we're exiting a ton of rings at the same time. It just adds
9028 * noise and overhead, there's no discernable change in runtime
9029 * over using system_wq.
9030 */
9031 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009032}
9033
9034static int io_uring_release(struct inode *inode, struct file *file)
9035{
9036 struct io_ring_ctx *ctx = file->private_data;
9037
9038 file->private_data = NULL;
9039 io_ring_ctx_wait_and_kill(ctx);
9040 return 0;
9041}
9042
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009043struct io_task_cancel {
9044 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009045 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009046};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009047
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009048static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009049{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009050 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009051 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009052 bool ret;
9053
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009054 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009055 struct io_ring_ctx *ctx = req->ctx;
9056
9057 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009058 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009059 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009060 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009061 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009062 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009063 }
9064 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009065}
9066
Pavel Begunkove1915f72021-03-11 23:29:35 +00009067static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009068 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009069{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009070 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009071 LIST_HEAD(list);
9072
Jens Axboe79ebeae2021-08-10 15:18:27 -06009073 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009074 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009075 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009076 list_cut_position(&list, &ctx->defer_list, &de->list);
9077 break;
9078 }
9079 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009080 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009081 if (list_empty(&list))
9082 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009083
9084 while (!list_empty(&list)) {
9085 de = list_first_entry(&list, struct io_defer_entry, list);
9086 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009087 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009088 kfree(de);
9089 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009090 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009091}
9092
Pavel Begunkov1b007642021-03-06 11:02:17 +00009093static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9094{
9095 struct io_tctx_node *node;
9096 enum io_wq_cancel cret;
9097 bool ret = false;
9098
9099 mutex_lock(&ctx->uring_lock);
9100 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9101 struct io_uring_task *tctx = node->task->io_uring;
9102
9103 /*
9104 * io_wq will stay alive while we hold uring_lock, because it's
9105 * killed after ctx nodes, which requires to take the lock.
9106 */
9107 if (!tctx || !tctx->io_wq)
9108 continue;
9109 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9110 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9111 }
9112 mutex_unlock(&ctx->uring_lock);
9113
9114 return ret;
9115}
9116
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009117static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9118 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009119 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009120{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009121 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009122 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009123
9124 while (1) {
9125 enum io_wq_cancel cret;
9126 bool ret = false;
9127
Pavel Begunkov1b007642021-03-06 11:02:17 +00009128 if (!task) {
9129 ret |= io_uring_try_cancel_iowq(ctx);
9130 } else if (tctx && tctx->io_wq) {
9131 /*
9132 * Cancels requests of all rings, not only @ctx, but
9133 * it's fine as the task is in exit/exec.
9134 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009135 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009136 &cancel, true);
9137 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9138 }
9139
9140 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009141 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009142 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009143 while (!list_empty_careful(&ctx->iopoll_list)) {
9144 io_iopoll_try_reap_events(ctx);
9145 ret = true;
9146 }
9147 }
9148
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009149 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9150 ret |= io_poll_remove_all(ctx, task, cancel_all);
9151 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009152 if (task)
9153 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009154 if (!ret)
9155 break;
9156 cond_resched();
9157 }
9158}
9159
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009160static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009161{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009162 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009163 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009164 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009165
9166 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009167 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009168 if (unlikely(ret))
9169 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009170 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009171 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009172 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9173 node = kmalloc(sizeof(*node), GFP_KERNEL);
9174 if (!node)
9175 return -ENOMEM;
9176 node->ctx = ctx;
9177 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009178
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009179 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9180 node, GFP_KERNEL));
9181 if (ret) {
9182 kfree(node);
9183 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009184 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009185
9186 mutex_lock(&ctx->uring_lock);
9187 list_add(&node->ctx_node, &ctx->tctx_list);
9188 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009189 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009190 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009191 return 0;
9192}
9193
9194/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009195 * Note that this task has used io_uring. We use it for cancelation purposes.
9196 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009197static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009198{
9199 struct io_uring_task *tctx = current->io_uring;
9200
9201 if (likely(tctx && tctx->last == ctx))
9202 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009203 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009204}
9205
9206/*
Jens Axboe0f212202020-09-13 13:09:39 -06009207 * Remove this io_uring_file -> task mapping.
9208 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009209static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009210{
9211 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009212 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009213
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009214 if (!tctx)
9215 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009216 node = xa_erase(&tctx->xa, index);
9217 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009218 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009219
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009220 WARN_ON_ONCE(current != node->task);
9221 WARN_ON_ONCE(list_empty(&node->ctx_node));
9222
9223 mutex_lock(&node->ctx->uring_lock);
9224 list_del(&node->ctx_node);
9225 mutex_unlock(&node->ctx->uring_lock);
9226
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009227 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009228 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009229 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009230}
9231
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009232static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009233{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009234 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009235 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009236 unsigned long index;
9237
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009238 xa_for_each(&tctx->xa, index, node)
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009239 io_uring_del_tctx_node(index);
Marco Elverb16ef422021-05-27 11:25:48 +02009240 if (wq) {
9241 /*
9242 * Must be after io_uring_del_task_file() (removes nodes under
9243 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9244 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009245 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009246 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009247 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009248}
9249
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009250static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009251{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009252 if (tracked)
9253 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009254 return percpu_counter_sum(&tctx->inflight);
9255}
9256
Pavel Begunkov09899b12021-06-14 02:36:22 +01009257static void io_uring_drop_tctx_refs(struct task_struct *task)
9258{
9259 struct io_uring_task *tctx = task->io_uring;
9260 unsigned int refs = tctx->cached_refs;
9261
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009262 if (refs) {
9263 tctx->cached_refs = 0;
9264 percpu_counter_sub(&tctx->inflight, refs);
9265 put_task_struct_many(task, refs);
9266 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009267}
9268
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009269/*
9270 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9271 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9272 */
9273static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009274{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009275 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009276 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009277 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009278 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009279
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009280 WARN_ON_ONCE(sqd && sqd->thread != current);
9281
Palash Oswal6d042ff2021-04-27 18:21:49 +05309282 if (!current->io_uring)
9283 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009284 if (tctx->io_wq)
9285 io_wq_exit_start(tctx->io_wq);
9286
Jens Axboefdaf0832020-10-30 09:37:30 -06009287 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009288 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009289 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009290 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009291 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009292 if (!inflight)
9293 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009294
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009295 if (!sqd) {
9296 struct io_tctx_node *node;
9297 unsigned long index;
9298
9299 xa_for_each(&tctx->xa, index, node) {
9300 /* sqpoll task will cancel all its requests */
9301 if (node->ctx->sq_data)
9302 continue;
9303 io_uring_try_cancel_requests(node->ctx, current,
9304 cancel_all);
9305 }
9306 } else {
9307 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9308 io_uring_try_cancel_requests(ctx, current,
9309 cancel_all);
9310 }
9311
9312 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009313 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009314 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009315 * If we've seen completions, retry without waiting. This
9316 * avoids a race where a completion comes in before we did
9317 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009318 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009319 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009320 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009321 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009322 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009323 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009324
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009325 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009326 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009327 /* for exec all current's requests should be gone, kill tctx */
9328 __io_uring_free(current);
9329 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009330}
9331
Hao Xuf552a272021-08-12 12:14:35 +08009332void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009333{
Hao Xuf552a272021-08-12 12:14:35 +08009334 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009335}
9336
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009337static void *io_uring_validate_mmap_request(struct file *file,
9338 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009339{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009340 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009341 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009342 struct page *page;
9343 void *ptr;
9344
9345 switch (offset) {
9346 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009347 case IORING_OFF_CQ_RING:
9348 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009349 break;
9350 case IORING_OFF_SQES:
9351 ptr = ctx->sq_sqes;
9352 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009353 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009354 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009355 }
9356
9357 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009358 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009359 return ERR_PTR(-EINVAL);
9360
9361 return ptr;
9362}
9363
9364#ifdef CONFIG_MMU
9365
9366static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9367{
9368 size_t sz = vma->vm_end - vma->vm_start;
9369 unsigned long pfn;
9370 void *ptr;
9371
9372 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9373 if (IS_ERR(ptr))
9374 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009375
9376 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9377 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9378}
9379
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009380#else /* !CONFIG_MMU */
9381
9382static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9383{
9384 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9385}
9386
9387static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9388{
9389 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9390}
9391
9392static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9393 unsigned long addr, unsigned long len,
9394 unsigned long pgoff, unsigned long flags)
9395{
9396 void *ptr;
9397
9398 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9399 if (IS_ERR(ptr))
9400 return PTR_ERR(ptr);
9401
9402 return (unsigned long) ptr;
9403}
9404
9405#endif /* !CONFIG_MMU */
9406
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009407static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009408{
9409 DEFINE_WAIT(wait);
9410
9411 do {
9412 if (!io_sqring_full(ctx))
9413 break;
Jens Axboe90554202020-09-03 12:12:41 -06009414 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9415
9416 if (!io_sqring_full(ctx))
9417 break;
Jens Axboe90554202020-09-03 12:12:41 -06009418 schedule();
9419 } while (!signal_pending(current));
9420
9421 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009422 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009423}
9424
Hao Xuc73ebb62020-11-03 10:54:37 +08009425static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9426 struct __kernel_timespec __user **ts,
9427 const sigset_t __user **sig)
9428{
9429 struct io_uring_getevents_arg arg;
9430
9431 /*
9432 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9433 * is just a pointer to the sigset_t.
9434 */
9435 if (!(flags & IORING_ENTER_EXT_ARG)) {
9436 *sig = (const sigset_t __user *) argp;
9437 *ts = NULL;
9438 return 0;
9439 }
9440
9441 /*
9442 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9443 * timespec and sigset_t pointers if good.
9444 */
9445 if (*argsz != sizeof(arg))
9446 return -EINVAL;
9447 if (copy_from_user(&arg, argp, sizeof(arg)))
9448 return -EFAULT;
9449 *sig = u64_to_user_ptr(arg.sigmask);
9450 *argsz = arg.sigmask_sz;
9451 *ts = u64_to_user_ptr(arg.ts);
9452 return 0;
9453}
9454
Jens Axboe2b188cc2019-01-07 10:46:33 -07009455SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009456 u32, min_complete, u32, flags, const void __user *, argp,
9457 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009458{
9459 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009460 int submitted = 0;
9461 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009462 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009463
Jens Axboe4c6e2772020-07-01 11:29:10 -06009464 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009465
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009466 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9467 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009468 return -EINVAL;
9469
9470 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009471 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009472 return -EBADF;
9473
9474 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009475 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009476 goto out_fput;
9477
9478 ret = -ENXIO;
9479 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009480 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009481 goto out_fput;
9482
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009483 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009484 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009485 goto out;
9486
Jens Axboe6c271ce2019-01-10 11:22:30 -07009487 /*
9488 * For SQ polling, the thread will do all submissions and completions.
9489 * Just return the requested submit count, and wake the thread if
9490 * we were asked to.
9491 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009492 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009493 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009494 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009495
Jens Axboe21f96522021-08-14 09:04:40 -06009496 if (unlikely(ctx->sq_data->thread == NULL)) {
9497 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009498 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009499 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009500 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009501 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009502 if (flags & IORING_ENTER_SQ_WAIT) {
9503 ret = io_sqpoll_wait_sq(ctx);
9504 if (ret)
9505 goto out;
9506 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009507 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009508 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009509 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009510 if (unlikely(ret))
9511 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009512 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009513 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009514 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009515
9516 if (submitted != to_submit)
9517 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009518 }
9519 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009520 const sigset_t __user *sig;
9521 struct __kernel_timespec __user *ts;
9522
9523 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9524 if (unlikely(ret))
9525 goto out;
9526
Jens Axboe2b188cc2019-01-07 10:46:33 -07009527 min_complete = min(min_complete, ctx->cq_entries);
9528
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009529 /*
9530 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9531 * space applications don't need to do io completion events
9532 * polling again, they can rely on io_sq_thread to do polling
9533 * work, which can reduce cpu usage and uring_lock contention.
9534 */
9535 if (ctx->flags & IORING_SETUP_IOPOLL &&
9536 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009537 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009538 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009539 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009540 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541 }
9542
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009543out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009544 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009545out_fput:
9546 fdput(f);
9547 return submitted ? submitted : ret;
9548}
9549
Tobias Klauserbebdb652020-02-26 18:38:32 +01009550#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009551static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9552 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009553{
Jens Axboe87ce9552020-01-30 08:25:34 -07009554 struct user_namespace *uns = seq_user_ns(m);
9555 struct group_info *gi;
9556 kernel_cap_t cap;
9557 unsigned __capi;
9558 int g;
9559
9560 seq_printf(m, "%5d\n", id);
9561 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9562 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9563 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9564 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9565 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9566 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9567 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9568 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9569 seq_puts(m, "\n\tGroups:\t");
9570 gi = cred->group_info;
9571 for (g = 0; g < gi->ngroups; g++) {
9572 seq_put_decimal_ull(m, g ? " " : "",
9573 from_kgid_munged(uns, gi->gid[g]));
9574 }
9575 seq_puts(m, "\n\tCapEff:\t");
9576 cap = cred->cap_effective;
9577 CAP_FOR_EACH_U32(__capi)
9578 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9579 seq_putc(m, '\n');
9580 return 0;
9581}
9582
9583static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9584{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009585 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009586 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009587 int i;
9588
Jens Axboefad8e0d2020-09-28 08:57:48 -06009589 /*
9590 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9591 * since fdinfo case grabs it in the opposite direction of normal use
9592 * cases. If we fail to get the lock, we just don't iterate any
9593 * structures that could be going away outside the io_uring mutex.
9594 */
9595 has_lock = mutex_trylock(&ctx->uring_lock);
9596
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009597 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009598 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009599 if (!sq->thread)
9600 sq = NULL;
9601 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009602
9603 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9604 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009605 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009606 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -07009607 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009608
Jens Axboe87ce9552020-01-30 08:25:34 -07009609 if (f)
9610 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9611 else
9612 seq_printf(m, "%5u: <none>\n", i);
9613 }
9614 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009615 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009616 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +01009617 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -07009618
Pavel Begunkov4751f532021-04-01 15:43:55 +01009619 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -07009620 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009621 if (has_lock && !xa_empty(&ctx->personalities)) {
9622 unsigned long index;
9623 const struct cred *cred;
9624
Jens Axboe87ce9552020-01-30 08:25:34 -07009625 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009626 xa_for_each(&ctx->personalities, index, cred)
9627 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -07009628 }
Jens Axboed7718a92020-02-14 22:23:12 -07009629 seq_printf(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -06009630 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -07009631 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9632 struct hlist_head *list = &ctx->cancel_hash[i];
9633 struct io_kiocb *req;
9634
9635 hlist_for_each_entry(req, list, hash_node)
9636 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9637 req->task->task_works != NULL);
9638 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009639 spin_unlock(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009640 if (has_lock)
9641 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009642}
9643
9644static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9645{
9646 struct io_ring_ctx *ctx = f->private_data;
9647
9648 if (percpu_ref_tryget(&ctx->refs)) {
9649 __io_uring_show_fdinfo(ctx, m);
9650 percpu_ref_put(&ctx->refs);
9651 }
9652}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009653#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009654
Jens Axboe2b188cc2019-01-07 10:46:33 -07009655static const struct file_operations io_uring_fops = {
9656 .release = io_uring_release,
9657 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009658#ifndef CONFIG_MMU
9659 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9660 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9661#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009662 .poll = io_uring_poll,
9663 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009664#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009665 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009666#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009667};
9668
9669static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9670 struct io_uring_params *p)
9671{
Hristo Venev75b28af2019-08-26 17:23:46 +00009672 struct io_rings *rings;
9673 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009674
Jens Axboebd740482020-08-05 12:58:23 -06009675 /* make sure these are sane, as we already accounted them */
9676 ctx->sq_entries = p->sq_entries;
9677 ctx->cq_entries = p->cq_entries;
9678
Hristo Venev75b28af2019-08-26 17:23:46 +00009679 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9680 if (size == SIZE_MAX)
9681 return -EOVERFLOW;
9682
9683 rings = io_mem_alloc(size);
9684 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009685 return -ENOMEM;
9686
Hristo Venev75b28af2019-08-26 17:23:46 +00009687 ctx->rings = rings;
9688 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9689 rings->sq_ring_mask = p->sq_entries - 1;
9690 rings->cq_ring_mask = p->cq_entries - 1;
9691 rings->sq_ring_entries = p->sq_entries;
9692 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009693
9694 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009695 if (size == SIZE_MAX) {
9696 io_mem_free(ctx->rings);
9697 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009698 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009699 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009700
9701 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009702 if (!ctx->sq_sqes) {
9703 io_mem_free(ctx->rings);
9704 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009705 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009706 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009707
Jens Axboe2b188cc2019-01-07 10:46:33 -07009708 return 0;
9709}
9710
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009711static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9712{
9713 int ret, fd;
9714
9715 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9716 if (fd < 0)
9717 return fd;
9718
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009719 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009720 if (ret) {
9721 put_unused_fd(fd);
9722 return ret;
9723 }
9724 fd_install(fd, file);
9725 return fd;
9726}
9727
Jens Axboe2b188cc2019-01-07 10:46:33 -07009728/*
9729 * Allocate an anonymous fd, this is what constitutes the application
9730 * visible backing of an io_uring instance. The application mmaps this
9731 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9732 * we have to tie this fd to a socket for file garbage collection purposes.
9733 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009734static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009735{
9736 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009737#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009738 int ret;
9739
Jens Axboe2b188cc2019-01-07 10:46:33 -07009740 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9741 &ctx->ring_sock);
9742 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009743 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009744#endif
9745
Jens Axboe2b188cc2019-01-07 10:46:33 -07009746 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9747 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009748#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009749 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009750 sock_release(ctx->ring_sock);
9751 ctx->ring_sock = NULL;
9752 } else {
9753 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009754 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009755#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009756 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009757}
9758
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009759static int io_uring_create(unsigned entries, struct io_uring_params *p,
9760 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009761{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009762 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009763 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009764 int ret;
9765
Jens Axboe8110c1a2019-12-28 15:39:54 -07009766 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009767 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009768 if (entries > IORING_MAX_ENTRIES) {
9769 if (!(p->flags & IORING_SETUP_CLAMP))
9770 return -EINVAL;
9771 entries = IORING_MAX_ENTRIES;
9772 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009773
9774 /*
9775 * Use twice as many entries for the CQ ring. It's possible for the
9776 * application to drive a higher depth than the size of the SQ ring,
9777 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009778 * some flexibility in overcommitting a bit. If the application has
9779 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9780 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009781 */
9782 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009783 if (p->flags & IORING_SETUP_CQSIZE) {
9784 /*
9785 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9786 * to a power-of-two, if it isn't already. We do NOT impose
9787 * any cq vs sq ring sizing.
9788 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009789 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009790 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009791 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9792 if (!(p->flags & IORING_SETUP_CLAMP))
9793 return -EINVAL;
9794 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9795 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009796 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9797 if (p->cq_entries < p->sq_entries)
9798 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009799 } else {
9800 p->cq_entries = 2 * p->sq_entries;
9801 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009802
Jens Axboe2b188cc2019-01-07 10:46:33 -07009803 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009804 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009805 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009806 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009807 if (!capable(CAP_IPC_LOCK))
9808 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -06009809
9810 /*
9811 * This is just grabbed for accounting purposes. When a process exits,
9812 * the mm is exited and dropped before the files, hence we need to hang
9813 * on to this mm purely for the purposes of being able to unaccount
9814 * memory (locked/pinned vm). It's not used for anything else.
9815 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009816 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009817 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009818
Jens Axboe2b188cc2019-01-07 10:46:33 -07009819 ret = io_allocate_scq_urings(ctx, p);
9820 if (ret)
9821 goto err;
9822
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009823 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009824 if (ret)
9825 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009826 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +01009827 ret = io_rsrc_node_switch_start(ctx);
9828 if (ret)
9829 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009830 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009831
Jens Axboe2b188cc2019-01-07 10:46:33 -07009832 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009833 p->sq_off.head = offsetof(struct io_rings, sq.head);
9834 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9835 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9836 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9837 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9838 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9839 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009840
9841 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009842 p->cq_off.head = offsetof(struct io_rings, cq.head);
9843 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9844 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9845 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9846 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9847 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009848 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009849
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009850 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9851 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009852 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009853 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +01009854 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
9855 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009856
9857 if (copy_to_user(params, p, sizeof(*p))) {
9858 ret = -EFAULT;
9859 goto err;
9860 }
Jens Axboed1719f72020-07-30 13:43:53 -06009861
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009862 file = io_uring_get_file(ctx);
9863 if (IS_ERR(file)) {
9864 ret = PTR_ERR(file);
9865 goto err;
9866 }
9867
Jens Axboed1719f72020-07-30 13:43:53 -06009868 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009869 * Install ring fd as the very last thing, so we don't risk someone
9870 * having closed it before we finish setup
9871 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009872 ret = io_uring_install_fd(ctx, file);
9873 if (ret < 0) {
9874 /* fput will clean it up */
9875 fput(file);
9876 return ret;
9877 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009878
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009879 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009880 return ret;
9881err:
9882 io_ring_ctx_wait_and_kill(ctx);
9883 return ret;
9884}
9885
9886/*
9887 * Sets up an aio uring context, and returns the fd. Applications asks for a
9888 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9889 * params structure passed in.
9890 */
9891static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9892{
9893 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009894 int i;
9895
9896 if (copy_from_user(&p, params, sizeof(p)))
9897 return -EFAULT;
9898 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9899 if (p.resv[i])
9900 return -EINVAL;
9901 }
9902
Jens Axboe6c271ce2019-01-10 11:22:30 -07009903 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009904 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009905 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9906 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009907 return -EINVAL;
9908
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009909 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009910}
9911
9912SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9913 struct io_uring_params __user *, params)
9914{
9915 return io_uring_setup(entries, params);
9916}
9917
Jens Axboe66f4af92020-01-16 15:36:52 -07009918static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9919{
9920 struct io_uring_probe *p;
9921 size_t size;
9922 int i, ret;
9923
9924 size = struct_size(p, ops, nr_args);
9925 if (size == SIZE_MAX)
9926 return -EOVERFLOW;
9927 p = kzalloc(size, GFP_KERNEL);
9928 if (!p)
9929 return -ENOMEM;
9930
9931 ret = -EFAULT;
9932 if (copy_from_user(p, arg, size))
9933 goto out;
9934 ret = -EINVAL;
9935 if (memchr_inv(p, 0, size))
9936 goto out;
9937
9938 p->last_op = IORING_OP_LAST - 1;
9939 if (nr_args > IORING_OP_LAST)
9940 nr_args = IORING_OP_LAST;
9941
9942 for (i = 0; i < nr_args; i++) {
9943 p->ops[i].op = i;
9944 if (!io_op_defs[i].not_supported)
9945 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9946 }
9947 p->ops_len = i;
9948
9949 ret = 0;
9950 if (copy_to_user(arg, p, size))
9951 ret = -EFAULT;
9952out:
9953 kfree(p);
9954 return ret;
9955}
9956
Jens Axboe071698e2020-01-28 10:04:42 -07009957static int io_register_personality(struct io_ring_ctx *ctx)
9958{
Jens Axboe4379bf82021-02-15 13:40:22 -07009959 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009960 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009961 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009962
Jens Axboe4379bf82021-02-15 13:40:22 -07009963 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009964
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009965 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
9966 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -06009967 if (ret < 0) {
9968 put_cred(creds);
9969 return ret;
9970 }
9971 return id;
Jens Axboe071698e2020-01-28 10:04:42 -07009972}
9973
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009974static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9975 unsigned int nr_args)
9976{
9977 struct io_uring_restriction *res;
9978 size_t size;
9979 int i, ret;
9980
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009981 /* Restrictions allowed only if rings started disabled */
9982 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9983 return -EBADFD;
9984
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009985 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009986 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009987 return -EBUSY;
9988
9989 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9990 return -EINVAL;
9991
9992 size = array_size(nr_args, sizeof(*res));
9993 if (size == SIZE_MAX)
9994 return -EOVERFLOW;
9995
9996 res = memdup_user(arg, size);
9997 if (IS_ERR(res))
9998 return PTR_ERR(res);
9999
10000 ret = 0;
10001
10002 for (i = 0; i < nr_args; i++) {
10003 switch (res[i].opcode) {
10004 case IORING_RESTRICTION_REGISTER_OP:
10005 if (res[i].register_op >= IORING_REGISTER_LAST) {
10006 ret = -EINVAL;
10007 goto out;
10008 }
10009
10010 __set_bit(res[i].register_op,
10011 ctx->restrictions.register_op);
10012 break;
10013 case IORING_RESTRICTION_SQE_OP:
10014 if (res[i].sqe_op >= IORING_OP_LAST) {
10015 ret = -EINVAL;
10016 goto out;
10017 }
10018
10019 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10020 break;
10021 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10022 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10023 break;
10024 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10025 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10026 break;
10027 default:
10028 ret = -EINVAL;
10029 goto out;
10030 }
10031 }
10032
10033out:
10034 /* Reset all restrictions if an error happened */
10035 if (ret != 0)
10036 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10037 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010038 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010039
10040 kfree(res);
10041 return ret;
10042}
10043
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010044static int io_register_enable_rings(struct io_ring_ctx *ctx)
10045{
10046 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10047 return -EBADFD;
10048
10049 if (ctx->restrictions.registered)
10050 ctx->restricted = 1;
10051
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010052 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10053 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10054 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010055 return 0;
10056}
10057
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010058static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010059 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010060 unsigned nr_args)
10061{
10062 __u32 tmp;
10063 int err;
10064
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010065 if (up->resv)
10066 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010067 if (check_add_overflow(up->offset, nr_args, &tmp))
10068 return -EOVERFLOW;
10069 err = io_rsrc_node_switch_start(ctx);
10070 if (err)
10071 return err;
10072
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010073 switch (type) {
10074 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010075 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010076 case IORING_RSRC_BUFFER:
10077 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010078 }
10079 return -EINVAL;
10080}
10081
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010082static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10083 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010084{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010085 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010086
10087 if (!nr_args)
10088 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010089 memset(&up, 0, sizeof(up));
10090 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10091 return -EFAULT;
10092 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10093}
10094
10095static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010096 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010097{
10098 struct io_uring_rsrc_update2 up;
10099
10100 if (size != sizeof(up))
10101 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010102 if (copy_from_user(&up, arg, sizeof(up)))
10103 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010104 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010105 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010106 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010107}
10108
Pavel Begunkov792e3582021-04-25 14:32:21 +010010109static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010110 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010111{
10112 struct io_uring_rsrc_register rr;
10113
10114 /* keep it extendible */
10115 if (size != sizeof(rr))
10116 return -EINVAL;
10117
10118 memset(&rr, 0, sizeof(rr));
10119 if (copy_from_user(&rr, arg, size))
10120 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010121 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010122 return -EINVAL;
10123
Pavel Begunkov992da012021-06-10 16:37:37 +010010124 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010125 case IORING_RSRC_FILE:
10126 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10127 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010128 case IORING_RSRC_BUFFER:
10129 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10130 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010131 }
10132 return -EINVAL;
10133}
10134
Jens Axboefe764212021-06-17 10:19:54 -060010135static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10136 unsigned len)
10137{
10138 struct io_uring_task *tctx = current->io_uring;
10139 cpumask_var_t new_mask;
10140 int ret;
10141
10142 if (!tctx || !tctx->io_wq)
10143 return -EINVAL;
10144
10145 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10146 return -ENOMEM;
10147
10148 cpumask_clear(new_mask);
10149 if (len > cpumask_size())
10150 len = cpumask_size();
10151
10152 if (copy_from_user(new_mask, arg, len)) {
10153 free_cpumask_var(new_mask);
10154 return -EFAULT;
10155 }
10156
10157 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10158 free_cpumask_var(new_mask);
10159 return ret;
10160}
10161
10162static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10163{
10164 struct io_uring_task *tctx = current->io_uring;
10165
10166 if (!tctx || !tctx->io_wq)
10167 return -EINVAL;
10168
10169 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10170}
10171
Jens Axboe071698e2020-01-28 10:04:42 -070010172static bool io_register_op_must_quiesce(int op)
10173{
10174 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010175 case IORING_REGISTER_BUFFERS:
10176 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010177 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010178 case IORING_UNREGISTER_FILES:
10179 case IORING_REGISTER_FILES_UPDATE:
10180 case IORING_REGISTER_PROBE:
10181 case IORING_REGISTER_PERSONALITY:
10182 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010183 case IORING_REGISTER_FILES2:
10184 case IORING_REGISTER_FILES_UPDATE2:
10185 case IORING_REGISTER_BUFFERS2:
10186 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010187 case IORING_REGISTER_IOWQ_AFF:
10188 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe071698e2020-01-28 10:04:42 -070010189 return false;
10190 default:
10191 return true;
10192 }
10193}
10194
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010195static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10196{
10197 long ret;
10198
10199 percpu_ref_kill(&ctx->refs);
10200
10201 /*
10202 * Drop uring mutex before waiting for references to exit. If another
10203 * thread is currently inside io_uring_enter() it might need to grab the
10204 * uring_lock to make progress. If we hold it here across the drain
10205 * wait, then we can deadlock. It's safe to drop the mutex here, since
10206 * no new references will come in after we've killed the percpu ref.
10207 */
10208 mutex_unlock(&ctx->uring_lock);
10209 do {
10210 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10211 if (!ret)
10212 break;
10213 ret = io_run_task_work_sig();
10214 } while (ret >= 0);
10215 mutex_lock(&ctx->uring_lock);
10216
10217 if (ret)
10218 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10219 return ret;
10220}
10221
Jens Axboeedafcce2019-01-09 09:16:05 -070010222static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10223 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010224 __releases(ctx->uring_lock)
10225 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010226{
10227 int ret;
10228
Jens Axboe35fa71a2019-04-22 10:23:23 -060010229 /*
10230 * We're inside the ring mutex, if the ref is already dying, then
10231 * someone else killed the ctx or is already going through
10232 * io_uring_register().
10233 */
10234 if (percpu_ref_is_dying(&ctx->refs))
10235 return -ENXIO;
10236
Pavel Begunkov75c40212021-04-15 13:07:40 +010010237 if (ctx->restricted) {
10238 if (opcode >= IORING_REGISTER_LAST)
10239 return -EINVAL;
10240 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10241 if (!test_bit(opcode, ctx->restrictions.register_op))
10242 return -EACCES;
10243 }
10244
Jens Axboe071698e2020-01-28 10:04:42 -070010245 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010246 ret = io_ctx_quiesce(ctx);
10247 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010248 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010249 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010250
10251 switch (opcode) {
10252 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010253 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010254 break;
10255 case IORING_UNREGISTER_BUFFERS:
10256 ret = -EINVAL;
10257 if (arg || nr_args)
10258 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010259 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010260 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010261 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010262 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010263 break;
10264 case IORING_UNREGISTER_FILES:
10265 ret = -EINVAL;
10266 if (arg || nr_args)
10267 break;
10268 ret = io_sqe_files_unregister(ctx);
10269 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010270 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010271 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010272 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010273 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010274 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010275 ret = -EINVAL;
10276 if (nr_args != 1)
10277 break;
10278 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010279 if (ret)
10280 break;
10281 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10282 ctx->eventfd_async = 1;
10283 else
10284 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010285 break;
10286 case IORING_UNREGISTER_EVENTFD:
10287 ret = -EINVAL;
10288 if (arg || nr_args)
10289 break;
10290 ret = io_eventfd_unregister(ctx);
10291 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010292 case IORING_REGISTER_PROBE:
10293 ret = -EINVAL;
10294 if (!arg || nr_args > 256)
10295 break;
10296 ret = io_probe(ctx, arg, nr_args);
10297 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010298 case IORING_REGISTER_PERSONALITY:
10299 ret = -EINVAL;
10300 if (arg || nr_args)
10301 break;
10302 ret = io_register_personality(ctx);
10303 break;
10304 case IORING_UNREGISTER_PERSONALITY:
10305 ret = -EINVAL;
10306 if (arg)
10307 break;
10308 ret = io_unregister_personality(ctx, nr_args);
10309 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010310 case IORING_REGISTER_ENABLE_RINGS:
10311 ret = -EINVAL;
10312 if (arg || nr_args)
10313 break;
10314 ret = io_register_enable_rings(ctx);
10315 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010316 case IORING_REGISTER_RESTRICTIONS:
10317 ret = io_register_restrictions(ctx, arg, nr_args);
10318 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010319 case IORING_REGISTER_FILES2:
10320 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010321 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010322 case IORING_REGISTER_FILES_UPDATE2:
10323 ret = io_register_rsrc_update(ctx, arg, nr_args,
10324 IORING_RSRC_FILE);
10325 break;
10326 case IORING_REGISTER_BUFFERS2:
10327 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10328 break;
10329 case IORING_REGISTER_BUFFERS_UPDATE:
10330 ret = io_register_rsrc_update(ctx, arg, nr_args,
10331 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010332 break;
Jens Axboefe764212021-06-17 10:19:54 -060010333 case IORING_REGISTER_IOWQ_AFF:
10334 ret = -EINVAL;
10335 if (!arg || !nr_args)
10336 break;
10337 ret = io_register_iowq_aff(ctx, arg, nr_args);
10338 break;
10339 case IORING_UNREGISTER_IOWQ_AFF:
10340 ret = -EINVAL;
10341 if (arg || nr_args)
10342 break;
10343 ret = io_unregister_iowq_aff(ctx);
10344 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010345 default:
10346 ret = -EINVAL;
10347 break;
10348 }
10349
Jens Axboe071698e2020-01-28 10:04:42 -070010350 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010351 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010352 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010353 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010354 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010355 return ret;
10356}
10357
10358SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10359 void __user *, arg, unsigned int, nr_args)
10360{
10361 struct io_ring_ctx *ctx;
10362 long ret = -EBADF;
10363 struct fd f;
10364
10365 f = fdget(fd);
10366 if (!f.file)
10367 return -EBADF;
10368
10369 ret = -EOPNOTSUPP;
10370 if (f.file->f_op != &io_uring_fops)
10371 goto out_fput;
10372
10373 ctx = f.file->private_data;
10374
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010375 io_run_task_work();
10376
Jens Axboeedafcce2019-01-09 09:16:05 -070010377 mutex_lock(&ctx->uring_lock);
10378 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10379 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010380 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10381 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010382out_fput:
10383 fdput(f);
10384 return ret;
10385}
10386
Jens Axboe2b188cc2019-01-07 10:46:33 -070010387static int __init io_uring_init(void)
10388{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010389#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10390 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10391 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10392} while (0)
10393
10394#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10395 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10396 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10397 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10398 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10399 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10400 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10401 BUILD_BUG_SQE_ELEM(8, __u64, off);
10402 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10403 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010404 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010405 BUILD_BUG_SQE_ELEM(24, __u32, len);
10406 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10407 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10408 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10409 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010410 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10411 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010412 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10413 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10414 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10415 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10416 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10417 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10418 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10419 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010420 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010421 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10422 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010423 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010424 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010425 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010426 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010427
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010428 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10429 sizeof(struct io_uring_rsrc_update));
10430 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10431 sizeof(struct io_uring_rsrc_update2));
10432 /* should fit into one byte */
10433 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
10434
Jens Axboed3656342019-12-18 09:50:26 -070010435 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010436 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010437
Jens Axboe91f245d2021-02-09 13:48:50 -070010438 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10439 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010440 return 0;
10441};
10442__initcall(io_uring_init);