blob: e321cb0d00c415deb785863f53474657d8e9b381 [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;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +0100518 u32 file_slot;
Jens Axboe09952e32020-03-19 20:16:56 -0600519 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700520};
521
522struct io_sync {
523 struct file *file;
524 loff_t len;
525 loff_t off;
526 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700527 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700528};
529
Jens Axboefbf23842019-12-17 18:45:56 -0700530struct io_cancel {
531 struct file *file;
532 u64 addr;
533};
534
Jens Axboeb29472e2019-12-17 18:50:29 -0700535struct io_timeout {
536 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300537 u32 off;
538 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300539 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000540 /* head of the link, used by linked timeouts only */
541 struct io_kiocb *head;
Jens Axboe89b263f2021-08-10 15:14:18 -0600542 /* for linked completions */
543 struct io_kiocb *prev;
Jens Axboeb29472e2019-12-17 18:50:29 -0700544};
545
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100546struct io_timeout_rem {
547 struct file *file;
548 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000549
550 /* timeout update */
551 struct timespec64 ts;
552 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100553};
554
Jens Axboe9adbd452019-12-20 08:45:55 -0700555struct io_rw {
556 /* NOTE: kiocb has the file as the first member, so don't do it here */
557 struct kiocb kiocb;
558 u64 addr;
559 u64 len;
560};
561
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700562struct io_connect {
563 struct file *file;
564 struct sockaddr __user *addr;
565 int addr_len;
566};
567
Jens Axboee47293f2019-12-20 08:58:21 -0700568struct io_sr_msg {
569 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700570 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100571 struct compat_msghdr __user *umsg_compat;
572 struct user_msghdr __user *umsg;
573 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700574 };
Jens Axboee47293f2019-12-20 08:58:21 -0700575 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700576 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700577 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700578 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700579};
580
Jens Axboe15b71ab2019-12-11 11:20:36 -0700581struct io_open {
582 struct file *file;
583 int dfd;
Pavel Begunkovb9445592021-08-25 12:25:45 +0100584 u32 file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700585 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700586 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600587 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700588};
589
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000590struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700591 struct file *file;
592 u64 arg;
593 u32 nr_args;
594 u32 offset;
595};
596
Jens Axboe4840e412019-12-25 22:03:45 -0700597struct io_fadvise {
598 struct file *file;
599 u64 offset;
600 u32 len;
601 u32 advice;
602};
603
Jens Axboec1ca7572019-12-25 22:18:28 -0700604struct io_madvise {
605 struct file *file;
606 u64 addr;
607 u32 len;
608 u32 advice;
609};
610
Jens Axboe3e4827b2020-01-08 15:18:09 -0700611struct io_epoll {
612 struct file *file;
613 int epfd;
614 int op;
615 int fd;
616 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617};
618
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300619struct io_splice {
620 struct file *file_out;
621 struct file *file_in;
622 loff_t off_out;
623 loff_t off_in;
624 u64 len;
625 unsigned int flags;
626};
627
Jens Axboeddf0322d2020-02-23 16:41:33 -0700628struct io_provide_buf {
629 struct file *file;
630 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100631 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632 __u32 bgid;
633 __u16 nbufs;
634 __u16 bid;
635};
636
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700637struct io_statx {
638 struct file *file;
639 int dfd;
640 unsigned int mask;
641 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700642 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700643 struct statx __user *buffer;
644};
645
Jens Axboe36f4fa62020-09-05 11:14:22 -0600646struct io_shutdown {
647 struct file *file;
648 int how;
649};
650
Jens Axboe80a261f2020-09-28 14:23:58 -0600651struct io_rename {
652 struct file *file;
653 int old_dfd;
654 int new_dfd;
655 struct filename *oldpath;
656 struct filename *newpath;
657 int flags;
658};
659
Jens Axboe14a11432020-09-28 14:27:37 -0600660struct io_unlink {
661 struct file *file;
662 int dfd;
663 int flags;
664 struct filename *filename;
665};
666
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300667struct io_completion {
668 struct file *file;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000669 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300670};
671
Jens Axboef499a022019-12-02 16:28:46 -0700672struct io_async_connect {
673 struct sockaddr_storage address;
674};
675
Jens Axboe03b12302019-12-02 18:50:25 -0700676struct io_async_msghdr {
677 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000678 /* points to an allocated iov, if NULL we use fast_iov instead */
679 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700680 struct sockaddr __user *uaddr;
681 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700682 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700683};
684
Jens Axboef67676d2019-12-02 11:03:47 -0700685struct io_async_rw {
686 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600687 const struct iovec *free_iovec;
688 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600689 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600690 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700691};
692
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300693enum {
694 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
695 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
696 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
697 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
698 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700699 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300700
Pavel Begunkovdddca222021-04-27 16:13:52 +0100701 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100702 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300703 REQ_F_INFLIGHT_BIT,
704 REQ_F_CUR_POS_BIT,
705 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300706 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300707 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700708 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700709 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000710 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600711 REQ_F_REISSUE_BIT,
Pavel Begunkov8c130822021-03-22 01:58:32 +0000712 REQ_F_DONT_REISSUE_BIT,
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100713 REQ_F_CREDS_BIT,
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100714 REQ_F_REFCOUNT_BIT,
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100715 REQ_F_ARM_LTIMEOUT_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700716 /* keep async read/write and isreg together and in order */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100717 REQ_F_NOWAIT_READ_BIT,
718 REQ_F_NOWAIT_WRITE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700719 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700720
721 /* not a real bit, just to check we're not overflowing the space */
722 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300723};
724
725enum {
726 /* ctx owns file */
727 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
728 /* drain existing IO first */
729 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
730 /* linked sqes */
731 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
732 /* doesn't sever on completion < 0 */
733 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
734 /* IOSQE_ASYNC */
735 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700736 /* IOSQE_BUFFER_SELECT */
737 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300738
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300739 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100740 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000741 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300742 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
743 /* read/write uses file position */
744 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
745 /* must not punt to workers */
746 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100747 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300748 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300749 /* needs cleanup */
750 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700751 /* already went through poll handler */
752 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700753 /* buffer already selected */
754 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000755 /* completion is deferred through io_comp_state */
756 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600757 /* caller should reissue async */
758 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov8c130822021-03-22 01:58:32 +0000759 /* don't attempt request reissue, see io_rw_reissue() */
760 REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700761 /* supports async reads */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100762 REQ_F_NOWAIT_READ = BIT(REQ_F_NOWAIT_READ_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700763 /* supports async writes */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +0100764 REQ_F_NOWAIT_WRITE = BIT(REQ_F_NOWAIT_WRITE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700765 /* regular file */
766 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkovb8e64b52021-06-17 18:14:02 +0100767 /* has creds assigned */
768 REQ_F_CREDS = BIT(REQ_F_CREDS_BIT),
Pavel Begunkov20e60a32021-08-11 19:28:30 +0100769 /* skip refcounting if not set */
770 REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT),
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +0100771 /* there is a linked timeout that has to be armed */
772 REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700773};
774
775struct async_poll {
776 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600777 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300778};
779
Pavel Begunkovf237c302021-08-18 12:42:46 +0100780typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100781
Jens Axboe7cbf1722021-02-10 00:03:20 +0000782struct io_task_work {
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100783 union {
784 struct io_wq_work_node node;
785 struct llist_node fallback_node;
786 };
787 io_req_tw_func_t func;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000788};
789
Pavel Begunkov992da012021-06-10 16:37:37 +0100790enum {
791 IORING_RSRC_FILE = 0,
792 IORING_RSRC_BUFFER = 1,
793};
794
Jens Axboe09bb8392019-03-13 12:39:28 -0600795/*
796 * NOTE! Each of the iocb union members has the file pointer
797 * as the first entry in their struct definition. So you can
798 * access the file pointer through any of the sub-structs,
799 * or directly as just 'ki_filp' in this struct.
800 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700801struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700802 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600803 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700804 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700805 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100806 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700807 struct io_accept accept;
808 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700809 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700810 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100811 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700812 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700813 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700814 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700815 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000816 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700817 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700818 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700819 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300820 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700821 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700822 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600823 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600824 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600825 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300826 /* use only after cleaning per-op data, see io_clean_op() */
827 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700828 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700829
Jens Axboee8c2bc12020-08-15 18:44:09 -0700830 /* opcode allocated if it needs to store data for async defer */
831 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700832 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800833 /* polled IO has completed */
834 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700835
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700836 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300837 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700838
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300839 struct io_ring_ctx *ctx;
840 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700841 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300842 struct task_struct *task;
843 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700844
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000845 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000846 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700847
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100848 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300849 struct list_head inflight_entry;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +0100850 struct io_task_work io_task_work;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300851 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
852 struct hlist_node hash_node;
853 struct async_poll *apoll;
854 struct io_wq_work work;
Pavel Begunkovfe7e3252021-06-24 15:09:57 +0100855 const struct cred *creds;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +0100856
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100857 /* store used ubuf, so we can prevent reloading */
858 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859};
860
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000861struct io_tctx_node {
862 struct list_head ctx_node;
863 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000864 struct io_ring_ctx *ctx;
865};
866
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300867struct io_defer_entry {
868 struct list_head list;
869 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300870 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300871};
872
Jens Axboed3656342019-12-18 09:50:26 -0700873struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700874 /* needs req->file assigned */
875 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700876 /* hash wq insertion if file is a regular file */
877 unsigned hash_reg_file : 1;
878 /* unbound wq insertion if file is a non-regular file */
879 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700880 /* opcode is not supported by this kernel */
881 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700882 /* set if opcode supports polled "wait" */
883 unsigned pollin : 1;
884 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700885 /* op supports buffer selection */
886 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000887 /* do prep async if is going to be punted */
888 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600889 /* should block plug */
890 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700891 /* size of async data needed, if any */
892 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700893};
894
Jens Axboe09186822020-10-13 15:01:40 -0600895static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_NOP] = {},
897 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700898 .needs_file = 1,
899 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700900 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700901 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000902 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600903 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700904 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700907 .needs_file = 1,
908 .hash_reg_file = 1,
909 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700910 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000911 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600912 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700913 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700916 .needs_file = 1,
917 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300918 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700919 .needs_file = 1,
920 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700921 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600922 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700923 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700924 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300925 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700926 .needs_file = 1,
927 .hash_reg_file = 1,
928 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700929 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600930 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700931 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700932 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300933 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700934 .needs_file = 1,
935 .unbound_nonreg_file = 1,
936 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300937 [IORING_OP_POLL_REMOVE] = {},
938 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700939 .needs_file = 1,
940 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300941 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700942 .needs_file = 1,
943 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700944 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000945 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700946 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700947 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300948 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700949 .needs_file = 1,
950 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700951 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700952 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000953 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700954 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700955 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300956 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700957 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700958 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000959 [IORING_OP_TIMEOUT_REMOVE] = {
960 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700963 .needs_file = 1,
964 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700965 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700966 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300967 [IORING_OP_ASYNC_CANCEL] = {},
968 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700969 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700970 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300971 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700972 .needs_file = 1,
973 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700974 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000975 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700976 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700977 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300978 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700979 .needs_file = 1,
980 },
Jens Axboe44526be2021-02-15 13:32:18 -0700981 [IORING_OP_OPENAT] = {},
982 [IORING_OP_CLOSE] = {},
983 [IORING_OP_FILES_UPDATE] = {},
984 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300985 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700986 .needs_file = 1,
987 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700988 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700989 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600990 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700991 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700992 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300993 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700994 .needs_file = 1,
995 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700996 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600997 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700998 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700999 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001000 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -07001001 .needs_file = 1,
1002 },
Jens Axboe44526be2021-02-15 13:32:18 -07001003 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001004 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001005 .needs_file = 1,
1006 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001007 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001008 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001009 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001010 .needs_file = 1,
1011 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001012 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001013 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001014 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001015 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001016 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001017 [IORING_OP_EPOLL_CTL] = {
1018 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001019 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001020 [IORING_OP_SPLICE] = {
1021 .needs_file = 1,
1022 .hash_reg_file = 1,
1023 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001024 },
1025 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001026 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001027 [IORING_OP_TEE] = {
1028 .needs_file = 1,
1029 .hash_reg_file = 1,
1030 .unbound_nonreg_file = 1,
1031 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001032 [IORING_OP_SHUTDOWN] = {
1033 .needs_file = 1,
1034 },
Jens Axboe44526be2021-02-15 13:32:18 -07001035 [IORING_OP_RENAMEAT] = {},
1036 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001037};
1038
Pavel Begunkov0756a862021-08-15 10:40:25 +01001039/* requests with any of those set should undergo io_disarm_next() */
1040#define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL)
1041
Pavel Begunkov7a612352021-03-09 00:37:59 +00001042static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkoveef51da2021-06-14 02:36:15 +01001043static void io_uring_del_tctx_node(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001044static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1045 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001046 bool cancel_all);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01001047static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001048
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001049static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1050 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001051static void io_put_req(struct io_kiocb *req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001052static void io_put_req_deferred(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001053static void io_dismantle_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001054static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001055static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001056 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001057 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001058static void io_clean_op(struct io_kiocb *req);
Pavel Begunkovac177052021-08-09 13:04:02 +01001059static struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001060 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001061static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001062static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001063
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001064static void io_req_task_queue(struct io_kiocb *req);
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01001065static void io_submit_flush_completions(struct io_ring_ctx *ctx);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001066static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001067
Pavel Begunkovb9445592021-08-25 12:25:45 +01001068static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
1069 unsigned int issue_flags, u32 slot_index);
1070
Jens Axboe2b188cc2019-01-07 10:46:33 -07001071static struct kmem_cache *req_cachep;
1072
Jens Axboe09186822020-10-13 15:01:40 -06001073static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074
1075struct sock *io_uring_get_socket(struct file *file)
1076{
1077#if defined(CONFIG_UNIX)
1078 if (file->f_op == &io_uring_fops) {
1079 struct io_ring_ctx *ctx = file->private_data;
1080
1081 return ctx->ring_sock->sk;
1082 }
1083#endif
1084 return NULL;
1085}
1086EXPORT_SYMBOL(io_uring_get_socket);
1087
Pavel Begunkovf237c302021-08-18 12:42:46 +01001088static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked)
1089{
1090 if (!*locked) {
1091 mutex_lock(&ctx->uring_lock);
1092 *locked = true;
1093 }
1094}
1095
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001096#define io_for_each_link(pos, head) \
1097 for (pos = (head); pos; pos = pos->link)
1098
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001099/*
1100 * Shamelessly stolen from the mm implementation of page reference checking,
1101 * see commit f958d7b528b1 for details.
1102 */
1103#define req_ref_zero_or_close_to_overflow(req) \
1104 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1105
1106static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
1107{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001108 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001109 return atomic_inc_not_zero(&req->refs);
1110}
1111
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001112static inline bool req_ref_put_and_test(struct io_kiocb *req)
1113{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001114 if (likely(!(req->flags & REQ_F_REFCOUNT)))
1115 return true;
1116
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001117 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1118 return atomic_dec_and_test(&req->refs);
1119}
1120
1121static inline void req_ref_put(struct io_kiocb *req)
1122{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001123 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001124 WARN_ON_ONCE(req_ref_put_and_test(req));
1125}
1126
1127static inline void req_ref_get(struct io_kiocb *req)
1128{
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001129 WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT));
Pavel Begunkov21c843d2021-08-11 19:28:27 +01001130 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1131 atomic_inc(&req->refs);
1132}
1133
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001134static inline void __io_req_set_refcount(struct io_kiocb *req, int nr)
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001135{
1136 if (!(req->flags & REQ_F_REFCOUNT)) {
1137 req->flags |= REQ_F_REFCOUNT;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001138 atomic_set(&req->refs, nr);
Pavel Begunkov20e60a32021-08-11 19:28:30 +01001139 }
1140}
1141
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001142static inline void io_req_set_refcount(struct io_kiocb *req)
1143{
1144 __io_req_set_refcount(req, 1);
1145}
1146
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001147static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001148{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001149 struct io_ring_ctx *ctx = req->ctx;
1150
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001151 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001152 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001153 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001154 }
1155}
1156
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001157static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1158{
1159 bool got = percpu_ref_tryget(ref);
1160
1161 /* already at zero, wait for ->release() */
1162 if (!got)
1163 wait_for_completion(compl);
1164 percpu_ref_resurrect(ref);
1165 if (got)
1166 percpu_ref_put(ref);
1167}
1168
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001169static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1170 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001171{
1172 struct io_kiocb *req;
1173
Pavel Begunkov68207682021-03-22 01:58:25 +00001174 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001175 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001176 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001177 return true;
1178
1179 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001180 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001181 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001182 }
1183 return false;
1184}
1185
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001186static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001187{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001188 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001189}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001190
Jens Axboe2b188cc2019-01-07 10:46:33 -07001191static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1192{
1193 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1194
Jens Axboe0f158b42020-05-14 17:18:39 -06001195 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001196}
1197
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001198static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1199{
1200 return !req->timeout.off;
1201}
1202
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001203static void io_fallback_req_func(struct work_struct *work)
1204{
1205 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1206 fallback_work.work);
1207 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1208 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001209 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001210
1211 percpu_ref_get(&ctx->refs);
1212 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001213 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001214
Pavel Begunkovf237c302021-08-18 12:42:46 +01001215 if (locked) {
1216 if (ctx->submit_state.compl_nr)
1217 io_submit_flush_completions(ctx);
1218 mutex_unlock(&ctx->uring_lock);
1219 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001220 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001221
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001222}
1223
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1225{
1226 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001227 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228
1229 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1230 if (!ctx)
1231 return NULL;
1232
Jens Axboe78076bb2019-12-04 19:56:40 -07001233 /*
1234 * Use 5 bits less than the max cq entries, that should give us around
1235 * 32 entries per hash list if totally full and uniformly spread.
1236 */
1237 hash_bits = ilog2(p->cq_entries);
1238 hash_bits -= 5;
1239 if (hash_bits <= 0)
1240 hash_bits = 1;
1241 ctx->cancel_hash_bits = hash_bits;
1242 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1243 GFP_KERNEL);
1244 if (!ctx->cancel_hash)
1245 goto err;
1246 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1247
Pavel Begunkov62248432021-04-28 13:11:29 +01001248 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1249 if (!ctx->dummy_ubuf)
1250 goto err;
1251 /* set invalid range, so io_import_fixed() fails meeting it */
1252 ctx->dummy_ubuf->ubuf = -1UL;
1253
Roman Gushchin21482892019-05-07 10:01:48 -07001254 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001255 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1256 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257
1258 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001259 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001260 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001261 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001262 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001263 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001264 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001265 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001267 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001269 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001270 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001271 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001272 INIT_LIST_HEAD(&ctx->timeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001273 spin_lock_init(&ctx->rsrc_ref_lock);
1274 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001275 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1276 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001277 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001278 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001279 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001280 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001281 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001282err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001283 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001284 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001285 kfree(ctx);
1286 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287}
1288
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001289static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1290{
1291 struct io_rings *r = ctx->rings;
1292
1293 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1294 ctx->cq_extra--;
1295}
1296
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001297static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001298{
Jens Axboe2bc99302020-07-09 09:43:27 -06001299 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1300 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001301
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001302 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001303 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001304
Bob Liu9d858b22019-11-13 18:06:25 +08001305 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001306}
1307
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001308#define FFS_ASYNC_READ 0x1UL
1309#define FFS_ASYNC_WRITE 0x2UL
1310#ifdef CONFIG_64BIT
1311#define FFS_ISREG 0x4UL
1312#else
1313#define FFS_ISREG 0x0UL
1314#endif
1315#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1316
1317static inline bool io_req_ffs_set(struct io_kiocb *req)
1318{
1319 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1320}
1321
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001322static void io_req_track_inflight(struct io_kiocb *req)
1323{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001324 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001325 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001326 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001327 }
1328}
1329
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001330static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1331{
1332 req->flags &= ~REQ_F_LINK_TIMEOUT;
1333}
1334
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001335static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1336{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001337 if (WARN_ON_ONCE(!req->link))
1338 return NULL;
1339
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001340 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1341 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001342
1343 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001344 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001345 __io_req_set_refcount(req->link, 2);
1346 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001347}
1348
1349static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1350{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001351 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001352 return NULL;
1353 return __io_prep_linked_timeout(req);
1354}
1355
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001356static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001357{
Jens Axboed3656342019-12-18 09:50:26 -07001358 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001359 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001360
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001361 if (!(req->flags & REQ_F_CREDS)) {
1362 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001363 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001364 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001365
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001366 req->work.list.next = NULL;
1367 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001368 if (req->flags & REQ_F_FORCE_ASYNC)
1369 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1370
Jens Axboed3656342019-12-18 09:50:26 -07001371 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001372 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001373 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001374 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001375 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001376 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001377 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001378
1379 switch (req->opcode) {
1380 case IORING_OP_SPLICE:
1381 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001382 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1383 req->work.flags |= IO_WQ_WORK_UNBOUND;
1384 break;
1385 }
Jens Axboe561fb042019-10-24 07:25:42 -06001386}
1387
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001388static void io_prep_async_link(struct io_kiocb *req)
1389{
1390 struct io_kiocb *cur;
1391
Pavel Begunkov44eff402021-07-26 14:14:31 +01001392 if (req->flags & REQ_F_LINK_TIMEOUT) {
1393 struct io_ring_ctx *ctx = req->ctx;
1394
Jens Axboe79ebeae2021-08-10 15:18:27 -06001395 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001396 io_for_each_link(cur, req)
1397 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001398 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001399 } else {
1400 io_for_each_link(cur, req)
1401 io_prep_async_work(cur);
1402 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001403}
1404
Pavel Begunkovf237c302021-08-18 12:42:46 +01001405static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001406{
Jackie Liua197f662019-11-08 08:09:12 -07001407 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001408 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001409 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001410
Pavel Begunkovf237c302021-08-18 12:42:46 +01001411 /* must not take the lock, NULL it as a precaution */
1412 locked = NULL;
1413
Jens Axboe3bfe6102021-02-16 14:15:30 -07001414 BUG_ON(!tctx);
1415 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001416
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001417 /* init ->work of the whole link before punting */
1418 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001419
1420 /*
1421 * Not expected to happen, but if we do have a bug where this _can_
1422 * happen, catch it here and ensure the request is marked as
1423 * canceled. That will make io-wq go through the usual work cancel
1424 * procedure rather than attempt to run this request (or create a new
1425 * worker for it).
1426 */
1427 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1428 req->work.flags |= IO_WQ_WORK_CANCEL;
1429
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001430 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1431 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001432 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001433 if (link)
1434 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001435}
1436
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001437static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001438 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001439 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001440{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001441 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001442
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001443 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001444 atomic_set(&req->ctx->cq_timeouts,
1445 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001446 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001447 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001448 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001449 }
1450}
1451
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001452static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001453{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001454 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001455 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1456 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001457
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001458 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001459 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001460 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001461 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001462 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001463 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001464}
1465
Pavel Begunkov360428f2020-05-30 14:54:17 +03001466static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001467 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001468{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001469 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001470
Jens Axboe79ebeae2021-08-10 15:18:27 -06001471 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001472 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001473 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001474 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001475 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001476
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001477 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001478 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001479
1480 /*
1481 * Since seq can easily wrap around over time, subtract
1482 * the last seq at which timeouts were flushed before comparing.
1483 * Assuming not more than 2^31-1 events have happened since,
1484 * these subtractions won't have wrapped, so we can check if
1485 * target is in [last_seq, current_seq] by comparing the two.
1486 */
1487 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1488 events_got = seq - ctx->cq_last_tm_flush;
1489 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001490 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001491
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001492 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001493 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001494 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001495 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001496 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001497}
1498
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001499static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001500{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001501 if (ctx->off_timeout_used)
1502 io_flush_timeouts(ctx);
1503 if (ctx->drain_active)
1504 io_queue_deferred(ctx);
1505}
1506
1507static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1508{
1509 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1510 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001511 /* order cqe stores with ring update */
1512 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001513}
1514
Jens Axboe90554202020-09-03 12:12:41 -06001515static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1516{
1517 struct io_rings *r = ctx->rings;
1518
Pavel Begunkova566c552021-05-16 22:58:08 +01001519 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001520}
1521
Pavel Begunkov888aae22021-01-19 13:32:39 +00001522static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1523{
1524 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1525}
1526
Pavel Begunkovd068b502021-05-16 22:58:11 +01001527static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528{
Hristo Venev75b28af2019-08-26 17:23:46 +00001529 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001530 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001531
Stefan Bühler115e12e2019-04-24 23:54:18 +02001532 /*
1533 * writes to the cq entry need to come after reading head; the
1534 * control dependency is enough as we're using WRITE_ONCE to
1535 * fill the cq entry
1536 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001537 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538 return NULL;
1539
Pavel Begunkov888aae22021-01-19 13:32:39 +00001540 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001541 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001542}
1543
Jens Axboef2842ab2020-01-08 11:04:00 -07001544static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1545{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001546 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001547 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001548 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1549 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001550 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001551}
1552
Jens Axboe2c5d7632021-08-21 07:21:19 -06001553/*
1554 * This should only get called when at least one event has been posted.
1555 * Some applications rely on the eventfd notification count only changing
1556 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1557 * 1:1 relationship between how many times this function is called (and
1558 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1559 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001560static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001561{
Jens Axboe5fd46172021-08-06 14:04:31 -06001562 /*
1563 * wake_up_all() may seem excessive, but io_wake_function() and
1564 * io_should_wake() handle the termination of the loop and only
1565 * wake as many waiters as we need to.
1566 */
1567 if (wq_has_sleeper(&ctx->cq_wait))
1568 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001569 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1570 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001571 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001572 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001573 if (waitqueue_active(&ctx->poll_wait)) {
1574 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001575 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1576 }
Jens Axboe8c838782019-03-12 15:48:16 -06001577}
1578
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001579static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1580{
1581 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe5fd46172021-08-06 14:04:31 -06001582 if (wq_has_sleeper(&ctx->cq_wait))
1583 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001584 }
1585 if (io_should_trigger_evfd(ctx))
1586 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001587 if (waitqueue_active(&ctx->poll_wait)) {
1588 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001589 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1590 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001591}
1592
Jens Axboec4a2ed72019-11-21 21:01:26 -07001593/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001594static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595{
Jens Axboeb18032b2021-01-24 16:58:56 -07001596 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001597
Pavel Begunkova566c552021-05-16 22:58:08 +01001598 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001599 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001600
Jens Axboeb18032b2021-01-24 16:58:56 -07001601 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001602 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001603 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001604 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001605 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001606
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001607 if (!cqe && !force)
1608 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001609 ocqe = list_first_entry(&ctx->cq_overflow_list,
1610 struct io_overflow_cqe, list);
1611 if (cqe)
1612 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1613 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001614 io_account_cq_overflow(ctx);
1615
Jens Axboeb18032b2021-01-24 16:58:56 -07001616 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001617 list_del(&ocqe->list);
1618 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001619 }
1620
Pavel Begunkov09e88402020-12-17 00:24:38 +00001621 all_flushed = list_empty(&ctx->cq_overflow_list);
1622 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001623 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001624 WRITE_ONCE(ctx->rings->sq_flags,
1625 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001626 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001627
Jens Axboeb18032b2021-01-24 16:58:56 -07001628 if (posted)
1629 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001630 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001631 if (posted)
1632 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001633 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001634}
1635
Pavel Begunkov90f67362021-08-09 20:18:12 +01001636static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001637{
Jens Axboeca0a2652021-03-04 17:15:48 -07001638 bool ret = true;
1639
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001640 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001641 /* iopoll syncs against uring_lock, not completion_lock */
1642 if (ctx->flags & IORING_SETUP_IOPOLL)
1643 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001644 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001645 if (ctx->flags & IORING_SETUP_IOPOLL)
1646 mutex_unlock(&ctx->uring_lock);
1647 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001648
1649 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001650}
1651
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001652/* must to be called somewhat shortly after putting a request */
1653static inline void io_put_task(struct task_struct *task, int nr)
1654{
1655 struct io_uring_task *tctx = task->io_uring;
1656
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001657 if (likely(task == current)) {
1658 tctx->cached_refs += nr;
1659 } else {
1660 percpu_counter_sub(&tctx->inflight, nr);
1661 if (unlikely(atomic_read(&tctx->in_idle)))
1662 wake_up(&tctx->wait);
1663 put_task_struct_many(task, nr);
1664 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001665}
1666
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001667static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1668 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001669{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001670 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001671
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001672 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1673 if (!ocqe) {
1674 /*
1675 * If we're in ring overflow flush mode, or in task cancel mode,
1676 * or cannot allocate an overflow entry, then we need to drop it
1677 * on the floor.
1678 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001679 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001680 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001682 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001683 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001684 WRITE_ONCE(ctx->rings->sq_flags,
1685 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1686
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001687 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001688 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001689 ocqe->cqe.res = res;
1690 ocqe->cqe.flags = cflags;
1691 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1692 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693}
1694
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001695static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1696 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001697{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001698 struct io_uring_cqe *cqe;
1699
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001700 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001701
1702 /*
1703 * If we can't get a cq entry, userspace overflowed the
1704 * submission (by quite a lot). Increment the overflow count in
1705 * the ring.
1706 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001707 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001709 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001710 WRITE_ONCE(cqe->res, res);
1711 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001712 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001714 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715}
1716
Pavel Begunkov8d133262021-04-11 01:46:33 +01001717/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001718static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1719 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001720{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001721 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001722}
1723
Pavel Begunkov7a612352021-03-09 00:37:59 +00001724static void io_req_complete_post(struct io_kiocb *req, long res,
1725 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001727 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728
Jens Axboe79ebeae2021-08-10 15:18:27 -06001729 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001730 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001731 /*
1732 * If we're the last reference to this request, add to our locked
1733 * free_list cache.
1734 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001735 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001736 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001737 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001738 io_disarm_next(req);
1739 if (req->link) {
1740 io_req_task_queue(req->link);
1741 req->link = NULL;
1742 }
1743 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001744 io_dismantle_req(req);
1745 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001746 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001747 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001748 } else {
1749 if (!percpu_ref_tryget(&ctx->refs))
1750 req = NULL;
1751 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001752 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001753 spin_unlock(&ctx->completion_lock);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001754
Pavel Begunkov180f8292021-03-14 20:57:09 +00001755 if (req) {
1756 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001757 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001758 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759}
1760
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001761static inline bool io_req_needs_clean(struct io_kiocb *req)
1762{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001763 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001764}
1765
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001766static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001767 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001768{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001769 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001770 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001771 req->result = res;
1772 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001773 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001774}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775
Pavel Begunkov889fca72021-02-10 00:03:09 +00001776static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1777 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001778{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001779 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1780 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001781 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001782 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001783}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001784
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001785static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001786{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001787 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001788}
1789
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001790static void io_req_complete_failed(struct io_kiocb *req, long res)
1791{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001792 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001793 io_req_complete_post(req, res, 0);
1794}
1795
Pavel Begunkov864ea922021-08-09 13:04:08 +01001796/*
1797 * Don't initialise the fields below on every allocation, but do that in
1798 * advance and keep them valid across allocations.
1799 */
1800static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1801{
1802 req->ctx = ctx;
1803 req->link = NULL;
1804 req->async_data = NULL;
1805 /* not necessary, but safer to zero */
1806 req->result = 0;
1807}
1808
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001809static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001810 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001811{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001812 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001813 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001814 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001815 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001816}
1817
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001818/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001819static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001820{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001821 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001822 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001823
Jens Axboec7dae4b2021-02-09 19:53:37 -07001824 /*
1825 * If we have more than a batch's worth of requests in our IRQ side
1826 * locked cache, grab the lock and move them over to our submission
1827 * side cache.
1828 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001829 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001830 io_flush_cached_locked_reqs(ctx, state);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001831
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001832 nr = state->free_reqs;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001833 while (!list_empty(&state->free_list)) {
1834 struct io_kiocb *req = list_first_entry(&state->free_list,
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001835 struct io_kiocb, inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001836
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001837 list_del(&req->inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001838 state->reqs[nr++] = req;
1839 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001840 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001841 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001843 state->free_reqs = nr;
1844 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001845}
1846
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001847/*
1848 * A request might get retired back into the request caches even before opcode
1849 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1850 * Because of that, io_alloc_req() should be called only under ->uring_lock
1851 * and with extra caution to not get a request that is still worked on.
1852 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001853static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001854 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001856 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001857 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1858 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001859
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01001860 BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861
Pavel Begunkov864ea922021-08-09 13:04:08 +01001862 if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
1863 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001864
Pavel Begunkov864ea922021-08-09 13:04:08 +01001865 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1866 state->reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001867
Pavel Begunkov864ea922021-08-09 13:04:08 +01001868 /*
1869 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1870 * retry single alloc to be on the safe side.
1871 */
1872 if (unlikely(ret <= 0)) {
1873 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1874 if (!state->reqs[0])
1875 return NULL;
1876 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001878
1879 for (i = 0; i < ret; i++)
1880 io_preinit_req(state->reqs[i], ctx);
1881 state->free_reqs = ret;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001882got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001883 state->free_reqs--;
1884 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001885}
1886
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001887static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001888{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001889 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001890 fput(file);
1891}
1892
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001893static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001895 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001896
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001897 if (io_req_needs_clean(req))
1898 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001899 if (!(flags & REQ_F_FIXED_FILE))
1900 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001901 if (req->fixed_rsrc_refs)
1902 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001903 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001904 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001905 req->async_data = NULL;
1906 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001907}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001908
Pavel Begunkov216578e2020-10-13 09:44:00 +01001909static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001910{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001911 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001912
Pavel Begunkov216578e2020-10-13 09:44:00 +01001913 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001914 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001915
Jens Axboe79ebeae2021-08-10 15:18:27 -06001916 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001917 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001918 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001919 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001920
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001921 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001922}
1923
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001924static inline void io_remove_next_linked(struct io_kiocb *req)
1925{
1926 struct io_kiocb *nxt = req->link;
1927
1928 req->link = nxt->link;
1929 nxt->link = NULL;
1930}
1931
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001932static bool io_kill_linked_timeout(struct io_kiocb *req)
1933 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06001934 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001935{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001936 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001937
Pavel Begunkovb97e7362021-08-15 10:40:23 +01001938 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001939 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001940
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001941 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001942 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001943 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001944 io_cqring_fill_event(link->ctx, link->user_data,
1945 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001946 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001947 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001948 }
1949 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001950 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001951}
1952
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001953static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001954 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001955{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001956 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06001957
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001958 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001959 while (link) {
1960 nxt = link->link;
1961 link->link = NULL;
1962
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001963 trace_io_uring_fail_link(req, link);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001964 io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001965 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001966 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001967 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001968}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001969
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001970static bool io_disarm_next(struct io_kiocb *req)
1971 __must_hold(&req->ctx->completion_lock)
1972{
1973 bool posted = false;
1974
Pavel Begunkov0756a862021-08-15 10:40:25 +01001975 if (req->flags & REQ_F_ARM_LTIMEOUT) {
1976 struct io_kiocb *link = req->link;
1977
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001978 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01001979 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
1980 io_remove_next_linked(req);
1981 io_cqring_fill_event(link->ctx, link->user_data,
1982 -ECANCELED, 0);
1983 io_put_req_deferred(link);
1984 posted = true;
1985 }
1986 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06001987 struct io_ring_ctx *ctx = req->ctx;
1988
1989 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001990 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06001991 spin_unlock_irq(&ctx->timeout_lock);
1992 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001993 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01001994 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001995 posted |= (req->link != NULL);
1996 io_fail_links(req);
1997 }
1998 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06001999}
2000
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002001static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002002{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002003 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002004
Jens Axboe9e645e112019-05-10 16:07:28 -06002005 /*
2006 * If LINK is set, we have dependent requests in this chain. If we
2007 * didn't fail this request, queue the first one up, moving any other
2008 * dependencies to the next request. In case of failure, fail the rest
2009 * of the chain.
2010 */
Pavel Begunkov0756a862021-08-15 10:40:25 +01002011 if (req->flags & IO_DISARM_MASK) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002012 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002013 bool posted;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002014
Jens Axboe79ebeae2021-08-10 15:18:27 -06002015 spin_lock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002016 posted = io_disarm_next(req);
2017 if (posted)
2018 io_commit_cqring(req->ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002019 spin_unlock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002020 if (posted)
2021 io_cqring_ev_posted(ctx);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002022 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002023 nxt = req->link;
2024 req->link = NULL;
2025 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002026}
Jens Axboe2665abf2019-11-05 12:40:47 -07002027
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002028static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002029{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002030 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002031 return NULL;
2032 return __io_req_find_next(req);
2033}
2034
Pavel Begunkovf237c302021-08-18 12:42:46 +01002035static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002036{
2037 if (!ctx)
2038 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002039 if (*locked) {
Hao Xu99c8bc52021-08-21 06:19:54 +08002040 if (ctx->submit_state.compl_nr)
2041 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002042 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002043 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002044 }
2045 percpu_ref_put(&ctx->refs);
2046}
2047
Jens Axboe7cbf1722021-02-10 00:03:20 +00002048static void tctx_task_work(struct callback_head *cb)
2049{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002050 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002051 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002052 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2053 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002054
Pavel Begunkov16f72072021-06-17 18:14:09 +01002055 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002056 struct io_wq_work_node *node;
2057
2058 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002059 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002060 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002061 if (!node)
2062 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002063 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002064 if (!node)
2065 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002066
Pavel Begunkov6294f362021-08-10 17:53:55 +01002067 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002068 struct io_wq_work_node *next = node->next;
2069 struct io_kiocb *req = container_of(node, struct io_kiocb,
2070 io_task_work.node);
2071
2072 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002073 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002074 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002075 /* if not contended, grab and improve batching */
2076 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002077 percpu_ref_get(&ctx->refs);
2078 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002079 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002080 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002081 } while (node);
2082
Jens Axboe7cbf1722021-02-10 00:03:20 +00002083 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002084 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002085
Pavel Begunkovf237c302021-08-18 12:42:46 +01002086 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002087}
2088
Pavel Begunkove09ee512021-07-01 13:26:05 +01002089static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002090{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002091 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002092 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002093 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002094 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002095 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002096 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002097
2098 WARN_ON_ONCE(!tctx);
2099
Jens Axboe0b81e802021-02-16 10:33:53 -07002100 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002101 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002102 running = tctx->task_running;
2103 if (!running)
2104 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002105 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002106
2107 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002108 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002109 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002110
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002111 /*
2112 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2113 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2114 * processing task_work. There's no reliable way to tell if TWA_RESUME
2115 * will do the job.
2116 */
2117 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002118 if (!task_work_add(tsk, &tctx->task_work, notify)) {
2119 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002120 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002121 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002122
Pavel Begunkove09ee512021-07-01 13:26:05 +01002123 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002124 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002125 node = tctx->task_list.first;
2126 INIT_WQ_LIST(&tctx->task_list);
2127 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002128
Pavel Begunkove09ee512021-07-01 13:26:05 +01002129 while (node) {
2130 req = container_of(node, struct io_kiocb, io_task_work.node);
2131 node = node->next;
2132 if (llist_add(&req->io_task_work.fallback_node,
2133 &req->ctx->fallback_llist))
2134 schedule_delayed_work(&req->ctx->fallback_work, 1);
2135 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002136}
2137
Pavel Begunkovf237c302021-08-18 12:42:46 +01002138static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002139{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002140 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002141
Pavel Begunkove83acd72021-02-28 22:35:09 +00002142 /* ctx is guaranteed to stay alive while we hold uring_lock */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002143 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002144 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002145}
2146
Pavel Begunkovf237c302021-08-18 12:42:46 +01002147static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002148{
2149 struct io_ring_ctx *ctx = req->ctx;
2150
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002151 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002152 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002153 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002154 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002155 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002156 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002157 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002158}
2159
Pavel Begunkova3df76982021-02-18 22:32:52 +00002160static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2161{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002162 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002163 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002164 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002165}
2166
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002167static void io_req_task_queue(struct io_kiocb *req)
2168{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002169 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002170 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002171}
2172
Jens Axboe773af692021-07-27 10:25:55 -06002173static void io_req_task_queue_reissue(struct io_kiocb *req)
2174{
2175 req->io_task_work.func = io_queue_async_work;
2176 io_req_task_work_add(req);
2177}
2178
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002179static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002180{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002181 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002182
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002183 if (nxt)
2184 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002185}
2186
Jens Axboe9e645e112019-05-10 16:07:28 -06002187static void io_free_req(struct io_kiocb *req)
2188{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002189 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002190 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002191}
2192
Pavel Begunkovf237c302021-08-18 12:42:46 +01002193static void io_free_req_work(struct io_kiocb *req, bool *locked)
2194{
2195 io_free_req(req);
2196}
2197
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002198struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002199 struct task_struct *task;
2200 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002201 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002202};
2203
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002204static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002205{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002206 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002207 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002208 rb->task = NULL;
2209}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002210
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002211static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2212 struct req_batch *rb)
2213{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002214 if (rb->ctx_refs)
2215 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002216 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002217 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002218}
2219
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002220static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2221 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002222{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002223 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002224 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002225
Jens Axboee3bc8e92020-09-24 08:45:57 -06002226 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002227 if (rb->task)
2228 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002229 rb->task = req->task;
2230 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002231 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002232 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002233 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002234
Pavel Begunkovbd759042021-02-12 03:23:50 +00002235 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002236 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002237 else
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002238 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002239}
2240
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01002241static void io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002242 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002243{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002244 struct io_submit_state *state = &ctx->submit_state;
2245 int i, nr = state->compl_nr;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002246 struct req_batch rb;
2247
Jens Axboe79ebeae2021-08-10 15:18:27 -06002248 spin_lock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002249 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002250 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002251
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002252 __io_cqring_fill_event(ctx, req->user_data, req->result,
2253 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002254 }
2255 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002256 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002257 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002258
2259 io_init_req_batch(&rb);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002260 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002261 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov905c1722021-02-10 00:03:14 +00002262
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002263 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002264 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002265 }
2266
2267 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002268 state->compl_nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002269}
2270
Jens Axboeba816ad2019-09-28 11:36:45 -06002271/*
2272 * Drop reference to request, return next in chain (if there is one) if this
2273 * was the last reference to this request.
2274 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002275static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002276{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002277 struct io_kiocb *nxt = NULL;
2278
Jens Axboede9b4cc2021-02-24 13:28:27 -07002279 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002280 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002281 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002282 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002283 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284}
2285
Pavel Begunkov0d850352021-03-19 17:22:37 +00002286static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002287{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002288 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002289 io_free_req(req);
2290}
2291
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002292static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002293{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002294 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002295 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002296 io_req_task_work_add(req);
2297 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002298}
2299
Pavel Begunkov6c503152021-01-04 20:36:36 +00002300static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002301{
2302 /* See comment at the top of this file */
2303 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002304 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002305}
2306
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002307static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2308{
2309 struct io_rings *rings = ctx->rings;
2310
2311 /* make sure SQ entry isn't read before tail */
2312 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2313}
2314
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002315static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002316{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002317 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002318
Jens Axboebcda7ba2020-02-23 16:42:51 -07002319 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2320 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002321 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002322 kfree(kbuf);
2323 return cflags;
2324}
2325
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002326static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2327{
2328 struct io_buffer *kbuf;
2329
Pavel Begunkovae421d92021-08-17 20:28:08 +01002330 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2331 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002332 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2333 return io_put_kbuf(req, kbuf);
2334}
2335
Jens Axboe4c6e2772020-07-01 11:29:10 -06002336static inline bool io_run_task_work(void)
2337{
Nadav Amitef98eb02021-08-07 17:13:41 -07002338 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002339 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002340 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002341 return true;
2342 }
2343
2344 return false;
2345}
2346
Jens Axboedef596e2019-01-09 08:59:42 -07002347/*
2348 * Find and free completed poll iocbs
2349 */
2350static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002351 struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002352{
Jens Axboe8237e042019-12-28 10:48:22 -07002353 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002354 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002355
2356 /* order with ->result store in io_complete_rw_iopoll() */
2357 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002358
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002359 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002360 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002361 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002362 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002363
Pavel Begunkova8576af2021-08-15 10:40:21 +01002364 if (READ_ONCE(req->result) == -EAGAIN &&
Pavel Begunkov8c130822021-03-22 01:58:32 +00002365 !(req->flags & REQ_F_DONT_REISSUE)) {
Pavel Begunkovf1613402021-02-11 18:28:21 +00002366 req->iopoll_completed = 0;
Jens Axboe773af692021-07-27 10:25:55 -06002367 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002368 continue;
Pavel Begunkovf1613402021-02-11 18:28:21 +00002369 }
2370
Pavel Begunkovae421d92021-08-17 20:28:08 +01002371 __io_cqring_fill_event(ctx, req->user_data, req->result,
2372 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002373 (*nr_events)++;
2374
Jens Axboede9b4cc2021-02-24 13:28:27 -07002375 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002376 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002377 }
Jens Axboedef596e2019-01-09 08:59:42 -07002378
Jens Axboe09bb8392019-03-13 12:39:28 -06002379 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002380 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002381 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002382}
2383
Jens Axboedef596e2019-01-09 08:59:42 -07002384static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002385 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002386{
2387 struct io_kiocb *req, *tmp;
2388 LIST_HEAD(done);
2389 bool spin;
Jens Axboedef596e2019-01-09 08:59:42 -07002390
2391 /*
2392 * Only spin for completions if we don't have multiple devices hanging
2393 * off our complete list, and we're under the requested amount.
2394 */
Hao Xu915b3dd2021-06-28 05:37:30 +08002395 spin = !ctx->poll_multi_queue && *nr_events < min;
Jens Axboedef596e2019-01-09 08:59:42 -07002396
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002397 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002398 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002399 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002400
2401 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002402 * Move completed and retryable entries to our local lists.
2403 * If we find a request that requires polling, break out
2404 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002405 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002406 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002407 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002408 continue;
2409 }
2410 if (!list_empty(&done))
2411 break;
2412
2413 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002414 if (unlikely(ret < 0))
2415 return ret;
2416 else if (ret)
2417 spin = false;
Jens Axboedef596e2019-01-09 08:59:42 -07002418
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002419 /* iopoll may have completed current req */
2420 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002421 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002422 }
2423
2424 if (!list_empty(&done))
Pavel Begunkova8576af2021-08-15 10:40:21 +01002425 io_iopoll_complete(ctx, nr_events, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002426
Pavel Begunkova2416e12021-08-09 13:04:09 +01002427 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002428}
2429
2430/*
Jens Axboedef596e2019-01-09 08:59:42 -07002431 * We can't just wait for polled events to come to us, we have to actively
2432 * find and complete them.
2433 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002434static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002435{
2436 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2437 return;
2438
2439 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002440 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002441 unsigned int nr_events = 0;
2442
Pavel Begunkova8576af2021-08-15 10:40:21 +01002443 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002444
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002445 /* let it sleep and repeat later if can't complete a request */
2446 if (nr_events == 0)
2447 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002448 /*
2449 * Ensure we allow local-to-the-cpu processing to take place,
2450 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002451 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002452 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002453 if (need_resched()) {
2454 mutex_unlock(&ctx->uring_lock);
2455 cond_resched();
2456 mutex_lock(&ctx->uring_lock);
2457 }
Jens Axboedef596e2019-01-09 08:59:42 -07002458 }
2459 mutex_unlock(&ctx->uring_lock);
2460}
2461
Pavel Begunkov7668b922020-07-07 16:36:21 +03002462static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002463{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002464 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002465 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002466
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002467 /*
2468 * We disallow the app entering submit/complete with polling, but we
2469 * still need to lock the ring to prevent racing with polled issue
2470 * that got punted to a workqueue.
2471 */
2472 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002473 /*
2474 * Don't enter poll loop if we already have events pending.
2475 * If we do, we can potentially be spinning for commands that
2476 * already triggered a CQE (eg in error).
2477 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002478 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002479 __io_cqring_overflow_flush(ctx, false);
2480 if (io_cqring_events(ctx))
2481 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002482 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002483 /*
2484 * If a submit got punted to a workqueue, we can have the
2485 * application entering polling for a command before it gets
2486 * issued. That app will hold the uring_lock for the duration
2487 * of the poll right here, so we need to take a breather every
2488 * now and then to ensure that the issue has a chance to add
2489 * the poll to the issued list. Otherwise we can spin here
2490 * forever, while the workqueue is stuck trying to acquire the
2491 * very same mutex.
2492 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002493 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002494 u32 tail = ctx->cached_cq_tail;
2495
Jens Axboe500f9fb2019-08-19 12:15:59 -06002496 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002497 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002498 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002499
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002500 /* some requests don't go through iopoll_list */
2501 if (tail != ctx->cached_cq_tail ||
2502 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002503 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002504 }
Pavel Begunkova8576af2021-08-15 10:40:21 +01002505 ret = io_do_iopoll(ctx, &nr_events, min);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002506 } while (!ret && nr_events < min && !need_resched());
2507out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002508 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002509 return ret;
2510}
2511
Jens Axboe491381ce2019-10-17 09:20:46 -06002512static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002513{
Jens Axboe491381ce2019-10-17 09:20:46 -06002514 /*
2515 * Tell lockdep we inherited freeze protection from submission
2516 * thread.
2517 */
2518 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002519 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520
Pavel Begunkov1c986792021-03-22 01:58:31 +00002521 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2522 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523 }
2524}
2525
Jens Axboeb63534c2020-06-04 11:28:00 -06002526#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002527static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002528{
Pavel Begunkovab454432021-03-22 01:58:33 +00002529 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002530
Pavel Begunkovab454432021-03-22 01:58:33 +00002531 if (!rw)
2532 return !io_req_prep_async(req);
2533 /* may have left rw->iter inconsistent on -EIOCBQUEUED */
2534 iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter));
2535 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002536}
Jens Axboeb63534c2020-06-04 11:28:00 -06002537
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002538static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002539{
Jens Axboe355afae2020-09-02 09:30:31 -06002540 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002541 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002542
Jens Axboe355afae2020-09-02 09:30:31 -06002543 if (!S_ISBLK(mode) && !S_ISREG(mode))
2544 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002545 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2546 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002547 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002548 /*
2549 * If ref is dying, we might be running poll reap from the exit work.
2550 * Don't attempt to reissue from that path, just let it fail with
2551 * -EAGAIN.
2552 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002553 if (percpu_ref_is_dying(&ctx->refs))
2554 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002555 /*
2556 * Play it safe and assume not safe to re-import and reissue if we're
2557 * not in the original thread group (or in task context).
2558 */
2559 if (!same_thread_group(req->task, current) || !in_task())
2560 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002561 return true;
2562}
Jens Axboee82ad482021-04-02 19:45:34 -06002563#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002564static bool io_resubmit_prep(struct io_kiocb *req)
2565{
2566 return false;
2567}
Jens Axboee82ad482021-04-02 19:45:34 -06002568static bool io_rw_should_reissue(struct io_kiocb *req)
2569{
2570 return false;
2571}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002572#endif
2573
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002574static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002575{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002576 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2577 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002578 if (res != req->result) {
2579 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2580 io_rw_should_reissue(req)) {
2581 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002582 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002583 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002584 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002585 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002586 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002587 return false;
2588}
2589
Pavel Begunkovf237c302021-08-18 12:42:46 +01002590static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002591{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002592 unsigned int cflags = io_put_rw_kbuf(req);
2593 long res = req->result;
2594
2595 if (*locked) {
2596 struct io_ring_ctx *ctx = req->ctx;
2597 struct io_submit_state *state = &ctx->submit_state;
2598
2599 io_req_complete_state(req, res, cflags);
2600 state->compl_reqs[state->compl_nr++] = req;
2601 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
2602 io_submit_flush_completions(ctx);
2603 } else {
2604 io_req_complete_post(req, res, cflags);
2605 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002606}
2607
2608static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2609 unsigned int issue_flags)
2610{
2611 if (__io_complete_rw_common(req, res))
2612 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002613 __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002614}
2615
2616static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2617{
Jens Axboe9adbd452019-12-20 08:45:55 -07002618 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002619
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002620 if (__io_complete_rw_common(req, res))
2621 return;
2622 req->result = res;
2623 req->io_task_work.func = io_req_task_complete;
2624 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625}
2626
Jens Axboedef596e2019-01-09 08:59:42 -07002627static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2628{
Jens Axboe9adbd452019-12-20 08:45:55 -07002629 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002630
Jens Axboe491381ce2019-10-17 09:20:46 -06002631 if (kiocb->ki_flags & IOCB_WRITE)
2632 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002633 if (unlikely(res != req->result)) {
Jens Axboea1ff1e32021-04-12 06:40:02 -06002634 if (!(res == -EAGAIN && io_rw_should_reissue(req) &&
2635 io_resubmit_prep(req))) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002636 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002637 req->flags |= REQ_F_DONT_REISSUE;
2638 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002639 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002640
2641 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002642 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002643 smp_wmb();
2644 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002645}
2646
2647/*
2648 * After the iocb has been issued, it's safe to be found on the poll list.
2649 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002650 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002651 * accessing the kiocb cookie.
2652 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002653static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002654{
2655 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002656 const bool in_async = io_wq_current_is_worker();
2657
2658 /* workqueue context doesn't hold uring_lock, grab it now */
2659 if (unlikely(in_async))
2660 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002661
2662 /*
2663 * Track whether we have multiple files in our lists. This will impact
2664 * how we do polling eventually, not spinning if we're on potentially
2665 * different devices.
2666 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002667 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002668 ctx->poll_multi_queue = false;
2669 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002670 struct io_kiocb *list_req;
Hao Xu915b3dd2021-06-28 05:37:30 +08002671 unsigned int queue_num0, queue_num1;
Jens Axboedef596e2019-01-09 08:59:42 -07002672
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002673 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002674 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002675
2676 if (list_req->file != req->file) {
2677 ctx->poll_multi_queue = true;
2678 } else {
2679 queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie);
2680 queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie);
2681 if (queue_num0 != queue_num1)
2682 ctx->poll_multi_queue = true;
2683 }
Jens Axboedef596e2019-01-09 08:59:42 -07002684 }
2685
2686 /*
2687 * For fast devices, IO may have already completed. If it has, add
2688 * it to the front so we find it first.
2689 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002690 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002691 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002692 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002693 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002694
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002695 if (unlikely(in_async)) {
2696 /*
2697 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2698 * in sq thread task context or in io worker task context. If
2699 * current task context is sq thread, we don't need to check
2700 * whether should wake up sq thread.
2701 */
2702 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2703 wq_has_sleeper(&ctx->sq_data->wait))
2704 wake_up(&ctx->sq_data->wait);
2705
2706 mutex_unlock(&ctx->uring_lock);
2707 }
Jens Axboedef596e2019-01-09 08:59:42 -07002708}
2709
Jens Axboe4503b762020-06-01 10:00:27 -06002710static bool io_bdev_nowait(struct block_device *bdev)
2711{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002712 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002713}
2714
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715/*
2716 * If we tracked the file through the SCM inflight mechanism, we could support
2717 * any file. For now, just ensure that anything potentially problematic is done
2718 * inline.
2719 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002720static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002721{
2722 umode_t mode = file_inode(file)->i_mode;
2723
Jens Axboe4503b762020-06-01 10:00:27 -06002724 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002725 if (IS_ENABLED(CONFIG_BLOCK) &&
2726 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002727 return true;
2728 return false;
2729 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002730 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002731 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002732 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002733 if (IS_ENABLED(CONFIG_BLOCK) &&
2734 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002735 file->f_op != &io_uring_fops)
2736 return true;
2737 return false;
2738 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002739
Jens Axboec5b85622020-06-09 19:23:05 -06002740 /* any ->read/write should understand O_NONBLOCK */
2741 if (file->f_flags & O_NONBLOCK)
2742 return true;
2743
Jens Axboeaf197f52020-04-28 13:15:06 -06002744 if (!(file->f_mode & FMODE_NOWAIT))
2745 return false;
2746
2747 if (rw == READ)
2748 return file->f_op->read_iter != NULL;
2749
2750 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002751}
2752
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002753static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002754{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002755 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002756 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002757 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002758 return true;
2759
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002760 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002761}
2762
Pavel Begunkova88fc402020-09-30 22:57:53 +03002763static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764{
Jens Axboedef596e2019-01-09 08:59:42 -07002765 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002766 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002767 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002768 unsigned ioprio;
2769 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002770
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002771 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002772 req->flags |= REQ_F_ISREG;
2773
Jens Axboe2b188cc2019-01-07 10:46:33 -07002774 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002775 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002776 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002777 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002778 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002779 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002780 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2781 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2782 if (unlikely(ret))
2783 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002785 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2786 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2787 req->flags |= REQ_F_NOWAIT;
2788
Jens Axboe2b188cc2019-01-07 10:46:33 -07002789 ioprio = READ_ONCE(sqe->ioprio);
2790 if (ioprio) {
2791 ret = ioprio_check_cap(ioprio);
2792 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002793 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002794
2795 kiocb->ki_ioprio = ioprio;
2796 } else
2797 kiocb->ki_ioprio = get_current_ioprio();
2798
Jens Axboedef596e2019-01-09 08:59:42 -07002799 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002800 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2801 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002802 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803
Jens Axboedef596e2019-01-09 08:59:42 -07002804 kiocb->ki_flags |= IOCB_HIPRI;
2805 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002806 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002807 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002808 if (kiocb->ki_flags & IOCB_HIPRI)
2809 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002810 kiocb->ki_complete = io_complete_rw;
2811 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002812
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002813 if (req->opcode == IORING_OP_READ_FIXED ||
2814 req->opcode == IORING_OP_WRITE_FIXED) {
2815 req->imu = NULL;
2816 io_req_set_rsrc_node(req);
2817 }
2818
Jens Axboe3529d8c2019-12-19 18:24:38 -07002819 req->rw.addr = READ_ONCE(sqe->addr);
2820 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002821 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002822 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002823}
2824
2825static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2826{
2827 switch (ret) {
2828 case -EIOCBQUEUED:
2829 break;
2830 case -ERESTARTSYS:
2831 case -ERESTARTNOINTR:
2832 case -ERESTARTNOHAND:
2833 case -ERESTART_RESTARTBLOCK:
2834 /*
2835 * We can't just restart the syscall, since previously
2836 * submitted sqes may already be in progress. Just fail this
2837 * IO with EINTR.
2838 */
2839 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002840 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002841 default:
2842 kiocb->ki_complete(kiocb, ret, 0);
2843 }
2844}
2845
Jens Axboea1d7c392020-06-22 11:09:46 -06002846static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002847 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002848{
Jens Axboeba042912019-12-25 16:33:42 -07002849 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002850 struct io_async_rw *io = req->async_data;
Pavel Begunkov97284632021-04-08 19:28:03 +01002851 bool check_reissue = kiocb->ki_complete == io_complete_rw;
Jens Axboeba042912019-12-25 16:33:42 -07002852
Jens Axboe227c0c92020-08-13 11:51:40 -06002853 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002854 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002855 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002856 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002857 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002858 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002859 }
2860
Jens Axboeba042912019-12-25 16:33:42 -07002861 if (req->flags & REQ_F_CUR_POS)
2862 req->file->f_pos = kiocb->ki_pos;
Hao Xue149bd742021-06-28 05:48:05 +08002863 if (ret >= 0 && check_reissue)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002864 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002865 else
2866 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002867
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01002868 if (check_reissue && (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002869 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002870 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002871 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002872 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002873 req_set_fail(req);
Pavel Begunkovae421d92021-08-17 20:28:08 +01002874 __io_req_complete(req, issue_flags, ret,
2875 io_put_rw_kbuf(req));
Pavel Begunkov97284632021-04-08 19:28:03 +01002876 }
2877 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002878}
2879
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002880static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2881 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002882{
Jens Axboe9adbd452019-12-20 08:45:55 -07002883 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002884 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002885 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002886
Pavel Begunkov75769e32021-04-01 15:43:54 +01002887 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002888 return -EFAULT;
2889 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002890 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002891 return -EFAULT;
2892
2893 /*
2894 * May not be a start of buffer, set size appropriately
2895 * and advance us to the beginning.
2896 */
2897 offset = buf_addr - imu->ubuf;
2898 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002899
2900 if (offset) {
2901 /*
2902 * Don't use iov_iter_advance() here, as it's really slow for
2903 * using the latter parts of a big fixed buffer - it iterates
2904 * over each segment manually. We can cheat a bit here, because
2905 * we know that:
2906 *
2907 * 1) it's a BVEC iter, we set it up
2908 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2909 * first and last bvec
2910 *
2911 * So just find our index, and adjust the iterator afterwards.
2912 * If the offset is within the first bvec (or the whole first
2913 * bvec, just use iov_iter_advance(). This makes it easier
2914 * since we can just skip the first segment, which may not
2915 * be PAGE_SIZE aligned.
2916 */
2917 const struct bio_vec *bvec = imu->bvec;
2918
2919 if (offset <= bvec->bv_len) {
2920 iov_iter_advance(iter, offset);
2921 } else {
2922 unsigned long seg_skip;
2923
2924 /* skip first vec */
2925 offset -= bvec->bv_len;
2926 seg_skip = 1 + (offset >> PAGE_SHIFT);
2927
2928 iter->bvec = bvec + seg_skip;
2929 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002930 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002931 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002932 }
2933 }
2934
Pavel Begunkov847595d2021-02-04 13:52:06 +00002935 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002936}
2937
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002938static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
2939{
2940 struct io_ring_ctx *ctx = req->ctx;
2941 struct io_mapped_ubuf *imu = req->imu;
2942 u16 index, buf_index = req->buf_index;
2943
2944 if (likely(!imu)) {
2945 if (unlikely(buf_index >= ctx->nr_user_bufs))
2946 return -EFAULT;
2947 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2948 imu = READ_ONCE(ctx->user_bufs[index]);
2949 req->imu = imu;
2950 }
2951 return __io_import_fixed(req, rw, iter, imu);
2952}
2953
Jens Axboebcda7ba2020-02-23 16:42:51 -07002954static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2955{
2956 if (needs_lock)
2957 mutex_unlock(&ctx->uring_lock);
2958}
2959
2960static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2961{
2962 /*
2963 * "Normal" inline submissions always hold the uring_lock, since we
2964 * grab it from the system call. Same is true for the SQPOLL offload.
2965 * The only exception is when we've detached the request and issue it
2966 * from an async worker thread, grab the lock for that case.
2967 */
2968 if (needs_lock)
2969 mutex_lock(&ctx->uring_lock);
2970}
2971
2972static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2973 int bgid, struct io_buffer *kbuf,
2974 bool needs_lock)
2975{
2976 struct io_buffer *head;
2977
2978 if (req->flags & REQ_F_BUFFER_SELECTED)
2979 return kbuf;
2980
2981 io_ring_submit_lock(req->ctx, needs_lock);
2982
2983 lockdep_assert_held(&req->ctx->uring_lock);
2984
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002985 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002986 if (head) {
2987 if (!list_empty(&head->list)) {
2988 kbuf = list_last_entry(&head->list, struct io_buffer,
2989 list);
2990 list_del(&kbuf->list);
2991 } else {
2992 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002993 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002994 }
2995 if (*len > kbuf->len)
2996 *len = kbuf->len;
2997 } else {
2998 kbuf = ERR_PTR(-ENOBUFS);
2999 }
3000
3001 io_ring_submit_unlock(req->ctx, needs_lock);
3002
3003 return kbuf;
3004}
3005
Jens Axboe4d954c22020-02-27 07:31:19 -07003006static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3007 bool needs_lock)
3008{
3009 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003010 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003011
3012 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003013 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003014 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3015 if (IS_ERR(kbuf))
3016 return kbuf;
3017 req->rw.addr = (u64) (unsigned long) kbuf;
3018 req->flags |= REQ_F_BUFFER_SELECTED;
3019 return u64_to_user_ptr(kbuf->addr);
3020}
3021
3022#ifdef CONFIG_COMPAT
3023static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3024 bool needs_lock)
3025{
3026 struct compat_iovec __user *uiov;
3027 compat_ssize_t clen;
3028 void __user *buf;
3029 ssize_t len;
3030
3031 uiov = u64_to_user_ptr(req->rw.addr);
3032 if (!access_ok(uiov, sizeof(*uiov)))
3033 return -EFAULT;
3034 if (__get_user(clen, &uiov->iov_len))
3035 return -EFAULT;
3036 if (clen < 0)
3037 return -EINVAL;
3038
3039 len = clen;
3040 buf = io_rw_buffer_select(req, &len, needs_lock);
3041 if (IS_ERR(buf))
3042 return PTR_ERR(buf);
3043 iov[0].iov_base = buf;
3044 iov[0].iov_len = (compat_size_t) len;
3045 return 0;
3046}
3047#endif
3048
3049static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3050 bool needs_lock)
3051{
3052 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3053 void __user *buf;
3054 ssize_t len;
3055
3056 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3057 return -EFAULT;
3058
3059 len = iov[0].iov_len;
3060 if (len < 0)
3061 return -EINVAL;
3062 buf = io_rw_buffer_select(req, &len, needs_lock);
3063 if (IS_ERR(buf))
3064 return PTR_ERR(buf);
3065 iov[0].iov_base = buf;
3066 iov[0].iov_len = len;
3067 return 0;
3068}
3069
3070static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3071 bool needs_lock)
3072{
Jens Axboedddb3e22020-06-04 11:27:01 -06003073 if (req->flags & REQ_F_BUFFER_SELECTED) {
3074 struct io_buffer *kbuf;
3075
3076 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3077 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3078 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003079 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003080 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003081 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003082 return -EINVAL;
3083
3084#ifdef CONFIG_COMPAT
3085 if (req->ctx->compat)
3086 return io_compat_import(req, iov, needs_lock);
3087#endif
3088
3089 return __io_iov_buffer_select(req, iov, needs_lock);
3090}
3091
Pavel Begunkov847595d2021-02-04 13:52:06 +00003092static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3093 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003094{
Jens Axboe9adbd452019-12-20 08:45:55 -07003095 void __user *buf = u64_to_user_ptr(req->rw.addr);
3096 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003097 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003098 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003099
Pavel Begunkov7d009162019-11-25 23:14:40 +03003100 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003101 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003102 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003103 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003104
Jens Axboebcda7ba2020-02-23 16:42:51 -07003105 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003106 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003107 return -EINVAL;
3108
Jens Axboe3a6820f2019-12-22 15:19:35 -07003109 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003110 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003111 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003112 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003113 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003114 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003115 }
3116
Jens Axboe3a6820f2019-12-22 15:19:35 -07003117 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3118 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003119 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003120 }
3121
Jens Axboe4d954c22020-02-27 07:31:19 -07003122 if (req->flags & REQ_F_BUFFER_SELECT) {
3123 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003124 if (!ret)
3125 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003126 *iovec = NULL;
3127 return ret;
3128 }
3129
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003130 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3131 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003132}
3133
Jens Axboe0fef9482020-08-26 10:36:20 -06003134static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3135{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003136 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003137}
3138
Jens Axboe32960612019-09-23 11:05:34 -06003139/*
3140 * For files that don't have ->read_iter() and ->write_iter(), handle them
3141 * by looping over ->read() or ->write() manually.
3142 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003143static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003144{
Jens Axboe4017eb92020-10-22 14:14:12 -06003145 struct kiocb *kiocb = &req->rw.kiocb;
3146 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003147 ssize_t ret = 0;
3148
3149 /*
3150 * Don't support polled IO through this interface, and we can't
3151 * support non-blocking either. For the latter, this just causes
3152 * the kiocb to be handled from an async context.
3153 */
3154 if (kiocb->ki_flags & IOCB_HIPRI)
3155 return -EOPNOTSUPP;
3156 if (kiocb->ki_flags & IOCB_NOWAIT)
3157 return -EAGAIN;
3158
3159 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003160 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003161 ssize_t nr;
3162
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003163 if (!iov_iter_is_bvec(iter)) {
3164 iovec = iov_iter_iovec(iter);
3165 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003166 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3167 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003168 }
3169
Jens Axboe32960612019-09-23 11:05:34 -06003170 if (rw == READ) {
3171 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003172 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003173 } else {
3174 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003175 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003176 }
3177
3178 if (nr < 0) {
3179 if (!ret)
3180 ret = nr;
3181 break;
3182 }
3183 ret += nr;
3184 if (nr != iovec.iov_len)
3185 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003186 req->rw.len -= nr;
3187 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003188 iov_iter_advance(iter, nr);
3189 }
3190
3191 return ret;
3192}
3193
Jens Axboeff6165b2020-08-13 09:47:43 -06003194static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3195 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003196{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003197 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003198
Jens Axboeff6165b2020-08-13 09:47:43 -06003199 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003200 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003201 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003202 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003203 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003204 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003205 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003206 unsigned iov_off = 0;
3207
3208 rw->iter.iov = rw->fast_iov;
3209 if (iter->iov != fast_iov) {
3210 iov_off = iter->iov - fast_iov;
3211 rw->iter.iov += iov_off;
3212 }
3213 if (rw->fast_iov != fast_iov)
3214 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003215 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003216 } else {
3217 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003218 }
3219}
3220
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003221static inline int io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003222{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003223 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3224 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3225 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003226}
3227
Jens Axboeff6165b2020-08-13 09:47:43 -06003228static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3229 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003230 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003231{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003232 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003233 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003234 if (!req->async_data) {
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003235 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003236 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003237 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003238 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003239
Jens Axboeff6165b2020-08-13 09:47:43 -06003240 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003241 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003242 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003243}
3244
Pavel Begunkov73debe62020-09-30 22:57:54 +03003245static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003246{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003247 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003248 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003249 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003250
Pavel Begunkov2846c482020-11-07 13:16:27 +00003251 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003252 if (unlikely(ret < 0))
3253 return ret;
3254
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003255 iorw->bytes_done = 0;
3256 iorw->free_iovec = iov;
3257 if (iov)
3258 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003259 return 0;
3260}
3261
Pavel Begunkov73debe62020-09-30 22:57:54 +03003262static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003263{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003264 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3265 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003266 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003267}
3268
Jens Axboec1dd91d2020-08-03 16:43:59 -06003269/*
3270 * This is our waitqueue callback handler, registered through lock_page_async()
3271 * when we initially tried to do the IO with the iocb armed our waitqueue.
3272 * This gets called when the page is unlocked, and we generally expect that to
3273 * happen when the page IO is completed and the page is now uptodate. This will
3274 * queue a task_work based retry of the operation, attempting to copy the data
3275 * again. If the latter fails because the page was NOT uptodate, then we will
3276 * do a thread based blocking retry of the operation. That's the unexpected
3277 * slow path.
3278 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003279static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3280 int sync, void *arg)
3281{
3282 struct wait_page_queue *wpq;
3283 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003284 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003285
3286 wpq = container_of(wait, struct wait_page_queue, wait);
3287
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003288 if (!wake_page_match(wpq, key))
3289 return 0;
3290
Hao Xuc8d317a2020-09-29 20:00:45 +08003291 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003292 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003293 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003294 return 1;
3295}
3296
Jens Axboec1dd91d2020-08-03 16:43:59 -06003297/*
3298 * This controls whether a given IO request should be armed for async page
3299 * based retry. If we return false here, the request is handed to the async
3300 * worker threads for retry. If we're doing buffered reads on a regular file,
3301 * we prepare a private wait_page_queue entry and retry the operation. This
3302 * will either succeed because the page is now uptodate and unlocked, or it
3303 * will register a callback when the page is unlocked at IO completion. Through
3304 * that callback, io_uring uses task_work to setup a retry of the operation.
3305 * That retry will attempt the buffered read again. The retry will generally
3306 * succeed, or in rare cases where it fails, we then fall back to using the
3307 * async worker threads for a blocking retry.
3308 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003309static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003310{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003311 struct io_async_rw *rw = req->async_data;
3312 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003313 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003314
3315 /* never retry for NOWAIT, we just complete with -EAGAIN */
3316 if (req->flags & REQ_F_NOWAIT)
3317 return false;
3318
Jens Axboe227c0c92020-08-13 11:51:40 -06003319 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003320 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003321 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003322
Jens Axboebcf5a062020-05-22 09:24:42 -06003323 /*
3324 * just use poll if we can, and don't attempt if the fs doesn't
3325 * support callback based unlocks
3326 */
3327 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3328 return false;
3329
Jens Axboe3b2a4432020-08-16 10:58:43 -07003330 wait->wait.func = io_async_buf_func;
3331 wait->wait.private = req;
3332 wait->wait.flags = 0;
3333 INIT_LIST_HEAD(&wait->wait.entry);
3334 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003335 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003336 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003337 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003338}
3339
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003340static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003341{
3342 if (req->file->f_op->read_iter)
3343 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003344 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003345 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003346 else
3347 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003348}
3349
Pavel Begunkov889fca72021-02-10 00:03:09 +00003350static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003351{
3352 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003353 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003354 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003355 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003356 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003357 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003358
Pavel Begunkov2846c482020-11-07 13:16:27 +00003359 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003360 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003361 iovec = NULL;
3362 } else {
3363 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3364 if (ret < 0)
3365 return ret;
3366 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003367 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003368 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003369
Jens Axboefd6c2e42019-12-18 12:19:41 -07003370 /* Ensure we clear previously set non-block flag */
3371 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003372 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003373 else
3374 kiocb->ki_flags |= IOCB_NOWAIT;
3375
Pavel Begunkov24c74672020-06-21 13:09:51 +03003376 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003377 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003378 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003379 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003380 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003381
Pavel Begunkov632546c2020-11-07 13:16:26 +00003382 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003383 if (unlikely(ret)) {
3384 kfree(iovec);
3385 return ret;
3386 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387
Jens Axboe227c0c92020-08-13 11:51:40 -06003388 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003389
Jens Axboe230d50d2021-04-01 20:41:15 -06003390 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003391 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003392 /* IOPOLL retry should happen for io-wq threads */
3393 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003394 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003395 /* no retry on NONBLOCK nor RWF_NOWAIT */
3396 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003397 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003398 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003399 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003400 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003401 } else if (ret == -EIOCBQUEUED) {
3402 goto out_free;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003403 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003404 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003405 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003406 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003407 }
3408
Jens Axboe227c0c92020-08-13 11:51:40 -06003409 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003410 if (ret2)
3411 return ret2;
3412
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003413 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003414 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003415 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003416 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003417
Pavel Begunkovb23df912021-02-04 13:52:04 +00003418 do {
3419 io_size -= ret;
3420 rw->bytes_done += ret;
3421 /* if we can retry, do so with the callbacks armed */
3422 if (!io_rw_should_retry(req)) {
3423 kiocb->ki_flags &= ~IOCB_WAITQ;
3424 return -EAGAIN;
3425 }
3426
3427 /*
3428 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3429 * we get -EIOCBQUEUED, then we'll get a notification when the
3430 * desired page gets unlocked. We can also get a partial read
3431 * here, and if we do, then just retry at the new offset.
3432 */
3433 ret = io_iter_do_read(req, iter);
3434 if (ret == -EIOCBQUEUED)
3435 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003437 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003438 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003439done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003440 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003441out_free:
3442 /* it's faster to check here then delegate to kfree */
3443 if (iovec)
3444 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003445 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003446}
3447
Pavel Begunkov73debe62020-09-30 22:57:54 +03003448static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003449{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003450 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3451 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003452 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003453}
3454
Pavel Begunkov889fca72021-02-10 00:03:09 +00003455static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003456{
3457 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003458 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003459 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003460 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003461 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003462 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003463
Pavel Begunkov2846c482020-11-07 13:16:27 +00003464 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003465 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003466 iovec = NULL;
3467 } else {
3468 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3469 if (ret < 0)
3470 return ret;
3471 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003472 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003473 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003474
Jens Axboefd6c2e42019-12-18 12:19:41 -07003475 /* Ensure we clear previously set non-block flag */
3476 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003477 kiocb->ki_flags &= ~IOCB_NOWAIT;
3478 else
3479 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003480
Pavel Begunkov24c74672020-06-21 13:09:51 +03003481 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003482 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003483 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003484
Jens Axboe10d59342019-12-09 20:16:22 -07003485 /* file path doesn't support NOWAIT for non-direct_IO */
3486 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3487 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003488 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003489
Pavel Begunkov632546c2020-11-07 13:16:26 +00003490 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003491 if (unlikely(ret))
3492 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003493
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003494 /*
3495 * Open-code file_start_write here to grab freeze protection,
3496 * which will be released by another thread in
3497 * io_complete_rw(). Fool lockdep by telling it the lock got
3498 * released so that it doesn't complain about the held lock when
3499 * we return to userspace.
3500 */
3501 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003502 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003503 __sb_writers_release(file_inode(req->file)->i_sb,
3504 SB_FREEZE_WRITE);
3505 }
3506 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003507
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003508 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003509 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003510 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003511 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003512 else
3513 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003514
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003515 if (req->flags & REQ_F_REISSUE) {
3516 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003517 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003518 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003519
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003520 /*
3521 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3522 * retry them without IOCB_NOWAIT.
3523 */
3524 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3525 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003526 /* no retry on NONBLOCK nor RWF_NOWAIT */
3527 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003528 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003529 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003530 /* IOPOLL retry should happen for io-wq threads */
3531 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3532 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003533done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003534 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003535 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003536copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003537 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003538 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003539 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003540 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003541 }
Jens Axboe31b51512019-01-18 22:56:34 -07003542out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003543 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003544 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003545 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003546 return ret;
3547}
3548
Jens Axboe80a261f2020-09-28 14:23:58 -06003549static int io_renameat_prep(struct io_kiocb *req,
3550 const struct io_uring_sqe *sqe)
3551{
3552 struct io_rename *ren = &req->rename;
3553 const char __user *oldf, *newf;
3554
Jens Axboeed7eb252021-06-23 09:04:13 -06003555 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3556 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003557 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003558 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003559 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3560 return -EBADF;
3561
3562 ren->old_dfd = READ_ONCE(sqe->fd);
3563 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3564 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3565 ren->new_dfd = READ_ONCE(sqe->len);
3566 ren->flags = READ_ONCE(sqe->rename_flags);
3567
3568 ren->oldpath = getname(oldf);
3569 if (IS_ERR(ren->oldpath))
3570 return PTR_ERR(ren->oldpath);
3571
3572 ren->newpath = getname(newf);
3573 if (IS_ERR(ren->newpath)) {
3574 putname(ren->oldpath);
3575 return PTR_ERR(ren->newpath);
3576 }
3577
3578 req->flags |= REQ_F_NEED_CLEANUP;
3579 return 0;
3580}
3581
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003582static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003583{
3584 struct io_rename *ren = &req->rename;
3585 int ret;
3586
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003587 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003588 return -EAGAIN;
3589
3590 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3591 ren->newpath, ren->flags);
3592
3593 req->flags &= ~REQ_F_NEED_CLEANUP;
3594 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003595 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003596 io_req_complete(req, ret);
3597 return 0;
3598}
3599
Jens Axboe14a11432020-09-28 14:27:37 -06003600static int io_unlinkat_prep(struct io_kiocb *req,
3601 const struct io_uring_sqe *sqe)
3602{
3603 struct io_unlink *un = &req->unlink;
3604 const char __user *fname;
3605
Jens Axboe22634bc2021-06-23 09:07:45 -06003606 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3607 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003608 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3609 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003610 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003611 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3612 return -EBADF;
3613
3614 un->dfd = READ_ONCE(sqe->fd);
3615
3616 un->flags = READ_ONCE(sqe->unlink_flags);
3617 if (un->flags & ~AT_REMOVEDIR)
3618 return -EINVAL;
3619
3620 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3621 un->filename = getname(fname);
3622 if (IS_ERR(un->filename))
3623 return PTR_ERR(un->filename);
3624
3625 req->flags |= REQ_F_NEED_CLEANUP;
3626 return 0;
3627}
3628
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003629static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003630{
3631 struct io_unlink *un = &req->unlink;
3632 int ret;
3633
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003634 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003635 return -EAGAIN;
3636
3637 if (un->flags & AT_REMOVEDIR)
3638 ret = do_rmdir(un->dfd, un->filename);
3639 else
3640 ret = do_unlinkat(un->dfd, un->filename);
3641
3642 req->flags &= ~REQ_F_NEED_CLEANUP;
3643 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003644 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003645 io_req_complete(req, ret);
3646 return 0;
3647}
3648
Jens Axboe36f4fa62020-09-05 11:14:22 -06003649static int io_shutdown_prep(struct io_kiocb *req,
3650 const struct io_uring_sqe *sqe)
3651{
3652#if defined(CONFIG_NET)
3653 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3654 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003655 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3656 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003657 return -EINVAL;
3658
3659 req->shutdown.how = READ_ONCE(sqe->len);
3660 return 0;
3661#else
3662 return -EOPNOTSUPP;
3663#endif
3664}
3665
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003666static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003667{
3668#if defined(CONFIG_NET)
3669 struct socket *sock;
3670 int ret;
3671
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003672 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003673 return -EAGAIN;
3674
Linus Torvalds48aba792020-12-16 12:44:05 -08003675 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003676 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003677 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003678
3679 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003680 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003681 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003682 io_req_complete(req, ret);
3683 return 0;
3684#else
3685 return -EOPNOTSUPP;
3686#endif
3687}
3688
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003689static int __io_splice_prep(struct io_kiocb *req,
3690 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003691{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003692 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003693 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003694
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003695 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3696 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003697
3698 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003699 sp->len = READ_ONCE(sqe->len);
3700 sp->flags = READ_ONCE(sqe->splice_flags);
3701
3702 if (unlikely(sp->flags & ~valid_flags))
3703 return -EINVAL;
3704
Pavel Begunkov62906e82021-08-10 14:52:47 +01003705 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003706 (sp->flags & SPLICE_F_FD_IN_FIXED));
3707 if (!sp->file_in)
3708 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003709 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003710 return 0;
3711}
3712
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003713static int io_tee_prep(struct io_kiocb *req,
3714 const struct io_uring_sqe *sqe)
3715{
3716 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3717 return -EINVAL;
3718 return __io_splice_prep(req, sqe);
3719}
3720
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003721static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003722{
3723 struct io_splice *sp = &req->splice;
3724 struct file *in = sp->file_in;
3725 struct file *out = sp->file_out;
3726 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3727 long ret = 0;
3728
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003729 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003730 return -EAGAIN;
3731 if (sp->len)
3732 ret = do_tee(in, out, sp->len, flags);
3733
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003734 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3735 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003736 req->flags &= ~REQ_F_NEED_CLEANUP;
3737
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003738 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003739 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003740 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003741 return 0;
3742}
3743
3744static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3745{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003746 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003747
3748 sp->off_in = READ_ONCE(sqe->splice_off_in);
3749 sp->off_out = READ_ONCE(sqe->off);
3750 return __io_splice_prep(req, sqe);
3751}
3752
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003753static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003754{
3755 struct io_splice *sp = &req->splice;
3756 struct file *in = sp->file_in;
3757 struct file *out = sp->file_out;
3758 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3759 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003760 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003761
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003762 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003763 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003764
3765 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3766 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003767
Jens Axboe948a7742020-05-17 14:21:38 -06003768 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003769 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003770
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003771 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3772 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003773 req->flags &= ~REQ_F_NEED_CLEANUP;
3774
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003775 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003776 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003777 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003778 return 0;
3779}
3780
Jens Axboe2b188cc2019-01-07 10:46:33 -07003781/*
3782 * IORING_OP_NOP just posts a completion event, nothing else.
3783 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003784static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003785{
3786 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003787
Jens Axboedef596e2019-01-09 08:59:42 -07003788 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3789 return -EINVAL;
3790
Pavel Begunkov889fca72021-02-10 00:03:09 +00003791 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003792 return 0;
3793}
3794
Pavel Begunkov1155c762021-02-18 18:29:38 +00003795static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003796{
Jens Axboe6b063142019-01-10 22:13:58 -07003797 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003798
Jens Axboe09bb8392019-03-13 12:39:28 -06003799 if (!req->file)
3800 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003801
Jens Axboe6b063142019-01-10 22:13:58 -07003802 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003803 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003804 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
3805 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003806 return -EINVAL;
3807
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003808 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3809 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3810 return -EINVAL;
3811
3812 req->sync.off = READ_ONCE(sqe->off);
3813 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003814 return 0;
3815}
3816
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003817static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003818{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003819 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003820 int ret;
3821
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003822 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003823 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003824 return -EAGAIN;
3825
Jens Axboe9adbd452019-12-20 08:45:55 -07003826 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003827 end > 0 ? end : LLONG_MAX,
3828 req->sync.flags & IORING_FSYNC_DATASYNC);
3829 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003830 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003831 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003832 return 0;
3833}
3834
Jens Axboed63d1b52019-12-10 10:38:56 -07003835static int io_fallocate_prep(struct io_kiocb *req,
3836 const struct io_uring_sqe *sqe)
3837{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003838 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
3839 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07003840 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003841 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3842 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003843
3844 req->sync.off = READ_ONCE(sqe->off);
3845 req->sync.len = READ_ONCE(sqe->addr);
3846 req->sync.mode = READ_ONCE(sqe->len);
3847 return 0;
3848}
3849
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003850static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003851{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003852 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003853
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003854 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003855 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003856 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003857 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3858 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003859 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003860 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003861 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003862 return 0;
3863}
3864
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003865static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003866{
Jens Axboef8748882020-01-08 17:47:02 -07003867 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003868 int ret;
3869
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003870 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3871 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01003872 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003873 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003874 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003875 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003876
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003877 /* open.how should be already initialised */
3878 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003879 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003880
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003881 req->open.dfd = READ_ONCE(sqe->fd);
3882 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003883 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003884 if (IS_ERR(req->open.filename)) {
3885 ret = PTR_ERR(req->open.filename);
3886 req->open.filename = NULL;
3887 return ret;
3888 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01003889
3890 req->open.file_slot = READ_ONCE(sqe->file_index);
3891 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
3892 return -EINVAL;
3893
Jens Axboe4022e7a2020-03-19 19:23:18 -06003894 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003895 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003896 return 0;
3897}
3898
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003899static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3900{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003901 u64 mode = READ_ONCE(sqe->len);
3902 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003903
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003904 req->open.how = build_open_how(flags, mode);
3905 return __io_openat_prep(req, sqe);
3906}
3907
Jens Axboecebdb982020-01-08 17:59:24 -07003908static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3909{
3910 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003911 size_t len;
3912 int ret;
3913
Jens Axboecebdb982020-01-08 17:59:24 -07003914 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3915 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003916 if (len < OPEN_HOW_SIZE_VER0)
3917 return -EINVAL;
3918
3919 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3920 len);
3921 if (ret)
3922 return ret;
3923
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003924 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003925}
3926
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003927static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003928{
3929 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003930 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01003931 bool resolve_nonblock, nonblock_set;
3932 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003933 int ret;
3934
Jens Axboecebdb982020-01-08 17:59:24 -07003935 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003936 if (ret)
3937 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003938 nonblock_set = op.open_flag & O_NONBLOCK;
3939 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003940 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003941 /*
3942 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3943 * it'll always -EAGAIN
3944 */
3945 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3946 return -EAGAIN;
3947 op.lookup_flags |= LOOKUP_CACHED;
3948 op.open_flag |= O_NONBLOCK;
3949 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003950
Pavel Begunkovb9445592021-08-25 12:25:45 +01003951 if (!fixed) {
3952 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
3953 if (ret < 0)
3954 goto err;
3955 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003956
3957 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003958 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003959 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003960 * We could hang on to this 'fd' on retrying, but seems like
3961 * marginal gain for something that is now known to be a slower
3962 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07003963 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01003964 if (!fixed)
3965 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003966
3967 ret = PTR_ERR(file);
3968 /* only retry if RESOLVE_CACHED wasn't already set by application */
3969 if (ret == -EAGAIN &&
3970 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
3971 return -EAGAIN;
3972 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003973 }
3974
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003975 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
3976 file->f_flags &= ~O_NONBLOCK;
3977 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01003978
3979 if (!fixed)
3980 fd_install(ret, file);
3981 else
3982 ret = io_install_fixed_file(req, file, issue_flags,
3983 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003984err:
3985 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003986 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003987 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003988 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01003989 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003990 return 0;
3991}
3992
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003993static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003994{
Pavel Begunkove45cff52021-02-28 22:35:14 +00003995 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07003996}
3997
Jens Axboe067524e2020-03-02 16:32:28 -07003998static int io_remove_buffers_prep(struct io_kiocb *req,
3999 const struct io_uring_sqe *sqe)
4000{
4001 struct io_provide_buf *p = &req->pbuf;
4002 u64 tmp;
4003
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004004 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4005 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004006 return -EINVAL;
4007
4008 tmp = READ_ONCE(sqe->fd);
4009 if (!tmp || tmp > USHRT_MAX)
4010 return -EINVAL;
4011
4012 memset(p, 0, sizeof(*p));
4013 p->nbufs = tmp;
4014 p->bgid = READ_ONCE(sqe->buf_group);
4015 return 0;
4016}
4017
4018static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4019 int bgid, unsigned nbufs)
4020{
4021 unsigned i = 0;
4022
4023 /* shouldn't happen */
4024 if (!nbufs)
4025 return 0;
4026
4027 /* the head kbuf is the list itself */
4028 while (!list_empty(&buf->list)) {
4029 struct io_buffer *nxt;
4030
4031 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4032 list_del(&nxt->list);
4033 kfree(nxt);
4034 if (++i == nbufs)
4035 return i;
4036 }
4037 i++;
4038 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004039 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004040
4041 return i;
4042}
4043
Pavel Begunkov889fca72021-02-10 00:03:09 +00004044static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004045{
4046 struct io_provide_buf *p = &req->pbuf;
4047 struct io_ring_ctx *ctx = req->ctx;
4048 struct io_buffer *head;
4049 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004050 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004051
4052 io_ring_submit_lock(ctx, !force_nonblock);
4053
4054 lockdep_assert_held(&ctx->uring_lock);
4055
4056 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004057 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004058 if (head)
4059 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004060 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004061 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004062
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004063 /* complete before unlock, IOPOLL may need the lock */
4064 __io_req_complete(req, issue_flags, ret, 0);
4065 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004066 return 0;
4067}
4068
Jens Axboeddf0322d2020-02-23 16:41:33 -07004069static int io_provide_buffers_prep(struct io_kiocb *req,
4070 const struct io_uring_sqe *sqe)
4071{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004072 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004073 struct io_provide_buf *p = &req->pbuf;
4074 u64 tmp;
4075
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004076 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004077 return -EINVAL;
4078
4079 tmp = READ_ONCE(sqe->fd);
4080 if (!tmp || tmp > USHRT_MAX)
4081 return -E2BIG;
4082 p->nbufs = tmp;
4083 p->addr = READ_ONCE(sqe->addr);
4084 p->len = READ_ONCE(sqe->len);
4085
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004086 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4087 &size))
4088 return -EOVERFLOW;
4089 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4090 return -EOVERFLOW;
4091
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004092 size = (unsigned long)p->len * p->nbufs;
4093 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004094 return -EFAULT;
4095
4096 p->bgid = READ_ONCE(sqe->buf_group);
4097 tmp = READ_ONCE(sqe->off);
4098 if (tmp > USHRT_MAX)
4099 return -E2BIG;
4100 p->bid = tmp;
4101 return 0;
4102}
4103
4104static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4105{
4106 struct io_buffer *buf;
4107 u64 addr = pbuf->addr;
4108 int i, bid = pbuf->bid;
4109
4110 for (i = 0; i < pbuf->nbufs; i++) {
4111 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4112 if (!buf)
4113 break;
4114
4115 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004116 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004117 buf->bid = bid;
4118 addr += pbuf->len;
4119 bid++;
4120 if (!*head) {
4121 INIT_LIST_HEAD(&buf->list);
4122 *head = buf;
4123 } else {
4124 list_add_tail(&buf->list, &(*head)->list);
4125 }
4126 }
4127
4128 return i ? i : -ENOMEM;
4129}
4130
Pavel Begunkov889fca72021-02-10 00:03:09 +00004131static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004132{
4133 struct io_provide_buf *p = &req->pbuf;
4134 struct io_ring_ctx *ctx = req->ctx;
4135 struct io_buffer *head, *list;
4136 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004137 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004138
4139 io_ring_submit_lock(ctx, !force_nonblock);
4140
4141 lockdep_assert_held(&ctx->uring_lock);
4142
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004143 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004144
4145 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004146 if (ret >= 0 && !list) {
4147 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4148 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004149 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004150 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004151 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004152 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004153 /* complete before unlock, IOPOLL may need the lock */
4154 __io_req_complete(req, issue_flags, ret, 0);
4155 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004156 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004157}
4158
Jens Axboe3e4827b2020-01-08 15:18:09 -07004159static int io_epoll_ctl_prep(struct io_kiocb *req,
4160 const struct io_uring_sqe *sqe)
4161{
4162#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004163 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004164 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004165 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004166 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004167
4168 req->epoll.epfd = READ_ONCE(sqe->fd);
4169 req->epoll.op = READ_ONCE(sqe->len);
4170 req->epoll.fd = READ_ONCE(sqe->off);
4171
4172 if (ep_op_has_event(req->epoll.op)) {
4173 struct epoll_event __user *ev;
4174
4175 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4176 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4177 return -EFAULT;
4178 }
4179
4180 return 0;
4181#else
4182 return -EOPNOTSUPP;
4183#endif
4184}
4185
Pavel Begunkov889fca72021-02-10 00:03:09 +00004186static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004187{
4188#if defined(CONFIG_EPOLL)
4189 struct io_epoll *ie = &req->epoll;
4190 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004191 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004192
4193 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4194 if (force_nonblock && ret == -EAGAIN)
4195 return -EAGAIN;
4196
4197 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004198 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004199 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004200 return 0;
4201#else
4202 return -EOPNOTSUPP;
4203#endif
4204}
4205
Jens Axboec1ca7572019-12-25 22:18:28 -07004206static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4207{
4208#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004209 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004210 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004211 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4212 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004213
4214 req->madvise.addr = READ_ONCE(sqe->addr);
4215 req->madvise.len = READ_ONCE(sqe->len);
4216 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4217 return 0;
4218#else
4219 return -EOPNOTSUPP;
4220#endif
4221}
4222
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004223static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004224{
4225#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4226 struct io_madvise *ma = &req->madvise;
4227 int ret;
4228
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004229 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004230 return -EAGAIN;
4231
Minchan Kim0726b012020-10-17 16:14:50 -07004232 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004233 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004234 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004235 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004236 return 0;
4237#else
4238 return -EOPNOTSUPP;
4239#endif
4240}
4241
Jens Axboe4840e412019-12-25 22:03:45 -07004242static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4243{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004244 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004245 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004246 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4247 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004248
4249 req->fadvise.offset = READ_ONCE(sqe->off);
4250 req->fadvise.len = READ_ONCE(sqe->len);
4251 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4252 return 0;
4253}
4254
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004255static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004256{
4257 struct io_fadvise *fa = &req->fadvise;
4258 int ret;
4259
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004260 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004261 switch (fa->advice) {
4262 case POSIX_FADV_NORMAL:
4263 case POSIX_FADV_RANDOM:
4264 case POSIX_FADV_SEQUENTIAL:
4265 break;
4266 default:
4267 return -EAGAIN;
4268 }
4269 }
Jens Axboe4840e412019-12-25 22:03:45 -07004270
4271 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4272 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004273 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004274 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004275 return 0;
4276}
4277
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004278static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4279{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004280 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004281 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004282 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004283 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004284 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004285 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004286
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004287 req->statx.dfd = READ_ONCE(sqe->fd);
4288 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004289 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004290 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4291 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004292
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004293 return 0;
4294}
4295
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004296static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004297{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004298 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004299 int ret;
4300
Pavel Begunkov59d70012021-03-22 01:58:30 +00004301 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004302 return -EAGAIN;
4303
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004304 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4305 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004306
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004307 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004308 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004309 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004310 return 0;
4311}
4312
Jens Axboeb5dba592019-12-11 14:02:38 -07004313static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4314{
Jens Axboe14587a462020-09-05 11:36:08 -06004315 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004316 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004317 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004318 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004319 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004320 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004321 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004322
4323 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004324 return 0;
4325}
4326
Pavel Begunkov889fca72021-02-10 00:03:09 +00004327static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004328{
Jens Axboe9eac1902021-01-19 15:50:37 -07004329 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004330 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004331 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004332 struct file *file = NULL;
4333 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004334
Jens Axboe9eac1902021-01-19 15:50:37 -07004335 spin_lock(&files->file_lock);
4336 fdt = files_fdtable(files);
4337 if (close->fd >= fdt->max_fds) {
4338 spin_unlock(&files->file_lock);
4339 goto err;
4340 }
4341 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004342 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004343 spin_unlock(&files->file_lock);
4344 file = NULL;
4345 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004346 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004347
4348 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004349 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004350 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004351 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004352 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004353
Jens Axboe9eac1902021-01-19 15:50:37 -07004354 ret = __close_fd_get_file(close->fd, &file);
4355 spin_unlock(&files->file_lock);
4356 if (ret < 0) {
4357 if (ret == -ENOENT)
4358 ret = -EBADF;
4359 goto err;
4360 }
4361
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004362 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004363 ret = filp_close(file, current->files);
4364err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004365 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004366 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004367 if (file)
4368 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004369 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004370 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004371}
4372
Pavel Begunkov1155c762021-02-18 18:29:38 +00004373static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004374{
4375 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004376
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004377 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4378 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004379 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4380 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004381 return -EINVAL;
4382
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004383 req->sync.off = READ_ONCE(sqe->off);
4384 req->sync.len = READ_ONCE(sqe->len);
4385 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004386 return 0;
4387}
4388
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004389static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004390{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004391 int ret;
4392
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004393 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004394 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004395 return -EAGAIN;
4396
Jens Axboe9adbd452019-12-20 08:45:55 -07004397 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004398 req->sync.flags);
4399 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004400 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004401 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004402 return 0;
4403}
4404
YueHaibing469956e2020-03-04 15:53:52 +08004405#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004406static int io_setup_async_msg(struct io_kiocb *req,
4407 struct io_async_msghdr *kmsg)
4408{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004409 struct io_async_msghdr *async_msg = req->async_data;
4410
4411 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004412 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004413 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004414 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004415 return -ENOMEM;
4416 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004417 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004418 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004419 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004420 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004421 /* if were using fast_iov, set it to the new one */
4422 if (!async_msg->free_iov)
4423 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4424
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004425 return -EAGAIN;
4426}
4427
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004428static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4429 struct io_async_msghdr *iomsg)
4430{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004431 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004432 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004433 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004434 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004435}
4436
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004437static int io_sendmsg_prep_async(struct io_kiocb *req)
4438{
4439 int ret;
4440
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004441 ret = io_sendmsg_copy_hdr(req, req->async_data);
4442 if (!ret)
4443 req->flags |= REQ_F_NEED_CLEANUP;
4444 return ret;
4445}
4446
Jens Axboe3529d8c2019-12-19 18:24:38 -07004447static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004448{
Jens Axboee47293f2019-12-20 08:58:21 -07004449 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004450
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004451 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4452 return -EINVAL;
4453
Pavel Begunkov270a5942020-07-12 20:41:04 +03004454 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004455 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004456 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4457 if (sr->msg_flags & MSG_DONTWAIT)
4458 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004459
Jens Axboed8768362020-02-27 14:17:49 -07004460#ifdef CONFIG_COMPAT
4461 if (req->ctx->compat)
4462 sr->msg_flags |= MSG_CMSG_COMPAT;
4463#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004464 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004465}
4466
Pavel Begunkov889fca72021-02-10 00:03:09 +00004467static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004468{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004469 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004470 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004471 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004472 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004473 int ret;
4474
Florent Revestdba4a922020-12-04 12:36:04 +01004475 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004476 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004477 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004478
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004479 kmsg = req->async_data;
4480 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004481 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004482 if (ret)
4483 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004484 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004485 }
4486
Pavel Begunkov04411802021-04-01 15:44:00 +01004487 flags = req->sr_msg.msg_flags;
4488 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004489 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004490 if (flags & MSG_WAITALL)
4491 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4492
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004493 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004494 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004495 return io_setup_async_msg(req, kmsg);
4496 if (ret == -ERESTARTSYS)
4497 ret = -EINTR;
4498
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004499 /* fast path, check for non-NULL to avoid function call */
4500 if (kmsg->free_iov)
4501 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004502 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004503 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004504 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004505 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004506 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004507}
4508
Pavel Begunkov889fca72021-02-10 00:03:09 +00004509static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004510{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004511 struct io_sr_msg *sr = &req->sr_msg;
4512 struct msghdr msg;
4513 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004514 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004515 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004516 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004517 int ret;
4518
Florent Revestdba4a922020-12-04 12:36:04 +01004519 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004520 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004521 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004522
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004523 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4524 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004525 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004526
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004527 msg.msg_name = NULL;
4528 msg.msg_control = NULL;
4529 msg.msg_controllen = 0;
4530 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004531
Pavel Begunkov04411802021-04-01 15:44:00 +01004532 flags = req->sr_msg.msg_flags;
4533 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004534 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004535 if (flags & MSG_WAITALL)
4536 min_ret = iov_iter_count(&msg.msg_iter);
4537
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004538 msg.msg_flags = flags;
4539 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004540 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004541 return -EAGAIN;
4542 if (ret == -ERESTARTSYS)
4543 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004544
Stefan Metzmacher00312752021-03-20 20:33:36 +01004545 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004546 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004547 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004548 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004549}
4550
Pavel Begunkov1400e692020-07-12 20:41:05 +03004551static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4552 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004553{
4554 struct io_sr_msg *sr = &req->sr_msg;
4555 struct iovec __user *uiov;
4556 size_t iov_len;
4557 int ret;
4558
Pavel Begunkov1400e692020-07-12 20:41:05 +03004559 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4560 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004561 if (ret)
4562 return ret;
4563
4564 if (req->flags & REQ_F_BUFFER_SELECT) {
4565 if (iov_len > 1)
4566 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004567 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004568 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004569 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004570 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004571 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004572 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004573 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004574 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004575 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004576 if (ret > 0)
4577 ret = 0;
4578 }
4579
4580 return ret;
4581}
4582
4583#ifdef CONFIG_COMPAT
4584static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004585 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004586{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004587 struct io_sr_msg *sr = &req->sr_msg;
4588 struct compat_iovec __user *uiov;
4589 compat_uptr_t ptr;
4590 compat_size_t len;
4591 int ret;
4592
Pavel Begunkov4af34172021-04-11 01:46:30 +01004593 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4594 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004595 if (ret)
4596 return ret;
4597
4598 uiov = compat_ptr(ptr);
4599 if (req->flags & REQ_F_BUFFER_SELECT) {
4600 compat_ssize_t clen;
4601
4602 if (len > 1)
4603 return -EINVAL;
4604 if (!access_ok(uiov, sizeof(*uiov)))
4605 return -EFAULT;
4606 if (__get_user(clen, &uiov->iov_len))
4607 return -EFAULT;
4608 if (clen < 0)
4609 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004610 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004611 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004612 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004613 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004614 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004615 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004616 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004617 if (ret < 0)
4618 return ret;
4619 }
4620
4621 return 0;
4622}
Jens Axboe03b12302019-12-02 18:50:25 -07004623#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004624
Pavel Begunkov1400e692020-07-12 20:41:05 +03004625static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4626 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004627{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004628 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004629
4630#ifdef CONFIG_COMPAT
4631 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004632 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004633#endif
4634
Pavel Begunkov1400e692020-07-12 20:41:05 +03004635 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004636}
4637
Jens Axboebcda7ba2020-02-23 16:42:51 -07004638static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004639 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004640{
4641 struct io_sr_msg *sr = &req->sr_msg;
4642 struct io_buffer *kbuf;
4643
Jens Axboebcda7ba2020-02-23 16:42:51 -07004644 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4645 if (IS_ERR(kbuf))
4646 return kbuf;
4647
4648 sr->kbuf = kbuf;
4649 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004650 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004651}
4652
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004653static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4654{
4655 return io_put_kbuf(req, req->sr_msg.kbuf);
4656}
4657
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004658static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004659{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004660 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004661
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004662 ret = io_recvmsg_copy_hdr(req, req->async_data);
4663 if (!ret)
4664 req->flags |= REQ_F_NEED_CLEANUP;
4665 return ret;
4666}
4667
4668static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4669{
4670 struct io_sr_msg *sr = &req->sr_msg;
4671
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004672 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4673 return -EINVAL;
4674
Pavel Begunkov270a5942020-07-12 20:41:04 +03004675 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004676 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004677 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004678 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4679 if (sr->msg_flags & MSG_DONTWAIT)
4680 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004681
Jens Axboed8768362020-02-27 14:17:49 -07004682#ifdef CONFIG_COMPAT
4683 if (req->ctx->compat)
4684 sr->msg_flags |= MSG_CMSG_COMPAT;
4685#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004686 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004687}
4688
Pavel Begunkov889fca72021-02-10 00:03:09 +00004689static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004690{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004691 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004692 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004693 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004694 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004695 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004696 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004697 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004698
Florent Revestdba4a922020-12-04 12:36:04 +01004699 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004700 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004701 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004702
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004703 kmsg = req->async_data;
4704 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004705 ret = io_recvmsg_copy_hdr(req, &iomsg);
4706 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004707 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004708 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004709 }
4710
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004711 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004712 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004713 if (IS_ERR(kbuf))
4714 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004715 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004716 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4717 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004718 1, req->sr_msg.len);
4719 }
4720
Pavel Begunkov04411802021-04-01 15:44:00 +01004721 flags = req->sr_msg.msg_flags;
4722 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004723 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004724 if (flags & MSG_WAITALL)
4725 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4726
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004727 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4728 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004729 if (force_nonblock && ret == -EAGAIN)
4730 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004731 if (ret == -ERESTARTSYS)
4732 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004733
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004734 if (req->flags & REQ_F_BUFFER_SELECTED)
4735 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004736 /* fast path, check for non-NULL to avoid function call */
4737 if (kmsg->free_iov)
4738 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004739 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004740 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004741 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004742 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004743 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004744}
4745
Pavel Begunkov889fca72021-02-10 00:03:09 +00004746static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004747{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004748 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004749 struct io_sr_msg *sr = &req->sr_msg;
4750 struct msghdr msg;
4751 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004752 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004753 struct iovec iov;
4754 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004755 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004756 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004757 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004758
Florent Revestdba4a922020-12-04 12:36:04 +01004759 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004760 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004761 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004762
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004763 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004764 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004765 if (IS_ERR(kbuf))
4766 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004767 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004768 }
4769
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004770 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004771 if (unlikely(ret))
4772 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004773
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004774 msg.msg_name = NULL;
4775 msg.msg_control = NULL;
4776 msg.msg_controllen = 0;
4777 msg.msg_namelen = 0;
4778 msg.msg_iocb = NULL;
4779 msg.msg_flags = 0;
4780
Pavel Begunkov04411802021-04-01 15:44:00 +01004781 flags = req->sr_msg.msg_flags;
4782 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004783 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004784 if (flags & MSG_WAITALL)
4785 min_ret = iov_iter_count(&msg.msg_iter);
4786
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004787 ret = sock_recvmsg(sock, &msg, flags);
4788 if (force_nonblock && ret == -EAGAIN)
4789 return -EAGAIN;
4790 if (ret == -ERESTARTSYS)
4791 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004792out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004793 if (req->flags & REQ_F_BUFFER_SELECTED)
4794 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01004795 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004796 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004797 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004798 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004799}
4800
Jens Axboe3529d8c2019-12-19 18:24:38 -07004801static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004802{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004803 struct io_accept *accept = &req->accept;
4804
Jens Axboe14587a462020-09-05 11:36:08 -06004805 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004806 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004807 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004808 return -EINVAL;
4809
Jens Axboed55e5f52019-12-11 16:12:15 -07004810 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4811 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004812 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004813 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004814
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004815 accept->file_slot = READ_ONCE(sqe->file_index);
4816 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
4817 (accept->flags & SOCK_CLOEXEC)))
4818 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004819 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
4820 return -EINVAL;
4821 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
4822 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004823 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004824}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004825
Pavel Begunkov889fca72021-02-10 00:03:09 +00004826static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004827{
4828 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004829 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004830 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004831 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004832 struct file *file;
4833 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004834
Jiufei Xuee697dee2020-06-10 13:41:59 +08004835 if (req->file->f_flags & O_NONBLOCK)
4836 req->flags |= REQ_F_NOWAIT;
4837
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004838 if (!fixed) {
4839 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
4840 if (unlikely(fd < 0))
4841 return fd;
4842 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004843 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
4844 accept->flags);
4845 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004846 if (!fixed)
4847 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004848 ret = PTR_ERR(file);
4849 if (ret == -EAGAIN && force_nonblock)
4850 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004851 if (ret == -ERESTARTSYS)
4852 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004853 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004854 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004855 fd_install(fd, file);
4856 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004857 } else {
4858 ret = io_install_fixed_file(req, file, issue_flags,
4859 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004860 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004861 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004862 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004863}
4864
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004865static int io_connect_prep_async(struct io_kiocb *req)
4866{
4867 struct io_async_connect *io = req->async_data;
4868 struct io_connect *conn = &req->connect;
4869
4870 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4871}
4872
Jens Axboe3529d8c2019-12-19 18:24:38 -07004873static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004874{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004875 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004876
Jens Axboe14587a462020-09-05 11:36:08 -06004877 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004878 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004879 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
4880 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004881 return -EINVAL;
4882
Jens Axboe3529d8c2019-12-19 18:24:38 -07004883 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4884 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004885 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004886}
4887
Pavel Begunkov889fca72021-02-10 00:03:09 +00004888static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004889{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004890 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004891 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004892 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004893 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004894
Jens Axboee8c2bc12020-08-15 18:44:09 -07004895 if (req->async_data) {
4896 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004897 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004898 ret = move_addr_to_kernel(req->connect.addr,
4899 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004900 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004901 if (ret)
4902 goto out;
4903 io = &__io;
4904 }
4905
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004906 file_flags = force_nonblock ? O_NONBLOCK : 0;
4907
Jens Axboee8c2bc12020-08-15 18:44:09 -07004908 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004909 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004910 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004911 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004912 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004913 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004914 ret = -ENOMEM;
4915 goto out;
4916 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004917 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004918 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004919 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004920 if (ret == -ERESTARTSYS)
4921 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004922out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004923 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004924 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004925 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004926 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004927}
YueHaibing469956e2020-03-04 15:53:52 +08004928#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004929#define IO_NETOP_FN(op) \
4930static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4931{ \
4932 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004933}
4934
Jens Axboe99a10082021-02-19 09:35:19 -07004935#define IO_NETOP_PREP(op) \
4936IO_NETOP_FN(op) \
4937static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4938{ \
4939 return -EOPNOTSUPP; \
4940} \
4941
4942#define IO_NETOP_PREP_ASYNC(op) \
4943IO_NETOP_PREP(op) \
4944static int io_##op##_prep_async(struct io_kiocb *req) \
4945{ \
4946 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004947}
4948
Jens Axboe99a10082021-02-19 09:35:19 -07004949IO_NETOP_PREP_ASYNC(sendmsg);
4950IO_NETOP_PREP_ASYNC(recvmsg);
4951IO_NETOP_PREP_ASYNC(connect);
4952IO_NETOP_PREP(accept);
4953IO_NETOP_FN(send);
4954IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004955#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004956
Jens Axboed7718a92020-02-14 22:23:12 -07004957struct io_poll_table {
4958 struct poll_table_struct pt;
4959 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01004960 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07004961 int error;
4962};
4963
Jens Axboed7718a92020-02-14 22:23:12 -07004964static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01004965 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07004966{
Jens Axboed7718a92020-02-14 22:23:12 -07004967 /* for instances that support it check for an event match first: */
4968 if (mask && !(mask & poll->events))
4969 return 0;
4970
4971 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4972
4973 list_del_init(&poll->wait.entry);
4974
Jens Axboed7718a92020-02-14 22:23:12 -07004975 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01004976 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004977
Jens Axboed7718a92020-02-14 22:23:12 -07004978 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004979 * If this fails, then the task is exiting. When a task exits, the
4980 * work gets canceled, so just cancel this request as well instead
4981 * of executing it. We can't safely execute it anyway, as we may not
4982 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004983 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01004984 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07004985 return 1;
4986}
4987
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004988static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4989 __acquires(&req->ctx->completion_lock)
4990{
4991 struct io_ring_ctx *ctx = req->ctx;
4992
Jens Axboe316319e2021-08-19 09:41:42 -06004993 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01004994 if (unlikely(req->task->flags & PF_EXITING))
4995 WRITE_ONCE(poll->canceled, true);
4996
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004997 if (!req->result && !READ_ONCE(poll->canceled)) {
4998 struct poll_table_struct pt = { ._key = poll->events };
4999
5000 req->result = vfs_poll(req->file, &pt) & poll->events;
5001 }
5002
Jens Axboe79ebeae2021-08-10 15:18:27 -06005003 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005004 if (!req->result && !READ_ONCE(poll->canceled)) {
5005 add_wait_queue(poll->head, &poll->wait);
5006 return true;
5007 }
5008
5009 return false;
5010}
5011
Jens Axboed4e7cd32020-08-15 11:44:50 -07005012static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005013{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005014 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005015 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005016 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005017 return req->apoll->double_poll;
5018}
5019
5020static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5021{
5022 if (req->opcode == IORING_OP_POLL_ADD)
5023 return &req->poll;
5024 return &req->apoll->poll;
5025}
5026
5027static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005028 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005029{
5030 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005031
5032 lockdep_assert_held(&req->ctx->completion_lock);
5033
5034 if (poll && poll->head) {
5035 struct wait_queue_head *head = poll->head;
5036
Jens Axboe79ebeae2021-08-10 15:18:27 -06005037 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005038 list_del_init(&poll->wait.entry);
5039 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005040 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005041 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005042 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005043 }
5044}
5045
Pavel Begunkove27414b2021-04-09 09:13:20 +01005046static bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005047 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005048{
5049 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005050 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005051 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005052
Pavel Begunkove27414b2021-04-09 09:13:20 +01005053 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005054 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005055 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005056 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005057 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005058 }
Jens Axboeb69de282021-03-17 08:37:41 -06005059 if (req->poll.events & EPOLLONESHOT)
5060 flags = 0;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005061 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005062 req->poll.done = true;
5063 flags = 0;
5064 }
Hao Xu7b289c32021-04-13 15:20:39 +08005065 if (flags & IORING_CQE_F_MORE)
5066 ctx->cq_extra++;
5067
Jens Axboe18bceab2020-05-15 11:56:54 -06005068 io_commit_cqring(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005069 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005070}
5071
Pavel Begunkovf237c302021-08-18 12:42:46 +01005072static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005073{
Jens Axboe6d816e02020-08-11 08:04:14 -06005074 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005075 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005076
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005077 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005078 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005079 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005080 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005081
Pavel Begunkove27414b2021-04-09 09:13:20 +01005082 done = io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005083 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005084 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005085 hash_del(&req->hash_node);
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005086 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005087 req->result = 0;
5088 add_wait_queue(req->poll.head, &req->poll.wait);
5089 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005090 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005091 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005092
Jens Axboe88e41cf2021-02-22 22:08:01 -07005093 if (done) {
5094 nxt = io_put_req_find_next(req);
5095 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005096 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005097 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005098 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005099}
5100
5101static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5102 int sync, void *key)
5103{
5104 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005105 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005106 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005107 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005108
5109 /* for instances that support it check for an event match first: */
5110 if (mask && !(mask & poll->events))
5111 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005112 if (!(poll->events & EPOLLONESHOT))
5113 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005114
Jens Axboe8706e042020-09-28 08:38:54 -06005115 list_del_init(&wait->entry);
5116
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005117 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005118 bool done;
5119
Jens Axboe79ebeae2021-08-10 15:18:27 -06005120 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005121 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005122 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005123 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005124 /* make sure double remove sees this as being gone */
5125 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005126 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005127 if (!done) {
5128 /* use wait func handler, so it matches the rq type */
5129 poll->wait.func(&poll->wait, mode, sync, key);
5130 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005131 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005132 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005133 return 1;
5134}
5135
5136static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5137 wait_queue_func_t wake_func)
5138{
5139 poll->head = NULL;
5140 poll->done = false;
5141 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005142#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5143 /* mask in events that we always want/need */
5144 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005145 INIT_LIST_HEAD(&poll->wait.entry);
5146 init_waitqueue_func_entry(&poll->wait, wake_func);
5147}
5148
5149static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005150 struct wait_queue_head *head,
5151 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005152{
5153 struct io_kiocb *req = pt->req;
5154
5155 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005156 * The file being polled uses multiple waitqueues for poll handling
5157 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5158 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005159 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005160 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005161 struct io_poll_iocb *poll_one = poll;
5162
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005163 /* double add on the same waitqueue head, ignore */
5164 if (poll_one->head == head)
5165 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005166 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005167 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005168 if ((*poll_ptr)->head == head)
5169 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005170 pt->error = -EINVAL;
5171 return;
5172 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005173 /*
5174 * Can't handle multishot for double wait for now, turn it
5175 * into one-shot mode.
5176 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005177 if (!(poll_one->events & EPOLLONESHOT))
5178 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005179 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5180 if (!poll) {
5181 pt->error = -ENOMEM;
5182 return;
5183 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005184 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005185 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005186 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005187 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005188 }
5189
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005190 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005191 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005192
5193 if (poll->events & EPOLLEXCLUSIVE)
5194 add_wait_queue_exclusive(head, &poll->wait);
5195 else
5196 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005197}
5198
5199static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5200 struct poll_table_struct *p)
5201{
5202 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005203 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005204
Jens Axboe807abcb2020-07-17 17:09:27 -06005205 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005206}
5207
Pavel Begunkovf237c302021-08-18 12:42:46 +01005208static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005209{
Jens Axboed7718a92020-02-14 22:23:12 -07005210 struct async_poll *apoll = req->apoll;
5211 struct io_ring_ctx *ctx = req->ctx;
5212
Olivier Langlois236daeae2021-05-31 02:36:37 -04005213 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005214
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005215 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005216 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005217 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005218 }
5219
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005220 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005221 io_poll_remove_double(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005222 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005223
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005224 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005225 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005226 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005227 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005228}
5229
5230static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5231 void *key)
5232{
5233 struct io_kiocb *req = wait->private;
5234 struct io_poll_iocb *poll = &req->apoll->poll;
5235
5236 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5237 key_to_poll(key));
5238
5239 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5240}
5241
5242static void io_poll_req_insert(struct io_kiocb *req)
5243{
5244 struct io_ring_ctx *ctx = req->ctx;
5245 struct hlist_head *list;
5246
5247 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5248 hlist_add_head(&req->hash_node, list);
5249}
5250
5251static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5252 struct io_poll_iocb *poll,
5253 struct io_poll_table *ipt, __poll_t mask,
5254 wait_queue_func_t wake_func)
5255 __acquires(&ctx->completion_lock)
5256{
5257 struct io_ring_ctx *ctx = req->ctx;
5258 bool cancel = false;
5259
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005260 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005261 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005262 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005263 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005264
5265 ipt->pt._key = mask;
5266 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005267 ipt->error = 0;
5268 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005269
Jens Axboed7718a92020-02-14 22:23:12 -07005270 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005271 if (unlikely(!ipt->nr_entries) && !ipt->error)
5272 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005273
Jens Axboe79ebeae2021-08-10 15:18:27 -06005274 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005275 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005276 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005277 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005278 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005279 if (unlikely(list_empty(&poll->wait.entry))) {
5280 if (ipt->error)
5281 cancel = true;
5282 ipt->error = 0;
5283 mask = 0;
5284 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005285 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005286 list_del_init(&poll->wait.entry);
5287 else if (cancel)
5288 WRITE_ONCE(poll->canceled, true);
5289 else if (!poll->done) /* actually waiting for an event */
5290 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005291 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005292 }
5293
5294 return mask;
5295}
5296
Olivier Langlois59b735a2021-06-22 05:17:39 -07005297enum {
5298 IO_APOLL_OK,
5299 IO_APOLL_ABORTED,
5300 IO_APOLL_READY
5301};
5302
5303static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005304{
5305 const struct io_op_def *def = &io_op_defs[req->opcode];
5306 struct io_ring_ctx *ctx = req->ctx;
5307 struct async_poll *apoll;
5308 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005309 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005310 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005311
5312 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005313 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005314 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005315 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005316 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005317 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005318
5319 if (def->pollin) {
5320 rw = READ;
5321 mask |= POLLIN | POLLRDNORM;
5322
5323 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5324 if ((req->opcode == IORING_OP_RECVMSG) &&
5325 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5326 mask &= ~POLLIN;
5327 } else {
5328 rw = WRITE;
5329 mask |= POLLOUT | POLLWRNORM;
5330 }
5331
Jens Axboe9dab14b2020-08-25 12:27:50 -06005332 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005333 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005334 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005335
5336 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5337 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005338 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005339 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005340 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005341 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005342 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005343 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005344
5345 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5346 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005347 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005348 if (ret || ipt.error)
5349 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5350
Olivier Langlois236daeae2021-05-31 02:36:37 -04005351 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5352 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005353 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005354}
5355
5356static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005357 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005358 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005359{
Jens Axboeb41e9852020-02-17 09:52:41 -07005360 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005361
Jens Axboe50826202021-02-23 09:02:26 -07005362 if (!poll->head)
5363 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005364 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005365 if (do_cancel)
5366 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005367 if (!list_empty(&poll->wait.entry)) {
5368 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005369 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005370 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005371 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005372 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005373 return do_complete;
5374}
5375
Pavel Begunkov5d709042021-08-09 20:18:13 +01005376static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005377 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005378{
5379 bool do_complete;
5380
Jens Axboed4e7cd32020-08-15 11:44:50 -07005381 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005382 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005383
Jens Axboeb41e9852020-02-17 09:52:41 -07005384 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005385 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005386 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005387 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005388 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005389 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005390 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005391}
5392
Jens Axboe76e1b642020-09-26 15:05:03 -06005393/*
5394 * Returns true if we found and killed one or more poll requests
5395 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005396static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005397 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005398{
Jens Axboe78076bb2019-12-04 19:56:40 -07005399 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005400 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005401 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005402
Jens Axboe79ebeae2021-08-10 15:18:27 -06005403 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005404 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5405 struct hlist_head *list;
5406
5407 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005408 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005409 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005410 posted += io_poll_remove_one(req);
5411 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005412 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005413 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005414
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005415 if (posted)
5416 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005417
5418 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005419}
5420
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005421static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5422 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005423 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005424{
Jens Axboe78076bb2019-12-04 19:56:40 -07005425 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005426 struct io_kiocb *req;
5427
Jens Axboe78076bb2019-12-04 19:56:40 -07005428 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5429 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005430 if (sqe_addr != req->user_data)
5431 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005432 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5433 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005434 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005435 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005436 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005437}
5438
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005439static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5440 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005441 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005442{
5443 struct io_kiocb *req;
5444
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005445 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005446 if (!req)
5447 return -ENOENT;
5448 if (io_poll_remove_one(req))
5449 return 0;
5450
5451 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005452}
5453
Pavel Begunkov9096af32021-04-14 13:38:36 +01005454static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5455 unsigned int flags)
5456{
5457 u32 events;
5458
5459 events = READ_ONCE(sqe->poll32_events);
5460#ifdef __BIG_ENDIAN
5461 events = swahw32(events);
5462#endif
5463 if (!(flags & IORING_POLL_ADD_MULTI))
5464 events |= EPOLLONESHOT;
5465 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5466}
5467
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005468static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005469 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005470{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005471 struct io_poll_update *upd = &req->poll_update;
5472 u32 flags;
5473
Jens Axboe221c5eb2019-01-17 09:41:58 -07005474 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5475 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005476 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005477 return -EINVAL;
5478 flags = READ_ONCE(sqe->len);
5479 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5480 IORING_POLL_ADD_MULTI))
5481 return -EINVAL;
5482 /* meaningless without update */
5483 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005484 return -EINVAL;
5485
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005486 upd->old_user_data = READ_ONCE(sqe->addr);
5487 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5488 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005489
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005490 upd->new_user_data = READ_ONCE(sqe->off);
5491 if (!upd->update_user_data && upd->new_user_data)
5492 return -EINVAL;
5493 if (upd->update_events)
5494 upd->events = io_poll_parse_events(sqe, flags);
5495 else if (sqe->poll32_events)
5496 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005497
Jens Axboe221c5eb2019-01-17 09:41:58 -07005498 return 0;
5499}
5500
Jens Axboe221c5eb2019-01-17 09:41:58 -07005501static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5502 void *key)
5503{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005504 struct io_kiocb *req = wait->private;
5505 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005506
Jens Axboed7718a92020-02-14 22:23:12 -07005507 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005508}
5509
Jens Axboe221c5eb2019-01-17 09:41:58 -07005510static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5511 struct poll_table_struct *p)
5512{
5513 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5514
Jens Axboee8c2bc12020-08-15 18:44:09 -07005515 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005516}
5517
Jens Axboe3529d8c2019-12-19 18:24:38 -07005518static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005519{
5520 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005521 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005522
5523 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5524 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005525 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005526 return -EINVAL;
5527 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005528 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005529 return -EINVAL;
5530
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005531 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005532 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005533 return 0;
5534}
5535
Pavel Begunkov61e98202021-02-10 00:03:08 +00005536static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005537{
5538 struct io_poll_iocb *poll = &req->poll;
5539 struct io_ring_ctx *ctx = req->ctx;
5540 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005541 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005542
Jens Axboed7718a92020-02-14 22:23:12 -07005543 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005544
Jens Axboed7718a92020-02-14 22:23:12 -07005545 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5546 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005547
Jens Axboe8c838782019-03-12 15:48:16 -06005548 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005549 ipt.error = 0;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005550 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005551 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005552 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005553
Jens Axboe8c838782019-03-12 15:48:16 -06005554 if (mask) {
5555 io_cqring_ev_posted(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005556 if (poll->events & EPOLLONESHOT)
5557 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005558 }
Jens Axboe8c838782019-03-12 15:48:16 -06005559 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005560}
5561
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005562static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005563{
5564 struct io_ring_ctx *ctx = req->ctx;
5565 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005566 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005567 int ret;
5568
Jens Axboe79ebeae2021-08-10 15:18:27 -06005569 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005570 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005571 if (!preq) {
5572 ret = -ENOENT;
5573 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005574 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005575
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005576 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5577 completing = true;
5578 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5579 goto err;
5580 }
5581
Jens Axboecb3b200e2021-04-06 09:49:31 -06005582 /*
5583 * Don't allow racy completion with singleshot, as we cannot safely
5584 * update those. For multishot, if we're racing with completion, just
5585 * let completion re-add it.
5586 */
5587 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5588 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5589 ret = -EALREADY;
5590 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005591 }
5592 /* we now have a detached poll request. reissue. */
5593 ret = 0;
5594err:
Jens Axboeb69de282021-03-17 08:37:41 -06005595 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005596 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005597 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005598 io_req_complete(req, ret);
5599 return 0;
5600 }
5601 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005602 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005603 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005604 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005605 preq->poll.events |= IO_POLL_UNMASK;
5606 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005607 if (req->poll_update.update_user_data)
5608 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005609 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005610
Jens Axboeb69de282021-03-17 08:37:41 -06005611 /* complete update request, we're done with it */
5612 io_req_complete(req, ret);
5613
Jens Axboecb3b200e2021-04-06 09:49:31 -06005614 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005615 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005616 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005617 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005618 io_req_complete(preq, ret);
5619 }
Jens Axboeb69de282021-03-17 08:37:41 -06005620 }
5621 return 0;
5622}
5623
Pavel Begunkovf237c302021-08-18 12:42:46 +01005624static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005625{
Jens Axboe89850fc2021-08-10 15:11:51 -06005626 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005627 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005628}
5629
Jens Axboe5262f562019-09-17 12:26:57 -06005630static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5631{
Jens Axboead8a48a2019-11-15 08:49:11 -07005632 struct io_timeout_data *data = container_of(timer,
5633 struct io_timeout_data, timer);
5634 struct io_kiocb *req = data->req;
5635 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005636 unsigned long flags;
5637
Jens Axboe89850fc2021-08-10 15:11:51 -06005638 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005639 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005640 atomic_set(&req->ctx->cq_timeouts,
5641 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005642 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005643
Jens Axboe89850fc2021-08-10 15:11:51 -06005644 req->io_task_work.func = io_req_task_timeout;
5645 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005646 return HRTIMER_NORESTART;
5647}
5648
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005649static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5650 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005651 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005652{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005653 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005654 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005655 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005656
5657 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005658 found = user_data == req->user_data;
5659 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005660 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005661 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005662 if (!found)
5663 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005664
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005665 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005666 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005667 return ERR_PTR(-EALREADY);
5668 list_del_init(&req->timeout.list);
5669 return req;
5670}
5671
5672static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005673 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005674 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005675{
5676 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5677
5678 if (IS_ERR(req))
5679 return PTR_ERR(req);
5680
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005681 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005682 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005683 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005684 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005685}
5686
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005687static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5688 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06005689 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005690{
5691 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5692 struct io_timeout_data *data;
5693
5694 if (IS_ERR(req))
5695 return PTR_ERR(req);
5696
5697 req->timeout.off = 0; /* noseq */
5698 data = req->async_data;
5699 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5700 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5701 data->timer.function = io_timeout_fn;
5702 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5703 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005704}
5705
Jens Axboe3529d8c2019-12-19 18:24:38 -07005706static int io_timeout_remove_prep(struct io_kiocb *req,
5707 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005708{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005709 struct io_timeout_rem *tr = &req->timeout_rem;
5710
Jens Axboeb29472e2019-12-17 18:50:29 -07005711 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5712 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005713 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5714 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005715 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07005716 return -EINVAL;
5717
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005718 tr->addr = READ_ONCE(sqe->addr);
5719 tr->flags = READ_ONCE(sqe->timeout_flags);
5720 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5721 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5722 return -EINVAL;
5723 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5724 return -EFAULT;
5725 } else if (tr->flags) {
5726 /* timeout removal doesn't support flags */
5727 return -EINVAL;
5728 }
5729
Jens Axboeb29472e2019-12-17 18:50:29 -07005730 return 0;
5731}
5732
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005733static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5734{
5735 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5736 : HRTIMER_MODE_REL;
5737}
5738
Jens Axboe11365042019-10-16 09:08:32 -06005739/*
5740 * Remove or update an existing timeout command
5741 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005742static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005743{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005744 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005745 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005746 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005747
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005748 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
5749 spin_lock(&ctx->completion_lock);
5750 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005751 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005752 spin_unlock_irq(&ctx->timeout_lock);
5753 spin_unlock(&ctx->completion_lock);
5754 } else {
5755 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005756 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5757 io_translate_timeout_mode(tr->flags));
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005758 spin_unlock_irq(&ctx->timeout_lock);
5759 }
Jens Axboe11365042019-10-16 09:08:32 -06005760
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005761 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005762 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005763 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06005764 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005765}
5766
Jens Axboe3529d8c2019-12-19 18:24:38 -07005767static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005768 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005769{
Jens Axboead8a48a2019-11-15 08:49:11 -07005770 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005771 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005772 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005773
Jens Axboead8a48a2019-11-15 08:49:11 -07005774 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005775 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005776 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
5777 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06005778 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005779 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005780 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005781 flags = READ_ONCE(sqe->timeout_flags);
5782 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005783 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005784
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005785 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01005786 if (unlikely(off && !req->ctx->off_timeout_used))
5787 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07005788
Jens Axboee8c2bc12020-08-15 18:44:09 -07005789 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005790 return -ENOMEM;
5791
Jens Axboee8c2bc12020-08-15 18:44:09 -07005792 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005793 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005794
5795 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005796 return -EFAULT;
5797
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005798 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005799 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005800
5801 if (is_timeout_link) {
5802 struct io_submit_link *link = &req->ctx->submit_state.link;
5803
5804 if (!link->head)
5805 return -EINVAL;
5806 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
5807 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01005808 req->timeout.head = link->last;
5809 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005810 }
Jens Axboead8a48a2019-11-15 08:49:11 -07005811 return 0;
5812}
5813
Pavel Begunkov61e98202021-02-10 00:03:08 +00005814static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005815{
Jens Axboead8a48a2019-11-15 08:49:11 -07005816 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005817 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005818 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005819 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005820
Jens Axboe89850fc2021-08-10 15:11:51 -06005821 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005822
Jens Axboe5262f562019-09-17 12:26:57 -06005823 /*
5824 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005825 * timeout event to be satisfied. If it isn't set, then this is
5826 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005827 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005828 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005829 entry = ctx->timeout_list.prev;
5830 goto add;
5831 }
Jens Axboe5262f562019-09-17 12:26:57 -06005832
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005833 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5834 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005835
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005836 /* Update the last seq here in case io_flush_timeouts() hasn't.
5837 * This is safe because ->completion_lock is held, and submissions
5838 * and completions are never mixed in the same ->completion_lock section.
5839 */
5840 ctx->cq_last_tm_flush = tail;
5841
Jens Axboe5262f562019-09-17 12:26:57 -06005842 /*
5843 * Insertion sort, ensuring the first entry in the list is always
5844 * the one we need first.
5845 */
Jens Axboe5262f562019-09-17 12:26:57 -06005846 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005847 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5848 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005849
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005850 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005851 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005852 /* nxt.seq is behind @tail, otherwise would've been completed */
5853 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005854 break;
5855 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005856add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005857 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005858 data->timer.function = io_timeout_fn;
5859 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06005860 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005861 return 0;
5862}
5863
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005864struct io_cancel_data {
5865 struct io_ring_ctx *ctx;
5866 u64 user_data;
5867};
5868
Jens Axboe62755e32019-10-28 21:49:21 -06005869static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005870{
Jens Axboe62755e32019-10-28 21:49:21 -06005871 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005872 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06005873
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005874 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06005875}
5876
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005877static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
5878 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06005879{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005880 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06005881 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005882 int ret = 0;
5883
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005884 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005885 return -ENOENT;
5886
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005887 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005888 switch (cancel_ret) {
5889 case IO_WQ_CANCEL_OK:
5890 ret = 0;
5891 break;
5892 case IO_WQ_CANCEL_RUNNING:
5893 ret = -EALREADY;
5894 break;
5895 case IO_WQ_CANCEL_NOTFOUND:
5896 ret = -ENOENT;
5897 break;
5898 }
5899
Jens Axboee977d6d2019-11-05 12:39:45 -07005900 return ret;
5901}
5902
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005903static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07005904{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005905 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005906 int ret;
5907
Pavel Begunkovdadebc32021-08-23 13:30:44 +01005908 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005909
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005910 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01005911 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005912 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01005913
5914 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005915 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005916 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005917 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005918 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01005919 goto out;
5920 ret = io_poll_cancel(ctx, sqe_addr, false);
5921out:
5922 spin_unlock(&ctx->completion_lock);
5923 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005924}
5925
Jens Axboe3529d8c2019-12-19 18:24:38 -07005926static int io_async_cancel_prep(struct io_kiocb *req,
5927 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005928{
Jens Axboefbf23842019-12-17 18:45:56 -07005929 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005930 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005931 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5932 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005933 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
5934 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07005935 return -EINVAL;
5936
Jens Axboefbf23842019-12-17 18:45:56 -07005937 req->cancel.addr = READ_ONCE(sqe->addr);
5938 return 0;
5939}
5940
Pavel Begunkov61e98202021-02-10 00:03:08 +00005941static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005942{
5943 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005944 u64 sqe_addr = req->cancel.addr;
5945 struct io_tctx_node *node;
5946 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07005947
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005948 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00005949 if (ret != -ENOENT)
5950 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005951
5952 /* slow path, try all io-wq's */
5953 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
5954 ret = -ENOENT;
5955 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
5956 struct io_uring_task *tctx = node->task->io_uring;
5957
Pavel Begunkov58f99372021-03-12 16:25:55 +00005958 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
5959 if (ret != -ENOENT)
5960 break;
5961 }
5962 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00005963done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00005964 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005965 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005966 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005967 return 0;
5968}
5969
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005970static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005971 const struct io_uring_sqe *sqe)
5972{
Daniele Albano61710e42020-07-18 14:15:16 -06005973 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5974 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005975 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005976 return -EINVAL;
5977
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005978 req->rsrc_update.offset = READ_ONCE(sqe->off);
5979 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5980 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005981 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005982 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005983 return 0;
5984}
5985
Pavel Begunkov889fca72021-02-10 00:03:09 +00005986static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005987{
5988 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005989 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005990 int ret;
5991
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005992 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005993 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005994
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005995 up.offset = req->rsrc_update.offset;
5996 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005997 up.nr = 0;
5998 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01005999 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006000
6001 mutex_lock(&ctx->uring_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006002 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006003 &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006004 mutex_unlock(&ctx->uring_lock);
6005
6006 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006007 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006008 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006009 return 0;
6010}
6011
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006012static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006013{
Jens Axboed625c6e2019-12-17 19:53:05 -07006014 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006015 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006016 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006017 case IORING_OP_READV:
6018 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006019 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006020 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006021 case IORING_OP_WRITEV:
6022 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006023 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006024 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006025 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006026 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006027 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006028 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006029 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006030 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006031 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006032 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006033 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006034 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006035 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006036 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006037 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006038 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006039 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006040 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006041 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006042 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006043 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006044 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006045 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006046 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006047 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006048 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006049 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006050 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006051 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006052 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006053 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006054 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006055 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006056 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006057 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006058 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006059 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006060 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006061 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006062 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006063 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006064 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006065 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006066 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006067 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006068 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006069 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006070 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006071 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006072 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006073 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006074 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006075 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006076 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006077 case IORING_OP_SHUTDOWN:
6078 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006079 case IORING_OP_RENAMEAT:
6080 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006081 case IORING_OP_UNLINKAT:
6082 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006083 }
6084
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006085 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6086 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006087 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006088}
6089
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006090static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006091{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006092 if (!io_op_defs[req->opcode].needs_async_setup)
6093 return 0;
6094 if (WARN_ON_ONCE(req->async_data))
6095 return -EFAULT;
6096 if (io_alloc_async_data(req))
6097 return -EAGAIN;
6098
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006099 switch (req->opcode) {
6100 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006101 return io_rw_prep_async(req, READ);
6102 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006103 return io_rw_prep_async(req, WRITE);
6104 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006105 return io_sendmsg_prep_async(req);
6106 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006107 return io_recvmsg_prep_async(req);
6108 case IORING_OP_CONNECT:
6109 return io_connect_prep_async(req);
6110 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006111 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6112 req->opcode);
6113 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006114}
6115
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006116static u32 io_get_sequence(struct io_kiocb *req)
6117{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006118 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006119
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006120 /* need original cached_sq_head, but it was increased for each req */
6121 io_for_each_link(req, req)
6122 seq--;
6123 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006124}
6125
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006126static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006127{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006128 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006129 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006130 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006131 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006132 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006133
Pavel Begunkov3c199662021-06-15 16:47:57 +01006134 /*
6135 * If we need to drain a request in the middle of a link, drain the
6136 * head request and the next request/link after the current link.
6137 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6138 * maintained for every request of our link.
6139 */
6140 if (ctx->drain_next) {
6141 req->flags |= REQ_F_IO_DRAIN;
6142 ctx->drain_next = false;
6143 }
6144 /* not interested in head, start from the first linked */
6145 io_for_each_link(pos, req->link) {
6146 if (pos->flags & REQ_F_IO_DRAIN) {
6147 ctx->drain_next = true;
6148 req->flags |= REQ_F_IO_DRAIN;
6149 break;
6150 }
6151 }
6152
Jens Axboedef596e2019-01-09 08:59:42 -07006153 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006154 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006155 !(req->flags & REQ_F_IO_DRAIN))) {
6156 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006157 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006158 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006159
6160 seq = io_get_sequence(req);
6161 /* Still a chance to pass the sequence check */
6162 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006163 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006164
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006165 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006166 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006167 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006168 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006169 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006170 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006171 ret = -ENOMEM;
6172fail:
6173 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006174 return true;
6175 }
Jens Axboe31b51512019-01-18 22:56:34 -07006176
Jens Axboe79ebeae2021-08-10 15:18:27 -06006177 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006178 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006179 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006180 kfree(de);
Pavel Begunkovf237c302021-08-18 12:42:46 +01006181 io_queue_async_work(req, NULL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006182 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006183 }
6184
6185 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006186 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006187 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006188 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006189 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006190 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006191}
6192
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006193static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006194{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006195 if (req->flags & REQ_F_BUFFER_SELECTED) {
6196 switch (req->opcode) {
6197 case IORING_OP_READV:
6198 case IORING_OP_READ_FIXED:
6199 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006200 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006201 break;
6202 case IORING_OP_RECVMSG:
6203 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006204 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006205 break;
6206 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006207 }
6208
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006209 if (req->flags & REQ_F_NEED_CLEANUP) {
6210 switch (req->opcode) {
6211 case IORING_OP_READV:
6212 case IORING_OP_READ_FIXED:
6213 case IORING_OP_READ:
6214 case IORING_OP_WRITEV:
6215 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006216 case IORING_OP_WRITE: {
6217 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006218
6219 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006220 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006221 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006222 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006223 case IORING_OP_SENDMSG: {
6224 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006225
6226 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006227 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006228 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006229 case IORING_OP_SPLICE:
6230 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006231 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6232 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006233 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006234 case IORING_OP_OPENAT:
6235 case IORING_OP_OPENAT2:
6236 if (req->open.filename)
6237 putname(req->open.filename);
6238 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006239 case IORING_OP_RENAMEAT:
6240 putname(req->rename.oldpath);
6241 putname(req->rename.newpath);
6242 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006243 case IORING_OP_UNLINKAT:
6244 putname(req->unlink.filename);
6245 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006246 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006247 }
Jens Axboe75652a302021-04-15 09:52:40 -06006248 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6249 kfree(req->apoll->double_poll);
6250 kfree(req->apoll);
6251 req->apoll = NULL;
6252 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006253 if (req->flags & REQ_F_INFLIGHT) {
6254 struct io_uring_task *tctx = req->task->io_uring;
6255
6256 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006257 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006258 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006259 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006260
6261 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006262}
6263
Pavel Begunkov889fca72021-02-10 00:03:09 +00006264static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006265{
Jens Axboeedafcce2019-01-09 09:16:05 -07006266 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006267 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006268 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006269
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006270 if ((req->flags & REQ_F_CREDS) && req->creds != current_cred())
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006271 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006272
Jens Axboed625c6e2019-12-17 19:53:05 -07006273 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006274 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006275 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006276 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006277 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006278 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006279 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006280 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006281 break;
6282 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006283 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006284 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006285 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006286 break;
6287 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006288 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006289 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006291 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006292 break;
6293 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006294 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006295 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006296 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006297 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006298 break;
6299 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006300 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006301 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006302 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006303 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006304 break;
6305 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006306 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006307 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006308 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006309 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006310 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006311 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006312 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006313 break;
6314 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006315 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316 break;
6317 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006318 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006319 break;
6320 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006321 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006322 break;
6323 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006324 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006325 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006326 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006327 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006328 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006329 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006330 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006331 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006332 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006333 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006334 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006335 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006336 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006337 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006338 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006339 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006340 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006341 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006342 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006343 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006344 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006345 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006346 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006347 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006348 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006349 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006350 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006351 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006352 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006353 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006354 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006355 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006356 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006357 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006358 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006359 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006360 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006361 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006362 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006363 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006364 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006365 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006366 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006367 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006368 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006369 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006370 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006371 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006372 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006373 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374 default:
6375 ret = -EINVAL;
6376 break;
6377 }
Jens Axboe31b51512019-01-18 22:56:34 -07006378
Jens Axboe5730b272021-02-27 15:57:30 -07006379 if (creds)
6380 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006381 if (ret)
6382 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006383 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006384 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6385 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006386
6387 return 0;
6388}
6389
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006390static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6391{
6392 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6393
6394 req = io_put_req_find_next(req);
6395 return req ? &req->work : NULL;
6396}
6397
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006398static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006399{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006400 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006401 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006402 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006403
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006404 /* one will be dropped by ->io_free_work() after returning to io-wq */
6405 if (!(req->flags & REQ_F_REFCOUNT))
6406 __io_req_set_refcount(req, 2);
6407 else
6408 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006409
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006410 timeout = io_prep_linked_timeout(req);
6411 if (timeout)
6412 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006413
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006414 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006415 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006416 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006417
Jens Axboe561fb042019-10-24 07:25:42 -06006418 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006419 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006420 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006421 /*
6422 * We can get EAGAIN for polled IO even though we're
6423 * forcing a sync submission from here, since we can't
6424 * wait for request slots on the block side.
6425 */
6426 if (ret != -EAGAIN)
6427 break;
6428 cond_resched();
6429 } while (1);
6430 }
Jens Axboe31b51512019-01-18 22:56:34 -07006431
Pavel Begunkova3df76982021-02-18 22:32:52 +00006432 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006433 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006434 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006435}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006436
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006437static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006438 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006439{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006440 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006441}
6442
Jens Axboe09bb8392019-03-13 12:39:28 -06006443static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6444 int index)
6445{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006446 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006447
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006448 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006449}
6450
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006451static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006452{
6453 unsigned long file_ptr = (unsigned long) file;
6454
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006455 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006456 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006457 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006458 file_ptr |= FFS_ASYNC_WRITE;
6459 if (S_ISREG(file_inode(file)->i_mode))
6460 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006461 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006462}
6463
Pavel Begunkovac177052021-08-09 13:04:02 +01006464static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6465 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006466{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006467 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006468 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006469
Pavel Begunkovac177052021-08-09 13:04:02 +01006470 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6471 return NULL;
6472 fd = array_index_nospec(fd, ctx->nr_user_files);
6473 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6474 file = (struct file *) (file_ptr & FFS_MASK);
6475 file_ptr &= ~FFS_MASK;
6476 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006477 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006478 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006479 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006480}
6481
Pavel Begunkovac177052021-08-09 13:04:02 +01006482static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006483 struct io_kiocb *req, int fd)
6484{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006485 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006486
6487 trace_io_uring_file_get(ctx, fd);
6488
6489 /* we don't allow fixed io_uring files */
6490 if (file && unlikely(file->f_op == &io_uring_fops))
6491 io_req_track_inflight(req);
6492 return file;
6493}
6494
6495static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006496 struct io_kiocb *req, int fd, bool fixed)
6497{
6498 if (fixed)
6499 return io_file_get_fixed(ctx, req, fd);
6500 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006501 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006502}
6503
Pavel Begunkovf237c302021-08-18 12:42:46 +01006504static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006505{
6506 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006507 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006508
6509 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006510 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006511 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006512 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006513 } else {
6514 io_req_complete_post(req, -ETIME, 0);
6515 }
6516}
6517
Jens Axboe2665abf2019-11-05 12:40:47 -07006518static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6519{
Jens Axboead8a48a2019-11-15 08:49:11 -07006520 struct io_timeout_data *data = container_of(timer,
6521 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006522 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006523 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006524 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006525
Jens Axboe89b263f2021-08-10 15:14:18 -06006526 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006527 prev = req->timeout.head;
6528 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006529
6530 /*
6531 * We don't expect the list to be empty, that will only happen if we
6532 * race with the completion of the linked work.
6533 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006534 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006535 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006536 if (!req_ref_inc_not_zero(prev))
6537 prev = NULL;
6538 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006539 req->timeout.prev = prev;
6540 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006541
Jens Axboe89b263f2021-08-10 15:14:18 -06006542 req->io_task_work.func = io_req_task_link_timeout;
6543 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006544 return HRTIMER_NORESTART;
6545}
6546
Pavel Begunkovde968c12021-03-19 17:22:33 +00006547static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006548{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006549 struct io_ring_ctx *ctx = req->ctx;
6550
Jens Axboe89b263f2021-08-10 15:14:18 -06006551 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006552 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006553 * If the back reference is NULL, then our linked request finished
6554 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006555 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006556 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006557 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006558
Jens Axboead8a48a2019-11-15 08:49:11 -07006559 data->timer.function = io_link_timeout_fn;
6560 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6561 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006562 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006563 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006564 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006565 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006566}
6567
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006568static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006569 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006570{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006571 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006572 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006573
Olivier Langlois59b735a2021-06-22 05:17:39 -07006574issue_sqe:
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006575 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006576
6577 /*
6578 * We async punt it if the file wasn't marked NOWAIT, or if the file
6579 * doesn't support non-blocking read/write attempts
6580 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006581 if (likely(!ret)) {
Pavel Begunkove342c802021-01-19 13:32:47 +00006582 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006583 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006584 struct io_submit_state *state = &ctx->submit_state;
Jens Axboee65ef562019-03-12 10:16:44 -06006585
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006586 state->compl_reqs[state->compl_nr++] = req;
6587 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006588 io_submit_flush_completions(ctx);
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006589 return;
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006590 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006591
6592 linked_timeout = io_prep_linked_timeout(req);
6593 if (linked_timeout)
6594 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006595 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006596 linked_timeout = io_prep_linked_timeout(req);
6597
Olivier Langlois59b735a2021-06-22 05:17:39 -07006598 switch (io_arm_poll_handler(req)) {
6599 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006600 if (linked_timeout)
6601 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006602 goto issue_sqe;
6603 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006604 /*
6605 * Queued up for async execution, worker will release
6606 * submit reference when the iocb is actually submitted.
6607 */
Pavel Begunkovf237c302021-08-18 12:42:46 +01006608 io_queue_async_work(req, NULL);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006609 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006610 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006611
6612 if (linked_timeout)
6613 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006614 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006615 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006616 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006617}
6618
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006619static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006620 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006621{
Pavel Begunkov10c66902021-06-15 16:47:56 +01006622 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006623 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08006624
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006625 if (likely(!(req->flags & REQ_F_FORCE_ASYNC))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006626 __io_queue_sqe(req);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006627 } else {
6628 int ret = io_req_prep_async(req);
6629
6630 if (unlikely(ret))
6631 io_req_complete_failed(req, ret);
6632 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01006633 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07006634 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006635}
6636
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006637/*
6638 * Check SQE restrictions (opcode and flags).
6639 *
6640 * Returns 'true' if SQE is allowed, 'false' otherwise.
6641 */
6642static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6643 struct io_kiocb *req,
6644 unsigned int sqe_flags)
6645{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006646 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006647 return true;
6648
6649 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6650 return false;
6651
6652 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6653 ctx->restrictions.sqe_flags_required)
6654 return false;
6655
6656 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6657 ctx->restrictions.sqe_flags_required))
6658 return false;
6659
6660 return true;
6661}
6662
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006663static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006664 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006665 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006666{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006667 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006668 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006669 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006670
Pavel Begunkov864ea922021-08-09 13:04:08 +01006671 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006672 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006673 /* same numerical values with corresponding REQ_F_*, safe to copy */
6674 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006675 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006676 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006677 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006678 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006679
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006680 /* enforce forwards compatibility on users */
Pavel Begunkovdddca222021-04-27 16:13:52 +01006681 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006682 return -EINVAL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006683 if (unlikely(req->opcode >= IORING_OP_LAST))
6684 return -EINVAL;
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006685 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006686 return -EACCES;
6687
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006688 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6689 !io_op_defs[req->opcode].buffer_select)
6690 return -EOPNOTSUPP;
Pavel Begunkov3c199662021-06-15 16:47:57 +01006691 if (unlikely(sqe_flags & IOSQE_IO_DRAIN))
6692 ctx->drain_active = true;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006693
Jens Axboe003e8dc2021-03-06 09:22:27 -07006694 personality = READ_ONCE(sqe->personality);
6695 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006696 req->creds = xa_load(&ctx->personalities, personality);
6697 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07006698 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006699 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006700 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006701 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006702 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006703
Jens Axboe27926b62020-10-28 09:33:23 -06006704 /*
6705 * Plug now if we have more than 1 IO left after this, and the target
6706 * is potentially a read/write to block based storage.
6707 */
6708 if (!state->plug_started && state->ios_left > 1 &&
6709 io_op_defs[req->opcode].plug) {
6710 blk_start_plug(&state->plug);
6711 state->plug_started = true;
6712 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006713
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006714 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01006715 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01006716 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00006717 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006718 ret = -EBADF;
6719 }
6720
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006721 state->ios_left--;
6722 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006723}
6724
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006725static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006726 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006727 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006728{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006729 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006730 int ret;
6731
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006732 ret = io_init_req(ctx, req, sqe);
6733 if (unlikely(ret)) {
6734fail_req:
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006735 if (link->head) {
6736 /* fail even hard links since we don't submit */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006737 req_set_fail(link->head);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006738 io_req_complete_failed(link->head, -ECANCELED);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006739 link->head = NULL;
6740 }
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006741 io_req_complete_failed(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006742 return ret;
6743 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006744
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006745 ret = io_req_prep(req, sqe);
6746 if (unlikely(ret))
6747 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006748
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006749 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04006750 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
6751 req->flags, true,
6752 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006753
Jens Axboe6c271ce2019-01-10 11:22:30 -07006754 /*
6755 * If we already have a head request, queue this one for async
6756 * submittal once the head completes. If we don't have a head but
6757 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6758 * submitted sync once the chain is complete. If none of those
6759 * conditions are true (normal request), then just queue it.
6760 */
6761 if (link->head) {
6762 struct io_kiocb *head = link->head;
6763
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006764 ret = io_req_prep_async(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006765 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006766 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006767 trace_io_uring_link(ctx, req, head);
6768 link->last->link = req;
6769 link->last = req;
6770
6771 /* last request of a link, enqueue the link */
6772 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
6773 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01006774 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006775 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006776 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006777 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006778 link->head = req;
6779 link->last = req;
6780 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006781 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006782 }
6783 }
6784
6785 return 0;
6786}
6787
6788/*
6789 * Batched submission is done, ensure local IO is flushed out.
6790 */
6791static void io_submit_state_end(struct io_submit_state *state,
6792 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006793{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006794 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006795 io_queue_sqe(state->link.head);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006796 if (state->compl_nr)
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006797 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006798 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006799 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06006800}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006801
Jens Axboe9e645e112019-05-10 16:07:28 -06006802/*
6803 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006804 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006805static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006806 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006807{
6808 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006809 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006810 /* set only head, no need to init link_last in advance */
6811 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006812}
6813
Jens Axboe193155c2020-02-22 23:22:19 -07006814static void io_commit_sqring(struct io_ring_ctx *ctx)
6815{
Jens Axboe75c6a032020-01-28 10:15:23 -07006816 struct io_rings *rings = ctx->rings;
6817
6818 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006819 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006820 * since once we write the new head, the application could
6821 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006822 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006823 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006824}
6825
Jens Axboe9e645e112019-05-10 16:07:28 -06006826/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01006827 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006828 * that is mapped by userspace. This means that care needs to be taken to
6829 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006830 * being a good citizen. If members of the sqe are validated and then later
6831 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006832 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006833 */
6834static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006835{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01006836 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006837 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06006838
6839 /*
6840 * The cached sq head (or cq tail) serves two purposes:
6841 *
6842 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006843 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006844 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006845 * though the application is the one updating it.
6846 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006847 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006848 if (likely(head < ctx->sq_entries))
6849 return &ctx->sq_sqes[head];
6850
6851 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01006852 ctx->cq_extra--;
6853 WRITE_ONCE(ctx->rings->sq_dropped,
6854 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03006855 return NULL;
6856}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006857
Jens Axboe0f212202020-09-13 13:09:39 -06006858static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006859 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006860{
Pavel Begunkov09899b12021-06-14 02:36:22 +01006861 struct io_uring_task *tctx;
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006862 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006863
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006864 /* make sure SQ entry isn't read before tail */
6865 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006866 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6867 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006868
Pavel Begunkov09899b12021-06-14 02:36:22 +01006869 tctx = current->io_uring;
6870 tctx->cached_refs -= nr;
6871 if (unlikely(tctx->cached_refs < 0)) {
6872 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
6873
6874 percpu_counter_add(&tctx->inflight, refill);
6875 refcount_add(refill, &current->usage);
6876 tctx->cached_refs += refill;
6877 }
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006878 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006879
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006880 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006881 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006882 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006883
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006884 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006885 if (unlikely(!req)) {
6886 if (!submitted)
6887 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006888 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006889 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006890 sqe = io_get_sqe(ctx);
6891 if (unlikely(!sqe)) {
6892 kmem_cache_free(req_cachep, req);
6893 break;
6894 }
Jens Axboed3656342019-12-18 09:50:26 -07006895 /* will complete beyond this point, count as submitted */
6896 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006897 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006898 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006899 }
6900
Pavel Begunkov9466f432020-01-25 22:34:01 +03006901 if (unlikely(submitted != nr)) {
6902 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006903 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006904
Pavel Begunkov09899b12021-06-14 02:36:22 +01006905 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06006906 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006907 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006908
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006909 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006910 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6911 io_commit_sqring(ctx);
6912
Jens Axboe6c271ce2019-01-10 11:22:30 -07006913 return submitted;
6914}
6915
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006916static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
6917{
6918 return READ_ONCE(sqd->state);
6919}
6920
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006921static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6922{
6923 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06006924 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006925 WRITE_ONCE(ctx->rings->sq_flags,
6926 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006927 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006928}
6929
6930static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6931{
Jens Axboe79ebeae2021-08-10 15:18:27 -06006932 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006933 WRITE_ONCE(ctx->rings->sq_flags,
6934 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006935 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006936}
6937
Xiaoguang Wang08369242020-11-03 14:15:59 +08006938static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006939{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006940 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006941 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006942
Jens Axboec8d1ba52020-09-14 11:07:26 -06006943 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006944 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07006945 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
6946 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06006947
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006948 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6949 unsigned nr_events = 0;
Pavel Begunkov948e1942021-06-24 15:09:55 +01006950 const struct cred *creds = NULL;
6951
6952 if (ctx->sq_creds != current_cred())
6953 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006954
Xiaoguang Wang08369242020-11-03 14:15:59 +08006955 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006956 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkova8576af2021-08-15 10:40:21 +01006957 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006958
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01006959 /*
6960 * Don't submit if refs are dying, good for io_uring_register(),
6961 * but also it is relied upon by io_ring_exit_work()
6962 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00006963 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
6964 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006965 ret = io_submit_sqes(ctx, to_submit);
6966 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006967
Pavel Begunkovacfb3812021-05-16 22:58:03 +01006968 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
6969 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01006970 if (creds)
6971 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01006972 }
Jens Axboe90554202020-09-03 12:12:41 -06006973
Xiaoguang Wang08369242020-11-03 14:15:59 +08006974 return ret;
6975}
6976
6977static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6978{
6979 struct io_ring_ctx *ctx;
6980 unsigned sq_thread_idle = 0;
6981
Pavel Begunkovc9dca272021-03-10 13:13:55 +00006982 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6983 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006984 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006985}
6986
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006987static bool io_sqd_handle_event(struct io_sq_data *sqd)
6988{
6989 bool did_sig = false;
6990 struct ksignal ksig;
6991
6992 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
6993 signal_pending(current)) {
6994 mutex_unlock(&sqd->lock);
6995 if (signal_pending(current))
6996 did_sig = get_signal(&ksig);
6997 cond_resched();
6998 mutex_lock(&sqd->lock);
6999 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007000 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7001}
7002
Jens Axboe6c271ce2019-01-10 11:22:30 -07007003static int io_sq_thread(void *data)
7004{
Jens Axboe69fb2132020-09-14 11:16:23 -06007005 struct io_sq_data *sqd = data;
7006 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007007 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007008 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007009 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007010
Pavel Begunkov696ee882021-04-01 09:55:04 +01007011 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007012 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007013
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007014 if (sqd->sq_cpu != -1)
7015 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7016 else
7017 set_cpus_allowed_ptr(current, cpu_online_mask);
7018 current->flags |= PF_NO_SETAFFINITY;
7019
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007020 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007021 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007022 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007023
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007024 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7025 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007026 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007027 timeout = jiffies + sqd->sq_thread_idle;
7028 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007029
Jens Axboee95eee22020-09-08 09:11:32 -06007030 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007031 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007032 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007033
Xiaoguang Wang08369242020-11-03 14:15:59 +08007034 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7035 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007036 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007037 if (io_run_task_work())
7038 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007039
Xiaoguang Wang08369242020-11-03 14:15:59 +08007040 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007041 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007042 if (sqt_spin)
7043 timeout = jiffies + sqd->sq_thread_idle;
7044 continue;
7045 }
7046
Xiaoguang Wang08369242020-11-03 14:15:59 +08007047 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007048 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007049 bool needs_sched = true;
7050
Hao Xu724cb4f2021-04-21 23:19:11 +08007051 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007052 io_ring_set_wakeup_flag(ctx);
7053
Hao Xu724cb4f2021-04-21 23:19:11 +08007054 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7055 !list_empty_careful(&ctx->iopoll_list)) {
7056 needs_sched = false;
7057 break;
7058 }
7059 if (io_sqring_entries(ctx)) {
7060 needs_sched = false;
7061 break;
7062 }
7063 }
7064
7065 if (needs_sched) {
7066 mutex_unlock(&sqd->lock);
7067 schedule();
7068 mutex_lock(&sqd->lock);
7069 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007070 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7071 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007072 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007073
7074 finish_wait(&sqd->wait, &wait);
7075 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007076 }
7077
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007078 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007079 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007080 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007081 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007082 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007083 mutex_unlock(&sqd->lock);
7084
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007085 complete(&sqd->exited);
7086 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007087}
7088
Jens Axboebda52162019-09-24 13:47:15 -06007089struct io_wait_queue {
7090 struct wait_queue_entry wq;
7091 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007092 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007093 unsigned nr_timeouts;
7094};
7095
Pavel Begunkov6c503152021-01-04 20:36:36 +00007096static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007097{
7098 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007099 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007100
7101 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007102 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007103 * started waiting. For timeouts, we always want to return to userspace,
7104 * regardless of event count.
7105 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007106 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007107}
7108
7109static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7110 int wake_flags, void *key)
7111{
7112 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7113 wq);
7114
Pavel Begunkov6c503152021-01-04 20:36:36 +00007115 /*
7116 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7117 * the task, and the next invocation will do it.
7118 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007119 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007120 return autoremove_wake_function(curr, mode, wake_flags, key);
7121 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007122}
7123
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007124static int io_run_task_work_sig(void)
7125{
7126 if (io_run_task_work())
7127 return 1;
7128 if (!signal_pending(current))
7129 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007130 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007131 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007132 return -EINTR;
7133}
7134
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007135/* when returns >0, the caller should retry */
7136static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7137 struct io_wait_queue *iowq,
7138 signed long *timeout)
7139{
7140 int ret;
7141
7142 /* make sure we run task_work before checking for signals */
7143 ret = io_run_task_work_sig();
7144 if (ret || io_should_wake(iowq))
7145 return ret;
7146 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007147 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007148 return 1;
7149
7150 *timeout = schedule_timeout(*timeout);
7151 return !*timeout ? -ETIME : 1;
7152}
7153
Jens Axboe2b188cc2019-01-07 10:46:33 -07007154/*
7155 * Wait until events become available, if we don't already have some. The
7156 * application must reap them itself, as they reside on the shared cq ring.
7157 */
7158static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007159 const sigset_t __user *sig, size_t sigsz,
7160 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007161{
Pavel Begunkov902910992021-08-09 09:07:32 -06007162 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007163 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007164 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7165 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007166
Jens Axboeb41e9852020-02-17 09:52:41 -07007167 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007168 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007169 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007170 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007171 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007172 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007173 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007174
7175 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007176#ifdef CONFIG_COMPAT
7177 if (in_compat_syscall())
7178 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007179 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007180 else
7181#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007182 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007183
Jens Axboe2b188cc2019-01-07 10:46:33 -07007184 if (ret)
7185 return ret;
7186 }
7187
Hao Xuc73ebb62020-11-03 10:54:37 +08007188 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007189 struct timespec64 ts;
7190
Hao Xuc73ebb62020-11-03 10:54:37 +08007191 if (get_timespec64(&ts, uts))
7192 return -EFAULT;
7193 timeout = timespec64_to_jiffies(&ts);
7194 }
7195
Pavel Begunkov902910992021-08-09 09:07:32 -06007196 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7197 iowq.wq.private = current;
7198 INIT_LIST_HEAD(&iowq.wq.entry);
7199 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007200 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007201 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007202
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007203 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007204 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007205 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007206 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007207 ret = -EBUSY;
7208 break;
7209 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007210 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007211 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007212 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007213 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007214 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007215 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007216
Jens Axboeb7db41c2020-07-04 08:55:50 -06007217 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007218
Hristo Venev75b28af2019-08-26 17:23:46 +00007219 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007220}
7221
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007222static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007223{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007224 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007225
7226 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007227 kfree(table[i]);
7228 kfree(table);
7229}
7230
7231static void **io_alloc_page_table(size_t size)
7232{
7233 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7234 size_t init_size = size;
7235 void **table;
7236
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007237 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007238 if (!table)
7239 return NULL;
7240
7241 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007242 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007243
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007244 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007245 if (!table[i]) {
7246 io_free_page_table(table, init_size);
7247 return NULL;
7248 }
7249 size -= this_size;
7250 }
7251 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007252}
7253
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007254static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7255{
7256 percpu_ref_exit(&ref_node->refs);
7257 kfree(ref_node);
7258}
7259
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007260static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7261{
7262 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7263 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7264 unsigned long flags;
7265 bool first_add = false;
7266
7267 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7268 node->done = true;
7269
7270 while (!list_empty(&ctx->rsrc_ref_list)) {
7271 node = list_first_entry(&ctx->rsrc_ref_list,
7272 struct io_rsrc_node, node);
7273 /* recycle ref nodes in order */
7274 if (!node->done)
7275 break;
7276 list_del(&node->node);
7277 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7278 }
7279 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7280
7281 if (first_add)
7282 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7283}
7284
7285static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7286{
7287 struct io_rsrc_node *ref_node;
7288
7289 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7290 if (!ref_node)
7291 return NULL;
7292
7293 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7294 0, GFP_KERNEL)) {
7295 kfree(ref_node);
7296 return NULL;
7297 }
7298 INIT_LIST_HEAD(&ref_node->node);
7299 INIT_LIST_HEAD(&ref_node->rsrc_list);
7300 ref_node->done = false;
7301 return ref_node;
7302}
7303
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007304static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7305 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007306{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007307 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7308 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007309
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007310 if (data_to_kill) {
7311 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007312
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007313 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007314 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007315 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007316 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007317
Pavel Begunkov3e942492021-04-11 01:46:34 +01007318 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007319 percpu_ref_kill(&rsrc_node->refs);
7320 ctx->rsrc_node = NULL;
7321 }
7322
7323 if (!ctx->rsrc_node) {
7324 ctx->rsrc_node = ctx->rsrc_backup_node;
7325 ctx->rsrc_backup_node = NULL;
7326 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007327}
7328
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007329static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007330{
7331 if (ctx->rsrc_backup_node)
7332 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007333 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007334 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7335}
7336
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007337static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007338{
7339 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007340
Pavel Begunkov215c3902021-04-01 15:43:48 +01007341 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007342 if (data->quiesce)
7343 return -ENXIO;
7344
7345 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007346 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007347 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007348 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007349 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007350 io_rsrc_node_switch(ctx, data);
7351
Pavel Begunkov3e942492021-04-11 01:46:34 +01007352 /* kill initial ref, already quiesced if zero */
7353 if (atomic_dec_and_test(&data->refs))
7354 break;
Jens Axboec018db42021-08-09 08:15:50 -06007355 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007356 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007357 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007358 if (!ret) {
7359 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007360 break;
Jens Axboec018db42021-08-09 08:15:50 -06007361 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007362
Pavel Begunkov3e942492021-04-11 01:46:34 +01007363 atomic_inc(&data->refs);
7364 /* wait for all works potentially completing data->done */
7365 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007366 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007367
Hao Xu8bad28d2021-02-19 17:19:36 +08007368 ret = io_run_task_work_sig();
7369 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007370 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007371 data->quiesce = false;
7372
Hao Xu8bad28d2021-02-19 17:19:36 +08007373 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007374}
7375
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007376static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7377{
7378 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7379 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7380
7381 return &data->tags[table_idx][off];
7382}
7383
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007384static void io_rsrc_data_free(struct io_rsrc_data *data)
7385{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007386 size_t size = data->nr * sizeof(data->tags[0][0]);
7387
7388 if (data->tags)
7389 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007390 kfree(data);
7391}
7392
Pavel Begunkovd878c812021-06-14 02:36:18 +01007393static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7394 u64 __user *utags, unsigned nr,
7395 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007396{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007397 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007398 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007399 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007400
7401 data = kzalloc(sizeof(*data), GFP_KERNEL);
7402 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007403 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007404 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007405 if (!data->tags) {
7406 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007407 return -ENOMEM;
7408 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007409
7410 data->nr = nr;
7411 data->ctx = ctx;
7412 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007413 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007414 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007415 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007416 u64 *tag_slot = io_get_tag_slot(data, i);
7417
7418 if (copy_from_user(tag_slot, &utags[i],
7419 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007420 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007421 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007422 }
7423
Pavel Begunkov3e942492021-04-11 01:46:34 +01007424 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007425 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007426 *pdata = data;
7427 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007428fail:
7429 io_rsrc_data_free(data);
7430 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007431}
7432
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007433static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7434{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007435 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7436 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007437 return !!table->files;
7438}
7439
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007440static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007441{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007442 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007443 table->files = NULL;
7444}
7445
Jens Axboe2b188cc2019-01-07 10:46:33 -07007446static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7447{
7448#if defined(CONFIG_UNIX)
7449 if (ctx->ring_sock) {
7450 struct sock *sock = ctx->ring_sock->sk;
7451 struct sk_buff *skb;
7452
7453 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7454 kfree_skb(skb);
7455 }
7456#else
7457 int i;
7458
7459 for (i = 0; i < ctx->nr_user_files; i++) {
7460 struct file *file;
7461
7462 file = io_file_from_index(ctx, i);
7463 if (file)
7464 fput(file);
7465 }
7466#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007467 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007468 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007469 ctx->file_data = NULL;
7470 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007471}
7472
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007473static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7474{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007475 int ret;
7476
Pavel Begunkov08480402021-04-13 02:58:38 +01007477 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007478 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007479 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7480 if (!ret)
7481 __io_sqe_files_unregister(ctx);
7482 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007483}
7484
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007485static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007486 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007487{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007488 WARN_ON_ONCE(sqd->thread == current);
7489
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007490 /*
7491 * Do the dance but not conditional clear_bit() because it'd race with
7492 * other threads incrementing park_pending and setting the bit.
7493 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007494 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007495 if (atomic_dec_return(&sqd->park_pending))
7496 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007497 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007498}
7499
Jens Axboe86e0d672021-03-05 08:44:39 -07007500static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007501 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007502{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007503 WARN_ON_ONCE(sqd->thread == current);
7504
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007505 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007506 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007507 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007508 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007509 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007510}
7511
7512static void io_sq_thread_stop(struct io_sq_data *sqd)
7513{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007514 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007515 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007516
Jens Axboe05962f92021-03-06 13:58:48 -07007517 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007518 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007519 if (sqd->thread)
7520 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007521 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007522 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007523}
7524
Jens Axboe534ca6d2020-09-02 13:52:19 -06007525static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007526{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007527 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007528 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7529
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007530 io_sq_thread_stop(sqd);
7531 kfree(sqd);
7532 }
7533}
7534
7535static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7536{
7537 struct io_sq_data *sqd = ctx->sq_data;
7538
7539 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007540 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007541 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007542 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007543 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007544
7545 io_put_sq_data(sqd);
7546 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007547 }
7548}
7549
Jens Axboeaa061652020-09-02 14:50:27 -06007550static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7551{
7552 struct io_ring_ctx *ctx_attach;
7553 struct io_sq_data *sqd;
7554 struct fd f;
7555
7556 f = fdget(p->wq_fd);
7557 if (!f.file)
7558 return ERR_PTR(-ENXIO);
7559 if (f.file->f_op != &io_uring_fops) {
7560 fdput(f);
7561 return ERR_PTR(-EINVAL);
7562 }
7563
7564 ctx_attach = f.file->private_data;
7565 sqd = ctx_attach->sq_data;
7566 if (!sqd) {
7567 fdput(f);
7568 return ERR_PTR(-EINVAL);
7569 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007570 if (sqd->task_tgid != current->tgid) {
7571 fdput(f);
7572 return ERR_PTR(-EPERM);
7573 }
Jens Axboeaa061652020-09-02 14:50:27 -06007574
7575 refcount_inc(&sqd->refs);
7576 fdput(f);
7577 return sqd;
7578}
7579
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007580static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7581 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007582{
7583 struct io_sq_data *sqd;
7584
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007585 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007586 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7587 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007588 if (!IS_ERR(sqd)) {
7589 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007590 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007591 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007592 /* fall through for EPERM case, setup new sqd/task */
7593 if (PTR_ERR(sqd) != -EPERM)
7594 return sqd;
7595 }
Jens Axboeaa061652020-09-02 14:50:27 -06007596
Jens Axboe534ca6d2020-09-02 13:52:19 -06007597 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7598 if (!sqd)
7599 return ERR_PTR(-ENOMEM);
7600
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007601 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007602 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007603 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007604 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007605 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007606 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007607 return sqd;
7608}
7609
Jens Axboe6b063142019-01-10 22:13:58 -07007610#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007611/*
7612 * Ensure the UNIX gc is aware of our file set, so we are certain that
7613 * the io_uring can be safely unregistered on process exit, even if we have
7614 * loops in the file referencing.
7615 */
7616static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7617{
7618 struct sock *sk = ctx->ring_sock->sk;
7619 struct scm_fp_list *fpl;
7620 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007621 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007622
Jens Axboe6b063142019-01-10 22:13:58 -07007623 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7624 if (!fpl)
7625 return -ENOMEM;
7626
7627 skb = alloc_skb(0, GFP_KERNEL);
7628 if (!skb) {
7629 kfree(fpl);
7630 return -ENOMEM;
7631 }
7632
7633 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007634
Jens Axboe08a45172019-10-03 08:11:03 -06007635 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007636 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007637 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007638 struct file *file = io_file_from_index(ctx, i + offset);
7639
7640 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007641 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007642 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007643 unix_inflight(fpl->user, fpl->fp[nr_files]);
7644 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007645 }
7646
Jens Axboe08a45172019-10-03 08:11:03 -06007647 if (nr_files) {
7648 fpl->max = SCM_MAX_FD;
7649 fpl->count = nr_files;
7650 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007651 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007652 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7653 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007654
Jens Axboe08a45172019-10-03 08:11:03 -06007655 for (i = 0; i < nr_files; i++)
7656 fput(fpl->fp[i]);
7657 } else {
7658 kfree_skb(skb);
7659 kfree(fpl);
7660 }
Jens Axboe6b063142019-01-10 22:13:58 -07007661
7662 return 0;
7663}
7664
7665/*
7666 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7667 * causes regular reference counting to break down. We rely on the UNIX
7668 * garbage collection to take care of this problem for us.
7669 */
7670static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7671{
7672 unsigned left, total;
7673 int ret = 0;
7674
7675 total = 0;
7676 left = ctx->nr_user_files;
7677 while (left) {
7678 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007679
7680 ret = __io_sqe_files_scm(ctx, this_files, total);
7681 if (ret)
7682 break;
7683 left -= this_files;
7684 total += this_files;
7685 }
7686
7687 if (!ret)
7688 return 0;
7689
7690 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007691 struct file *file = io_file_from_index(ctx, total);
7692
7693 if (file)
7694 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007695 total++;
7696 }
7697
7698 return ret;
7699}
7700#else
7701static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7702{
7703 return 0;
7704}
7705#endif
7706
Pavel Begunkov47e90392021-04-01 15:43:56 +01007707static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007708{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007709 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007710#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007711 struct sock *sock = ctx->ring_sock->sk;
7712 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7713 struct sk_buff *skb;
7714 int i;
7715
7716 __skb_queue_head_init(&list);
7717
7718 /*
7719 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7720 * remove this entry and rearrange the file array.
7721 */
7722 skb = skb_dequeue(head);
7723 while (skb) {
7724 struct scm_fp_list *fp;
7725
7726 fp = UNIXCB(skb).fp;
7727 for (i = 0; i < fp->count; i++) {
7728 int left;
7729
7730 if (fp->fp[i] != file)
7731 continue;
7732
7733 unix_notinflight(fp->user, fp->fp[i]);
7734 left = fp->count - 1 - i;
7735 if (left) {
7736 memmove(&fp->fp[i], &fp->fp[i + 1],
7737 left * sizeof(struct file *));
7738 }
7739 fp->count--;
7740 if (!fp->count) {
7741 kfree_skb(skb);
7742 skb = NULL;
7743 } else {
7744 __skb_queue_tail(&list, skb);
7745 }
7746 fput(file);
7747 file = NULL;
7748 break;
7749 }
7750
7751 if (!file)
7752 break;
7753
7754 __skb_queue_tail(&list, skb);
7755
7756 skb = skb_dequeue(head);
7757 }
7758
7759 if (skb_peek(&list)) {
7760 spin_lock_irq(&head->lock);
7761 while ((skb = __skb_dequeue(&list)) != NULL)
7762 __skb_queue_tail(head, skb);
7763 spin_unlock_irq(&head->lock);
7764 }
7765#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007766 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007767#endif
7768}
7769
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007770static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007771{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007772 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007773 struct io_ring_ctx *ctx = rsrc_data->ctx;
7774 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007775
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007776 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7777 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007778
7779 if (prsrc->tag) {
7780 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007781
7782 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007783 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007784 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01007785 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007786 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007787 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007788 io_cqring_ev_posted(ctx);
7789 io_ring_submit_unlock(ctx, lock_ring);
7790 }
7791
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007792 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007793 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007794 }
7795
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007796 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01007797 if (atomic_dec_and_test(&rsrc_data->refs))
7798 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007799}
7800
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007801static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007802{
7803 struct io_ring_ctx *ctx;
7804 struct llist_node *node;
7805
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007806 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7807 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007808
7809 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007810 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007811 struct llist_node *next = node->next;
7812
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007813 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007814 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007815 node = next;
7816 }
7817}
7818
Jens Axboe05f3fb32019-12-09 11:22:50 -07007819static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01007820 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007821{
7822 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007823 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007824 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007825 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007826
7827 if (ctx->file_data)
7828 return -EBUSY;
7829 if (!nr_args)
7830 return -EINVAL;
7831 if (nr_args > IORING_MAX_FIXED_FILES)
7832 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01007833 if (nr_args > rlimit(RLIMIT_NOFILE))
7834 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007835 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007836 if (ret)
7837 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007838 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
7839 &ctx->file_data);
7840 if (ret)
7841 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007842
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007843 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007844 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007845 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007846
Jens Axboe05f3fb32019-12-09 11:22:50 -07007847 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01007848 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007849 ret = -EFAULT;
7850 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007851 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007852 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01007853 if (fd == -1) {
7854 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007855 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01007856 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007857 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007858 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007859
Jens Axboe05f3fb32019-12-09 11:22:50 -07007860 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007861 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007862 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007863 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007864
7865 /*
7866 * Don't allow io_uring instances to be registered. If UNIX
7867 * isn't enabled, then this causes a reference cycle and this
7868 * instance can never get freed. If UNIX is enabled we'll
7869 * handle it just fine, but there's still no point in allowing
7870 * a ring fd as it doesn't support regular read/write anyway.
7871 */
7872 if (file->f_op == &io_uring_fops) {
7873 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007874 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007875 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007876 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007877 }
7878
Jens Axboe05f3fb32019-12-09 11:22:50 -07007879 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007880 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01007881 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007882 return ret;
7883 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007884
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007885 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007886 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007887out_fput:
7888 for (i = 0; i < ctx->nr_user_files; i++) {
7889 file = io_file_from_index(ctx, i);
7890 if (file)
7891 fput(file);
7892 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007893 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007894 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007895out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007896 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007897 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007898 return ret;
7899}
7900
Jens Axboec3a31e62019-10-03 13:59:56 -06007901static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7902 int index)
7903{
7904#if defined(CONFIG_UNIX)
7905 struct sock *sock = ctx->ring_sock->sk;
7906 struct sk_buff_head *head = &sock->sk_receive_queue;
7907 struct sk_buff *skb;
7908
7909 /*
7910 * See if we can merge this file into an existing skb SCM_RIGHTS
7911 * file set. If there's no room, fall back to allocating a new skb
7912 * and filling it in.
7913 */
7914 spin_lock_irq(&head->lock);
7915 skb = skb_peek(head);
7916 if (skb) {
7917 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7918
7919 if (fpl->count < SCM_MAX_FD) {
7920 __skb_unlink(skb, head);
7921 spin_unlock_irq(&head->lock);
7922 fpl->fp[fpl->count] = get_file(file);
7923 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7924 fpl->count++;
7925 spin_lock_irq(&head->lock);
7926 __skb_queue_head(head, skb);
7927 } else {
7928 skb = NULL;
7929 }
7930 }
7931 spin_unlock_irq(&head->lock);
7932
7933 if (skb) {
7934 fput(file);
7935 return 0;
7936 }
7937
7938 return __io_sqe_files_scm(ctx, 1, index);
7939#else
7940 return 0;
7941#endif
7942}
7943
Pavel Begunkovb9445592021-08-25 12:25:45 +01007944static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
7945 unsigned int issue_flags, u32 slot_index)
7946{
7947 struct io_ring_ctx *ctx = req->ctx;
7948 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
7949 struct io_fixed_file *file_slot;
7950 int ret = -EBADF;
7951
7952 io_ring_submit_lock(ctx, !force_nonblock);
7953 if (file->f_op == &io_uring_fops)
7954 goto err;
7955 ret = -ENXIO;
7956 if (!ctx->file_data)
7957 goto err;
7958 ret = -EINVAL;
7959 if (slot_index >= ctx->nr_user_files)
7960 goto err;
7961
7962 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
7963 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
7964 ret = -EBADF;
7965 if (file_slot->file_ptr)
7966 goto err;
7967
7968 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
7969 io_fixed_file_set(file_slot, file);
7970 ret = io_sqe_file_register(ctx, file, slot_index);
7971 if (ret) {
7972 file_slot->file_ptr = 0;
7973 goto err;
7974 }
7975
7976 ret = 0;
7977err:
7978 io_ring_submit_unlock(ctx, !force_nonblock);
7979 if (ret)
7980 fput(file);
7981 return ret;
7982}
7983
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007984static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
Pavel Begunkove7c78372021-04-01 15:43:45 +01007985 struct io_rsrc_node *node, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007986{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007987 struct io_rsrc_put *prsrc;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007988
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007989 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7990 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007991 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007992
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007993 prsrc->tag = *io_get_tag_slot(data, idx);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007994 prsrc->rsrc = rsrc;
Pavel Begunkove7c78372021-04-01 15:43:45 +01007995 list_add(&prsrc->list, &node->rsrc_list);
Hillf Dantona5318d32020-03-23 17:47:15 +08007996 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007997}
7998
7999static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008000 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008001 unsigned nr_args)
8002{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008003 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008004 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008005 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008006 struct io_fixed_file *file_slot;
8007 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008008 int fd, i, err = 0;
8009 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008010 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008011
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008012 if (!ctx->file_data)
8013 return -ENXIO;
8014 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008015 return -EINVAL;
8016
Pavel Begunkov67973b92021-01-26 13:51:09 +00008017 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008018 u64 tag = 0;
8019
8020 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8021 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008022 err = -EFAULT;
8023 break;
8024 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008025 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8026 err = -EINVAL;
8027 break;
8028 }
noah4e0377a2021-01-26 15:23:28 -05008029 if (fd == IORING_REGISTER_FILES_SKIP)
8030 continue;
8031
Pavel Begunkov67973b92021-01-26 13:51:09 +00008032 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008033 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008034
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008035 if (file_slot->file_ptr) {
8036 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008037 err = io_queue_rsrc_removal(data, up->offset + done,
8038 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008039 if (err)
8040 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008041 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008042 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008043 }
8044 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008045 file = fget(fd);
8046 if (!file) {
8047 err = -EBADF;
8048 break;
8049 }
8050 /*
8051 * Don't allow io_uring instances to be registered. If
8052 * UNIX isn't enabled, then this causes a reference
8053 * cycle and this instance can never get freed. If UNIX
8054 * is enabled we'll handle it just fine, but there's
8055 * still no point in allowing a ring fd as it doesn't
8056 * support regular read/write anyway.
8057 */
8058 if (file->f_op == &io_uring_fops) {
8059 fput(file);
8060 err = -EBADF;
8061 break;
8062 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008063 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008064 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008065 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008066 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008067 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008068 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008069 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008070 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008071 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008072 }
8073
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008074 if (needs_switch)
8075 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008076 return done ? done : err;
8077}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008078
Jens Axboe685fe7f2021-03-08 09:37:51 -07008079static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8080 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008081{
Jens Axboee9418942021-02-19 12:33:30 -07008082 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008083 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008084 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008085
Yang Yingliang362a9e62021-07-20 16:38:05 +08008086 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008087 hash = ctx->hash_map;
8088 if (!hash) {
8089 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008090 if (!hash) {
8091 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008092 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008093 }
Jens Axboee9418942021-02-19 12:33:30 -07008094 refcount_set(&hash->refs, 1);
8095 init_waitqueue_head(&hash->wait);
8096 ctx->hash_map = hash;
8097 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008098 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008099
8100 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008101 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008102 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008103 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008104
Jens Axboed25e3a32021-02-16 11:41:41 -07008105 /* Do QD, or 4 * CPUS, whatever is smallest */
8106 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008107
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008108 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008109}
8110
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008111static int io_uring_alloc_task_context(struct task_struct *task,
8112 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008113{
8114 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008115 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008116
Pavel Begunkov09899b12021-06-14 02:36:22 +01008117 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008118 if (unlikely(!tctx))
8119 return -ENOMEM;
8120
Jens Axboed8a6df12020-10-15 16:24:45 -06008121 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8122 if (unlikely(ret)) {
8123 kfree(tctx);
8124 return ret;
8125 }
8126
Jens Axboe685fe7f2021-03-08 09:37:51 -07008127 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008128 if (IS_ERR(tctx->io_wq)) {
8129 ret = PTR_ERR(tctx->io_wq);
8130 percpu_counter_destroy(&tctx->inflight);
8131 kfree(tctx);
8132 return ret;
8133 }
8134
Jens Axboe0f212202020-09-13 13:09:39 -06008135 xa_init(&tctx->xa);
8136 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008137 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008138 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008139 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008140 spin_lock_init(&tctx->task_lock);
8141 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008142 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008143 return 0;
8144}
8145
8146void __io_uring_free(struct task_struct *tsk)
8147{
8148 struct io_uring_task *tctx = tsk->io_uring;
8149
8150 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008151 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008152 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008153
Jens Axboed8a6df12020-10-15 16:24:45 -06008154 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008155 kfree(tctx);
8156 tsk->io_uring = NULL;
8157}
8158
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008159static int io_sq_offload_create(struct io_ring_ctx *ctx,
8160 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008161{
8162 int ret;
8163
Jens Axboed25e3a32021-02-16 11:41:41 -07008164 /* Retain compatibility with failing for an invalid attach attempt */
8165 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8166 IORING_SETUP_ATTACH_WQ) {
8167 struct fd f;
8168
8169 f = fdget(p->wq_fd);
8170 if (!f.file)
8171 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008172 if (f.file->f_op != &io_uring_fops) {
8173 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008174 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008175 }
8176 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008177 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008178 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008179 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008180 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008181 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008182
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008183 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008184 if (IS_ERR(sqd)) {
8185 ret = PTR_ERR(sqd);
8186 goto err;
8187 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008188
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008189 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008190 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008191 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8192 if (!ctx->sq_thread_idle)
8193 ctx->sq_thread_idle = HZ;
8194
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008195 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008196 list_add(&ctx->sqd_list, &sqd->ctx_list);
8197 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008198 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008199 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008200 io_sq_thread_unpark(sqd);
8201
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008202 if (ret < 0)
8203 goto err;
8204 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008205 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008206
Jens Axboe6c271ce2019-01-10 11:22:30 -07008207 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008208 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008209
Jens Axboe917257d2019-04-13 09:28:55 -06008210 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008211 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008212 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008213 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008214 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008215 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008216 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008217
8218 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008219 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008220 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8221 if (IS_ERR(tsk)) {
8222 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008223 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008224 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008225
Jens Axboe46fe18b2021-03-04 12:39:36 -07008226 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008227 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008228 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008229 if (ret)
8230 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008231 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8232 /* Can't have SQ_AFF without SQPOLL */
8233 ret = -EINVAL;
8234 goto err;
8235 }
8236
Jens Axboe2b188cc2019-01-07 10:46:33 -07008237 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008238err_sqpoll:
8239 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008240err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008241 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008242 return ret;
8243}
8244
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008245static inline void __io_unaccount_mem(struct user_struct *user,
8246 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008247{
8248 atomic_long_sub(nr_pages, &user->locked_vm);
8249}
8250
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008251static inline int __io_account_mem(struct user_struct *user,
8252 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008253{
8254 unsigned long page_limit, cur_pages, new_pages;
8255
8256 /* Don't allow more pages than we can safely lock */
8257 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8258
8259 do {
8260 cur_pages = atomic_long_read(&user->locked_vm);
8261 new_pages = cur_pages + nr_pages;
8262 if (new_pages > page_limit)
8263 return -ENOMEM;
8264 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8265 new_pages) != cur_pages);
8266
8267 return 0;
8268}
8269
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008270static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008271{
Jens Axboe62e398b2021-02-21 16:19:37 -07008272 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008273 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008274
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008275 if (ctx->mm_account)
8276 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008277}
8278
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008279static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008280{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008281 int ret;
8282
Jens Axboe62e398b2021-02-21 16:19:37 -07008283 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008284 ret = __io_account_mem(ctx->user, nr_pages);
8285 if (ret)
8286 return ret;
8287 }
8288
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008289 if (ctx->mm_account)
8290 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008291
8292 return 0;
8293}
8294
Jens Axboe2b188cc2019-01-07 10:46:33 -07008295static void io_mem_free(void *ptr)
8296{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008297 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008298
Mark Rutland52e04ef2019-04-30 17:30:21 +01008299 if (!ptr)
8300 return;
8301
8302 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008303 if (put_page_testzero(page))
8304 free_compound_page(page);
8305}
8306
8307static void *io_mem_alloc(size_t size)
8308{
8309 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008310 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008311
8312 return (void *) __get_free_pages(gfp_flags, get_order(size));
8313}
8314
Hristo Venev75b28af2019-08-26 17:23:46 +00008315static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8316 size_t *sq_offset)
8317{
8318 struct io_rings *rings;
8319 size_t off, sq_array_size;
8320
8321 off = struct_size(rings, cqes, cq_entries);
8322 if (off == SIZE_MAX)
8323 return SIZE_MAX;
8324
8325#ifdef CONFIG_SMP
8326 off = ALIGN(off, SMP_CACHE_BYTES);
8327 if (off == 0)
8328 return SIZE_MAX;
8329#endif
8330
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008331 if (sq_offset)
8332 *sq_offset = off;
8333
Hristo Venev75b28af2019-08-26 17:23:46 +00008334 sq_array_size = array_size(sizeof(u32), sq_entries);
8335 if (sq_array_size == SIZE_MAX)
8336 return SIZE_MAX;
8337
8338 if (check_add_overflow(off, sq_array_size, &off))
8339 return SIZE_MAX;
8340
Hristo Venev75b28af2019-08-26 17:23:46 +00008341 return off;
8342}
8343
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008344static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008345{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008346 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008347 unsigned int i;
8348
Pavel Begunkov62248432021-04-28 13:11:29 +01008349 if (imu != ctx->dummy_ubuf) {
8350 for (i = 0; i < imu->nr_bvecs; i++)
8351 unpin_user_page(imu->bvec[i].bv_page);
8352 if (imu->acct_pages)
8353 io_unaccount_mem(ctx, imu->acct_pages);
8354 kvfree(imu);
8355 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008356 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008357}
8358
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008359static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8360{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008361 io_buffer_unmap(ctx, &prsrc->buf);
8362 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008363}
8364
8365static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008366{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008367 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008368
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008369 for (i = 0; i < ctx->nr_user_bufs; i++)
8370 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008371 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008372 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008373 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008374 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008375 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008376}
8377
Jens Axboeedafcce2019-01-09 09:16:05 -07008378static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8379{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008380 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008381
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008382 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008383 return -ENXIO;
8384
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008385 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8386 if (!ret)
8387 __io_sqe_buffers_unregister(ctx);
8388 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008389}
8390
8391static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8392 void __user *arg, unsigned index)
8393{
8394 struct iovec __user *src;
8395
8396#ifdef CONFIG_COMPAT
8397 if (ctx->compat) {
8398 struct compat_iovec __user *ciovs;
8399 struct compat_iovec ciov;
8400
8401 ciovs = (struct compat_iovec __user *) arg;
8402 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8403 return -EFAULT;
8404
Jens Axboed55e5f52019-12-11 16:12:15 -07008405 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008406 dst->iov_len = ciov.iov_len;
8407 return 0;
8408 }
8409#endif
8410 src = (struct iovec __user *) arg;
8411 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8412 return -EFAULT;
8413 return 0;
8414}
8415
Jens Axboede293932020-09-17 16:19:16 -06008416/*
8417 * Not super efficient, but this is just a registration time. And we do cache
8418 * the last compound head, so generally we'll only do a full search if we don't
8419 * match that one.
8420 *
8421 * We check if the given compound head page has already been accounted, to
8422 * avoid double accounting it. This allows us to account the full size of the
8423 * page, not just the constituent pages of a huge page.
8424 */
8425static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8426 int nr_pages, struct page *hpage)
8427{
8428 int i, j;
8429
8430 /* check current page array */
8431 for (i = 0; i < nr_pages; i++) {
8432 if (!PageCompound(pages[i]))
8433 continue;
8434 if (compound_head(pages[i]) == hpage)
8435 return true;
8436 }
8437
8438 /* check previously registered pages */
8439 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008440 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008441
8442 for (j = 0; j < imu->nr_bvecs; j++) {
8443 if (!PageCompound(imu->bvec[j].bv_page))
8444 continue;
8445 if (compound_head(imu->bvec[j].bv_page) == hpage)
8446 return true;
8447 }
8448 }
8449
8450 return false;
8451}
8452
8453static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8454 int nr_pages, struct io_mapped_ubuf *imu,
8455 struct page **last_hpage)
8456{
8457 int i, ret;
8458
Pavel Begunkov216e5832021-05-29 12:01:02 +01008459 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008460 for (i = 0; i < nr_pages; i++) {
8461 if (!PageCompound(pages[i])) {
8462 imu->acct_pages++;
8463 } else {
8464 struct page *hpage;
8465
8466 hpage = compound_head(pages[i]);
8467 if (hpage == *last_hpage)
8468 continue;
8469 *last_hpage = hpage;
8470 if (headpage_already_acct(ctx, pages, i, hpage))
8471 continue;
8472 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8473 }
8474 }
8475
8476 if (!imu->acct_pages)
8477 return 0;
8478
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008479 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008480 if (ret)
8481 imu->acct_pages = 0;
8482 return ret;
8483}
8484
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008485static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008486 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008487 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008488{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008489 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008490 struct vm_area_struct **vmas = NULL;
8491 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008492 unsigned long off, start, end, ubuf;
8493 size_t size;
8494 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008495
Pavel Begunkov62248432021-04-28 13:11:29 +01008496 if (!iov->iov_base) {
8497 *pimu = ctx->dummy_ubuf;
8498 return 0;
8499 }
8500
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008501 ubuf = (unsigned long) iov->iov_base;
8502 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8503 start = ubuf >> PAGE_SHIFT;
8504 nr_pages = end - start;
8505
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008506 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008507 ret = -ENOMEM;
8508
8509 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8510 if (!pages)
8511 goto done;
8512
8513 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8514 GFP_KERNEL);
8515 if (!vmas)
8516 goto done;
8517
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008518 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008519 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008520 goto done;
8521
8522 ret = 0;
8523 mmap_read_lock(current->mm);
8524 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8525 pages, vmas);
8526 if (pret == nr_pages) {
8527 /* don't support file backed memory */
8528 for (i = 0; i < nr_pages; i++) {
8529 struct vm_area_struct *vma = vmas[i];
8530
Pavel Begunkov40dad762021-06-09 15:26:54 +01008531 if (vma_is_shmem(vma))
8532 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008533 if (vma->vm_file &&
8534 !is_file_hugepages(vma->vm_file)) {
8535 ret = -EOPNOTSUPP;
8536 break;
8537 }
8538 }
8539 } else {
8540 ret = pret < 0 ? pret : -EFAULT;
8541 }
8542 mmap_read_unlock(current->mm);
8543 if (ret) {
8544 /*
8545 * if we did partial map, or found file backed vmas,
8546 * release any pages we did get
8547 */
8548 if (pret > 0)
8549 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008550 goto done;
8551 }
8552
8553 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8554 if (ret) {
8555 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008556 goto done;
8557 }
8558
8559 off = ubuf & ~PAGE_MASK;
8560 size = iov->iov_len;
8561 for (i = 0; i < nr_pages; i++) {
8562 size_t vec_len;
8563
8564 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8565 imu->bvec[i].bv_page = pages[i];
8566 imu->bvec[i].bv_len = vec_len;
8567 imu->bvec[i].bv_offset = off;
8568 off = 0;
8569 size -= vec_len;
8570 }
8571 /* store original address for later verification */
8572 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008573 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008574 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008575 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008576 ret = 0;
8577done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008578 if (ret)
8579 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008580 kvfree(pages);
8581 kvfree(vmas);
8582 return ret;
8583}
8584
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008585static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008586{
Pavel Begunkov87094462021-04-11 01:46:36 +01008587 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8588 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008589}
8590
8591static int io_buffer_validate(struct iovec *iov)
8592{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008593 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8594
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008595 /*
8596 * Don't impose further limits on the size and buffer
8597 * constraints here, we'll -EINVAL later when IO is
8598 * submitted if they are wrong.
8599 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008600 if (!iov->iov_base)
8601 return iov->iov_len ? -EFAULT : 0;
8602 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008603 return -EFAULT;
8604
8605 /* arbitrary limit, but we need something */
8606 if (iov->iov_len > SZ_1G)
8607 return -EFAULT;
8608
Pavel Begunkov50e96982021-03-24 22:59:01 +00008609 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8610 return -EOVERFLOW;
8611
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008612 return 0;
8613}
8614
8615static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008616 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008617{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008618 struct page *last_hpage = NULL;
8619 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008620 int i, ret;
8621 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008622
Pavel Begunkov87094462021-04-11 01:46:36 +01008623 if (ctx->user_bufs)
8624 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01008625 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01008626 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008627 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008628 if (ret)
8629 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008630 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
8631 if (ret)
8632 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008633 ret = io_buffers_map_alloc(ctx, nr_args);
8634 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08008635 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008636 return ret;
8637 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008638
Pavel Begunkov87094462021-04-11 01:46:36 +01008639 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07008640 ret = io_copy_iov(ctx, &iov, arg, i);
8641 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008642 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008643 ret = io_buffer_validate(&iov);
8644 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008645 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008646 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008647 ret = -EINVAL;
8648 break;
8649 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008650
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008651 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
8652 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008653 if (ret)
8654 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008655 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008656
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008657 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008658
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008659 ctx->buf_data = data;
8660 if (ret)
8661 __io_sqe_buffers_unregister(ctx);
8662 else
8663 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07008664 return ret;
8665}
8666
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008667static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
8668 struct io_uring_rsrc_update2 *up,
8669 unsigned int nr_args)
8670{
8671 u64 __user *tags = u64_to_user_ptr(up->tags);
8672 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008673 struct page *last_hpage = NULL;
8674 bool needs_switch = false;
8675 __u32 done;
8676 int i, err;
8677
8678 if (!ctx->buf_data)
8679 return -ENXIO;
8680 if (up->offset + nr_args > ctx->nr_user_bufs)
8681 return -EINVAL;
8682
8683 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008684 struct io_mapped_ubuf *imu;
8685 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008686 u64 tag = 0;
8687
8688 err = io_copy_iov(ctx, &iov, iovs, done);
8689 if (err)
8690 break;
8691 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
8692 err = -EFAULT;
8693 break;
8694 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008695 err = io_buffer_validate(&iov);
8696 if (err)
8697 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008698 if (!iov.iov_base && tag) {
8699 err = -EINVAL;
8700 break;
8701 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008702 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
8703 if (err)
8704 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008705
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008706 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01008707 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008708 err = io_queue_rsrc_removal(ctx->buf_data, offset,
8709 ctx->rsrc_node, ctx->user_bufs[i]);
8710 if (unlikely(err)) {
8711 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008712 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008713 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008714 ctx->user_bufs[i] = NULL;
8715 needs_switch = true;
8716 }
8717
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008718 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008719 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008720 }
8721
8722 if (needs_switch)
8723 io_rsrc_node_switch(ctx, ctx->buf_data);
8724 return done ? done : err;
8725}
8726
Jens Axboe9b402842019-04-11 11:45:41 -06008727static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8728{
8729 __s32 __user *fds = arg;
8730 int fd;
8731
8732 if (ctx->cq_ev_fd)
8733 return -EBUSY;
8734
8735 if (copy_from_user(&fd, fds, sizeof(*fds)))
8736 return -EFAULT;
8737
8738 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8739 if (IS_ERR(ctx->cq_ev_fd)) {
8740 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01008741
Jens Axboe9b402842019-04-11 11:45:41 -06008742 ctx->cq_ev_fd = NULL;
8743 return ret;
8744 }
8745
8746 return 0;
8747}
8748
8749static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8750{
8751 if (ctx->cq_ev_fd) {
8752 eventfd_ctx_put(ctx->cq_ev_fd);
8753 ctx->cq_ev_fd = NULL;
8754 return 0;
8755 }
8756
8757 return -ENXIO;
8758}
8759
Jens Axboe5a2e7452020-02-23 16:23:11 -07008760static void io_destroy_buffers(struct io_ring_ctx *ctx)
8761{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07008762 struct io_buffer *buf;
8763 unsigned long index;
8764
8765 xa_for_each(&ctx->io_buffers, index, buf)
8766 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008767}
8768
Pavel Begunkov72558342021-08-09 20:18:09 +01008769static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008770{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008771 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008772
Pavel Begunkovbb943b82021-08-09 20:18:10 +01008773 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
8774 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00008775 kmem_cache_free(req_cachep, req);
8776 }
8777}
8778
Jens Axboe4010fec2021-02-27 15:04:18 -07008779static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008780{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008781 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008782
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008783 mutex_lock(&ctx->uring_lock);
8784
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008785 if (state->free_reqs) {
8786 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
8787 state->free_reqs = 0;
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008788 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008789
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008790 io_flush_cached_locked_reqs(ctx, state);
8791 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008792 mutex_unlock(&ctx->uring_lock);
8793}
8794
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008795static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008796{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008797 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008798 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008799}
8800
Jens Axboe2b188cc2019-01-07 10:46:33 -07008801static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8802{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008803 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008804
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008805 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008806 mmdrop(ctx->mm_account);
8807 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008808 }
Jens Axboedef596e2019-01-09 08:59:42 -07008809
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008810 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
8811 io_wait_rsrc_data(ctx->buf_data);
8812 io_wait_rsrc_data(ctx->file_data);
8813
Hao Xu8bad28d2021-02-19 17:19:36 +08008814 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008815 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008816 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008817 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01008818 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01008819 if (ctx->rings)
8820 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08008821 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008822 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008823 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01008824 if (ctx->sq_creds)
8825 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07008826
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008827 /* there are no registered resources left, nobody uses it */
8828 if (ctx->rsrc_node)
8829 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00008830 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008831 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008832 flush_delayed_work(&ctx->rsrc_put_work);
8833
8834 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
8835 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008836
8837#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008838 if (ctx->ring_sock) {
8839 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008840 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008841 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008842#endif
8843
Hristo Venev75b28af2019-08-26 17:23:46 +00008844 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008845 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008846
8847 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008848 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008849 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008850 if (ctx->hash_map)
8851 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008852 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01008853 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008854 kfree(ctx);
8855}
8856
8857static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8858{
8859 struct io_ring_ctx *ctx = file->private_data;
8860 __poll_t mask = 0;
8861
Pavel Begunkov311997b2021-06-14 23:37:28 +01008862 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008863 /*
8864 * synchronizes with barrier from wq_has_sleeper call in
8865 * io_commit_cqring
8866 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008867 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008868 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008869 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008870
8871 /*
8872 * Don't flush cqring overflow list here, just do a simple check.
8873 * Otherwise there could possible be ABBA deadlock:
8874 * CPU0 CPU1
8875 * ---- ----
8876 * lock(&ctx->uring_lock);
8877 * lock(&ep->mtx);
8878 * lock(&ctx->uring_lock);
8879 * lock(&ep->mtx);
8880 *
8881 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8882 * pushs them to do the flush.
8883 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01008884 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008885 mask |= EPOLLIN | EPOLLRDNORM;
8886
8887 return mask;
8888}
8889
8890static int io_uring_fasync(int fd, struct file *file, int on)
8891{
8892 struct io_ring_ctx *ctx = file->private_data;
8893
8894 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8895}
8896
Yejune Deng0bead8c2020-12-24 11:02:20 +08008897static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008898{
Jens Axboe4379bf82021-02-15 13:40:22 -07008899 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008900
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008901 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07008902 if (creds) {
8903 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008904 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008905 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008906
8907 return -EINVAL;
8908}
8909
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008910struct io_tctx_exit {
8911 struct callback_head task_work;
8912 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00008913 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008914};
8915
8916static void io_tctx_exit_cb(struct callback_head *cb)
8917{
8918 struct io_uring_task *tctx = current->io_uring;
8919 struct io_tctx_exit *work;
8920
8921 work = container_of(cb, struct io_tctx_exit, task_work);
8922 /*
8923 * When @in_idle, we're in cancellation and it's racy to remove the
8924 * node. It'll be removed by the end of cancellation, just ignore it.
8925 */
8926 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01008927 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008928 complete(&work->completion);
8929}
8930
Pavel Begunkov28090c12021-04-25 23:34:45 +01008931static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8932{
8933 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8934
8935 return req->ctx == data;
8936}
8937
Jens Axboe85faa7b2020-04-09 18:14:00 -06008938static void io_ring_exit_work(struct work_struct *work)
8939{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008940 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008941 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01008942 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008943 struct io_tctx_exit exit;
8944 struct io_tctx_node *node;
8945 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06008946
Jens Axboe56952e92020-06-17 15:00:04 -06008947 /*
8948 * If we're doing polled IO and end up having requests being
8949 * submitted async (out-of-line), then completions can come in while
8950 * we're waiting for refs to drop. We need to reap these manually,
8951 * as nobody else will be looking for them.
8952 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008953 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008954 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01008955 if (ctx->sq_data) {
8956 struct io_sq_data *sqd = ctx->sq_data;
8957 struct task_struct *tsk;
8958
8959 io_sq_thread_park(sqd);
8960 tsk = sqd->thread;
8961 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
8962 io_wq_cancel_cb(tsk->io_uring->io_wq,
8963 io_cancel_ctx_cb, ctx, true);
8964 io_sq_thread_unpark(sqd);
8965 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008966
Pavel Begunkov58d3be22021-08-09 13:04:17 +01008967 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
8968 /* there is little hope left, don't run it too often */
8969 interval = HZ * 60;
8970 }
8971 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008972
Pavel Begunkov7f006512021-04-14 13:38:34 +01008973 init_completion(&exit.completion);
8974 init_task_work(&exit.task_work, io_tctx_exit_cb);
8975 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01008976 /*
8977 * Some may use context even when all refs and requests have been put,
8978 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01008979 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01008980 * this lock/unlock section also waits them to finish.
8981 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008982 mutex_lock(&ctx->uring_lock);
8983 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008984 WARN_ON_ONCE(time_after(jiffies, timeout));
8985
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008986 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
8987 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01008988 /* don't spin on a single task if cancellation failed */
8989 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008990 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
8991 if (WARN_ON_ONCE(ret))
8992 continue;
8993 wake_up_process(node->task);
8994
8995 mutex_unlock(&ctx->uring_lock);
8996 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008997 mutex_lock(&ctx->uring_lock);
8998 }
8999 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009000 spin_lock(&ctx->completion_lock);
9001 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009002
Jens Axboe85faa7b2020-04-09 18:14:00 -06009003 io_ring_ctx_free(ctx);
9004}
9005
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009006/* Returns true if we found and killed one or more timeouts */
9007static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009008 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009009{
9010 struct io_kiocb *req, *tmp;
9011 int canceled = 0;
9012
Jens Axboe79ebeae2021-08-10 15:18:27 -06009013 spin_lock(&ctx->completion_lock);
9014 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009015 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009016 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009017 io_kill_timeout(req, -ECANCELED);
9018 canceled++;
9019 }
9020 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009021 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009022 if (canceled != 0)
9023 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009024 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009025 if (canceled != 0)
9026 io_cqring_ev_posted(ctx);
9027 return canceled != 0;
9028}
9029
Jens Axboe2b188cc2019-01-07 10:46:33 -07009030static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9031{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009032 unsigned long index;
9033 struct creds *creds;
9034
Jens Axboe2b188cc2019-01-07 10:46:33 -07009035 mutex_lock(&ctx->uring_lock);
9036 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009037 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009038 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009039 xa_for_each(&ctx->personalities, index, creds)
9040 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009041 mutex_unlock(&ctx->uring_lock);
9042
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009043 io_kill_timeouts(ctx, NULL, true);
9044 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009045
Jens Axboe15dff282019-11-13 09:09:23 -07009046 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009047 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009048
Jens Axboe85faa7b2020-04-09 18:14:00 -06009049 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009050 /*
9051 * Use system_unbound_wq to avoid spawning tons of event kworkers
9052 * if we're exiting a ton of rings at the same time. It just adds
9053 * noise and overhead, there's no discernable change in runtime
9054 * over using system_wq.
9055 */
9056 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009057}
9058
9059static int io_uring_release(struct inode *inode, struct file *file)
9060{
9061 struct io_ring_ctx *ctx = file->private_data;
9062
9063 file->private_data = NULL;
9064 io_ring_ctx_wait_and_kill(ctx);
9065 return 0;
9066}
9067
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009068struct io_task_cancel {
9069 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009070 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009071};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009072
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009073static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009074{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009075 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009076 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009077 bool ret;
9078
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009079 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009080 struct io_ring_ctx *ctx = req->ctx;
9081
9082 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009083 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009084 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009085 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009086 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009087 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009088 }
9089 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009090}
9091
Pavel Begunkove1915f72021-03-11 23:29:35 +00009092static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009093 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009094{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009095 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009096 LIST_HEAD(list);
9097
Jens Axboe79ebeae2021-08-10 15:18:27 -06009098 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009099 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009100 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009101 list_cut_position(&list, &ctx->defer_list, &de->list);
9102 break;
9103 }
9104 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009105 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009106 if (list_empty(&list))
9107 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009108
9109 while (!list_empty(&list)) {
9110 de = list_first_entry(&list, struct io_defer_entry, list);
9111 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009112 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009113 kfree(de);
9114 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009115 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009116}
9117
Pavel Begunkov1b007642021-03-06 11:02:17 +00009118static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9119{
9120 struct io_tctx_node *node;
9121 enum io_wq_cancel cret;
9122 bool ret = false;
9123
9124 mutex_lock(&ctx->uring_lock);
9125 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9126 struct io_uring_task *tctx = node->task->io_uring;
9127
9128 /*
9129 * io_wq will stay alive while we hold uring_lock, because it's
9130 * killed after ctx nodes, which requires to take the lock.
9131 */
9132 if (!tctx || !tctx->io_wq)
9133 continue;
9134 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9135 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9136 }
9137 mutex_unlock(&ctx->uring_lock);
9138
9139 return ret;
9140}
9141
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009142static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9143 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009144 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009145{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009146 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009147 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009148
9149 while (1) {
9150 enum io_wq_cancel cret;
9151 bool ret = false;
9152
Pavel Begunkov1b007642021-03-06 11:02:17 +00009153 if (!task) {
9154 ret |= io_uring_try_cancel_iowq(ctx);
9155 } else if (tctx && tctx->io_wq) {
9156 /*
9157 * Cancels requests of all rings, not only @ctx, but
9158 * it's fine as the task is in exit/exec.
9159 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009160 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009161 &cancel, true);
9162 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9163 }
9164
9165 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009166 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009167 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009168 while (!list_empty_careful(&ctx->iopoll_list)) {
9169 io_iopoll_try_reap_events(ctx);
9170 ret = true;
9171 }
9172 }
9173
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009174 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9175 ret |= io_poll_remove_all(ctx, task, cancel_all);
9176 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009177 if (task)
9178 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009179 if (!ret)
9180 break;
9181 cond_resched();
9182 }
9183}
9184
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009185static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009186{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009187 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009188 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009189 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009190
9191 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009192 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009193 if (unlikely(ret))
9194 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009195 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009196 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009197 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9198 node = kmalloc(sizeof(*node), GFP_KERNEL);
9199 if (!node)
9200 return -ENOMEM;
9201 node->ctx = ctx;
9202 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009203
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009204 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9205 node, GFP_KERNEL));
9206 if (ret) {
9207 kfree(node);
9208 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009209 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009210
9211 mutex_lock(&ctx->uring_lock);
9212 list_add(&node->ctx_node, &ctx->tctx_list);
9213 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009214 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009215 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009216 return 0;
9217}
9218
9219/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009220 * Note that this task has used io_uring. We use it for cancelation purposes.
9221 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009222static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009223{
9224 struct io_uring_task *tctx = current->io_uring;
9225
9226 if (likely(tctx && tctx->last == ctx))
9227 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009228 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009229}
9230
9231/*
Jens Axboe0f212202020-09-13 13:09:39 -06009232 * Remove this io_uring_file -> task mapping.
9233 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009234static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009235{
9236 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009237 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009238
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009239 if (!tctx)
9240 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009241 node = xa_erase(&tctx->xa, index);
9242 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009243 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009244
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009245 WARN_ON_ONCE(current != node->task);
9246 WARN_ON_ONCE(list_empty(&node->ctx_node));
9247
9248 mutex_lock(&node->ctx->uring_lock);
9249 list_del(&node->ctx_node);
9250 mutex_unlock(&node->ctx->uring_lock);
9251
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009252 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009253 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009254 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009255}
9256
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009257static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009258{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009259 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009260 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009261 unsigned long index;
9262
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009263 xa_for_each(&tctx->xa, index, node)
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009264 io_uring_del_tctx_node(index);
Marco Elverb16ef422021-05-27 11:25:48 +02009265 if (wq) {
9266 /*
9267 * Must be after io_uring_del_task_file() (removes nodes under
9268 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9269 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009270 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009271 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009272 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009273}
9274
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009275static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009276{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009277 if (tracked)
9278 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009279 return percpu_counter_sum(&tctx->inflight);
9280}
9281
Pavel Begunkov09899b12021-06-14 02:36:22 +01009282static void io_uring_drop_tctx_refs(struct task_struct *task)
9283{
9284 struct io_uring_task *tctx = task->io_uring;
9285 unsigned int refs = tctx->cached_refs;
9286
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009287 if (refs) {
9288 tctx->cached_refs = 0;
9289 percpu_counter_sub(&tctx->inflight, refs);
9290 put_task_struct_many(task, refs);
9291 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009292}
9293
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009294/*
9295 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9296 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9297 */
9298static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009299{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009300 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009301 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009302 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009303 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009304
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009305 WARN_ON_ONCE(sqd && sqd->thread != current);
9306
Palash Oswal6d042ff2021-04-27 18:21:49 +05309307 if (!current->io_uring)
9308 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009309 if (tctx->io_wq)
9310 io_wq_exit_start(tctx->io_wq);
9311
Jens Axboefdaf0832020-10-30 09:37:30 -06009312 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009313 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009314 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009315 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009316 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009317 if (!inflight)
9318 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009319
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009320 if (!sqd) {
9321 struct io_tctx_node *node;
9322 unsigned long index;
9323
9324 xa_for_each(&tctx->xa, index, node) {
9325 /* sqpoll task will cancel all its requests */
9326 if (node->ctx->sq_data)
9327 continue;
9328 io_uring_try_cancel_requests(node->ctx, current,
9329 cancel_all);
9330 }
9331 } else {
9332 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9333 io_uring_try_cancel_requests(ctx, current,
9334 cancel_all);
9335 }
9336
9337 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009338 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009339 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009340 * If we've seen completions, retry without waiting. This
9341 * avoids a race where a completion comes in before we did
9342 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009343 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009344 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009345 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009346 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009347 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009348 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009349
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009350 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009351 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009352 /* for exec all current's requests should be gone, kill tctx */
9353 __io_uring_free(current);
9354 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009355}
9356
Hao Xuf552a272021-08-12 12:14:35 +08009357void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009358{
Hao Xuf552a272021-08-12 12:14:35 +08009359 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009360}
9361
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009362static void *io_uring_validate_mmap_request(struct file *file,
9363 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009364{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009365 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009366 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009367 struct page *page;
9368 void *ptr;
9369
9370 switch (offset) {
9371 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009372 case IORING_OFF_CQ_RING:
9373 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009374 break;
9375 case IORING_OFF_SQES:
9376 ptr = ctx->sq_sqes;
9377 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009378 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009379 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009380 }
9381
9382 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009383 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009384 return ERR_PTR(-EINVAL);
9385
9386 return ptr;
9387}
9388
9389#ifdef CONFIG_MMU
9390
9391static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9392{
9393 size_t sz = vma->vm_end - vma->vm_start;
9394 unsigned long pfn;
9395 void *ptr;
9396
9397 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9398 if (IS_ERR(ptr))
9399 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009400
9401 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9402 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9403}
9404
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009405#else /* !CONFIG_MMU */
9406
9407static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9408{
9409 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9410}
9411
9412static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9413{
9414 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9415}
9416
9417static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9418 unsigned long addr, unsigned long len,
9419 unsigned long pgoff, unsigned long flags)
9420{
9421 void *ptr;
9422
9423 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9424 if (IS_ERR(ptr))
9425 return PTR_ERR(ptr);
9426
9427 return (unsigned long) ptr;
9428}
9429
9430#endif /* !CONFIG_MMU */
9431
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009432static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009433{
9434 DEFINE_WAIT(wait);
9435
9436 do {
9437 if (!io_sqring_full(ctx))
9438 break;
Jens Axboe90554202020-09-03 12:12:41 -06009439 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9440
9441 if (!io_sqring_full(ctx))
9442 break;
Jens Axboe90554202020-09-03 12:12:41 -06009443 schedule();
9444 } while (!signal_pending(current));
9445
9446 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009447 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009448}
9449
Hao Xuc73ebb62020-11-03 10:54:37 +08009450static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9451 struct __kernel_timespec __user **ts,
9452 const sigset_t __user **sig)
9453{
9454 struct io_uring_getevents_arg arg;
9455
9456 /*
9457 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9458 * is just a pointer to the sigset_t.
9459 */
9460 if (!(flags & IORING_ENTER_EXT_ARG)) {
9461 *sig = (const sigset_t __user *) argp;
9462 *ts = NULL;
9463 return 0;
9464 }
9465
9466 /*
9467 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9468 * timespec and sigset_t pointers if good.
9469 */
9470 if (*argsz != sizeof(arg))
9471 return -EINVAL;
9472 if (copy_from_user(&arg, argp, sizeof(arg)))
9473 return -EFAULT;
9474 *sig = u64_to_user_ptr(arg.sigmask);
9475 *argsz = arg.sigmask_sz;
9476 *ts = u64_to_user_ptr(arg.ts);
9477 return 0;
9478}
9479
Jens Axboe2b188cc2019-01-07 10:46:33 -07009480SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009481 u32, min_complete, u32, flags, const void __user *, argp,
9482 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009483{
9484 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485 int submitted = 0;
9486 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009487 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009488
Jens Axboe4c6e2772020-07-01 11:29:10 -06009489 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009490
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009491 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9492 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009493 return -EINVAL;
9494
9495 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009496 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009497 return -EBADF;
9498
9499 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009500 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009501 goto out_fput;
9502
9503 ret = -ENXIO;
9504 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009505 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009506 goto out_fput;
9507
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009508 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009509 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009510 goto out;
9511
Jens Axboe6c271ce2019-01-10 11:22:30 -07009512 /*
9513 * For SQ polling, the thread will do all submissions and completions.
9514 * Just return the requested submit count, and wake the thread if
9515 * we were asked to.
9516 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009517 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009518 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009519 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009520
Jens Axboe21f96522021-08-14 09:04:40 -06009521 if (unlikely(ctx->sq_data->thread == NULL)) {
9522 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009523 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009524 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009525 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009526 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009527 if (flags & IORING_ENTER_SQ_WAIT) {
9528 ret = io_sqpoll_wait_sq(ctx);
9529 if (ret)
9530 goto out;
9531 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009532 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009533 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009534 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009535 if (unlikely(ret))
9536 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009537 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009538 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009539 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009540
9541 if (submitted != to_submit)
9542 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009543 }
9544 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009545 const sigset_t __user *sig;
9546 struct __kernel_timespec __user *ts;
9547
9548 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9549 if (unlikely(ret))
9550 goto out;
9551
Jens Axboe2b188cc2019-01-07 10:46:33 -07009552 min_complete = min(min_complete, ctx->cq_entries);
9553
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009554 /*
9555 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9556 * space applications don't need to do io completion events
9557 * polling again, they can rely on io_sq_thread to do polling
9558 * work, which can reduce cpu usage and uring_lock contention.
9559 */
9560 if (ctx->flags & IORING_SETUP_IOPOLL &&
9561 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009562 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009563 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009564 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009565 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009566 }
9567
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009568out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009569 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009570out_fput:
9571 fdput(f);
9572 return submitted ? submitted : ret;
9573}
9574
Tobias Klauserbebdb652020-02-26 18:38:32 +01009575#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009576static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9577 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009578{
Jens Axboe87ce9552020-01-30 08:25:34 -07009579 struct user_namespace *uns = seq_user_ns(m);
9580 struct group_info *gi;
9581 kernel_cap_t cap;
9582 unsigned __capi;
9583 int g;
9584
9585 seq_printf(m, "%5d\n", id);
9586 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9587 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9588 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9589 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9590 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9591 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9592 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9593 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9594 seq_puts(m, "\n\tGroups:\t");
9595 gi = cred->group_info;
9596 for (g = 0; g < gi->ngroups; g++) {
9597 seq_put_decimal_ull(m, g ? " " : "",
9598 from_kgid_munged(uns, gi->gid[g]));
9599 }
9600 seq_puts(m, "\n\tCapEff:\t");
9601 cap = cred->cap_effective;
9602 CAP_FOR_EACH_U32(__capi)
9603 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9604 seq_putc(m, '\n');
9605 return 0;
9606}
9607
9608static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9609{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009610 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009611 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009612 int i;
9613
Jens Axboefad8e0d2020-09-28 08:57:48 -06009614 /*
9615 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9616 * since fdinfo case grabs it in the opposite direction of normal use
9617 * cases. If we fail to get the lock, we just don't iterate any
9618 * structures that could be going away outside the io_uring mutex.
9619 */
9620 has_lock = mutex_trylock(&ctx->uring_lock);
9621
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009622 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009623 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009624 if (!sq->thread)
9625 sq = NULL;
9626 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009627
9628 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9629 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009630 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009631 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -07009632 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009633
Jens Axboe87ce9552020-01-30 08:25:34 -07009634 if (f)
9635 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9636 else
9637 seq_printf(m, "%5u: <none>\n", i);
9638 }
9639 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009640 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009641 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +01009642 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -07009643
Pavel Begunkov4751f532021-04-01 15:43:55 +01009644 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -07009645 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009646 if (has_lock && !xa_empty(&ctx->personalities)) {
9647 unsigned long index;
9648 const struct cred *cred;
9649
Jens Axboe87ce9552020-01-30 08:25:34 -07009650 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009651 xa_for_each(&ctx->personalities, index, cred)
9652 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -07009653 }
Jens Axboed7718a92020-02-14 22:23:12 -07009654 seq_printf(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -06009655 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -07009656 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9657 struct hlist_head *list = &ctx->cancel_hash[i];
9658 struct io_kiocb *req;
9659
9660 hlist_for_each_entry(req, list, hash_node)
9661 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9662 req->task->task_works != NULL);
9663 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009664 spin_unlock(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009665 if (has_lock)
9666 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009667}
9668
9669static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9670{
9671 struct io_ring_ctx *ctx = f->private_data;
9672
9673 if (percpu_ref_tryget(&ctx->refs)) {
9674 __io_uring_show_fdinfo(ctx, m);
9675 percpu_ref_put(&ctx->refs);
9676 }
9677}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009678#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009679
Jens Axboe2b188cc2019-01-07 10:46:33 -07009680static const struct file_operations io_uring_fops = {
9681 .release = io_uring_release,
9682 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009683#ifndef CONFIG_MMU
9684 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9685 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9686#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009687 .poll = io_uring_poll,
9688 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009689#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009690 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009691#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009692};
9693
9694static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9695 struct io_uring_params *p)
9696{
Hristo Venev75b28af2019-08-26 17:23:46 +00009697 struct io_rings *rings;
9698 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009699
Jens Axboebd740482020-08-05 12:58:23 -06009700 /* make sure these are sane, as we already accounted them */
9701 ctx->sq_entries = p->sq_entries;
9702 ctx->cq_entries = p->cq_entries;
9703
Hristo Venev75b28af2019-08-26 17:23:46 +00009704 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9705 if (size == SIZE_MAX)
9706 return -EOVERFLOW;
9707
9708 rings = io_mem_alloc(size);
9709 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009710 return -ENOMEM;
9711
Hristo Venev75b28af2019-08-26 17:23:46 +00009712 ctx->rings = rings;
9713 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9714 rings->sq_ring_mask = p->sq_entries - 1;
9715 rings->cq_ring_mask = p->cq_entries - 1;
9716 rings->sq_ring_entries = p->sq_entries;
9717 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009718
9719 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009720 if (size == SIZE_MAX) {
9721 io_mem_free(ctx->rings);
9722 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009723 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009724 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009725
9726 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009727 if (!ctx->sq_sqes) {
9728 io_mem_free(ctx->rings);
9729 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009730 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009731 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009732
Jens Axboe2b188cc2019-01-07 10:46:33 -07009733 return 0;
9734}
9735
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009736static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9737{
9738 int ret, fd;
9739
9740 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9741 if (fd < 0)
9742 return fd;
9743
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009744 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009745 if (ret) {
9746 put_unused_fd(fd);
9747 return ret;
9748 }
9749 fd_install(fd, file);
9750 return fd;
9751}
9752
Jens Axboe2b188cc2019-01-07 10:46:33 -07009753/*
9754 * Allocate an anonymous fd, this is what constitutes the application
9755 * visible backing of an io_uring instance. The application mmaps this
9756 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9757 * we have to tie this fd to a socket for file garbage collection purposes.
9758 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009759static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009760{
9761 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009762#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009763 int ret;
9764
Jens Axboe2b188cc2019-01-07 10:46:33 -07009765 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9766 &ctx->ring_sock);
9767 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009768 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009769#endif
9770
Jens Axboe2b188cc2019-01-07 10:46:33 -07009771 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9772 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009773#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009774 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009775 sock_release(ctx->ring_sock);
9776 ctx->ring_sock = NULL;
9777 } else {
9778 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009779 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009780#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009781 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009782}
9783
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009784static int io_uring_create(unsigned entries, struct io_uring_params *p,
9785 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009786{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009787 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009788 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009789 int ret;
9790
Jens Axboe8110c1a2019-12-28 15:39:54 -07009791 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009792 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009793 if (entries > IORING_MAX_ENTRIES) {
9794 if (!(p->flags & IORING_SETUP_CLAMP))
9795 return -EINVAL;
9796 entries = IORING_MAX_ENTRIES;
9797 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009798
9799 /*
9800 * Use twice as many entries for the CQ ring. It's possible for the
9801 * application to drive a higher depth than the size of the SQ ring,
9802 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009803 * some flexibility in overcommitting a bit. If the application has
9804 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9805 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009806 */
9807 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009808 if (p->flags & IORING_SETUP_CQSIZE) {
9809 /*
9810 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9811 * to a power-of-two, if it isn't already. We do NOT impose
9812 * any cq vs sq ring sizing.
9813 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009814 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009815 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009816 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9817 if (!(p->flags & IORING_SETUP_CLAMP))
9818 return -EINVAL;
9819 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9820 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009821 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9822 if (p->cq_entries < p->sq_entries)
9823 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009824 } else {
9825 p->cq_entries = 2 * p->sq_entries;
9826 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009827
Jens Axboe2b188cc2019-01-07 10:46:33 -07009828 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009829 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009830 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009831 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009832 if (!capable(CAP_IPC_LOCK))
9833 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -06009834
9835 /*
9836 * This is just grabbed for accounting purposes. When a process exits,
9837 * the mm is exited and dropped before the files, hence we need to hang
9838 * on to this mm purely for the purposes of being able to unaccount
9839 * memory (locked/pinned vm). It's not used for anything else.
9840 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009841 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009842 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009843
Jens Axboe2b188cc2019-01-07 10:46:33 -07009844 ret = io_allocate_scq_urings(ctx, p);
9845 if (ret)
9846 goto err;
9847
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009848 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009849 if (ret)
9850 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009851 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +01009852 ret = io_rsrc_node_switch_start(ctx);
9853 if (ret)
9854 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009855 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009856
Jens Axboe2b188cc2019-01-07 10:46:33 -07009857 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009858 p->sq_off.head = offsetof(struct io_rings, sq.head);
9859 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9860 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9861 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9862 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9863 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9864 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009865
9866 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009867 p->cq_off.head = offsetof(struct io_rings, cq.head);
9868 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9869 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9870 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9871 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9872 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009873 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009874
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009875 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9876 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009877 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009878 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +01009879 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
9880 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009881
9882 if (copy_to_user(params, p, sizeof(*p))) {
9883 ret = -EFAULT;
9884 goto err;
9885 }
Jens Axboed1719f72020-07-30 13:43:53 -06009886
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009887 file = io_uring_get_file(ctx);
9888 if (IS_ERR(file)) {
9889 ret = PTR_ERR(file);
9890 goto err;
9891 }
9892
Jens Axboed1719f72020-07-30 13:43:53 -06009893 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009894 * Install ring fd as the very last thing, so we don't risk someone
9895 * having closed it before we finish setup
9896 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009897 ret = io_uring_install_fd(ctx, file);
9898 if (ret < 0) {
9899 /* fput will clean it up */
9900 fput(file);
9901 return ret;
9902 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009903
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009904 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009905 return ret;
9906err:
9907 io_ring_ctx_wait_and_kill(ctx);
9908 return ret;
9909}
9910
9911/*
9912 * Sets up an aio uring context, and returns the fd. Applications asks for a
9913 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9914 * params structure passed in.
9915 */
9916static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9917{
9918 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009919 int i;
9920
9921 if (copy_from_user(&p, params, sizeof(p)))
9922 return -EFAULT;
9923 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9924 if (p.resv[i])
9925 return -EINVAL;
9926 }
9927
Jens Axboe6c271ce2019-01-10 11:22:30 -07009928 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009929 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009930 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9931 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009932 return -EINVAL;
9933
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009934 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009935}
9936
9937SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9938 struct io_uring_params __user *, params)
9939{
9940 return io_uring_setup(entries, params);
9941}
9942
Jens Axboe66f4af92020-01-16 15:36:52 -07009943static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9944{
9945 struct io_uring_probe *p;
9946 size_t size;
9947 int i, ret;
9948
9949 size = struct_size(p, ops, nr_args);
9950 if (size == SIZE_MAX)
9951 return -EOVERFLOW;
9952 p = kzalloc(size, GFP_KERNEL);
9953 if (!p)
9954 return -ENOMEM;
9955
9956 ret = -EFAULT;
9957 if (copy_from_user(p, arg, size))
9958 goto out;
9959 ret = -EINVAL;
9960 if (memchr_inv(p, 0, size))
9961 goto out;
9962
9963 p->last_op = IORING_OP_LAST - 1;
9964 if (nr_args > IORING_OP_LAST)
9965 nr_args = IORING_OP_LAST;
9966
9967 for (i = 0; i < nr_args; i++) {
9968 p->ops[i].op = i;
9969 if (!io_op_defs[i].not_supported)
9970 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9971 }
9972 p->ops_len = i;
9973
9974 ret = 0;
9975 if (copy_to_user(arg, p, size))
9976 ret = -EFAULT;
9977out:
9978 kfree(p);
9979 return ret;
9980}
9981
Jens Axboe071698e2020-01-28 10:04:42 -07009982static int io_register_personality(struct io_ring_ctx *ctx)
9983{
Jens Axboe4379bf82021-02-15 13:40:22 -07009984 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009985 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009986 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009987
Jens Axboe4379bf82021-02-15 13:40:22 -07009988 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009989
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009990 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
9991 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -06009992 if (ret < 0) {
9993 put_cred(creds);
9994 return ret;
9995 }
9996 return id;
Jens Axboe071698e2020-01-28 10:04:42 -07009997}
9998
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009999static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10000 unsigned int nr_args)
10001{
10002 struct io_uring_restriction *res;
10003 size_t size;
10004 int i, ret;
10005
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010006 /* Restrictions allowed only if rings started disabled */
10007 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10008 return -EBADFD;
10009
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010010 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010011 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010012 return -EBUSY;
10013
10014 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10015 return -EINVAL;
10016
10017 size = array_size(nr_args, sizeof(*res));
10018 if (size == SIZE_MAX)
10019 return -EOVERFLOW;
10020
10021 res = memdup_user(arg, size);
10022 if (IS_ERR(res))
10023 return PTR_ERR(res);
10024
10025 ret = 0;
10026
10027 for (i = 0; i < nr_args; i++) {
10028 switch (res[i].opcode) {
10029 case IORING_RESTRICTION_REGISTER_OP:
10030 if (res[i].register_op >= IORING_REGISTER_LAST) {
10031 ret = -EINVAL;
10032 goto out;
10033 }
10034
10035 __set_bit(res[i].register_op,
10036 ctx->restrictions.register_op);
10037 break;
10038 case IORING_RESTRICTION_SQE_OP:
10039 if (res[i].sqe_op >= IORING_OP_LAST) {
10040 ret = -EINVAL;
10041 goto out;
10042 }
10043
10044 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10045 break;
10046 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10047 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10048 break;
10049 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10050 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10051 break;
10052 default:
10053 ret = -EINVAL;
10054 goto out;
10055 }
10056 }
10057
10058out:
10059 /* Reset all restrictions if an error happened */
10060 if (ret != 0)
10061 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10062 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010063 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010064
10065 kfree(res);
10066 return ret;
10067}
10068
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010069static int io_register_enable_rings(struct io_ring_ctx *ctx)
10070{
10071 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10072 return -EBADFD;
10073
10074 if (ctx->restrictions.registered)
10075 ctx->restricted = 1;
10076
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010077 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10078 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10079 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010080 return 0;
10081}
10082
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010083static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010084 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010085 unsigned nr_args)
10086{
10087 __u32 tmp;
10088 int err;
10089
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010090 if (up->resv)
10091 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010092 if (check_add_overflow(up->offset, nr_args, &tmp))
10093 return -EOVERFLOW;
10094 err = io_rsrc_node_switch_start(ctx);
10095 if (err)
10096 return err;
10097
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010098 switch (type) {
10099 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010100 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010101 case IORING_RSRC_BUFFER:
10102 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010103 }
10104 return -EINVAL;
10105}
10106
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010107static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10108 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010109{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010110 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010111
10112 if (!nr_args)
10113 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010114 memset(&up, 0, sizeof(up));
10115 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10116 return -EFAULT;
10117 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10118}
10119
10120static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010121 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010122{
10123 struct io_uring_rsrc_update2 up;
10124
10125 if (size != sizeof(up))
10126 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010127 if (copy_from_user(&up, arg, sizeof(up)))
10128 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010129 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010130 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010131 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010132}
10133
Pavel Begunkov792e3582021-04-25 14:32:21 +010010134static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010135 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010136{
10137 struct io_uring_rsrc_register rr;
10138
10139 /* keep it extendible */
10140 if (size != sizeof(rr))
10141 return -EINVAL;
10142
10143 memset(&rr, 0, sizeof(rr));
10144 if (copy_from_user(&rr, arg, size))
10145 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010146 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010147 return -EINVAL;
10148
Pavel Begunkov992da012021-06-10 16:37:37 +010010149 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010150 case IORING_RSRC_FILE:
10151 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10152 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010153 case IORING_RSRC_BUFFER:
10154 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10155 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010156 }
10157 return -EINVAL;
10158}
10159
Jens Axboefe764212021-06-17 10:19:54 -060010160static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10161 unsigned len)
10162{
10163 struct io_uring_task *tctx = current->io_uring;
10164 cpumask_var_t new_mask;
10165 int ret;
10166
10167 if (!tctx || !tctx->io_wq)
10168 return -EINVAL;
10169
10170 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10171 return -ENOMEM;
10172
10173 cpumask_clear(new_mask);
10174 if (len > cpumask_size())
10175 len = cpumask_size();
10176
10177 if (copy_from_user(new_mask, arg, len)) {
10178 free_cpumask_var(new_mask);
10179 return -EFAULT;
10180 }
10181
10182 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10183 free_cpumask_var(new_mask);
10184 return ret;
10185}
10186
10187static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10188{
10189 struct io_uring_task *tctx = current->io_uring;
10190
10191 if (!tctx || !tctx->io_wq)
10192 return -EINVAL;
10193
10194 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10195}
10196
Jens Axboe071698e2020-01-28 10:04:42 -070010197static bool io_register_op_must_quiesce(int op)
10198{
10199 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010200 case IORING_REGISTER_BUFFERS:
10201 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010202 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010203 case IORING_UNREGISTER_FILES:
10204 case IORING_REGISTER_FILES_UPDATE:
10205 case IORING_REGISTER_PROBE:
10206 case IORING_REGISTER_PERSONALITY:
10207 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010208 case IORING_REGISTER_FILES2:
10209 case IORING_REGISTER_FILES_UPDATE2:
10210 case IORING_REGISTER_BUFFERS2:
10211 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010212 case IORING_REGISTER_IOWQ_AFF:
10213 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe071698e2020-01-28 10:04:42 -070010214 return false;
10215 default:
10216 return true;
10217 }
10218}
10219
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010220static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10221{
10222 long ret;
10223
10224 percpu_ref_kill(&ctx->refs);
10225
10226 /*
10227 * Drop uring mutex before waiting for references to exit. If another
10228 * thread is currently inside io_uring_enter() it might need to grab the
10229 * uring_lock to make progress. If we hold it here across the drain
10230 * wait, then we can deadlock. It's safe to drop the mutex here, since
10231 * no new references will come in after we've killed the percpu ref.
10232 */
10233 mutex_unlock(&ctx->uring_lock);
10234 do {
10235 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10236 if (!ret)
10237 break;
10238 ret = io_run_task_work_sig();
10239 } while (ret >= 0);
10240 mutex_lock(&ctx->uring_lock);
10241
10242 if (ret)
10243 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10244 return ret;
10245}
10246
Jens Axboeedafcce2019-01-09 09:16:05 -070010247static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10248 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010249 __releases(ctx->uring_lock)
10250 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010251{
10252 int ret;
10253
Jens Axboe35fa71a2019-04-22 10:23:23 -060010254 /*
10255 * We're inside the ring mutex, if the ref is already dying, then
10256 * someone else killed the ctx or is already going through
10257 * io_uring_register().
10258 */
10259 if (percpu_ref_is_dying(&ctx->refs))
10260 return -ENXIO;
10261
Pavel Begunkov75c40212021-04-15 13:07:40 +010010262 if (ctx->restricted) {
10263 if (opcode >= IORING_REGISTER_LAST)
10264 return -EINVAL;
10265 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10266 if (!test_bit(opcode, ctx->restrictions.register_op))
10267 return -EACCES;
10268 }
10269
Jens Axboe071698e2020-01-28 10:04:42 -070010270 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010271 ret = io_ctx_quiesce(ctx);
10272 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010273 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010274 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010275
10276 switch (opcode) {
10277 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010278 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010279 break;
10280 case IORING_UNREGISTER_BUFFERS:
10281 ret = -EINVAL;
10282 if (arg || nr_args)
10283 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010284 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010285 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010286 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010287 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010288 break;
10289 case IORING_UNREGISTER_FILES:
10290 ret = -EINVAL;
10291 if (arg || nr_args)
10292 break;
10293 ret = io_sqe_files_unregister(ctx);
10294 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010295 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010296 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010297 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010298 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010299 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010300 ret = -EINVAL;
10301 if (nr_args != 1)
10302 break;
10303 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010304 if (ret)
10305 break;
10306 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10307 ctx->eventfd_async = 1;
10308 else
10309 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010310 break;
10311 case IORING_UNREGISTER_EVENTFD:
10312 ret = -EINVAL;
10313 if (arg || nr_args)
10314 break;
10315 ret = io_eventfd_unregister(ctx);
10316 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010317 case IORING_REGISTER_PROBE:
10318 ret = -EINVAL;
10319 if (!arg || nr_args > 256)
10320 break;
10321 ret = io_probe(ctx, arg, nr_args);
10322 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010323 case IORING_REGISTER_PERSONALITY:
10324 ret = -EINVAL;
10325 if (arg || nr_args)
10326 break;
10327 ret = io_register_personality(ctx);
10328 break;
10329 case IORING_UNREGISTER_PERSONALITY:
10330 ret = -EINVAL;
10331 if (arg)
10332 break;
10333 ret = io_unregister_personality(ctx, nr_args);
10334 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010335 case IORING_REGISTER_ENABLE_RINGS:
10336 ret = -EINVAL;
10337 if (arg || nr_args)
10338 break;
10339 ret = io_register_enable_rings(ctx);
10340 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010341 case IORING_REGISTER_RESTRICTIONS:
10342 ret = io_register_restrictions(ctx, arg, nr_args);
10343 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010344 case IORING_REGISTER_FILES2:
10345 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010346 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010347 case IORING_REGISTER_FILES_UPDATE2:
10348 ret = io_register_rsrc_update(ctx, arg, nr_args,
10349 IORING_RSRC_FILE);
10350 break;
10351 case IORING_REGISTER_BUFFERS2:
10352 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10353 break;
10354 case IORING_REGISTER_BUFFERS_UPDATE:
10355 ret = io_register_rsrc_update(ctx, arg, nr_args,
10356 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010357 break;
Jens Axboefe764212021-06-17 10:19:54 -060010358 case IORING_REGISTER_IOWQ_AFF:
10359 ret = -EINVAL;
10360 if (!arg || !nr_args)
10361 break;
10362 ret = io_register_iowq_aff(ctx, arg, nr_args);
10363 break;
10364 case IORING_UNREGISTER_IOWQ_AFF:
10365 ret = -EINVAL;
10366 if (arg || nr_args)
10367 break;
10368 ret = io_unregister_iowq_aff(ctx);
10369 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010370 default:
10371 ret = -EINVAL;
10372 break;
10373 }
10374
Jens Axboe071698e2020-01-28 10:04:42 -070010375 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010376 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010377 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010378 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010379 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010380 return ret;
10381}
10382
10383SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10384 void __user *, arg, unsigned int, nr_args)
10385{
10386 struct io_ring_ctx *ctx;
10387 long ret = -EBADF;
10388 struct fd f;
10389
10390 f = fdget(fd);
10391 if (!f.file)
10392 return -EBADF;
10393
10394 ret = -EOPNOTSUPP;
10395 if (f.file->f_op != &io_uring_fops)
10396 goto out_fput;
10397
10398 ctx = f.file->private_data;
10399
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010400 io_run_task_work();
10401
Jens Axboeedafcce2019-01-09 09:16:05 -070010402 mutex_lock(&ctx->uring_lock);
10403 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10404 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010405 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10406 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010407out_fput:
10408 fdput(f);
10409 return ret;
10410}
10411
Jens Axboe2b188cc2019-01-07 10:46:33 -070010412static int __init io_uring_init(void)
10413{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010414#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10415 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10416 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10417} while (0)
10418
10419#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10420 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10421 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10422 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10423 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10424 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10425 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10426 BUILD_BUG_SQE_ELEM(8, __u64, off);
10427 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10428 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010429 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010430 BUILD_BUG_SQE_ELEM(24, __u32, len);
10431 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10432 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10433 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10434 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010435 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10436 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010437 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10438 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10439 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10440 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10441 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10442 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10443 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10444 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010445 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010446 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10447 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010448 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010449 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010450 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010451 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010452
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010453 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10454 sizeof(struct io_uring_rsrc_update));
10455 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10456 sizeof(struct io_uring_rsrc_update2));
10457 /* should fit into one byte */
10458 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
10459
Jens Axboed3656342019-12-18 09:50:26 -070010460 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010461 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010462
Jens Axboe91f245d2021-02-09 13:48:50 -070010463 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10464 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010465 return 0;
10466};
10467__initcall(io_uring_init);