blob: 5059049da242faf86e5603b425d527ca30f72462 [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
Hao Xua8295b92021-08-27 17:46:09 +08001191static inline void req_fail_link_node(struct io_kiocb *req, int res)
1192{
1193 req_set_fail(req);
1194 req->result = res;
1195}
1196
Jens Axboe2b188cc2019-01-07 10:46:33 -07001197static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1198{
1199 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1200
Jens Axboe0f158b42020-05-14 17:18:39 -06001201 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001202}
1203
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001204static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1205{
1206 return !req->timeout.off;
1207}
1208
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001209static void io_fallback_req_func(struct work_struct *work)
1210{
1211 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
1212 fallback_work.work);
1213 struct llist_node *node = llist_del_all(&ctx->fallback_llist);
1214 struct io_kiocb *req, *tmp;
Pavel Begunkovf237c302021-08-18 12:42:46 +01001215 bool locked = false;
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001216
1217 percpu_ref_get(&ctx->refs);
1218 llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node)
Pavel Begunkovf237c302021-08-18 12:42:46 +01001219 req->io_task_work.func(req, &locked);
Pavel Begunkov5636c002021-08-18 12:42:45 +01001220
Pavel Begunkovf237c302021-08-18 12:42:46 +01001221 if (locked) {
1222 if (ctx->submit_state.compl_nr)
1223 io_submit_flush_completions(ctx);
1224 mutex_unlock(&ctx->uring_lock);
1225 }
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001226 percpu_ref_put(&ctx->refs);
Pavel Begunkovf237c302021-08-18 12:42:46 +01001227
Pavel Begunkovf56165e2021-08-09 20:18:07 +01001228}
1229
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1231{
1232 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001233 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001234
1235 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1236 if (!ctx)
1237 return NULL;
1238
Jens Axboe78076bb2019-12-04 19:56:40 -07001239 /*
1240 * Use 5 bits less than the max cq entries, that should give us around
1241 * 32 entries per hash list if totally full and uniformly spread.
1242 */
1243 hash_bits = ilog2(p->cq_entries);
1244 hash_bits -= 5;
1245 if (hash_bits <= 0)
1246 hash_bits = 1;
1247 ctx->cancel_hash_bits = hash_bits;
1248 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1249 GFP_KERNEL);
1250 if (!ctx->cancel_hash)
1251 goto err;
1252 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1253
Pavel Begunkov62248432021-04-28 13:11:29 +01001254 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1255 if (!ctx->dummy_ubuf)
1256 goto err;
1257 /* set invalid range, so io_import_fixed() fails meeting it */
1258 ctx->dummy_ubuf->ubuf = -1UL;
1259
Roman Gushchin21482892019-05-07 10:01:48 -07001260 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001261 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1262 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263
1264 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001265 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001266 INIT_LIST_HEAD(&ctx->sqd_list);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001267 init_waitqueue_head(&ctx->poll_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001268 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001269 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001270 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001271 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272 mutex_init(&ctx->uring_lock);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001273 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274 spin_lock_init(&ctx->completion_lock);
Jens Axboe89850fc2021-08-10 15:11:51 -06001275 spin_lock_init(&ctx->timeout_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001276 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001277 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001278 INIT_LIST_HEAD(&ctx->timeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001279 spin_lock_init(&ctx->rsrc_ref_lock);
1280 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001281 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1282 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001283 INIT_LIST_HEAD(&ctx->tctx_list);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001284 INIT_LIST_HEAD(&ctx->submit_state.free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001285 INIT_LIST_HEAD(&ctx->locked_free_list);
Pavel Begunkov9011bf92021-06-30 21:54:03 +01001286 INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001288err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001289 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001290 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001291 kfree(ctx);
1292 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293}
1294
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001295static void io_account_cq_overflow(struct io_ring_ctx *ctx)
1296{
1297 struct io_rings *r = ctx->rings;
1298
1299 WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1);
1300 ctx->cq_extra--;
1301}
1302
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001303static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001304{
Jens Axboe2bc99302020-07-09 09:43:27 -06001305 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1306 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001307
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001308 return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail;
Jens Axboe2bc99302020-07-09 09:43:27 -06001309 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001310
Bob Liu9d858b22019-11-13 18:06:25 +08001311 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001312}
1313
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01001314#define FFS_ASYNC_READ 0x1UL
1315#define FFS_ASYNC_WRITE 0x2UL
1316#ifdef CONFIG_64BIT
1317#define FFS_ISREG 0x4UL
1318#else
1319#define FFS_ISREG 0x0UL
1320#endif
1321#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
1322
1323static inline bool io_req_ffs_set(struct io_kiocb *req)
1324{
1325 return IS_ENABLED(CONFIG_64BIT) && (req->flags & REQ_F_FIXED_FILE);
1326}
1327
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001328static void io_req_track_inflight(struct io_kiocb *req)
1329{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001330 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001331 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001332 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001333 }
1334}
1335
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001336static inline void io_unprep_linked_timeout(struct io_kiocb *req)
1337{
1338 req->flags &= ~REQ_F_LINK_TIMEOUT;
1339}
1340
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001341static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req)
1342{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01001343 if (WARN_ON_ONCE(!req->link))
1344 return NULL;
1345
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001346 req->flags &= ~REQ_F_ARM_LTIMEOUT;
1347 req->flags |= REQ_F_LINK_TIMEOUT;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001348
1349 /* linked timeouts should have two refs once prep'ed */
Pavel Begunkov48dcd382021-08-15 10:40:18 +01001350 io_req_set_refcount(req);
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001351 __io_req_set_refcount(req->link, 2);
1352 return req->link;
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001353}
1354
1355static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
1356{
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01001357 if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT)))
Pavel Begunkovfd08e532021-08-11 19:28:31 +01001358 return NULL;
1359 return __io_prep_linked_timeout(req);
1360}
1361
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001362static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001363{
Jens Axboed3656342019-12-18 09:50:26 -07001364 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001365 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001366
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001367 if (!(req->flags & REQ_F_CREDS)) {
1368 req->flags |= REQ_F_CREDS;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01001369 req->creds = get_current_cred();
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01001370 }
Jens Axboe003e8dc2021-03-06 09:22:27 -07001371
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001372 req->work.list.next = NULL;
1373 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001374 if (req->flags & REQ_F_FORCE_ASYNC)
1375 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1376
Jens Axboed3656342019-12-18 09:50:26 -07001377 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001378 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001379 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001380 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001381 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001382 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001383 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001384
1385 switch (req->opcode) {
1386 case IORING_OP_SPLICE:
1387 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001388 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1389 req->work.flags |= IO_WQ_WORK_UNBOUND;
1390 break;
1391 }
Jens Axboe561fb042019-10-24 07:25:42 -06001392}
1393
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001394static void io_prep_async_link(struct io_kiocb *req)
1395{
1396 struct io_kiocb *cur;
1397
Pavel Begunkov44eff402021-07-26 14:14:31 +01001398 if (req->flags & REQ_F_LINK_TIMEOUT) {
1399 struct io_ring_ctx *ctx = req->ctx;
1400
Jens Axboe79ebeae2021-08-10 15:18:27 -06001401 spin_lock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001402 io_for_each_link(cur, req)
1403 io_prep_async_work(cur);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001404 spin_unlock(&ctx->completion_lock);
Pavel Begunkov44eff402021-07-26 14:14:31 +01001405 } else {
1406 io_for_each_link(cur, req)
1407 io_prep_async_work(cur);
1408 }
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001409}
1410
Pavel Begunkovf237c302021-08-18 12:42:46 +01001411static void io_queue_async_work(struct io_kiocb *req, bool *locked)
Jens Axboe561fb042019-10-24 07:25:42 -06001412{
Jackie Liua197f662019-11-08 08:09:12 -07001413 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001414 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001415 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001416
Pavel Begunkovf237c302021-08-18 12:42:46 +01001417 /* must not take the lock, NULL it as a precaution */
1418 locked = NULL;
1419
Jens Axboe3bfe6102021-02-16 14:15:30 -07001420 BUG_ON(!tctx);
1421 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001422
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001423 /* init ->work of the whole link before punting */
1424 io_prep_async_link(req);
Jens Axboe991468d2021-07-23 11:53:54 -06001425
1426 /*
1427 * Not expected to happen, but if we do have a bug where this _can_
1428 * happen, catch it here and ensure the request is marked as
1429 * canceled. That will make io-wq go through the usual work cancel
1430 * procedure rather than attempt to run this request (or create a new
1431 * worker for it).
1432 */
1433 if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
1434 req->work.flags |= IO_WQ_WORK_CANCEL;
1435
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001436 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1437 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001438 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001439 if (link)
1440 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001441}
1442
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001443static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001444 __must_hold(&req->ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06001445 __must_hold(&req->ctx->timeout_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001446{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001447 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001448
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001449 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001450 atomic_set(&req->ctx->cq_timeouts,
1451 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001452 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001453 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001454 io_put_req_deferred(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001455 }
1456}
1457
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001458static void io_queue_deferred(struct io_ring_ctx *ctx)
Pavel Begunkov04518942020-05-26 20:34:05 +03001459{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001460 while (!list_empty(&ctx->defer_list)) {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001461 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1462 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001463
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001464 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001465 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001466 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001467 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001468 kfree(de);
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001469 }
Pavel Begunkov04518942020-05-26 20:34:05 +03001470}
1471
Pavel Begunkov360428f2020-05-30 14:54:17 +03001472static void io_flush_timeouts(struct io_ring_ctx *ctx)
Jens Axboe89850fc2021-08-10 15:11:51 -06001473 __must_hold(&ctx->completion_lock)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001474{
Pavel Begunkov441b8a72021-06-14 23:37:31 +01001475 u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001476
Jens Axboe79ebeae2021-08-10 15:18:27 -06001477 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001478 while (!list_empty(&ctx->timeout_list)) {
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001479 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001480 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001481 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001482
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001483 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001484 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001485
1486 /*
1487 * Since seq can easily wrap around over time, subtract
1488 * the last seq at which timeouts were flushed before comparing.
1489 * Assuming not more than 2^31-1 events have happened since,
1490 * these subtractions won't have wrapped, so we can check if
1491 * target is in [last_seq, current_seq] by comparing the two.
1492 */
1493 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1494 events_got = seq - ctx->cq_last_tm_flush;
1495 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001496 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001497
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001498 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001499 io_kill_timeout(req, 0);
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01001500 }
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001501 ctx->cq_last_tm_flush = seq;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001502 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001503}
1504
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001505static void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -06001506{
Pavel Begunkov2335f6f2021-06-15 16:47:58 +01001507 if (ctx->off_timeout_used)
1508 io_flush_timeouts(ctx);
1509 if (ctx->drain_active)
1510 io_queue_deferred(ctx);
1511}
1512
1513static inline void io_commit_cqring(struct io_ring_ctx *ctx)
1514{
1515 if (unlikely(ctx->off_timeout_used || ctx->drain_active))
1516 __io_commit_cqring_flush(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001517 /* order cqe stores with ring update */
1518 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001519}
1520
Jens Axboe90554202020-09-03 12:12:41 -06001521static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1522{
1523 struct io_rings *r = ctx->rings;
1524
Pavel Begunkova566c552021-05-16 22:58:08 +01001525 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries;
Jens Axboe90554202020-09-03 12:12:41 -06001526}
1527
Pavel Begunkov888aae22021-01-19 13:32:39 +00001528static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1529{
1530 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1531}
1532
Pavel Begunkovd068b502021-05-16 22:58:11 +01001533static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534{
Hristo Venev75b28af2019-08-26 17:23:46 +00001535 struct io_rings *rings = ctx->rings;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001536 unsigned tail, mask = ctx->cq_entries - 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001537
Stefan Bühler115e12e2019-04-24 23:54:18 +02001538 /*
1539 * writes to the cq entry need to come after reading head; the
1540 * control dependency is enough as we're using WRITE_ONCE to
1541 * fill the cq entry
1542 */
Pavel Begunkova566c552021-05-16 22:58:08 +01001543 if (__io_cqring_events(ctx) == ctx->cq_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001544 return NULL;
1545
Pavel Begunkov888aae22021-01-19 13:32:39 +00001546 tail = ctx->cached_cq_tail++;
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01001547 return &rings->cqes[tail & mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001548}
1549
Jens Axboef2842ab2020-01-08 11:04:00 -07001550static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1551{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001552 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001553 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001554 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1555 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001556 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001557}
1558
Jens Axboe2c5d7632021-08-21 07:21:19 -06001559/*
1560 * This should only get called when at least one event has been posted.
1561 * Some applications rely on the eventfd notification count only changing
1562 * IFF a new CQE has been added to the CQ ring. There's no depedency on
1563 * 1:1 relationship between how many times this function is called (and
1564 * hence the eventfd count) and number of CQEs posted to the CQ ring.
1565 */
Jens Axboeb41e9852020-02-17 09:52:41 -07001566static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001567{
Jens Axboe5fd46172021-08-06 14:04:31 -06001568 /*
1569 * wake_up_all() may seem excessive, but io_wake_function() and
1570 * io_should_wake() handle the termination of the loop and only
1571 * wake as many waiters as we need to.
1572 */
1573 if (wq_has_sleeper(&ctx->cq_wait))
1574 wake_up_all(&ctx->cq_wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001575 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1576 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001577 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001578 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001579 if (waitqueue_active(&ctx->poll_wait)) {
1580 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001581 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1582 }
Jens Axboe8c838782019-03-12 15:48:16 -06001583}
1584
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001585static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1586{
1587 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe5fd46172021-08-06 14:04:31 -06001588 if (wq_has_sleeper(&ctx->cq_wait))
1589 wake_up_all(&ctx->cq_wait);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001590 }
1591 if (io_should_trigger_evfd(ctx))
1592 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkov311997b2021-06-14 23:37:28 +01001593 if (waitqueue_active(&ctx->poll_wait)) {
1594 wake_up_interruptible(&ctx->poll_wait);
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001595 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1596 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001597}
1598
Jens Axboec4a2ed72019-11-21 21:01:26 -07001599/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001600static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001601{
Jens Axboeb18032b2021-01-24 16:58:56 -07001602 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001603
Pavel Begunkova566c552021-05-16 22:58:08 +01001604 if (!force && __io_cqring_events(ctx) == ctx->cq_entries)
Pavel Begunkove23de152020-12-17 00:24:37 +00001605 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001606
Jens Axboeb18032b2021-01-24 16:58:56 -07001607 posted = false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001608 spin_lock(&ctx->completion_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001609 while (!list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkovd068b502021-05-16 22:58:11 +01001610 struct io_uring_cqe *cqe = io_get_cqe(ctx);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001611 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001612
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001613 if (!cqe && !force)
1614 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001615 ocqe = list_first_entry(&ctx->cq_overflow_list,
1616 struct io_overflow_cqe, list);
1617 if (cqe)
1618 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1619 else
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001620 io_account_cq_overflow(ctx);
1621
Jens Axboeb18032b2021-01-24 16:58:56 -07001622 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001623 list_del(&ocqe->list);
1624 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001625 }
1626
Pavel Begunkov09e88402020-12-17 00:24:38 +00001627 all_flushed = list_empty(&ctx->cq_overflow_list);
1628 if (all_flushed) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001629 clear_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001630 WRITE_ONCE(ctx->rings->sq_flags,
1631 ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001632 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001633
Jens Axboeb18032b2021-01-24 16:58:56 -07001634 if (posted)
1635 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001636 spin_unlock(&ctx->completion_lock);
Jens Axboeb18032b2021-01-24 16:58:56 -07001637 if (posted)
1638 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001639 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001640}
1641
Pavel Begunkov90f67362021-08-09 20:18:12 +01001642static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001643{
Jens Axboeca0a2652021-03-04 17:15:48 -07001644 bool ret = true;
1645
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001646 if (test_bit(0, &ctx->check_cq_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00001647 /* iopoll syncs against uring_lock, not completion_lock */
1648 if (ctx->flags & IORING_SETUP_IOPOLL)
1649 mutex_lock(&ctx->uring_lock);
Pavel Begunkov90f67362021-08-09 20:18:12 +01001650 ret = __io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001651 if (ctx->flags & IORING_SETUP_IOPOLL)
1652 mutex_unlock(&ctx->uring_lock);
1653 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001654
1655 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001656}
1657
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001658/* must to be called somewhat shortly after putting a request */
1659static inline void io_put_task(struct task_struct *task, int nr)
1660{
1661 struct io_uring_task *tctx = task->io_uring;
1662
Pavel Begunkove98e49b2021-08-18 17:01:43 +01001663 if (likely(task == current)) {
1664 tctx->cached_refs += nr;
1665 } else {
1666 percpu_counter_sub(&tctx->inflight, nr);
1667 if (unlikely(atomic_read(&tctx->in_idle)))
1668 wake_up(&tctx->wait);
1669 put_task_struct_many(task, nr);
1670 }
Pavel Begunkov6a290a12021-08-09 13:04:13 +01001671}
1672
Pavel Begunkov9a108672021-08-27 11:55:01 +01001673static void io_task_refs_refill(struct io_uring_task *tctx)
1674{
1675 unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR;
1676
1677 percpu_counter_add(&tctx->inflight, refill);
1678 refcount_add(refill, &current->usage);
1679 tctx->cached_refs += refill;
1680}
1681
1682static inline void io_get_task_refs(int nr)
1683{
1684 struct io_uring_task *tctx = current->io_uring;
1685
1686 tctx->cached_refs -= nr;
1687 if (unlikely(tctx->cached_refs < 0))
1688 io_task_refs_refill(tctx);
1689}
1690
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001691static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1692 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001694 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001695
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001696 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1697 if (!ocqe) {
1698 /*
1699 * If we're in ring overflow flush mode, or in task cancel mode,
1700 * or cannot allocate an overflow entry, then we need to drop it
1701 * on the floor.
1702 */
Pavel Begunkov8f6ed492021-05-16 22:58:10 +01001703 io_account_cq_overflow(ctx);
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001704 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001705 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001706 if (list_empty(&ctx->cq_overflow_list)) {
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01001707 set_bit(0, &ctx->check_cq_overflow);
Nadav Amit20c0b382021-08-07 17:13:42 -07001708 WRITE_ONCE(ctx->rings->sq_flags,
1709 ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW);
1710
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001711 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001712 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001713 ocqe->cqe.res = res;
1714 ocqe->cqe.flags = cflags;
1715 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1716 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717}
1718
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001719static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1720 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001721{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722 struct io_uring_cqe *cqe;
1723
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001724 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725
1726 /*
1727 * If we can't get a cq entry, userspace overflowed the
1728 * submission (by quite a lot). Increment the overflow count in
1729 * the ring.
1730 */
Pavel Begunkovd068b502021-05-16 22:58:11 +01001731 cqe = io_get_cqe(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001733 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734 WRITE_ONCE(cqe->res, res);
1735 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001736 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001738 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001739}
1740
Pavel Begunkov8d133262021-04-11 01:46:33 +01001741/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001742static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1743 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001744{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001745 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001746}
1747
Pavel Begunkov7a612352021-03-09 00:37:59 +00001748static void io_req_complete_post(struct io_kiocb *req, long res,
1749 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001750{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001751 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001752
Jens Axboe79ebeae2021-08-10 15:18:27 -06001753 spin_lock(&ctx->completion_lock);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001754 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001755 /*
1756 * If we're the last reference to this request, add to our locked
1757 * free_list cache.
1758 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001759 if (req_ref_put_and_test(req)) {
Pavel Begunkov7a612352021-03-09 00:37:59 +00001760 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov0756a862021-08-15 10:40:25 +01001761 if (req->flags & IO_DISARM_MASK)
Pavel Begunkov7a612352021-03-09 00:37:59 +00001762 io_disarm_next(req);
1763 if (req->link) {
1764 io_req_task_queue(req->link);
1765 req->link = NULL;
1766 }
1767 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001768 io_dismantle_req(req);
1769 io_put_task(req->task, 1);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001770 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001771 ctx->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001772 } else {
1773 if (!percpu_ref_tryget(&ctx->refs))
1774 req = NULL;
1775 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001776 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06001777 spin_unlock(&ctx->completion_lock);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001778
Pavel Begunkov180f8292021-03-14 20:57:09 +00001779 if (req) {
1780 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001781 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001782 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783}
1784
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001785static inline bool io_req_needs_clean(struct io_kiocb *req)
1786{
Pavel Begunkovc8543572021-06-17 18:14:04 +01001787 return req->flags & IO_REQ_CLEAN_FLAGS;
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001788}
1789
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001790static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001791 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001792{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001793 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001794 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001795 req->result = res;
1796 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001797 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001798}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001799
Pavel Begunkov889fca72021-02-10 00:03:09 +00001800static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1801 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001802{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001803 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1804 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001805 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001806 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001807}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001808
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001809static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001810{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001811 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001812}
1813
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001814static void io_req_complete_failed(struct io_kiocb *req, long res)
1815{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001816 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001817 io_req_complete_post(req, res, 0);
1818}
1819
Pavel Begunkov864ea922021-08-09 13:04:08 +01001820/*
1821 * Don't initialise the fields below on every allocation, but do that in
1822 * advance and keep them valid across allocations.
1823 */
1824static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx)
1825{
1826 req->ctx = ctx;
1827 req->link = NULL;
1828 req->async_data = NULL;
1829 /* not necessary, but safer to zero */
1830 req->result = 0;
1831}
1832
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001833static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001834 struct io_submit_state *state)
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001835{
Jens Axboe79ebeae2021-08-10 15:18:27 -06001836 spin_lock(&ctx->completion_lock);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001837 list_splice_init(&ctx->locked_free_list, &state->free_list);
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001838 ctx->locked_free_nr = 0;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001839 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001840}
1841
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001842/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001843static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001844{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001845 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001846 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001847
Jens Axboec7dae4b2021-02-09 19:53:37 -07001848 /*
1849 * If we have more than a batch's worth of requests in our IRQ side
1850 * locked cache, grab the lock and move them over to our submission
1851 * side cache.
1852 */
Pavel Begunkovd0acdee2021-05-16 22:58:12 +01001853 if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH)
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001854 io_flush_cached_locked_reqs(ctx, state);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001855
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001856 nr = state->free_reqs;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01001857 while (!list_empty(&state->free_list)) {
1858 struct io_kiocb *req = list_first_entry(&state->free_list,
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001859 struct io_kiocb, inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001860
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001861 list_del(&req->inflight_entry);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001862 state->reqs[nr++] = req;
1863 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001864 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001865 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001866
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001867 state->free_reqs = nr;
1868 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001869}
1870
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001871/*
1872 * A request might get retired back into the request caches even before opcode
1873 * handlers and io_issue_sqe() are done with it, e.g. inline completion path.
1874 * Because of that, io_alloc_req() should be called only under ->uring_lock
1875 * and with extra caution to not get a request that is still worked on.
1876 */
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001877static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01001878 __must_hold(&ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001879{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001880 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkov864ea922021-08-09 13:04:08 +01001881 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
1882 int ret, i;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001883
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01001884 BUILD_BUG_ON(ARRAY_SIZE(state->reqs) < IO_REQ_ALLOC_BATCH);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001885
Pavel Begunkov864ea922021-08-09 13:04:08 +01001886 if (likely(state->free_reqs || io_flush_cached_reqs(ctx)))
1887 goto got_req;
Jens Axboe2579f912019-01-09 09:10:43 -07001888
Pavel Begunkov864ea922021-08-09 13:04:08 +01001889 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1890 state->reqs);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001891
Pavel Begunkov864ea922021-08-09 13:04:08 +01001892 /*
1893 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1894 * retry single alloc to be on the safe side.
1895 */
1896 if (unlikely(ret <= 0)) {
1897 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1898 if (!state->reqs[0])
1899 return NULL;
1900 ret = 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901 }
Pavel Begunkov864ea922021-08-09 13:04:08 +01001902
1903 for (i = 0; i < ret; i++)
1904 io_preinit_req(state->reqs[i], ctx);
1905 state->free_reqs = ret;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001906got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001907 state->free_reqs--;
1908 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909}
1910
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001911static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001912{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001913 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001914 fput(file);
1915}
1916
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001917static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001918{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001919 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001920
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001921 if (io_req_needs_clean(req))
1922 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001923 if (!(flags & REQ_F_FIXED_FILE))
1924 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001925 if (req->fixed_rsrc_refs)
1926 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001927 if (req->async_data) {
Pavel Begunkov094bae42021-03-19 17:22:42 +00001928 kfree(req->async_data);
Pavel Begunkov99ebe4e2021-06-26 21:40:49 +01001929 req->async_data = NULL;
1930 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001931}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001932
Pavel Begunkov216578e2020-10-13 09:44:00 +01001933static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001934{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001935 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001936
Pavel Begunkov216578e2020-10-13 09:44:00 +01001937 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001938 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001939
Jens Axboe79ebeae2021-08-10 15:18:27 -06001940 spin_lock(&ctx->completion_lock);
Pavel Begunkovbb943b82021-08-09 20:18:10 +01001941 list_add(&req->inflight_entry, &ctx->locked_free_list);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001942 ctx->locked_free_nr++;
Jens Axboe79ebeae2021-08-10 15:18:27 -06001943 spin_unlock(&ctx->completion_lock);
Pavel Begunkovc34b0252021-08-09 20:18:08 +01001944
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001945 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001946}
1947
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001948static inline void io_remove_next_linked(struct io_kiocb *req)
1949{
1950 struct io_kiocb *nxt = req->link;
1951
1952 req->link = nxt->link;
1953 nxt->link = NULL;
1954}
1955
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001956static bool io_kill_linked_timeout(struct io_kiocb *req)
1957 __must_hold(&req->ctx->completion_lock)
Jens Axboe89b263f2021-08-10 15:14:18 -06001958 __must_hold(&req->ctx->timeout_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001959{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001960 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001961
Pavel Begunkovb97e7362021-08-15 10:40:23 +01001962 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001963 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001964
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001965 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001966 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001967 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001968 io_cqring_fill_event(link->ctx, link->user_data,
1969 -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001970 io_put_req_deferred(link);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001971 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001972 }
1973 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001974 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001975}
1976
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001977static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001978 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001979{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001980 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06001981
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001982 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001983 while (link) {
Hao Xua8295b92021-08-27 17:46:09 +08001984 long res = -ECANCELED;
1985
1986 if (link->flags & REQ_F_FAIL)
1987 res = link->result;
1988
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001989 nxt = link->link;
1990 link->link = NULL;
1991
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001992 trace_io_uring_fail_link(req, link);
Hao Xua8295b92021-08-27 17:46:09 +08001993 io_cqring_fill_event(link->ctx, link->user_data, res, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01001994 io_put_req_deferred(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001995 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001996 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001997}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001998
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001999static bool io_disarm_next(struct io_kiocb *req)
2000 __must_hold(&req->ctx->completion_lock)
2001{
2002 bool posted = false;
2003
Pavel Begunkov0756a862021-08-15 10:40:25 +01002004 if (req->flags & REQ_F_ARM_LTIMEOUT) {
2005 struct io_kiocb *link = req->link;
2006
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01002007 req->flags &= ~REQ_F_ARM_LTIMEOUT;
Pavel Begunkov0756a862021-08-15 10:40:25 +01002008 if (link && link->opcode == IORING_OP_LINK_TIMEOUT) {
2009 io_remove_next_linked(req);
2010 io_cqring_fill_event(link->ctx, link->user_data,
2011 -ECANCELED, 0);
2012 io_put_req_deferred(link);
2013 posted = true;
2014 }
2015 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
Jens Axboe89b263f2021-08-10 15:14:18 -06002016 struct io_ring_ctx *ctx = req->ctx;
2017
2018 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002019 posted = io_kill_linked_timeout(req);
Jens Axboe89b263f2021-08-10 15:14:18 -06002020 spin_unlock_irq(&ctx->timeout_lock);
2021 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002022 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01002023 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002024 posted |= (req->link != NULL);
2025 io_fail_links(req);
2026 }
2027 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06002028}
2029
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002030static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002031{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002032 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002033
Jens Axboe9e645e112019-05-10 16:07:28 -06002034 /*
2035 * If LINK is set, we have dependent requests in this chain. If we
2036 * didn't fail this request, queue the first one up, moving any other
2037 * dependencies to the next request. In case of failure, fail the rest
2038 * of the chain.
2039 */
Pavel Begunkov0756a862021-08-15 10:40:25 +01002040 if (req->flags & IO_DISARM_MASK) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002041 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002042 bool posted;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002043
Jens Axboe79ebeae2021-08-10 15:18:27 -06002044 spin_lock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002045 posted = io_disarm_next(req);
2046 if (posted)
2047 io_commit_cqring(req->ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002048 spin_unlock(&ctx->completion_lock);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002049 if (posted)
2050 io_cqring_ev_posted(ctx);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002051 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00002052 nxt = req->link;
2053 req->link = NULL;
2054 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002055}
Jens Axboe2665abf2019-11-05 12:40:47 -07002056
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002057static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002058{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002059 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002060 return NULL;
2061 return __io_req_find_next(req);
2062}
2063
Pavel Begunkovf237c302021-08-18 12:42:46 +01002064static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked)
Pavel Begunkov2c323952021-02-28 22:04:53 +00002065{
2066 if (!ctx)
2067 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002068 if (*locked) {
Hao Xu99c8bc52021-08-21 06:19:54 +08002069 if (ctx->submit_state.compl_nr)
2070 io_submit_flush_completions(ctx);
Pavel Begunkov2c323952021-02-28 22:04:53 +00002071 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovf237c302021-08-18 12:42:46 +01002072 *locked = false;
Pavel Begunkov2c323952021-02-28 22:04:53 +00002073 }
2074 percpu_ref_put(&ctx->refs);
2075}
2076
Jens Axboe7cbf1722021-02-10 00:03:20 +00002077static void tctx_task_work(struct callback_head *cb)
2078{
Pavel Begunkovf237c302021-08-18 12:42:46 +01002079 bool locked = false;
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002080 struct io_ring_ctx *ctx = NULL;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002081 struct io_uring_task *tctx = container_of(cb, struct io_uring_task,
2082 task_work);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002083
Pavel Begunkov16f72072021-06-17 18:14:09 +01002084 while (1) {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002085 struct io_wq_work_node *node;
2086
2087 spin_lock_irq(&tctx->task_lock);
Pavel Begunkovc6538be2021-06-17 18:14:08 +01002088 node = tctx->task_list.first;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002089 INIT_WQ_LIST(&tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002090 if (!node)
2091 tctx->task_running = false;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002092 spin_unlock_irq(&tctx->task_lock);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002093 if (!node)
2094 break;
Pavel Begunkov3f184072021-06-17 18:14:06 +01002095
Pavel Begunkov6294f362021-08-10 17:53:55 +01002096 do {
Pavel Begunkov3f184072021-06-17 18:14:06 +01002097 struct io_wq_work_node *next = node->next;
2098 struct io_kiocb *req = container_of(node, struct io_kiocb,
2099 io_task_work.node);
2100
2101 if (req->ctx != ctx) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002102 ctx_flush_and_put(ctx, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002103 ctx = req->ctx;
Pavel Begunkov126180b2021-08-18 12:42:47 +01002104 /* if not contended, grab and improve batching */
2105 locked = mutex_trylock(&ctx->uring_lock);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002106 percpu_ref_get(&ctx->refs);
2107 }
Pavel Begunkovf237c302021-08-18 12:42:46 +01002108 req->io_task_work.func(req, &locked);
Pavel Begunkov3f184072021-06-17 18:14:06 +01002109 node = next;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002110 } while (node);
2111
Jens Axboe7cbf1722021-02-10 00:03:20 +00002112 cond_resched();
Pavel Begunkov3f184072021-06-17 18:14:06 +01002113 }
Pavel Begunkovebd0df22021-06-17 18:14:07 +01002114
Pavel Begunkovf237c302021-08-18 12:42:46 +01002115 ctx_flush_and_put(ctx, &locked);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002116}
2117
Pavel Begunkove09ee512021-07-01 13:26:05 +01002118static void io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00002119{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002120 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002121 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002122 enum task_work_notify_mode notify;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002123 struct io_wq_work_node *node;
Jens Axboe0b81e802021-02-16 10:33:53 -07002124 unsigned long flags;
Pavel Begunkov6294f362021-08-10 17:53:55 +01002125 bool running;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002126
2127 WARN_ON_ONCE(!tctx);
2128
Jens Axboe0b81e802021-02-16 10:33:53 -07002129 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002130 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002131 running = tctx->task_running;
2132 if (!running)
2133 tctx->task_running = true;
Jens Axboe0b81e802021-02-16 10:33:53 -07002134 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002135
2136 /* task_work already pending, we're done */
Pavel Begunkov6294f362021-08-10 17:53:55 +01002137 if (running)
Pavel Begunkove09ee512021-07-01 13:26:05 +01002138 return;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002139
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002140 /*
2141 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2142 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2143 * processing task_work. There's no reliable way to tell if TWA_RESUME
2144 * will do the job.
2145 */
2146 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002147 if (!task_work_add(tsk, &tctx->task_work, notify)) {
2148 wake_up_process(tsk);
Pavel Begunkove09ee512021-07-01 13:26:05 +01002149 return;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00002150 }
Pavel Begunkov2215bed2021-08-09 13:04:06 +01002151
Pavel Begunkove09ee512021-07-01 13:26:05 +01002152 spin_lock_irqsave(&tctx->task_lock, flags);
Pavel Begunkov6294f362021-08-10 17:53:55 +01002153 tctx->task_running = false;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002154 node = tctx->task_list.first;
2155 INIT_WQ_LIST(&tctx->task_list);
2156 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002157
Pavel Begunkove09ee512021-07-01 13:26:05 +01002158 while (node) {
2159 req = container_of(node, struct io_kiocb, io_task_work.node);
2160 node = node->next;
2161 if (llist_add(&req->io_task_work.fallback_node,
2162 &req->ctx->fallback_llist))
2163 schedule_delayed_work(&req->ctx->fallback_work, 1);
2164 }
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002165}
2166
Pavel Begunkovf237c302021-08-18 12:42:46 +01002167static void io_req_task_cancel(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002168{
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002169 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002170
Pavel Begunkove83acd72021-02-28 22:35:09 +00002171 /* ctx is guaranteed to stay alive while we hold uring_lock */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002172 io_tw_lock(ctx, locked);
Pavel Begunkov25935532021-03-19 17:22:40 +00002173 io_req_complete_failed(req, req->result);
Jens Axboec40f6372020-06-25 15:39:59 -06002174}
2175
Pavel Begunkovf237c302021-08-18 12:42:46 +01002176static void io_req_task_submit(struct io_kiocb *req, bool *locked)
Jens Axboec40f6372020-06-25 15:39:59 -06002177{
2178 struct io_ring_ctx *ctx = req->ctx;
2179
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002180 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkovf237c302021-08-18 12:42:46 +01002181 io_tw_lock(ctx, locked);
Jens Axboe316319e2021-08-19 09:41:42 -06002182 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkovaf066f32021-08-09 13:04:19 +01002183 if (likely(!(req->task->flags & PF_EXITING)))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002184 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002185 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002186 io_req_complete_failed(req, -EFAULT);
Jens Axboe9e645e112019-05-10 16:07:28 -06002187}
2188
Pavel Begunkova3df76982021-02-18 22:32:52 +00002189static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2190{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002191 req->result = ret;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002192 req->io_task_work.func = io_req_task_cancel;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002193 io_req_task_work_add(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002194}
2195
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002196static void io_req_task_queue(struct io_kiocb *req)
2197{
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01002198 req->io_task_work.func = io_req_task_submit;
Pavel Begunkove09ee512021-07-01 13:26:05 +01002199 io_req_task_work_add(req);
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002200}
2201
Jens Axboe773af692021-07-27 10:25:55 -06002202static void io_req_task_queue_reissue(struct io_kiocb *req)
2203{
2204 req->io_task_work.func = io_queue_async_work;
2205 io_req_task_work_add(req);
2206}
2207
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002208static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002209{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002210 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002211
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002212 if (nxt)
2213 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002214}
2215
Jens Axboe9e645e112019-05-10 16:07:28 -06002216static void io_free_req(struct io_kiocb *req)
2217{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002218 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002219 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002220}
2221
Pavel Begunkovf237c302021-08-18 12:42:46 +01002222static void io_free_req_work(struct io_kiocb *req, bool *locked)
2223{
2224 io_free_req(req);
2225}
2226
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002227struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002228 struct task_struct *task;
2229 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002230 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002231};
2232
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002233static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002234{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002235 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002236 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002237 rb->task = NULL;
2238}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002239
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002240static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2241 struct req_batch *rb)
2242{
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002243 if (rb->ctx_refs)
2244 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkove98e49b2021-08-18 17:01:43 +01002245 if (rb->task)
Pavel Begunkove9dbe222021-08-09 13:04:20 +01002246 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002247}
2248
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002249static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2250 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002251{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002252 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002253 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002254
Jens Axboee3bc8e92020-09-24 08:45:57 -06002255 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002256 if (rb->task)
2257 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002258 rb->task = req->task;
2259 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002260 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002261 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002262 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002263
Pavel Begunkovbd759042021-02-12 03:23:50 +00002264 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002265 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002266 else
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002267 list_add(&req->inflight_entry, &state->free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002268}
2269
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01002270static void io_submit_flush_completions(struct io_ring_ctx *ctx)
Jens Axboea141dd82021-08-12 12:48:34 -06002271 __must_hold(&ctx->uring_lock)
Pavel Begunkov905c1722021-02-10 00:03:14 +00002272{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002273 struct io_submit_state *state = &ctx->submit_state;
2274 int i, nr = state->compl_nr;
Pavel Begunkov905c1722021-02-10 00:03:14 +00002275 struct req_batch rb;
2276
Jens Axboe79ebeae2021-08-10 15:18:27 -06002277 spin_lock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002278 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002279 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002280
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002281 __io_cqring_fill_event(ctx, req->user_data, req->result,
2282 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002283 }
2284 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06002285 spin_unlock(&ctx->completion_lock);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002286 io_cqring_ev_posted(ctx);
Pavel Begunkov5182ed22021-06-26 21:40:48 +01002287
2288 io_init_req_batch(&rb);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002289 for (i = 0; i < nr; i++) {
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002290 struct io_kiocb *req = state->compl_reqs[i];
Pavel Begunkov905c1722021-02-10 00:03:14 +00002291
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002292 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002293 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002294 }
2295
2296 io_req_free_batch_finish(ctx, &rb);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01002297 state->compl_nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002298}
2299
Jens Axboeba816ad2019-09-28 11:36:45 -06002300/*
2301 * Drop reference to request, return next in chain (if there is one) if this
2302 * was the last reference to this request.
2303 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002304static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002305{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002306 struct io_kiocb *nxt = NULL;
2307
Jens Axboede9b4cc2021-02-24 13:28:27 -07002308 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002309 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002310 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002311 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002312 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002313}
2314
Pavel Begunkov0d850352021-03-19 17:22:37 +00002315static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002317 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002318 io_free_req(req);
2319}
2320
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002321static inline void io_put_req_deferred(struct io_kiocb *req)
Pavel Begunkov216578e2020-10-13 09:44:00 +01002322{
Pavel Begunkov91c2f692021-08-11 19:28:28 +01002323 if (req_ref_put_and_test(req)) {
Pavel Begunkovf237c302021-08-18 12:42:46 +01002324 req->io_task_work.func = io_free_req_work;
Pavel Begunkov543af3a2021-08-09 13:04:15 +01002325 io_req_task_work_add(req);
2326 }
Pavel Begunkov216578e2020-10-13 09:44:00 +01002327}
2328
Pavel Begunkov6c503152021-01-04 20:36:36 +00002329static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002330{
2331 /* See comment at the top of this file */
2332 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002333 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002334}
2335
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002336static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2337{
2338 struct io_rings *rings = ctx->rings;
2339
2340 /* make sure SQ entry isn't read before tail */
2341 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2342}
2343
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002344static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002345{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002346 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002347
Jens Axboebcda7ba2020-02-23 16:42:51 -07002348 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2349 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002350 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002351 kfree(kbuf);
2352 return cflags;
2353}
2354
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002355static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2356{
2357 struct io_buffer *kbuf;
2358
Pavel Begunkovae421d92021-08-17 20:28:08 +01002359 if (likely(!(req->flags & REQ_F_BUFFER_SELECTED)))
2360 return 0;
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002361 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2362 return io_put_kbuf(req, kbuf);
2363}
2364
Jens Axboe4c6e2772020-07-01 11:29:10 -06002365static inline bool io_run_task_work(void)
2366{
Nadav Amitef98eb02021-08-07 17:13:41 -07002367 if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
Jens Axboe4c6e2772020-07-01 11:29:10 -06002368 __set_current_state(TASK_RUNNING);
Nadav Amitef98eb02021-08-07 17:13:41 -07002369 tracehook_notify_signal();
Jens Axboe4c6e2772020-07-01 11:29:10 -06002370 return true;
2371 }
2372
2373 return false;
2374}
2375
Jens Axboedef596e2019-01-09 08:59:42 -07002376/*
2377 * Find and free completed poll iocbs
2378 */
2379static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002380 struct list_head *done)
Jens Axboedef596e2019-01-09 08:59:42 -07002381{
Jens Axboe8237e042019-12-28 10:48:22 -07002382 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002383 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002384
2385 /* order with ->result store in io_complete_rw_iopoll() */
2386 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002387
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002388 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002389 while (!list_empty(done)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002390 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002391 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002392
Pavel Begunkova8576af2021-08-15 10:40:21 +01002393 if (READ_ONCE(req->result) == -EAGAIN &&
Pavel Begunkov8c130822021-03-22 01:58:32 +00002394 !(req->flags & REQ_F_DONT_REISSUE)) {
Pavel Begunkovf1613402021-02-11 18:28:21 +00002395 req->iopoll_completed = 0;
Jens Axboe773af692021-07-27 10:25:55 -06002396 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002397 continue;
Pavel Begunkovf1613402021-02-11 18:28:21 +00002398 }
2399
Pavel Begunkovae421d92021-08-17 20:28:08 +01002400 __io_cqring_fill_event(ctx, req->user_data, req->result,
2401 io_put_rw_kbuf(req));
Jens Axboedef596e2019-01-09 08:59:42 -07002402 (*nr_events)++;
2403
Jens Axboede9b4cc2021-02-24 13:28:27 -07002404 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002405 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002406 }
Jens Axboedef596e2019-01-09 08:59:42 -07002407
Jens Axboe09bb8392019-03-13 12:39:28 -06002408 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002409 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002410 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002411}
2412
Jens Axboedef596e2019-01-09 08:59:42 -07002413static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
Pavel Begunkova8576af2021-08-15 10:40:21 +01002414 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002415{
2416 struct io_kiocb *req, *tmp;
2417 LIST_HEAD(done);
2418 bool spin;
Jens Axboedef596e2019-01-09 08:59:42 -07002419
2420 /*
2421 * Only spin for completions if we don't have multiple devices hanging
2422 * off our complete list, and we're under the requested amount.
2423 */
Hao Xu915b3dd2021-06-28 05:37:30 +08002424 spin = !ctx->poll_multi_queue && *nr_events < min;
Jens Axboedef596e2019-01-09 08:59:42 -07002425
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002426 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002427 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkova2416e12021-08-09 13:04:09 +01002428 int ret;
Jens Axboedef596e2019-01-09 08:59:42 -07002429
2430 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002431 * Move completed and retryable entries to our local lists.
2432 * If we find a request that requires polling, break out
2433 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002434 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002435 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002436 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002437 continue;
2438 }
2439 if (!list_empty(&done))
2440 break;
2441
2442 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
Pavel Begunkova2416e12021-08-09 13:04:09 +01002443 if (unlikely(ret < 0))
2444 return ret;
2445 else if (ret)
2446 spin = false;
Jens Axboedef596e2019-01-09 08:59:42 -07002447
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002448 /* iopoll may have completed current req */
2449 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002450 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002451 }
2452
2453 if (!list_empty(&done))
Pavel Begunkova8576af2021-08-15 10:40:21 +01002454 io_iopoll_complete(ctx, nr_events, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002455
Pavel Begunkova2416e12021-08-09 13:04:09 +01002456 return 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002457}
2458
2459/*
Jens Axboedef596e2019-01-09 08:59:42 -07002460 * We can't just wait for polled events to come to us, we have to actively
2461 * find and complete them.
2462 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002463static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002464{
2465 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2466 return;
2467
2468 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002469 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002470 unsigned int nr_events = 0;
2471
Pavel Begunkova8576af2021-08-15 10:40:21 +01002472 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002473
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002474 /* let it sleep and repeat later if can't complete a request */
2475 if (nr_events == 0)
2476 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002477 /*
2478 * Ensure we allow local-to-the-cpu processing to take place,
2479 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002480 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002481 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002482 if (need_resched()) {
2483 mutex_unlock(&ctx->uring_lock);
2484 cond_resched();
2485 mutex_lock(&ctx->uring_lock);
2486 }
Jens Axboedef596e2019-01-09 08:59:42 -07002487 }
2488 mutex_unlock(&ctx->uring_lock);
2489}
2490
Pavel Begunkov7668b922020-07-07 16:36:21 +03002491static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002492{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002493 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002494 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002495
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002496 /*
2497 * We disallow the app entering submit/complete with polling, but we
2498 * still need to lock the ring to prevent racing with polled issue
2499 * that got punted to a workqueue.
2500 */
2501 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002502 /*
2503 * Don't enter poll loop if we already have events pending.
2504 * If we do, we can potentially be spinning for commands that
2505 * already triggered a CQE (eg in error).
2506 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01002507 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002508 __io_cqring_overflow_flush(ctx, false);
2509 if (io_cqring_events(ctx))
2510 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002511 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002512 /*
2513 * If a submit got punted to a workqueue, we can have the
2514 * application entering polling for a command before it gets
2515 * issued. That app will hold the uring_lock for the duration
2516 * of the poll right here, so we need to take a breather every
2517 * now and then to ensure that the issue has a chance to add
2518 * the poll to the issued list. Otherwise we can spin here
2519 * forever, while the workqueue is stuck trying to acquire the
2520 * very same mutex.
2521 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002522 if (list_empty(&ctx->iopoll_list)) {
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002523 u32 tail = ctx->cached_cq_tail;
2524
Jens Axboe500f9fb2019-08-19 12:15:59 -06002525 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002526 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002527 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002528
Pavel Begunkov8f487ef2021-07-08 13:37:06 +01002529 /* some requests don't go through iopoll_list */
2530 if (tail != ctx->cached_cq_tail ||
2531 list_empty(&ctx->iopoll_list))
Pavel Begunkove9979b32021-04-13 02:58:45 +01002532 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002533 }
Pavel Begunkova8576af2021-08-15 10:40:21 +01002534 ret = io_do_iopoll(ctx, &nr_events, min);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002535 } while (!ret && nr_events < min && !need_resched());
2536out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002537 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002538 return ret;
2539}
2540
Jens Axboe491381ce2019-10-17 09:20:46 -06002541static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002542{
Jens Axboe491381ce2019-10-17 09:20:46 -06002543 /*
2544 * Tell lockdep we inherited freeze protection from submission
2545 * thread.
2546 */
2547 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002548 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002549
Pavel Begunkov1c986792021-03-22 01:58:31 +00002550 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2551 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552 }
2553}
2554
Jens Axboeb63534c2020-06-04 11:28:00 -06002555#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002556static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002557{
Pavel Begunkovab454432021-03-22 01:58:33 +00002558 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002559
Pavel Begunkovab454432021-03-22 01:58:33 +00002560 if (!rw)
2561 return !io_req_prep_async(req);
2562 /* may have left rw->iter inconsistent on -EIOCBQUEUED */
2563 iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter));
2564 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002565}
Jens Axboeb63534c2020-06-04 11:28:00 -06002566
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002567static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002568{
Jens Axboe355afae2020-09-02 09:30:31 -06002569 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002571
Jens Axboe355afae2020-09-02 09:30:31 -06002572 if (!S_ISBLK(mode) && !S_ISREG(mode))
2573 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002574 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2575 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002576 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002577 /*
2578 * If ref is dying, we might be running poll reap from the exit work.
2579 * Don't attempt to reissue from that path, just let it fail with
2580 * -EAGAIN.
2581 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002582 if (percpu_ref_is_dying(&ctx->refs))
2583 return false;
Jens Axboeef046882021-07-27 10:50:31 -06002584 /*
2585 * Play it safe and assume not safe to re-import and reissue if we're
2586 * not in the original thread group (or in task context).
2587 */
2588 if (!same_thread_group(req->task, current) || !in_task())
2589 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002590 return true;
2591}
Jens Axboee82ad482021-04-02 19:45:34 -06002592#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002593static bool io_resubmit_prep(struct io_kiocb *req)
2594{
2595 return false;
2596}
Jens Axboee82ad482021-04-02 19:45:34 -06002597static bool io_rw_should_reissue(struct io_kiocb *req)
2598{
2599 return false;
2600}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002601#endif
2602
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002603static bool __io_complete_rw_common(struct io_kiocb *req, long res)
Jens Axboea1d7c392020-06-22 11:09:46 -06002604{
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002605 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2606 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002607 if (res != req->result) {
2608 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2609 io_rw_should_reissue(req)) {
2610 req->flags |= REQ_F_REISSUE;
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002611 return true;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002612 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002613 req_set_fail(req);
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002614 req->result = res;
Pavel Begunkov9532b992021-03-22 01:58:34 +00002615 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002616 return false;
2617}
2618
Pavel Begunkovf237c302021-08-18 12:42:46 +01002619static void io_req_task_complete(struct io_kiocb *req, bool *locked)
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002620{
Pavel Begunkov126180b2021-08-18 12:42:47 +01002621 unsigned int cflags = io_put_rw_kbuf(req);
2622 long res = req->result;
2623
2624 if (*locked) {
2625 struct io_ring_ctx *ctx = req->ctx;
2626 struct io_submit_state *state = &ctx->submit_state;
2627
2628 io_req_complete_state(req, res, cflags);
2629 state->compl_reqs[state->compl_nr++] = req;
2630 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
2631 io_submit_flush_completions(ctx);
2632 } else {
2633 io_req_complete_post(req, res, cflags);
2634 }
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002635}
2636
2637static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
2638 unsigned int issue_flags)
2639{
2640 if (__io_complete_rw_common(req, res))
2641 return;
Pavel Begunkovf237c302021-08-18 12:42:46 +01002642 __io_req_complete(req, 0, req->result, io_put_rw_kbuf(req));
Jens Axboeba816ad2019-09-28 11:36:45 -06002643}
2644
2645static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2646{
Jens Axboe9adbd452019-12-20 08:45:55 -07002647 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002648
Jens Axboe8ef12ef2021-08-10 15:15:25 -06002649 if (__io_complete_rw_common(req, res))
2650 return;
2651 req->result = res;
2652 req->io_task_work.func = io_req_task_complete;
2653 io_req_task_work_add(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654}
2655
Jens Axboedef596e2019-01-09 08:59:42 -07002656static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2657{
Jens Axboe9adbd452019-12-20 08:45:55 -07002658 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002659
Jens Axboe491381ce2019-10-17 09:20:46 -06002660 if (kiocb->ki_flags & IOCB_WRITE)
2661 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002662 if (unlikely(res != req->result)) {
Jens Axboea1ff1e32021-04-12 06:40:02 -06002663 if (!(res == -EAGAIN && io_rw_should_reissue(req) &&
2664 io_resubmit_prep(req))) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002665 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002666 req->flags |= REQ_F_DONT_REISSUE;
2667 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002668 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002669
2670 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002671 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002672 smp_wmb();
2673 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002674}
2675
2676/*
2677 * After the iocb has been issued, it's safe to be found on the poll list.
2678 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002679 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002680 * accessing the kiocb cookie.
2681 */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002682static void io_iopoll_req_issued(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07002683{
2684 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002685 const bool in_async = io_wq_current_is_worker();
2686
2687 /* workqueue context doesn't hold uring_lock, grab it now */
2688 if (unlikely(in_async))
2689 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002690
2691 /*
2692 * Track whether we have multiple files in our lists. This will impact
2693 * how we do polling eventually, not spinning if we're on potentially
2694 * different devices.
2695 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002696 if (list_empty(&ctx->iopoll_list)) {
Hao Xu915b3dd2021-06-28 05:37:30 +08002697 ctx->poll_multi_queue = false;
2698 } else if (!ctx->poll_multi_queue) {
Jens Axboedef596e2019-01-09 08:59:42 -07002699 struct io_kiocb *list_req;
Hao Xu915b3dd2021-06-28 05:37:30 +08002700 unsigned int queue_num0, queue_num1;
Jens Axboedef596e2019-01-09 08:59:42 -07002701
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002702 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002703 inflight_entry);
Hao Xu915b3dd2021-06-28 05:37:30 +08002704
2705 if (list_req->file != req->file) {
2706 ctx->poll_multi_queue = true;
2707 } else {
2708 queue_num0 = blk_qc_t_to_queue_num(list_req->rw.kiocb.ki_cookie);
2709 queue_num1 = blk_qc_t_to_queue_num(req->rw.kiocb.ki_cookie);
2710 if (queue_num0 != queue_num1)
2711 ctx->poll_multi_queue = true;
2712 }
Jens Axboedef596e2019-01-09 08:59:42 -07002713 }
2714
2715 /*
2716 * For fast devices, IO may have already completed. If it has, add
2717 * it to the front so we find it first.
2718 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002719 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002720 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002721 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002722 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002723
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01002724 if (unlikely(in_async)) {
2725 /*
2726 * If IORING_SETUP_SQPOLL is enabled, sqes are either handle
2727 * in sq thread task context or in io worker task context. If
2728 * current task context is sq thread, we don't need to check
2729 * whether should wake up sq thread.
2730 */
2731 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
2732 wq_has_sleeper(&ctx->sq_data->wait))
2733 wake_up(&ctx->sq_data->wait);
2734
2735 mutex_unlock(&ctx->uring_lock);
2736 }
Jens Axboedef596e2019-01-09 08:59:42 -07002737}
2738
Jens Axboe4503b762020-06-01 10:00:27 -06002739static bool io_bdev_nowait(struct block_device *bdev)
2740{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002741 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002742}
2743
Jens Axboe2b188cc2019-01-07 10:46:33 -07002744/*
2745 * If we tracked the file through the SCM inflight mechanism, we could support
2746 * any file. For now, just ensure that anything potentially problematic is done
2747 * inline.
2748 */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002749static bool __io_file_supports_nowait(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002750{
2751 umode_t mode = file_inode(file)->i_mode;
2752
Jens Axboe4503b762020-06-01 10:00:27 -06002753 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002754 if (IS_ENABLED(CONFIG_BLOCK) &&
2755 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002756 return true;
2757 return false;
2758 }
Pavel Begunkov976517f2021-06-09 12:07:25 +01002759 if (S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002761 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002762 if (IS_ENABLED(CONFIG_BLOCK) &&
2763 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002764 file->f_op != &io_uring_fops)
2765 return true;
2766 return false;
2767 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768
Jens Axboec5b85622020-06-09 19:23:05 -06002769 /* any ->read/write should understand O_NONBLOCK */
2770 if (file->f_flags & O_NONBLOCK)
2771 return true;
2772
Jens Axboeaf197f52020-04-28 13:15:06 -06002773 if (!(file->f_mode & FMODE_NOWAIT))
2774 return false;
2775
2776 if (rw == READ)
2777 return file->f_op->read_iter != NULL;
2778
2779 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002780}
2781
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002782static bool io_file_supports_nowait(struct io_kiocb *req, int rw)
Jens Axboe7b29f922021-03-12 08:30:14 -07002783{
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002784 if (rw == READ && (req->flags & REQ_F_NOWAIT_READ))
Jens Axboe7b29f922021-03-12 08:30:14 -07002785 return true;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002786 else if (rw == WRITE && (req->flags & REQ_F_NOWAIT_WRITE))
Jens Axboe7b29f922021-03-12 08:30:14 -07002787 return true;
2788
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01002789 return __io_file_supports_nowait(req->file, rw);
Jens Axboe7b29f922021-03-12 08:30:14 -07002790}
2791
Pavel Begunkova88fc402020-09-30 22:57:53 +03002792static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002793{
Jens Axboedef596e2019-01-09 08:59:42 -07002794 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002795 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002796 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002797 unsigned ioprio;
2798 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002799
Pavel Begunkovc97d8a02021-08-09 13:04:04 +01002800 if (!io_req_ffs_set(req) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002801 req->flags |= REQ_F_ISREG;
2802
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002804 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002805 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002806 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002807 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002809 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2810 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2811 if (unlikely(ret))
2812 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002813
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002814 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2815 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2816 req->flags |= REQ_F_NOWAIT;
2817
Jens Axboe2b188cc2019-01-07 10:46:33 -07002818 ioprio = READ_ONCE(sqe->ioprio);
2819 if (ioprio) {
2820 ret = ioprio_check_cap(ioprio);
2821 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002822 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002823
2824 kiocb->ki_ioprio = ioprio;
2825 } else
2826 kiocb->ki_ioprio = get_current_ioprio();
2827
Jens Axboedef596e2019-01-09 08:59:42 -07002828 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002829 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2830 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002831 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832
Jens Axboedef596e2019-01-09 08:59:42 -07002833 kiocb->ki_flags |= IOCB_HIPRI;
2834 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002835 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002836 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002837 if (kiocb->ki_flags & IOCB_HIPRI)
2838 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002839 kiocb->ki_complete = io_complete_rw;
2840 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002841
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002842 if (req->opcode == IORING_OP_READ_FIXED ||
2843 req->opcode == IORING_OP_WRITE_FIXED) {
2844 req->imu = NULL;
2845 io_req_set_rsrc_node(req);
2846 }
2847
Jens Axboe3529d8c2019-12-19 18:24:38 -07002848 req->rw.addr = READ_ONCE(sqe->addr);
2849 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002850 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002851 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852}
2853
2854static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2855{
2856 switch (ret) {
2857 case -EIOCBQUEUED:
2858 break;
2859 case -ERESTARTSYS:
2860 case -ERESTARTNOINTR:
2861 case -ERESTARTNOHAND:
2862 case -ERESTART_RESTARTBLOCK:
2863 /*
2864 * We can't just restart the syscall, since previously
2865 * submitted sqes may already be in progress. Just fail this
2866 * IO with EINTR.
2867 */
2868 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002869 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870 default:
2871 kiocb->ki_complete(kiocb, ret, 0);
2872 }
2873}
2874
Jens Axboea1d7c392020-06-22 11:09:46 -06002875static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002876 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002877{
Jens Axboeba042912019-12-25 16:33:42 -07002878 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002879 struct io_async_rw *io = req->async_data;
Pavel Begunkov97284632021-04-08 19:28:03 +01002880 bool check_reissue = kiocb->ki_complete == io_complete_rw;
Jens Axboeba042912019-12-25 16:33:42 -07002881
Jens Axboe227c0c92020-08-13 11:51:40 -06002882 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002883 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002884 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002885 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002886 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002887 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002888 }
2889
Jens Axboeba042912019-12-25 16:33:42 -07002890 if (req->flags & REQ_F_CUR_POS)
2891 req->file->f_pos = kiocb->ki_pos;
Hao Xue149bd742021-06-28 05:48:05 +08002892 if (ret >= 0 && check_reissue)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002893 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002894 else
2895 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002896
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01002897 if (check_reissue && (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov97284632021-04-08 19:28:03 +01002898 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002899 if (io_resubmit_prep(req)) {
Jens Axboe773af692021-07-27 10:25:55 -06002900 io_req_task_queue_reissue(req);
Pavel Begunkov8c130822021-03-22 01:58:32 +00002901 } else {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002902 req_set_fail(req);
Pavel Begunkovae421d92021-08-17 20:28:08 +01002903 __io_req_complete(req, issue_flags, ret,
2904 io_put_rw_kbuf(req));
Pavel Begunkov97284632021-04-08 19:28:03 +01002905 }
2906 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002907}
2908
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002909static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2910 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002911{
Jens Axboe9adbd452019-12-20 08:45:55 -07002912 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002913 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002914 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002915
Pavel Begunkov75769e32021-04-01 15:43:54 +01002916 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002917 return -EFAULT;
2918 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002919 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002920 return -EFAULT;
2921
2922 /*
2923 * May not be a start of buffer, set size appropriately
2924 * and advance us to the beginning.
2925 */
2926 offset = buf_addr - imu->ubuf;
2927 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002928
2929 if (offset) {
2930 /*
2931 * Don't use iov_iter_advance() here, as it's really slow for
2932 * using the latter parts of a big fixed buffer - it iterates
2933 * over each segment manually. We can cheat a bit here, because
2934 * we know that:
2935 *
2936 * 1) it's a BVEC iter, we set it up
2937 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2938 * first and last bvec
2939 *
2940 * So just find our index, and adjust the iterator afterwards.
2941 * If the offset is within the first bvec (or the whole first
2942 * bvec, just use iov_iter_advance(). This makes it easier
2943 * since we can just skip the first segment, which may not
2944 * be PAGE_SIZE aligned.
2945 */
2946 const struct bio_vec *bvec = imu->bvec;
2947
2948 if (offset <= bvec->bv_len) {
2949 iov_iter_advance(iter, offset);
2950 } else {
2951 unsigned long seg_skip;
2952
2953 /* skip first vec */
2954 offset -= bvec->bv_len;
2955 seg_skip = 1 + (offset >> PAGE_SHIFT);
2956
2957 iter->bvec = bvec + seg_skip;
2958 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002959 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002960 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002961 }
2962 }
2963
Pavel Begunkov847595d2021-02-04 13:52:06 +00002964 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002965}
2966
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002967static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
2968{
2969 struct io_ring_ctx *ctx = req->ctx;
2970 struct io_mapped_ubuf *imu = req->imu;
2971 u16 index, buf_index = req->buf_index;
2972
2973 if (likely(!imu)) {
2974 if (unlikely(buf_index >= ctx->nr_user_bufs))
2975 return -EFAULT;
2976 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2977 imu = READ_ONCE(ctx->user_bufs[index]);
2978 req->imu = imu;
2979 }
2980 return __io_import_fixed(req, rw, iter, imu);
2981}
2982
Jens Axboebcda7ba2020-02-23 16:42:51 -07002983static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2984{
2985 if (needs_lock)
2986 mutex_unlock(&ctx->uring_lock);
2987}
2988
2989static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2990{
2991 /*
2992 * "Normal" inline submissions always hold the uring_lock, since we
2993 * grab it from the system call. Same is true for the SQPOLL offload.
2994 * The only exception is when we've detached the request and issue it
2995 * from an async worker thread, grab the lock for that case.
2996 */
2997 if (needs_lock)
2998 mutex_lock(&ctx->uring_lock);
2999}
3000
3001static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3002 int bgid, struct io_buffer *kbuf,
3003 bool needs_lock)
3004{
3005 struct io_buffer *head;
3006
3007 if (req->flags & REQ_F_BUFFER_SELECTED)
3008 return kbuf;
3009
3010 io_ring_submit_lock(req->ctx, needs_lock);
3011
3012 lockdep_assert_held(&req->ctx->uring_lock);
3013
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003014 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003015 if (head) {
3016 if (!list_empty(&head->list)) {
3017 kbuf = list_last_entry(&head->list, struct io_buffer,
3018 list);
3019 list_del(&kbuf->list);
3020 } else {
3021 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003022 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07003023 }
3024 if (*len > kbuf->len)
3025 *len = kbuf->len;
3026 } else {
3027 kbuf = ERR_PTR(-ENOBUFS);
3028 }
3029
3030 io_ring_submit_unlock(req->ctx, needs_lock);
3031
3032 return kbuf;
3033}
3034
Jens Axboe4d954c22020-02-27 07:31:19 -07003035static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3036 bool needs_lock)
3037{
3038 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003039 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003040
3041 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003042 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003043 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3044 if (IS_ERR(kbuf))
3045 return kbuf;
3046 req->rw.addr = (u64) (unsigned long) kbuf;
3047 req->flags |= REQ_F_BUFFER_SELECTED;
3048 return u64_to_user_ptr(kbuf->addr);
3049}
3050
3051#ifdef CONFIG_COMPAT
3052static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3053 bool needs_lock)
3054{
3055 struct compat_iovec __user *uiov;
3056 compat_ssize_t clen;
3057 void __user *buf;
3058 ssize_t len;
3059
3060 uiov = u64_to_user_ptr(req->rw.addr);
3061 if (!access_ok(uiov, sizeof(*uiov)))
3062 return -EFAULT;
3063 if (__get_user(clen, &uiov->iov_len))
3064 return -EFAULT;
3065 if (clen < 0)
3066 return -EINVAL;
3067
3068 len = clen;
3069 buf = io_rw_buffer_select(req, &len, needs_lock);
3070 if (IS_ERR(buf))
3071 return PTR_ERR(buf);
3072 iov[0].iov_base = buf;
3073 iov[0].iov_len = (compat_size_t) len;
3074 return 0;
3075}
3076#endif
3077
3078static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3079 bool needs_lock)
3080{
3081 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3082 void __user *buf;
3083 ssize_t len;
3084
3085 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3086 return -EFAULT;
3087
3088 len = iov[0].iov_len;
3089 if (len < 0)
3090 return -EINVAL;
3091 buf = io_rw_buffer_select(req, &len, needs_lock);
3092 if (IS_ERR(buf))
3093 return PTR_ERR(buf);
3094 iov[0].iov_base = buf;
3095 iov[0].iov_len = len;
3096 return 0;
3097}
3098
3099static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3100 bool needs_lock)
3101{
Jens Axboedddb3e22020-06-04 11:27:01 -06003102 if (req->flags & REQ_F_BUFFER_SELECTED) {
3103 struct io_buffer *kbuf;
3104
3105 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3106 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3107 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003108 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003109 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003110 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003111 return -EINVAL;
3112
3113#ifdef CONFIG_COMPAT
3114 if (req->ctx->compat)
3115 return io_compat_import(req, iov, needs_lock);
3116#endif
3117
3118 return __io_iov_buffer_select(req, iov, needs_lock);
3119}
3120
Pavel Begunkov847595d2021-02-04 13:52:06 +00003121static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3122 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003123{
Jens Axboe9adbd452019-12-20 08:45:55 -07003124 void __user *buf = u64_to_user_ptr(req->rw.addr);
3125 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003126 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003127 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003128
Pavel Begunkov7d009162019-11-25 23:14:40 +03003129 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003130 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003131 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003132 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003133
Jens Axboebcda7ba2020-02-23 16:42:51 -07003134 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003135 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003136 return -EINVAL;
3137
Jens Axboe3a6820f2019-12-22 15:19:35 -07003138 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003139 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003140 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003141 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003142 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003143 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003144 }
3145
Jens Axboe3a6820f2019-12-22 15:19:35 -07003146 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3147 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003148 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003149 }
3150
Jens Axboe4d954c22020-02-27 07:31:19 -07003151 if (req->flags & REQ_F_BUFFER_SELECT) {
3152 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003153 if (!ret)
3154 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003155 *iovec = NULL;
3156 return ret;
3157 }
3158
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003159 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3160 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003161}
3162
Jens Axboe0fef9482020-08-26 10:36:20 -06003163static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3164{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003165 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003166}
3167
Jens Axboe32960612019-09-23 11:05:34 -06003168/*
3169 * For files that don't have ->read_iter() and ->write_iter(), handle them
3170 * by looping over ->read() or ->write() manually.
3171 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003172static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003173{
Jens Axboe4017eb92020-10-22 14:14:12 -06003174 struct kiocb *kiocb = &req->rw.kiocb;
3175 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003176 ssize_t ret = 0;
3177
3178 /*
3179 * Don't support polled IO through this interface, and we can't
3180 * support non-blocking either. For the latter, this just causes
3181 * the kiocb to be handled from an async context.
3182 */
3183 if (kiocb->ki_flags & IOCB_HIPRI)
3184 return -EOPNOTSUPP;
3185 if (kiocb->ki_flags & IOCB_NOWAIT)
3186 return -EAGAIN;
3187
3188 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003189 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003190 ssize_t nr;
3191
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003192 if (!iov_iter_is_bvec(iter)) {
3193 iovec = iov_iter_iovec(iter);
3194 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003195 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3196 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003197 }
3198
Jens Axboe32960612019-09-23 11:05:34 -06003199 if (rw == READ) {
3200 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003201 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003202 } else {
3203 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003204 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003205 }
3206
3207 if (nr < 0) {
3208 if (!ret)
3209 ret = nr;
3210 break;
3211 }
3212 ret += nr;
3213 if (nr != iovec.iov_len)
3214 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003215 req->rw.len -= nr;
3216 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003217 iov_iter_advance(iter, nr);
3218 }
3219
3220 return ret;
3221}
3222
Jens Axboeff6165b2020-08-13 09:47:43 -06003223static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3224 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003225{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003226 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003227
Jens Axboeff6165b2020-08-13 09:47:43 -06003228 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003229 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003230 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003231 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003232 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003233 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003234 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003235 unsigned iov_off = 0;
3236
3237 rw->iter.iov = rw->fast_iov;
3238 if (iter->iov != fast_iov) {
3239 iov_off = iter->iov - fast_iov;
3240 rw->iter.iov += iov_off;
3241 }
3242 if (rw->fast_iov != fast_iov)
3243 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003244 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003245 } else {
3246 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003247 }
3248}
3249
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003250static inline int io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003251{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003252 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3253 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3254 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003255}
3256
Jens Axboeff6165b2020-08-13 09:47:43 -06003257static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3258 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003259 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003260{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003261 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003262 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003263 if (!req->async_data) {
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003264 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003265 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003266 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003267 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003268
Jens Axboeff6165b2020-08-13 09:47:43 -06003269 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003270 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003271 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003272}
3273
Pavel Begunkov73debe62020-09-30 22:57:54 +03003274static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003275{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003276 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003277 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003278 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003279
Pavel Begunkov2846c482020-11-07 13:16:27 +00003280 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003281 if (unlikely(ret < 0))
3282 return ret;
3283
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003284 iorw->bytes_done = 0;
3285 iorw->free_iovec = iov;
3286 if (iov)
3287 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003288 return 0;
3289}
3290
Pavel Begunkov73debe62020-09-30 22:57:54 +03003291static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003292{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003293 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3294 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003295 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003296}
3297
Jens Axboec1dd91d2020-08-03 16:43:59 -06003298/*
3299 * This is our waitqueue callback handler, registered through lock_page_async()
3300 * when we initially tried to do the IO with the iocb armed our waitqueue.
3301 * This gets called when the page is unlocked, and we generally expect that to
3302 * happen when the page IO is completed and the page is now uptodate. This will
3303 * queue a task_work based retry of the operation, attempting to copy the data
3304 * again. If the latter fails because the page was NOT uptodate, then we will
3305 * do a thread based blocking retry of the operation. That's the unexpected
3306 * slow path.
3307 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003308static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3309 int sync, void *arg)
3310{
3311 struct wait_page_queue *wpq;
3312 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003313 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003314
3315 wpq = container_of(wait, struct wait_page_queue, wait);
3316
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003317 if (!wake_page_match(wpq, key))
3318 return 0;
3319
Hao Xuc8d317a2020-09-29 20:00:45 +08003320 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003321 list_del_init(&wait->entry);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003322 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003323 return 1;
3324}
3325
Jens Axboec1dd91d2020-08-03 16:43:59 -06003326/*
3327 * This controls whether a given IO request should be armed for async page
3328 * based retry. If we return false here, the request is handed to the async
3329 * worker threads for retry. If we're doing buffered reads on a regular file,
3330 * we prepare a private wait_page_queue entry and retry the operation. This
3331 * will either succeed because the page is now uptodate and unlocked, or it
3332 * will register a callback when the page is unlocked at IO completion. Through
3333 * that callback, io_uring uses task_work to setup a retry of the operation.
3334 * That retry will attempt the buffered read again. The retry will generally
3335 * succeed, or in rare cases where it fails, we then fall back to using the
3336 * async worker threads for a blocking retry.
3337 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003338static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003339{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003340 struct io_async_rw *rw = req->async_data;
3341 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003342 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003343
3344 /* never retry for NOWAIT, we just complete with -EAGAIN */
3345 if (req->flags & REQ_F_NOWAIT)
3346 return false;
3347
Jens Axboe227c0c92020-08-13 11:51:40 -06003348 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003349 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003350 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003351
Jens Axboebcf5a062020-05-22 09:24:42 -06003352 /*
3353 * just use poll if we can, and don't attempt if the fs doesn't
3354 * support callback based unlocks
3355 */
3356 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3357 return false;
3358
Jens Axboe3b2a4432020-08-16 10:58:43 -07003359 wait->wait.func = io_async_buf_func;
3360 wait->wait.private = req;
3361 wait->wait.flags = 0;
3362 INIT_LIST_HEAD(&wait->wait.entry);
3363 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003364 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003365 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003366 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003367}
3368
Pavel Begunkovaeab9502021-06-14 02:36:24 +01003369static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
Jens Axboebcf5a062020-05-22 09:24:42 -06003370{
3371 if (req->file->f_op->read_iter)
3372 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003373 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003374 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003375 else
3376 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003377}
3378
Pavel Begunkov889fca72021-02-10 00:03:09 +00003379static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003380{
3381 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003382 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003383 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003384 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003385 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003386 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387
Pavel Begunkov2846c482020-11-07 13:16:27 +00003388 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003389 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003390 iovec = NULL;
3391 } else {
3392 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3393 if (ret < 0)
3394 return ret;
3395 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003396 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003397 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003398
Jens Axboefd6c2e42019-12-18 12:19:41 -07003399 /* Ensure we clear previously set non-block flag */
3400 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003401 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003402 else
3403 kiocb->ki_flags |= IOCB_NOWAIT;
3404
Pavel Begunkov24c74672020-06-21 13:09:51 +03003405 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003406 if (force_nonblock && !io_file_supports_nowait(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003407 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003408 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003409 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003410
Pavel Begunkov632546c2020-11-07 13:16:26 +00003411 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003412 if (unlikely(ret)) {
3413 kfree(iovec);
3414 return ret;
3415 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003416
Jens Axboe227c0c92020-08-13 11:51:40 -06003417 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003418
Jens Axboe230d50d2021-04-01 20:41:15 -06003419 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003420 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003421 /* IOPOLL retry should happen for io-wq threads */
3422 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003423 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003424 /* no retry on NONBLOCK nor RWF_NOWAIT */
3425 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003426 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003427 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003428 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003429 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003430 } else if (ret == -EIOCBQUEUED) {
3431 goto out_free;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003432 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003433 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003434 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003435 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 }
3437
Jens Axboe227c0c92020-08-13 11:51:40 -06003438 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003439 if (ret2)
3440 return ret2;
3441
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003442 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003443 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003444 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003445 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003446
Pavel Begunkovb23df912021-02-04 13:52:04 +00003447 do {
3448 io_size -= ret;
3449 rw->bytes_done += ret;
3450 /* if we can retry, do so with the callbacks armed */
3451 if (!io_rw_should_retry(req)) {
3452 kiocb->ki_flags &= ~IOCB_WAITQ;
3453 return -EAGAIN;
3454 }
3455
3456 /*
3457 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3458 * we get -EIOCBQUEUED, then we'll get a notification when the
3459 * desired page gets unlocked. We can also get a partial read
3460 * here, and if we do, then just retry at the new offset.
3461 */
3462 ret = io_iter_do_read(req, iter);
3463 if (ret == -EIOCBQUEUED)
3464 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003465 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003466 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003467 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003468done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003469 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003470out_free:
3471 /* it's faster to check here then delegate to kfree */
3472 if (iovec)
3473 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003474 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003475}
3476
Pavel Begunkov73debe62020-09-30 22:57:54 +03003477static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003478{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003479 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3480 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003481 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003482}
3483
Pavel Begunkov889fca72021-02-10 00:03:09 +00003484static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003485{
3486 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003487 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003488 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003489 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003490 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003491 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003492
Pavel Begunkov2846c482020-11-07 13:16:27 +00003493 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003494 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003495 iovec = NULL;
3496 } else {
3497 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3498 if (ret < 0)
3499 return ret;
3500 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003501 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003502 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003503
Jens Axboefd6c2e42019-12-18 12:19:41 -07003504 /* Ensure we clear previously set non-block flag */
3505 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003506 kiocb->ki_flags &= ~IOCB_NOWAIT;
3507 else
3508 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003509
Pavel Begunkov24c74672020-06-21 13:09:51 +03003510 /* If the file doesn't support async, just async punt */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01003511 if (force_nonblock && !io_file_supports_nowait(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003512 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003513
Jens Axboe10d59342019-12-09 20:16:22 -07003514 /* file path doesn't support NOWAIT for non-direct_IO */
3515 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3516 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003517 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003518
Pavel Begunkov632546c2020-11-07 13:16:26 +00003519 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003520 if (unlikely(ret))
3521 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003522
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003523 /*
3524 * Open-code file_start_write here to grab freeze protection,
3525 * which will be released by another thread in
3526 * io_complete_rw(). Fool lockdep by telling it the lock got
3527 * released so that it doesn't complain about the held lock when
3528 * we return to userspace.
3529 */
3530 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003531 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003532 __sb_writers_release(file_inode(req->file)->i_sb,
3533 SB_FREEZE_WRITE);
3534 }
3535 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003536
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003537 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003538 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003539 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003540 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003541 else
3542 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003543
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003544 if (req->flags & REQ_F_REISSUE) {
3545 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003546 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003547 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003548
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003549 /*
3550 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3551 * retry them without IOCB_NOWAIT.
3552 */
3553 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3554 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003555 /* no retry on NONBLOCK nor RWF_NOWAIT */
3556 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003557 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003558 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003559 /* IOPOLL retry should happen for io-wq threads */
3560 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3561 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003562done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003563 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003564 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003565copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003566 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003567 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003568 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003569 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003570 }
Jens Axboe31b51512019-01-18 22:56:34 -07003571out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003572 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003573 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003574 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003575 return ret;
3576}
3577
Jens Axboe80a261f2020-09-28 14:23:58 -06003578static int io_renameat_prep(struct io_kiocb *req,
3579 const struct io_uring_sqe *sqe)
3580{
3581 struct io_rename *ren = &req->rename;
3582 const char __user *oldf, *newf;
3583
Jens Axboeed7eb252021-06-23 09:04:13 -06003584 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3585 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003586 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeed7eb252021-06-23 09:04:13 -06003587 return -EINVAL;
Jens Axboe80a261f2020-09-28 14:23:58 -06003588 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3589 return -EBADF;
3590
3591 ren->old_dfd = READ_ONCE(sqe->fd);
3592 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3593 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3594 ren->new_dfd = READ_ONCE(sqe->len);
3595 ren->flags = READ_ONCE(sqe->rename_flags);
3596
3597 ren->oldpath = getname(oldf);
3598 if (IS_ERR(ren->oldpath))
3599 return PTR_ERR(ren->oldpath);
3600
3601 ren->newpath = getname(newf);
3602 if (IS_ERR(ren->newpath)) {
3603 putname(ren->oldpath);
3604 return PTR_ERR(ren->newpath);
3605 }
3606
3607 req->flags |= REQ_F_NEED_CLEANUP;
3608 return 0;
3609}
3610
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003611static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003612{
3613 struct io_rename *ren = &req->rename;
3614 int ret;
3615
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003616 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003617 return -EAGAIN;
3618
3619 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3620 ren->newpath, ren->flags);
3621
3622 req->flags &= ~REQ_F_NEED_CLEANUP;
3623 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003624 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003625 io_req_complete(req, ret);
3626 return 0;
3627}
3628
Jens Axboe14a11432020-09-28 14:27:37 -06003629static int io_unlinkat_prep(struct io_kiocb *req,
3630 const struct io_uring_sqe *sqe)
3631{
3632 struct io_unlink *un = &req->unlink;
3633 const char __user *fname;
3634
Jens Axboe22634bc2021-06-23 09:07:45 -06003635 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3636 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003637 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3638 sqe->splice_fd_in)
Jens Axboe22634bc2021-06-23 09:07:45 -06003639 return -EINVAL;
Jens Axboe14a11432020-09-28 14:27:37 -06003640 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3641 return -EBADF;
3642
3643 un->dfd = READ_ONCE(sqe->fd);
3644
3645 un->flags = READ_ONCE(sqe->unlink_flags);
3646 if (un->flags & ~AT_REMOVEDIR)
3647 return -EINVAL;
3648
3649 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3650 un->filename = getname(fname);
3651 if (IS_ERR(un->filename))
3652 return PTR_ERR(un->filename);
3653
3654 req->flags |= REQ_F_NEED_CLEANUP;
3655 return 0;
3656}
3657
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003658static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003659{
3660 struct io_unlink *un = &req->unlink;
3661 int ret;
3662
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003663 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003664 return -EAGAIN;
3665
3666 if (un->flags & AT_REMOVEDIR)
3667 ret = do_rmdir(un->dfd, un->filename);
3668 else
3669 ret = do_unlinkat(un->dfd, un->filename);
3670
3671 req->flags &= ~REQ_F_NEED_CLEANUP;
3672 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003673 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003674 io_req_complete(req, ret);
3675 return 0;
3676}
3677
Jens Axboe36f4fa62020-09-05 11:14:22 -06003678static int io_shutdown_prep(struct io_kiocb *req,
3679 const struct io_uring_sqe *sqe)
3680{
3681#if defined(CONFIG_NET)
3682 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3683 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003684 if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3685 sqe->buf_index || sqe->splice_fd_in))
Jens Axboe36f4fa62020-09-05 11:14:22 -06003686 return -EINVAL;
3687
3688 req->shutdown.how = READ_ONCE(sqe->len);
3689 return 0;
3690#else
3691 return -EOPNOTSUPP;
3692#endif
3693}
3694
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003695static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003696{
3697#if defined(CONFIG_NET)
3698 struct socket *sock;
3699 int ret;
3700
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003701 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003702 return -EAGAIN;
3703
Linus Torvalds48aba792020-12-16 12:44:05 -08003704 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003705 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003706 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003707
3708 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003709 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003710 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003711 io_req_complete(req, ret);
3712 return 0;
3713#else
3714 return -EOPNOTSUPP;
3715#endif
3716}
3717
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003718static int __io_splice_prep(struct io_kiocb *req,
3719 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003720{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003721 struct io_splice *sp = &req->splice;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003722 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003723
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003724 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3725 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003726
3727 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003728 sp->len = READ_ONCE(sqe->len);
3729 sp->flags = READ_ONCE(sqe->splice_flags);
3730
3731 if (unlikely(sp->flags & ~valid_flags))
3732 return -EINVAL;
3733
Pavel Begunkov62906e82021-08-10 14:52:47 +01003734 sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in),
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003735 (sp->flags & SPLICE_F_FD_IN_FIXED));
3736 if (!sp->file_in)
3737 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003738 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003739 return 0;
3740}
3741
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003742static int io_tee_prep(struct io_kiocb *req,
3743 const struct io_uring_sqe *sqe)
3744{
3745 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3746 return -EINVAL;
3747 return __io_splice_prep(req, sqe);
3748}
3749
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003750static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003751{
3752 struct io_splice *sp = &req->splice;
3753 struct file *in = sp->file_in;
3754 struct file *out = sp->file_out;
3755 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3756 long ret = 0;
3757
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003758 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003759 return -EAGAIN;
3760 if (sp->len)
3761 ret = do_tee(in, out, sp->len, flags);
3762
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003763 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3764 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003765 req->flags &= ~REQ_F_NEED_CLEANUP;
3766
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003767 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003768 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003769 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003770 return 0;
3771}
3772
3773static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3774{
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01003775 struct io_splice *sp = &req->splice;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003776
3777 sp->off_in = READ_ONCE(sqe->splice_off_in);
3778 sp->off_out = READ_ONCE(sqe->off);
3779 return __io_splice_prep(req, sqe);
3780}
3781
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003782static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003783{
3784 struct io_splice *sp = &req->splice;
3785 struct file *in = sp->file_in;
3786 struct file *out = sp->file_out;
3787 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3788 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003789 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003790
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003791 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003792 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003793
3794 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3795 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003796
Jens Axboe948a7742020-05-17 14:21:38 -06003797 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003798 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003799
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003800 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3801 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003802 req->flags &= ~REQ_F_NEED_CLEANUP;
3803
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003804 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003805 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003806 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003807 return 0;
3808}
3809
Jens Axboe2b188cc2019-01-07 10:46:33 -07003810/*
3811 * IORING_OP_NOP just posts a completion event, nothing else.
3812 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003813static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003814{
3815 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003816
Jens Axboedef596e2019-01-09 08:59:42 -07003817 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3818 return -EINVAL;
3819
Pavel Begunkov889fca72021-02-10 00:03:09 +00003820 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003821 return 0;
3822}
3823
Pavel Begunkov1155c762021-02-18 18:29:38 +00003824static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003825{
Jens Axboe6b063142019-01-10 22:13:58 -07003826 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003827
Jens Axboe09bb8392019-03-13 12:39:28 -06003828 if (!req->file)
3829 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003830
Jens Axboe6b063142019-01-10 22:13:58 -07003831 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003832 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003833 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
3834 sqe->splice_fd_in))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003835 return -EINVAL;
3836
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003837 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3838 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3839 return -EINVAL;
3840
3841 req->sync.off = READ_ONCE(sqe->off);
3842 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003843 return 0;
3844}
3845
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003846static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003847{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003848 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003849 int ret;
3850
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003851 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003852 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003853 return -EAGAIN;
3854
Jens Axboe9adbd452019-12-20 08:45:55 -07003855 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003856 end > 0 ? end : LLONG_MAX,
3857 req->sync.flags & IORING_FSYNC_DATASYNC);
3858 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003859 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003860 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003861 return 0;
3862}
3863
Jens Axboed63d1b52019-12-10 10:38:56 -07003864static int io_fallocate_prep(struct io_kiocb *req,
3865 const struct io_uring_sqe *sqe)
3866{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01003867 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags ||
3868 sqe->splice_fd_in)
Jens Axboed63d1b52019-12-10 10:38:56 -07003869 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003870 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3871 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003872
3873 req->sync.off = READ_ONCE(sqe->off);
3874 req->sync.len = READ_ONCE(sqe->addr);
3875 req->sync.mode = READ_ONCE(sqe->len);
3876 return 0;
3877}
3878
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003879static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003880{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003881 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003882
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003883 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003884 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003885 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003886 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3887 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003888 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003889 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003890 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003891 return 0;
3892}
3893
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003894static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003895{
Jens Axboef8748882020-01-08 17:47:02 -07003896 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003897 int ret;
3898
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003899 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3900 return -EINVAL;
Pavel Begunkovb9445592021-08-25 12:25:45 +01003901 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003902 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003903 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003904 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003905
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003906 /* open.how should be already initialised */
3907 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003908 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003909
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003910 req->open.dfd = READ_ONCE(sqe->fd);
3911 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003912 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003913 if (IS_ERR(req->open.filename)) {
3914 ret = PTR_ERR(req->open.filename);
3915 req->open.filename = NULL;
3916 return ret;
3917 }
Pavel Begunkovb9445592021-08-25 12:25:45 +01003918
3919 req->open.file_slot = READ_ONCE(sqe->file_index);
3920 if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC))
3921 return -EINVAL;
3922
Jens Axboe4022e7a2020-03-19 19:23:18 -06003923 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003924 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003925 return 0;
3926}
3927
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003928static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3929{
Pavel Begunkovd3fddf62021-08-09 13:04:16 +01003930 u64 mode = READ_ONCE(sqe->len);
3931 u64 flags = READ_ONCE(sqe->open_flags);
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003932
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003933 req->open.how = build_open_how(flags, mode);
3934 return __io_openat_prep(req, sqe);
3935}
3936
Jens Axboecebdb982020-01-08 17:59:24 -07003937static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3938{
3939 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003940 size_t len;
3941 int ret;
3942
Jens Axboecebdb982020-01-08 17:59:24 -07003943 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3944 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003945 if (len < OPEN_HOW_SIZE_VER0)
3946 return -EINVAL;
3947
3948 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3949 len);
3950 if (ret)
3951 return ret;
3952
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003953 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003954}
3955
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003956static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003957{
3958 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003959 struct file *file;
Pavel Begunkovb9445592021-08-25 12:25:45 +01003960 bool resolve_nonblock, nonblock_set;
3961 bool fixed = !!req->open.file_slot;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003962 int ret;
3963
Jens Axboecebdb982020-01-08 17:59:24 -07003964 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003965 if (ret)
3966 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003967 nonblock_set = op.open_flag & O_NONBLOCK;
3968 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003969 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003970 /*
3971 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3972 * it'll always -EAGAIN
3973 */
3974 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3975 return -EAGAIN;
3976 op.lookup_flags |= LOOKUP_CACHED;
3977 op.open_flag |= O_NONBLOCK;
3978 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003979
Pavel Begunkovb9445592021-08-25 12:25:45 +01003980 if (!fixed) {
3981 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
3982 if (ret < 0)
3983 goto err;
3984 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003985
3986 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003987 if (IS_ERR(file)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003988 /*
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003989 * We could hang on to this 'fd' on retrying, but seems like
3990 * marginal gain for something that is now known to be a slower
3991 * path. So just put it, and we'll get a new one when we retry.
Jens Axboe3a81fd02020-12-10 12:25:36 -07003992 */
Pavel Begunkovb9445592021-08-25 12:25:45 +01003993 if (!fixed)
3994 put_unused_fd(ret);
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01003995
3996 ret = PTR_ERR(file);
3997 /* only retry if RESOLVE_CACHED wasn't already set by application */
3998 if (ret == -EAGAIN &&
3999 (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)))
4000 return -EAGAIN;
4001 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004002 }
4003
Pavel Begunkov12dcb58a2021-06-24 15:10:00 +01004004 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
4005 file->f_flags &= ~O_NONBLOCK;
4006 fsnotify_open(file);
Pavel Begunkovb9445592021-08-25 12:25:45 +01004007
4008 if (!fixed)
4009 fd_install(ret, file);
4010 else
4011 ret = io_install_fixed_file(req, file, issue_flags,
4012 req->open.file_slot - 1);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004013err:
4014 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004015 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004016 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004017 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004018 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004019 return 0;
4020}
4021
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004022static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004023{
Pavel Begunkove45cff52021-02-28 22:35:14 +00004024 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07004025}
4026
Jens Axboe067524e2020-03-02 16:32:28 -07004027static int io_remove_buffers_prep(struct io_kiocb *req,
4028 const struct io_uring_sqe *sqe)
4029{
4030 struct io_provide_buf *p = &req->pbuf;
4031 u64 tmp;
4032
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004033 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
4034 sqe->splice_fd_in)
Jens Axboe067524e2020-03-02 16:32:28 -07004035 return -EINVAL;
4036
4037 tmp = READ_ONCE(sqe->fd);
4038 if (!tmp || tmp > USHRT_MAX)
4039 return -EINVAL;
4040
4041 memset(p, 0, sizeof(*p));
4042 p->nbufs = tmp;
4043 p->bgid = READ_ONCE(sqe->buf_group);
4044 return 0;
4045}
4046
4047static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4048 int bgid, unsigned nbufs)
4049{
4050 unsigned i = 0;
4051
4052 /* shouldn't happen */
4053 if (!nbufs)
4054 return 0;
4055
4056 /* the head kbuf is the list itself */
4057 while (!list_empty(&buf->list)) {
4058 struct io_buffer *nxt;
4059
4060 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4061 list_del(&nxt->list);
4062 kfree(nxt);
4063 if (++i == nbufs)
4064 return i;
4065 }
4066 i++;
4067 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004068 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004069
4070 return i;
4071}
4072
Pavel Begunkov889fca72021-02-10 00:03:09 +00004073static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004074{
4075 struct io_provide_buf *p = &req->pbuf;
4076 struct io_ring_ctx *ctx = req->ctx;
4077 struct io_buffer *head;
4078 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004079 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004080
4081 io_ring_submit_lock(ctx, !force_nonblock);
4082
4083 lockdep_assert_held(&ctx->uring_lock);
4084
4085 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004086 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07004087 if (head)
4088 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004089 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004090 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004091
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004092 /* complete before unlock, IOPOLL may need the lock */
4093 __io_req_complete(req, issue_flags, ret, 0);
4094 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07004095 return 0;
4096}
4097
Jens Axboeddf0322d2020-02-23 16:41:33 -07004098static int io_provide_buffers_prep(struct io_kiocb *req,
4099 const struct io_uring_sqe *sqe)
4100{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004101 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004102 struct io_provide_buf *p = &req->pbuf;
4103 u64 tmp;
4104
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004105 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004106 return -EINVAL;
4107
4108 tmp = READ_ONCE(sqe->fd);
4109 if (!tmp || tmp > USHRT_MAX)
4110 return -E2BIG;
4111 p->nbufs = tmp;
4112 p->addr = READ_ONCE(sqe->addr);
4113 p->len = READ_ONCE(sqe->len);
4114
Pavel Begunkov38134ad2021-04-15 13:07:39 +01004115 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
4116 &size))
4117 return -EOVERFLOW;
4118 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
4119 return -EOVERFLOW;
4120
Pavel Begunkovd81269f2021-03-19 10:21:19 +00004121 size = (unsigned long)p->len * p->nbufs;
4122 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004123 return -EFAULT;
4124
4125 p->bgid = READ_ONCE(sqe->buf_group);
4126 tmp = READ_ONCE(sqe->off);
4127 if (tmp > USHRT_MAX)
4128 return -E2BIG;
4129 p->bid = tmp;
4130 return 0;
4131}
4132
4133static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4134{
4135 struct io_buffer *buf;
4136 u64 addr = pbuf->addr;
4137 int i, bid = pbuf->bid;
4138
4139 for (i = 0; i < pbuf->nbufs; i++) {
4140 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4141 if (!buf)
4142 break;
4143
4144 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03004145 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004146 buf->bid = bid;
4147 addr += pbuf->len;
4148 bid++;
4149 if (!*head) {
4150 INIT_LIST_HEAD(&buf->list);
4151 *head = buf;
4152 } else {
4153 list_add_tail(&buf->list, &(*head)->list);
4154 }
4155 }
4156
4157 return i ? i : -ENOMEM;
4158}
4159
Pavel Begunkov889fca72021-02-10 00:03:09 +00004160static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004161{
4162 struct io_provide_buf *p = &req->pbuf;
4163 struct io_ring_ctx *ctx = req->ctx;
4164 struct io_buffer *head, *list;
4165 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004166 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004167
4168 io_ring_submit_lock(ctx, !force_nonblock);
4169
4170 lockdep_assert_held(&ctx->uring_lock);
4171
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004172 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004173
4174 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004175 if (ret >= 0 && !list) {
4176 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4177 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004178 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004179 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004180 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004181 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004182 /* complete before unlock, IOPOLL may need the lock */
4183 __io_req_complete(req, issue_flags, ret, 0);
4184 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004185 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004186}
4187
Jens Axboe3e4827b2020-01-08 15:18:09 -07004188static int io_epoll_ctl_prep(struct io_kiocb *req,
4189 const struct io_uring_sqe *sqe)
4190{
4191#if defined(CONFIG_EPOLL)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004192 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004193 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004194 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004195 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004196
4197 req->epoll.epfd = READ_ONCE(sqe->fd);
4198 req->epoll.op = READ_ONCE(sqe->len);
4199 req->epoll.fd = READ_ONCE(sqe->off);
4200
4201 if (ep_op_has_event(req->epoll.op)) {
4202 struct epoll_event __user *ev;
4203
4204 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4205 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4206 return -EFAULT;
4207 }
4208
4209 return 0;
4210#else
4211 return -EOPNOTSUPP;
4212#endif
4213}
4214
Pavel Begunkov889fca72021-02-10 00:03:09 +00004215static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004216{
4217#if defined(CONFIG_EPOLL)
4218 struct io_epoll *ie = &req->epoll;
4219 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004220 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004221
4222 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4223 if (force_nonblock && ret == -EAGAIN)
4224 return -EAGAIN;
4225
4226 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004227 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004228 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004229 return 0;
4230#else
4231 return -EOPNOTSUPP;
4232#endif
4233}
4234
Jens Axboec1ca7572019-12-25 22:18:28 -07004235static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4236{
4237#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004238 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in)
Jens Axboec1ca7572019-12-25 22:18:28 -07004239 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004240 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4241 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004242
4243 req->madvise.addr = READ_ONCE(sqe->addr);
4244 req->madvise.len = READ_ONCE(sqe->len);
4245 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4246 return 0;
4247#else
4248 return -EOPNOTSUPP;
4249#endif
4250}
4251
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004252static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004253{
4254#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4255 struct io_madvise *ma = &req->madvise;
4256 int ret;
4257
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004258 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004259 return -EAGAIN;
4260
Minchan Kim0726b012020-10-17 16:14:50 -07004261 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004262 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004263 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004264 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004265 return 0;
4266#else
4267 return -EOPNOTSUPP;
4268#endif
4269}
4270
Jens Axboe4840e412019-12-25 22:03:45 -07004271static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4272{
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004273 if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in)
Jens Axboe4840e412019-12-25 22:03:45 -07004274 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004275 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4276 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004277
4278 req->fadvise.offset = READ_ONCE(sqe->off);
4279 req->fadvise.len = READ_ONCE(sqe->len);
4280 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4281 return 0;
4282}
4283
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004284static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004285{
4286 struct io_fadvise *fa = &req->fadvise;
4287 int ret;
4288
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004289 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004290 switch (fa->advice) {
4291 case POSIX_FADV_NORMAL:
4292 case POSIX_FADV_RANDOM:
4293 case POSIX_FADV_SEQUENTIAL:
4294 break;
4295 default:
4296 return -EAGAIN;
4297 }
4298 }
Jens Axboe4840e412019-12-25 22:03:45 -07004299
4300 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4301 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004302 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004303 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004304 return 0;
4305}
4306
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004307static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4308{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004309 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004310 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004311 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004312 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004313 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004314 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004315
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004316 req->statx.dfd = READ_ONCE(sqe->fd);
4317 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004318 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004319 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4320 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004321
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004322 return 0;
4323}
4324
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004325static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004326{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004327 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004328 int ret;
4329
Pavel Begunkov59d70012021-03-22 01:58:30 +00004330 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004331 return -EAGAIN;
4332
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004333 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4334 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004335
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004336 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004337 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004338 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004339 return 0;
4340}
4341
Jens Axboeb5dba592019-12-11 14:02:38 -07004342static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4343{
Jens Axboe14587a462020-09-05 11:36:08 -06004344 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004345 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004346 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004347 sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in)
Jens Axboeb5dba592019-12-11 14:02:38 -07004348 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004349 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004350 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004351
4352 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004353 return 0;
4354}
4355
Pavel Begunkov889fca72021-02-10 00:03:09 +00004356static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004357{
Jens Axboe9eac1902021-01-19 15:50:37 -07004358 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004359 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004360 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004361 struct file *file = NULL;
4362 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004363
Jens Axboe9eac1902021-01-19 15:50:37 -07004364 spin_lock(&files->file_lock);
4365 fdt = files_fdtable(files);
4366 if (close->fd >= fdt->max_fds) {
4367 spin_unlock(&files->file_lock);
4368 goto err;
4369 }
4370 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004371 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004372 spin_unlock(&files->file_lock);
4373 file = NULL;
4374 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004375 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004376
4377 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004378 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004379 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004380 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004381 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004382
Jens Axboe9eac1902021-01-19 15:50:37 -07004383 ret = __close_fd_get_file(close->fd, &file);
4384 spin_unlock(&files->file_lock);
4385 if (ret < 0) {
4386 if (ret == -ENOENT)
4387 ret = -EBADF;
4388 goto err;
4389 }
4390
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004391 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004392 ret = filp_close(file, current->files);
4393err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004394 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004395 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004396 if (file)
4397 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004398 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004399 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004400}
4401
Pavel Begunkov1155c762021-02-18 18:29:38 +00004402static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004403{
4404 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004405
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004406 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4407 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004408 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index ||
4409 sqe->splice_fd_in))
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004410 return -EINVAL;
4411
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004412 req->sync.off = READ_ONCE(sqe->off);
4413 req->sync.len = READ_ONCE(sqe->len);
4414 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004415 return 0;
4416}
4417
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004418static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004419{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004420 int ret;
4421
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004422 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004423 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004424 return -EAGAIN;
4425
Jens Axboe9adbd452019-12-20 08:45:55 -07004426 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004427 req->sync.flags);
4428 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004429 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004430 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004431 return 0;
4432}
4433
YueHaibing469956e2020-03-04 15:53:52 +08004434#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004435static int io_setup_async_msg(struct io_kiocb *req,
4436 struct io_async_msghdr *kmsg)
4437{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004438 struct io_async_msghdr *async_msg = req->async_data;
4439
4440 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004441 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004442 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004443 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004444 return -ENOMEM;
4445 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004446 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004447 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004448 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004449 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004450 /* if were using fast_iov, set it to the new one */
4451 if (!async_msg->free_iov)
4452 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4453
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004454 return -EAGAIN;
4455}
4456
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004457static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4458 struct io_async_msghdr *iomsg)
4459{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004460 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004461 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004462 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004463 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004464}
4465
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004466static int io_sendmsg_prep_async(struct io_kiocb *req)
4467{
4468 int ret;
4469
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004470 ret = io_sendmsg_copy_hdr(req, req->async_data);
4471 if (!ret)
4472 req->flags |= REQ_F_NEED_CLEANUP;
4473 return ret;
4474}
4475
Jens Axboe3529d8c2019-12-19 18:24:38 -07004476static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004477{
Jens Axboee47293f2019-12-20 08:58:21 -07004478 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004479
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004480 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4481 return -EINVAL;
4482
Pavel Begunkov270a5942020-07-12 20:41:04 +03004483 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004484 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004485 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4486 if (sr->msg_flags & MSG_DONTWAIT)
4487 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004488
Jens Axboed8768362020-02-27 14:17:49 -07004489#ifdef CONFIG_COMPAT
4490 if (req->ctx->compat)
4491 sr->msg_flags |= MSG_CMSG_COMPAT;
4492#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004493 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004494}
4495
Pavel Begunkov889fca72021-02-10 00:03:09 +00004496static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004497{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004498 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004499 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004500 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004501 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004502 int ret;
4503
Florent Revestdba4a922020-12-04 12:36:04 +01004504 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004505 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004506 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004507
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004508 kmsg = req->async_data;
4509 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004510 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004511 if (ret)
4512 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004513 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004514 }
4515
Pavel Begunkov04411802021-04-01 15:44:00 +01004516 flags = req->sr_msg.msg_flags;
4517 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004518 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004519 if (flags & MSG_WAITALL)
4520 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4521
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004522 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004523 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004524 return io_setup_async_msg(req, kmsg);
4525 if (ret == -ERESTARTSYS)
4526 ret = -EINTR;
4527
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004528 /* fast path, check for non-NULL to avoid function call */
4529 if (kmsg->free_iov)
4530 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004531 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004532 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004533 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004534 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004535 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004536}
4537
Pavel Begunkov889fca72021-02-10 00:03:09 +00004538static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004539{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004540 struct io_sr_msg *sr = &req->sr_msg;
4541 struct msghdr msg;
4542 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004543 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004544 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004545 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004546 int ret;
4547
Florent Revestdba4a922020-12-04 12:36:04 +01004548 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004549 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004550 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004551
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004552 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4553 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004554 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004555
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004556 msg.msg_name = NULL;
4557 msg.msg_control = NULL;
4558 msg.msg_controllen = 0;
4559 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004560
Pavel Begunkov04411802021-04-01 15:44:00 +01004561 flags = req->sr_msg.msg_flags;
4562 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004563 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004564 if (flags & MSG_WAITALL)
4565 min_ret = iov_iter_count(&msg.msg_iter);
4566
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004567 msg.msg_flags = flags;
4568 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004569 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004570 return -EAGAIN;
4571 if (ret == -ERESTARTSYS)
4572 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004573
Stefan Metzmacher00312752021-03-20 20:33:36 +01004574 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004575 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004576 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004577 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004578}
4579
Pavel Begunkov1400e692020-07-12 20:41:05 +03004580static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4581 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004582{
4583 struct io_sr_msg *sr = &req->sr_msg;
4584 struct iovec __user *uiov;
4585 size_t iov_len;
4586 int ret;
4587
Pavel Begunkov1400e692020-07-12 20:41:05 +03004588 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4589 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004590 if (ret)
4591 return ret;
4592
4593 if (req->flags & REQ_F_BUFFER_SELECT) {
4594 if (iov_len > 1)
4595 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004596 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004597 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004598 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004599 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004600 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004601 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004602 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004603 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004604 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004605 if (ret > 0)
4606 ret = 0;
4607 }
4608
4609 return ret;
4610}
4611
4612#ifdef CONFIG_COMPAT
4613static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004614 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004615{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004616 struct io_sr_msg *sr = &req->sr_msg;
4617 struct compat_iovec __user *uiov;
4618 compat_uptr_t ptr;
4619 compat_size_t len;
4620 int ret;
4621
Pavel Begunkov4af34172021-04-11 01:46:30 +01004622 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4623 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004624 if (ret)
4625 return ret;
4626
4627 uiov = compat_ptr(ptr);
4628 if (req->flags & REQ_F_BUFFER_SELECT) {
4629 compat_ssize_t clen;
4630
4631 if (len > 1)
4632 return -EINVAL;
4633 if (!access_ok(uiov, sizeof(*uiov)))
4634 return -EFAULT;
4635 if (__get_user(clen, &uiov->iov_len))
4636 return -EFAULT;
4637 if (clen < 0)
4638 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004639 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004640 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004641 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004642 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004643 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004644 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004645 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004646 if (ret < 0)
4647 return ret;
4648 }
4649
4650 return 0;
4651}
Jens Axboe03b12302019-12-02 18:50:25 -07004652#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004653
Pavel Begunkov1400e692020-07-12 20:41:05 +03004654static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4655 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004656{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004657 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004658
4659#ifdef CONFIG_COMPAT
4660 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004661 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004662#endif
4663
Pavel Begunkov1400e692020-07-12 20:41:05 +03004664 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004665}
4666
Jens Axboebcda7ba2020-02-23 16:42:51 -07004667static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004668 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004669{
4670 struct io_sr_msg *sr = &req->sr_msg;
4671 struct io_buffer *kbuf;
4672
Jens Axboebcda7ba2020-02-23 16:42:51 -07004673 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4674 if (IS_ERR(kbuf))
4675 return kbuf;
4676
4677 sr->kbuf = kbuf;
4678 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004679 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004680}
4681
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004682static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4683{
4684 return io_put_kbuf(req, req->sr_msg.kbuf);
4685}
4686
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004687static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004688{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004689 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004690
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004691 ret = io_recvmsg_copy_hdr(req, req->async_data);
4692 if (!ret)
4693 req->flags |= REQ_F_NEED_CLEANUP;
4694 return ret;
4695}
4696
4697static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4698{
4699 struct io_sr_msg *sr = &req->sr_msg;
4700
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004701 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4702 return -EINVAL;
4703
Pavel Begunkov270a5942020-07-12 20:41:04 +03004704 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004705 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004706 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004707 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4708 if (sr->msg_flags & MSG_DONTWAIT)
4709 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004710
Jens Axboed8768362020-02-27 14:17:49 -07004711#ifdef CONFIG_COMPAT
4712 if (req->ctx->compat)
4713 sr->msg_flags |= MSG_CMSG_COMPAT;
4714#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004715 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004716}
4717
Pavel Begunkov889fca72021-02-10 00:03:09 +00004718static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004719{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004720 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004721 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004722 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004723 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004724 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004725 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004726 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004727
Florent Revestdba4a922020-12-04 12:36:04 +01004728 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004729 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004730 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004731
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004732 kmsg = req->async_data;
4733 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004734 ret = io_recvmsg_copy_hdr(req, &iomsg);
4735 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004736 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004737 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004738 }
4739
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004740 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004741 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004742 if (IS_ERR(kbuf))
4743 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004744 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004745 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4746 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004747 1, req->sr_msg.len);
4748 }
4749
Pavel Begunkov04411802021-04-01 15:44:00 +01004750 flags = req->sr_msg.msg_flags;
4751 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004752 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004753 if (flags & MSG_WAITALL)
4754 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4755
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004756 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4757 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004758 if (force_nonblock && ret == -EAGAIN)
4759 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004760 if (ret == -ERESTARTSYS)
4761 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004762
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004763 if (req->flags & REQ_F_BUFFER_SELECTED)
4764 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004765 /* fast path, check for non-NULL to avoid function call */
4766 if (kmsg->free_iov)
4767 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004768 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004769 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004770 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004771 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004772 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004773}
4774
Pavel Begunkov889fca72021-02-10 00:03:09 +00004775static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004776{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004777 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004778 struct io_sr_msg *sr = &req->sr_msg;
4779 struct msghdr msg;
4780 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004781 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004782 struct iovec iov;
4783 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004784 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004785 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004786 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004787
Florent Revestdba4a922020-12-04 12:36:04 +01004788 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004789 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004790 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004791
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004792 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004793 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004794 if (IS_ERR(kbuf))
4795 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004796 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004797 }
4798
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004799 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004800 if (unlikely(ret))
4801 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004802
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004803 msg.msg_name = NULL;
4804 msg.msg_control = NULL;
4805 msg.msg_controllen = 0;
4806 msg.msg_namelen = 0;
4807 msg.msg_iocb = NULL;
4808 msg.msg_flags = 0;
4809
Pavel Begunkov04411802021-04-01 15:44:00 +01004810 flags = req->sr_msg.msg_flags;
4811 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004812 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004813 if (flags & MSG_WAITALL)
4814 min_ret = iov_iter_count(&msg.msg_iter);
4815
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004816 ret = sock_recvmsg(sock, &msg, flags);
4817 if (force_nonblock && ret == -EAGAIN)
4818 return -EAGAIN;
4819 if (ret == -ERESTARTSYS)
4820 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004821out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004822 if (req->flags & REQ_F_BUFFER_SELECTED)
4823 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01004824 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004825 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004826 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004827 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004828}
4829
Jens Axboe3529d8c2019-12-19 18:24:38 -07004830static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004831{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004832 struct io_accept *accept = &req->accept;
4833
Jens Axboe14587a462020-09-05 11:36:08 -06004834 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004835 return -EINVAL;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004836 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004837 return -EINVAL;
4838
Jens Axboed55e5f52019-12-11 16:12:15 -07004839 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4840 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004841 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004842 accept->nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004843
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004844 accept->file_slot = READ_ONCE(sqe->file_index);
4845 if (accept->file_slot && ((req->open.how.flags & O_CLOEXEC) ||
4846 (accept->flags & SOCK_CLOEXEC)))
4847 return -EINVAL;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004848 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
4849 return -EINVAL;
4850 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
4851 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004852 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004853}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004854
Pavel Begunkov889fca72021-02-10 00:03:09 +00004855static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004856{
4857 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004858 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004859 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004860 bool fixed = !!accept->file_slot;
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004861 struct file *file;
4862 int ret, fd;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004863
Jiufei Xuee697dee2020-06-10 13:41:59 +08004864 if (req->file->f_flags & O_NONBLOCK)
4865 req->flags |= REQ_F_NOWAIT;
4866
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004867 if (!fixed) {
4868 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
4869 if (unlikely(fd < 0))
4870 return fd;
4871 }
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004872 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
4873 accept->flags);
4874 if (IS_ERR(file)) {
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004875 if (!fixed)
4876 put_unused_fd(fd);
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004877 ret = PTR_ERR(file);
4878 if (ret == -EAGAIN && force_nonblock)
4879 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004880 if (ret == -ERESTARTSYS)
4881 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004882 req_set_fail(req);
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004883 } else if (!fixed) {
Pavel Begunkova7083ad2021-08-25 12:25:46 +01004884 fd_install(fd, file);
4885 ret = fd;
Pavel Begunkovaaa4db12021-08-25 12:25:47 +01004886 } else {
4887 ret = io_install_fixed_file(req, file, issue_flags,
4888 accept->file_slot - 1);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004889 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004890 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004891 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004892}
4893
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004894static int io_connect_prep_async(struct io_kiocb *req)
4895{
4896 struct io_async_connect *io = req->async_data;
4897 struct io_connect *conn = &req->connect;
4898
4899 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4900}
4901
Jens Axboe3529d8c2019-12-19 18:24:38 -07004902static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004903{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004904 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004905
Jens Axboe14587a462020-09-05 11:36:08 -06004906 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004907 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01004908 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags ||
4909 sqe->splice_fd_in)
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004910 return -EINVAL;
4911
Jens Axboe3529d8c2019-12-19 18:24:38 -07004912 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4913 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004914 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004915}
4916
Pavel Begunkov889fca72021-02-10 00:03:09 +00004917static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004918{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004919 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004920 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004921 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004922 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004923
Jens Axboee8c2bc12020-08-15 18:44:09 -07004924 if (req->async_data) {
4925 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004926 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004927 ret = move_addr_to_kernel(req->connect.addr,
4928 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004929 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004930 if (ret)
4931 goto out;
4932 io = &__io;
4933 }
4934
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004935 file_flags = force_nonblock ? O_NONBLOCK : 0;
4936
Jens Axboee8c2bc12020-08-15 18:44:09 -07004937 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004938 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004939 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004940 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004941 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004942 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004943 ret = -ENOMEM;
4944 goto out;
4945 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004946 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004947 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004948 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004949 if (ret == -ERESTARTSYS)
4950 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004951out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004952 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004953 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004954 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004955 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004956}
YueHaibing469956e2020-03-04 15:53:52 +08004957#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004958#define IO_NETOP_FN(op) \
4959static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4960{ \
4961 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004962}
4963
Jens Axboe99a10082021-02-19 09:35:19 -07004964#define IO_NETOP_PREP(op) \
4965IO_NETOP_FN(op) \
4966static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4967{ \
4968 return -EOPNOTSUPP; \
4969} \
4970
4971#define IO_NETOP_PREP_ASYNC(op) \
4972IO_NETOP_PREP(op) \
4973static int io_##op##_prep_async(struct io_kiocb *req) \
4974{ \
4975 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004976}
4977
Jens Axboe99a10082021-02-19 09:35:19 -07004978IO_NETOP_PREP_ASYNC(sendmsg);
4979IO_NETOP_PREP_ASYNC(recvmsg);
4980IO_NETOP_PREP_ASYNC(connect);
4981IO_NETOP_PREP(accept);
4982IO_NETOP_FN(send);
4983IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004984#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004985
Jens Axboed7718a92020-02-14 22:23:12 -07004986struct io_poll_table {
4987 struct poll_table_struct pt;
4988 struct io_kiocb *req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01004989 int nr_entries;
Jens Axboed7718a92020-02-14 22:23:12 -07004990 int error;
4991};
4992
Jens Axboed7718a92020-02-14 22:23:12 -07004993static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01004994 __poll_t mask, io_req_tw_func_t func)
Jens Axboed7718a92020-02-14 22:23:12 -07004995{
Jens Axboed7718a92020-02-14 22:23:12 -07004996 /* for instances that support it check for an event match first: */
4997 if (mask && !(mask & poll->events))
4998 return 0;
4999
5000 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5001
5002 list_del_init(&poll->wait.entry);
5003
Jens Axboed7718a92020-02-14 22:23:12 -07005004 req->result = mask;
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01005005 req->io_task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005006
Jens Axboed7718a92020-02-14 22:23:12 -07005007 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005008 * If this fails, then the task is exiting. When a task exits, the
5009 * work gets canceled, so just cancel this request as well instead
5010 * of executing it. We can't safely execute it anyway, as we may not
5011 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005012 */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005013 io_req_task_work_add(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005014 return 1;
5015}
5016
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005017static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5018 __acquires(&req->ctx->completion_lock)
5019{
5020 struct io_ring_ctx *ctx = req->ctx;
5021
Jens Axboe316319e2021-08-19 09:41:42 -06005022 /* req->task == current here, checking PF_EXITING is safe */
Pavel Begunkove09ee512021-07-01 13:26:05 +01005023 if (unlikely(req->task->flags & PF_EXITING))
5024 WRITE_ONCE(poll->canceled, true);
5025
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005026 if (!req->result && !READ_ONCE(poll->canceled)) {
5027 struct poll_table_struct pt = { ._key = poll->events };
5028
5029 req->result = vfs_poll(req->file, &pt) & poll->events;
5030 }
5031
Jens Axboe79ebeae2021-08-10 15:18:27 -06005032 spin_lock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005033 if (!req->result && !READ_ONCE(poll->canceled)) {
5034 add_wait_queue(poll->head, &poll->wait);
5035 return true;
5036 }
5037
5038 return false;
5039}
5040
Jens Axboed4e7cd32020-08-15 11:44:50 -07005041static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005042{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005043 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005044 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005045 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005046 return req->apoll->double_poll;
5047}
5048
5049static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5050{
5051 if (req->opcode == IORING_OP_POLL_ADD)
5052 return &req->poll;
5053 return &req->apoll->poll;
5054}
5055
5056static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005057 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07005058{
5059 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005060
5061 lockdep_assert_held(&req->ctx->completion_lock);
5062
5063 if (poll && poll->head) {
5064 struct wait_queue_head *head = poll->head;
5065
Jens Axboe79ebeae2021-08-10 15:18:27 -06005066 spin_lock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005067 list_del_init(&poll->wait.entry);
5068 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07005069 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005070 poll->head = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005071 spin_unlock_irq(&head->lock);
Jens Axboe18bceab2020-05-15 11:56:54 -06005072 }
5073}
5074
Pavel Begunkove27414b2021-04-09 09:13:20 +01005075static bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005076 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06005077{
5078 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005079 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005080 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06005081
Pavel Begunkove27414b2021-04-09 09:13:20 +01005082 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07005083 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005084 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005085 } else {
Jens Axboe50826202021-02-23 09:02:26 -07005086 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01005087 }
Jens Axboeb69de282021-03-17 08:37:41 -06005088 if (req->poll.events & EPOLLONESHOT)
5089 flags = 0;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005090 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005091 req->poll.done = true;
5092 flags = 0;
5093 }
Hao Xu7b289c32021-04-13 15:20:39 +08005094 if (flags & IORING_CQE_F_MORE)
5095 ctx->cq_extra++;
5096
Jens Axboe18bceab2020-05-15 11:56:54 -06005097 io_commit_cqring(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005098 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06005099}
5100
Pavel Begunkovf237c302021-08-18 12:42:46 +01005101static void io_poll_task_func(struct io_kiocb *req, bool *locked)
Jens Axboe18bceab2020-05-15 11:56:54 -06005102{
Jens Axboe6d816e02020-08-11 08:04:14 -06005103 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005104 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005105
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005106 if (io_poll_rewait(req, &req->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005107 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005108 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005109 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005110
Pavel Begunkove27414b2021-04-09 09:13:20 +01005111 done = io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005112 if (done) {
Hao Xua890d012021-07-28 11:03:22 +08005113 io_poll_remove_double(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005114 hash_del(&req->hash_node);
Pavel Begunkovf40b9642021-04-09 09:13:19 +01005115 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07005116 req->result = 0;
5117 add_wait_queue(req->poll.head, &req->poll.wait);
5118 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005119 spin_unlock(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005120 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005121
Jens Axboe88e41cf2021-02-22 22:08:01 -07005122 if (done) {
5123 nxt = io_put_req_find_next(req);
5124 if (nxt)
Pavel Begunkovf237c302021-08-18 12:42:46 +01005125 io_req_task_submit(nxt, locked);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005126 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03005127 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005128}
5129
5130static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5131 int sync, void *key)
5132{
5133 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005134 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005135 __poll_t mask = key_to_poll(key);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005136 unsigned long flags;
Jens Axboe18bceab2020-05-15 11:56:54 -06005137
5138 /* for instances that support it check for an event match first: */
5139 if (mask && !(mask & poll->events))
5140 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07005141 if (!(poll->events & EPOLLONESHOT))
5142 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06005143
Jens Axboe8706e042020-09-28 08:38:54 -06005144 list_del_init(&wait->entry);
5145
Jens Axboe9ce85ef2021-07-09 08:20:28 -06005146 if (poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005147 bool done;
5148
Jens Axboe79ebeae2021-08-10 15:18:27 -06005149 spin_lock_irqsave(&poll->head->lock, flags);
Jens Axboe807abcb2020-07-17 17:09:27 -06005150 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005151 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005152 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005153 /* make sure double remove sees this as being gone */
5154 wait->private = NULL;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005155 spin_unlock_irqrestore(&poll->head->lock, flags);
Jens Axboec8b5e262020-10-25 13:53:26 -06005156 if (!done) {
5157 /* use wait func handler, so it matches the rq type */
5158 poll->wait.func(&poll->wait, mode, sync, key);
5159 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005160 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07005161 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005162 return 1;
5163}
5164
5165static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5166 wait_queue_func_t wake_func)
5167{
5168 poll->head = NULL;
5169 poll->done = false;
5170 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06005171#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
5172 /* mask in events that we always want/need */
5173 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06005174 INIT_LIST_HEAD(&poll->wait.entry);
5175 init_waitqueue_func_entry(&poll->wait, wake_func);
5176}
5177
5178static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005179 struct wait_queue_head *head,
5180 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005181{
5182 struct io_kiocb *req = pt->req;
5183
5184 /*
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005185 * The file being polled uses multiple waitqueues for poll handling
5186 * (e.g. one for read, one for write). Setup a separate io_poll_iocb
5187 * if this happens.
Jens Axboe18bceab2020-05-15 11:56:54 -06005188 */
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005189 if (unlikely(pt->nr_entries)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005190 struct io_poll_iocb *poll_one = poll;
5191
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005192 /* double add on the same waitqueue head, ignore */
5193 if (poll_one->head == head)
5194 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005195 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005196 if (*poll_ptr) {
Pavel Begunkov23a65db2021-08-17 20:28:11 +01005197 if ((*poll_ptr)->head == head)
5198 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005199 pt->error = -EINVAL;
5200 return;
5201 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005202 /*
5203 * Can't handle multishot for double wait for now, turn it
5204 * into one-shot mode.
5205 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005206 if (!(poll_one->events & EPOLLONESHOT))
5207 poll_one->events |= EPOLLONESHOT;
Jens Axboe18bceab2020-05-15 11:56:54 -06005208 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5209 if (!poll) {
5210 pt->error = -ENOMEM;
5211 return;
5212 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005213 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005214 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005215 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005216 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005217 }
5218
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005219 pt->nr_entries++;
Jens Axboe18bceab2020-05-15 11:56:54 -06005220 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005221
5222 if (poll->events & EPOLLEXCLUSIVE)
5223 add_wait_queue_exclusive(head, &poll->wait);
5224 else
5225 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005226}
5227
5228static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5229 struct poll_table_struct *p)
5230{
5231 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005232 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005233
Jens Axboe807abcb2020-07-17 17:09:27 -06005234 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005235}
5236
Pavel Begunkovf237c302021-08-18 12:42:46 +01005237static void io_async_task_func(struct io_kiocb *req, bool *locked)
Jens Axboed7718a92020-02-14 22:23:12 -07005238{
Jens Axboed7718a92020-02-14 22:23:12 -07005239 struct async_poll *apoll = req->apoll;
5240 struct io_ring_ctx *ctx = req->ctx;
5241
Olivier Langlois236daeae2021-05-31 02:36:37 -04005242 trace_io_uring_task_run(req->ctx, req, req->opcode, req->user_data);
Jens Axboed7718a92020-02-14 22:23:12 -07005243
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005244 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005245 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005246 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005247 }
5248
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005249 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005250 io_poll_remove_double(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005251 spin_unlock(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005252
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005253 if (!READ_ONCE(apoll->poll.canceled))
Pavel Begunkovf237c302021-08-18 12:42:46 +01005254 io_req_task_submit(req, locked);
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005255 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005256 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005257}
5258
5259static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5260 void *key)
5261{
5262 struct io_kiocb *req = wait->private;
5263 struct io_poll_iocb *poll = &req->apoll->poll;
5264
5265 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5266 key_to_poll(key));
5267
5268 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5269}
5270
5271static void io_poll_req_insert(struct io_kiocb *req)
5272{
5273 struct io_ring_ctx *ctx = req->ctx;
5274 struct hlist_head *list;
5275
5276 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5277 hlist_add_head(&req->hash_node, list);
5278}
5279
5280static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5281 struct io_poll_iocb *poll,
5282 struct io_poll_table *ipt, __poll_t mask,
5283 wait_queue_func_t wake_func)
5284 __acquires(&ctx->completion_lock)
5285{
5286 struct io_ring_ctx *ctx = req->ctx;
5287 bool cancel = false;
5288
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005289 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005290 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005291 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005292 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005293
5294 ipt->pt._key = mask;
5295 ipt->req = req;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005296 ipt->error = 0;
5297 ipt->nr_entries = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005298
Jens Axboed7718a92020-02-14 22:23:12 -07005299 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
Pavel Begunkov68b11e82021-07-20 10:50:43 +01005300 if (unlikely(!ipt->nr_entries) && !ipt->error)
5301 ipt->error = -EINVAL;
Jens Axboed7718a92020-02-14 22:23:12 -07005302
Jens Axboe79ebeae2021-08-10 15:18:27 -06005303 spin_lock(&ctx->completion_lock);
Hao Xua890d012021-07-28 11:03:22 +08005304 if (ipt->error || (mask && (poll->events & EPOLLONESHOT)))
Pavel Begunkov46fee9a2021-07-20 10:50:44 +01005305 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005306 if (likely(poll->head)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005307 spin_lock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005308 if (unlikely(list_empty(&poll->wait.entry))) {
5309 if (ipt->error)
5310 cancel = true;
5311 ipt->error = 0;
5312 mask = 0;
5313 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005314 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005315 list_del_init(&poll->wait.entry);
5316 else if (cancel)
5317 WRITE_ONCE(poll->canceled, true);
5318 else if (!poll->done) /* actually waiting for an event */
5319 io_poll_req_insert(req);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005320 spin_unlock_irq(&poll->head->lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005321 }
5322
5323 return mask;
5324}
5325
Olivier Langlois59b735a2021-06-22 05:17:39 -07005326enum {
5327 IO_APOLL_OK,
5328 IO_APOLL_ABORTED,
5329 IO_APOLL_READY
5330};
5331
5332static int io_arm_poll_handler(struct io_kiocb *req)
Jens Axboed7718a92020-02-14 22:23:12 -07005333{
5334 const struct io_op_def *def = &io_op_defs[req->opcode];
5335 struct io_ring_ctx *ctx = req->ctx;
5336 struct async_poll *apoll;
5337 struct io_poll_table ipt;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005338 __poll_t ret, mask = EPOLLONESHOT | POLLERR | POLLPRI;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005339 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005340
5341 if (!req->file || !file_can_poll(req->file))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005342 return IO_APOLL_ABORTED;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005343 if (req->flags & REQ_F_POLLED)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005344 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005345 if (!def->pollin && !def->pollout)
Olivier Langlois59b735a2021-06-22 05:17:39 -07005346 return IO_APOLL_ABORTED;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005347
5348 if (def->pollin) {
5349 rw = READ;
5350 mask |= POLLIN | POLLRDNORM;
5351
5352 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5353 if ((req->opcode == IORING_OP_RECVMSG) &&
5354 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5355 mask &= ~POLLIN;
5356 } else {
5357 rw = WRITE;
5358 mask |= POLLOUT | POLLWRNORM;
5359 }
5360
Jens Axboe9dab14b2020-08-25 12:27:50 -06005361 /* if we can't nonblock try, then no point in arming a poll handler */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01005362 if (!io_file_supports_nowait(req, rw))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005363 return IO_APOLL_ABORTED;
Jens Axboed7718a92020-02-14 22:23:12 -07005364
5365 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5366 if (unlikely(!apoll))
Olivier Langlois59b735a2021-06-22 05:17:39 -07005367 return IO_APOLL_ABORTED;
Jens Axboe807abcb2020-07-17 17:09:27 -06005368 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005369 req->apoll = apoll;
Pavel Begunkovb2d9c3d2021-06-26 21:40:44 +01005370 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005371 ipt.pt._qproc = io_async_queue_proc;
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005372 io_req_set_refcount(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005373
5374 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5375 io_async_wake);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005376 spin_unlock(&ctx->completion_lock);
Hao Xu41a51692021-08-12 15:47:02 +08005377 if (ret || ipt.error)
5378 return ret ? IO_APOLL_READY : IO_APOLL_ABORTED;
5379
Olivier Langlois236daeae2021-05-31 02:36:37 -04005380 trace_io_uring_poll_arm(ctx, req, req->opcode, req->user_data,
5381 mask, apoll->poll.events);
Olivier Langlois59b735a2021-06-22 05:17:39 -07005382 return IO_APOLL_OK;
Jens Axboed7718a92020-02-14 22:23:12 -07005383}
5384
5385static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005386 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005387 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005388{
Jens Axboeb41e9852020-02-17 09:52:41 -07005389 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005390
Jens Axboe50826202021-02-23 09:02:26 -07005391 if (!poll->head)
5392 return false;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005393 spin_lock_irq(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005394 if (do_cancel)
5395 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005396 if (!list_empty(&poll->wait.entry)) {
5397 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005398 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005399 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005400 spin_unlock_irq(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005401 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005402 return do_complete;
5403}
5404
Pavel Begunkov5d709042021-08-09 20:18:13 +01005405static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005406 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005407{
5408 bool do_complete;
5409
Jens Axboed4e7cd32020-08-15 11:44:50 -07005410 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005411 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005412
Jens Axboeb41e9852020-02-17 09:52:41 -07005413 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005414 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005415 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005416 req_set_fail(req);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005417 io_put_req_deferred(req);
Pavel Begunkov5d709042021-08-09 20:18:13 +01005418 }
Jens Axboeb41e9852020-02-17 09:52:41 -07005419 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005420}
5421
Jens Axboe76e1b642020-09-26 15:05:03 -06005422/*
5423 * Returns true if we found and killed one or more poll requests
5424 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005425static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005426 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005427{
Jens Axboe78076bb2019-12-04 19:56:40 -07005428 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005429 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005430 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005431
Jens Axboe79ebeae2021-08-10 15:18:27 -06005432 spin_lock(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005433 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5434 struct hlist_head *list;
5435
5436 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005437 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005438 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005439 posted += io_poll_remove_one(req);
5440 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005441 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005442 spin_unlock(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005443
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005444 if (posted)
5445 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005446
5447 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005448}
5449
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005450static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5451 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005452 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005453{
Jens Axboe78076bb2019-12-04 19:56:40 -07005454 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005455 struct io_kiocb *req;
5456
Jens Axboe78076bb2019-12-04 19:56:40 -07005457 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5458 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005459 if (sqe_addr != req->user_data)
5460 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005461 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5462 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005463 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005464 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005465 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005466}
5467
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005468static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5469 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005470 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005471{
5472 struct io_kiocb *req;
5473
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005474 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005475 if (!req)
5476 return -ENOENT;
5477 if (io_poll_remove_one(req))
5478 return 0;
5479
5480 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005481}
5482
Pavel Begunkov9096af32021-04-14 13:38:36 +01005483static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5484 unsigned int flags)
5485{
5486 u32 events;
5487
5488 events = READ_ONCE(sqe->poll32_events);
5489#ifdef __BIG_ENDIAN
5490 events = swahw32(events);
5491#endif
5492 if (!(flags & IORING_POLL_ADD_MULTI))
5493 events |= EPOLLONESHOT;
5494 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5495}
5496
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005497static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005498 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005499{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005500 struct io_poll_update *upd = &req->poll_update;
5501 u32 flags;
5502
Jens Axboe221c5eb2019-01-17 09:41:58 -07005503 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5504 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005505 if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in)
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005506 return -EINVAL;
5507 flags = READ_ONCE(sqe->len);
5508 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5509 IORING_POLL_ADD_MULTI))
5510 return -EINVAL;
5511 /* meaningless without update */
5512 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005513 return -EINVAL;
5514
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005515 upd->old_user_data = READ_ONCE(sqe->addr);
5516 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5517 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005518
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005519 upd->new_user_data = READ_ONCE(sqe->off);
5520 if (!upd->update_user_data && upd->new_user_data)
5521 return -EINVAL;
5522 if (upd->update_events)
5523 upd->events = io_poll_parse_events(sqe, flags);
5524 else if (sqe->poll32_events)
5525 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005526
Jens Axboe221c5eb2019-01-17 09:41:58 -07005527 return 0;
5528}
5529
Jens Axboe221c5eb2019-01-17 09:41:58 -07005530static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5531 void *key)
5532{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005533 struct io_kiocb *req = wait->private;
5534 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005535
Jens Axboed7718a92020-02-14 22:23:12 -07005536 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005537}
5538
Jens Axboe221c5eb2019-01-17 09:41:58 -07005539static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5540 struct poll_table_struct *p)
5541{
5542 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5543
Jens Axboee8c2bc12020-08-15 18:44:09 -07005544 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005545}
5546
Jens Axboe3529d8c2019-12-19 18:24:38 -07005547static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005548{
5549 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005550 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005551
5552 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5553 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005554 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005555 return -EINVAL;
5556 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005557 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005558 return -EINVAL;
5559
Pavel Begunkov48dcd382021-08-15 10:40:18 +01005560 io_req_set_refcount(req);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005561 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005562 return 0;
5563}
5564
Pavel Begunkov61e98202021-02-10 00:03:08 +00005565static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005566{
5567 struct io_poll_iocb *poll = &req->poll;
5568 struct io_ring_ctx *ctx = req->ctx;
5569 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005570 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005571
Jens Axboed7718a92020-02-14 22:23:12 -07005572 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005573
Jens Axboed7718a92020-02-14 22:23:12 -07005574 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5575 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005576
Jens Axboe8c838782019-03-12 15:48:16 -06005577 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005578 ipt.error = 0;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005579 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005580 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06005581 spin_unlock(&ctx->completion_lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005582
Jens Axboe8c838782019-03-12 15:48:16 -06005583 if (mask) {
5584 io_cqring_ev_posted(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005585 if (poll->events & EPOLLONESHOT)
5586 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005587 }
Jens Axboe8c838782019-03-12 15:48:16 -06005588 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005589}
5590
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005591static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005592{
5593 struct io_ring_ctx *ctx = req->ctx;
5594 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005595 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005596 int ret;
5597
Jens Axboe79ebeae2021-08-10 15:18:27 -06005598 spin_lock(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005599 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005600 if (!preq) {
5601 ret = -ENOENT;
5602 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005603 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005604
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005605 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5606 completing = true;
5607 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5608 goto err;
5609 }
5610
Jens Axboecb3b200e2021-04-06 09:49:31 -06005611 /*
5612 * Don't allow racy completion with singleshot, as we cannot safely
5613 * update those. For multishot, if we're racing with completion, just
5614 * let completion re-add it.
5615 */
5616 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5617 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5618 ret = -EALREADY;
5619 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005620 }
5621 /* we now have a detached poll request. reissue. */
5622 ret = 0;
5623err:
Jens Axboeb69de282021-03-17 08:37:41 -06005624 if (ret < 0) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06005625 spin_unlock(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005626 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005627 io_req_complete(req, ret);
5628 return 0;
5629 }
5630 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005631 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005632 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005633 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005634 preq->poll.events |= IO_POLL_UNMASK;
5635 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005636 if (req->poll_update.update_user_data)
5637 preq->user_data = req->poll_update.new_user_data;
Jens Axboe79ebeae2021-08-10 15:18:27 -06005638 spin_unlock(&ctx->completion_lock);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005639
Jens Axboeb69de282021-03-17 08:37:41 -06005640 /* complete update request, we're done with it */
5641 io_req_complete(req, ret);
5642
Jens Axboecb3b200e2021-04-06 09:49:31 -06005643 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005644 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005645 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005646 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005647 io_req_complete(preq, ret);
5648 }
Jens Axboeb69de282021-03-17 08:37:41 -06005649 }
5650 return 0;
5651}
5652
Pavel Begunkovf237c302021-08-18 12:42:46 +01005653static void io_req_task_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89850fc2021-08-10 15:11:51 -06005654{
Jens Axboe89850fc2021-08-10 15:11:51 -06005655 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005656 io_req_complete_post(req, -ETIME, 0);
Jens Axboe89850fc2021-08-10 15:11:51 -06005657}
5658
Jens Axboe5262f562019-09-17 12:26:57 -06005659static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5660{
Jens Axboead8a48a2019-11-15 08:49:11 -07005661 struct io_timeout_data *data = container_of(timer,
5662 struct io_timeout_data, timer);
5663 struct io_kiocb *req = data->req;
5664 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005665 unsigned long flags;
5666
Jens Axboe89850fc2021-08-10 15:11:51 -06005667 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005668 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005669 atomic_set(&req->ctx->cq_timeouts,
5670 atomic_read(&req->ctx->cq_timeouts) + 1);
Jens Axboe89850fc2021-08-10 15:11:51 -06005671 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005672
Jens Axboe89850fc2021-08-10 15:11:51 -06005673 req->io_task_work.func = io_req_task_timeout;
5674 io_req_task_work_add(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005675 return HRTIMER_NORESTART;
5676}
5677
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005678static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5679 __u64 user_data)
Jens Axboe89850fc2021-08-10 15:11:51 -06005680 __must_hold(&ctx->timeout_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005681{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005682 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005683 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005684 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005685
5686 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005687 found = user_data == req->user_data;
5688 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005689 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005690 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005691 if (!found)
5692 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005693
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005694 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005695 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005696 return ERR_PTR(-EALREADY);
5697 list_del_init(&req->timeout.list);
5698 return req;
5699}
5700
5701static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005702 __must_hold(&ctx->completion_lock)
Jens Axboe89850fc2021-08-10 15:11:51 -06005703 __must_hold(&ctx->timeout_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005704{
5705 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5706
5707 if (IS_ERR(req))
5708 return PTR_ERR(req);
5709
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005710 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005711 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkov91c2f692021-08-11 19:28:28 +01005712 io_put_req_deferred(req);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005713 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005714}
5715
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005716static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5717 struct timespec64 *ts, enum hrtimer_mode mode)
Jens Axboe89850fc2021-08-10 15:11:51 -06005718 __must_hold(&ctx->timeout_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005719{
5720 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5721 struct io_timeout_data *data;
5722
5723 if (IS_ERR(req))
5724 return PTR_ERR(req);
5725
5726 req->timeout.off = 0; /* noseq */
5727 data = req->async_data;
5728 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5729 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5730 data->timer.function = io_timeout_fn;
5731 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5732 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005733}
5734
Jens Axboe3529d8c2019-12-19 18:24:38 -07005735static int io_timeout_remove_prep(struct io_kiocb *req,
5736 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005737{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005738 struct io_timeout_rem *tr = &req->timeout_rem;
5739
Jens Axboeb29472e2019-12-17 18:50:29 -07005740 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5741 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005742 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5743 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005744 if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in)
Jens Axboeb29472e2019-12-17 18:50:29 -07005745 return -EINVAL;
5746
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005747 tr->addr = READ_ONCE(sqe->addr);
5748 tr->flags = READ_ONCE(sqe->timeout_flags);
5749 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5750 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5751 return -EINVAL;
5752 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5753 return -EFAULT;
5754 } else if (tr->flags) {
5755 /* timeout removal doesn't support flags */
5756 return -EINVAL;
5757 }
5758
Jens Axboeb29472e2019-12-17 18:50:29 -07005759 return 0;
5760}
5761
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005762static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5763{
5764 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5765 : HRTIMER_MODE_REL;
5766}
5767
Jens Axboe11365042019-10-16 09:08:32 -06005768/*
5769 * Remove or update an existing timeout command
5770 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005771static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005772{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005773 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005774 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005775 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005776
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005777 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) {
5778 spin_lock(&ctx->completion_lock);
5779 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005780 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005781 spin_unlock_irq(&ctx->timeout_lock);
5782 spin_unlock(&ctx->completion_lock);
5783 } else {
5784 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005785 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5786 io_translate_timeout_mode(tr->flags));
Pavel Begunkovec3c3d02021-08-18 10:50:52 +01005787 spin_unlock_irq(&ctx->timeout_lock);
5788 }
Jens Axboe11365042019-10-16 09:08:32 -06005789
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005790 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005791 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005792 io_req_complete_post(req, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06005793 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005794}
5795
Jens Axboe3529d8c2019-12-19 18:24:38 -07005796static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005797 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005798{
Jens Axboead8a48a2019-11-15 08:49:11 -07005799 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005800 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005801 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005802
Jens Axboead8a48a2019-11-15 08:49:11 -07005803 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005804 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005805 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 ||
5806 sqe->splice_fd_in)
Jens Axboea41525a2019-10-15 16:48:15 -06005807 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005808 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005809 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005810 flags = READ_ONCE(sqe->timeout_flags);
5811 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005812 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005813
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005814 req->timeout.off = off;
Pavel Begunkovf18ee4c2021-06-14 23:37:25 +01005815 if (unlikely(off && !req->ctx->off_timeout_used))
5816 req->ctx->off_timeout_used = true;
Jens Axboe26a61672019-12-20 09:02:01 -07005817
Jens Axboee8c2bc12020-08-15 18:44:09 -07005818 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005819 return -ENOMEM;
5820
Jens Axboee8c2bc12020-08-15 18:44:09 -07005821 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005822 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005823
5824 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005825 return -EFAULT;
5826
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005827 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005828 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005829
5830 if (is_timeout_link) {
5831 struct io_submit_link *link = &req->ctx->submit_state.link;
5832
5833 if (!link->head)
5834 return -EINVAL;
5835 if (link->last->opcode == IORING_OP_LINK_TIMEOUT)
5836 return -EINVAL;
Pavel Begunkov4d13d1a2021-08-15 10:40:24 +01005837 req->timeout.head = link->last;
5838 link->last->flags |= REQ_F_ARM_LTIMEOUT;
Pavel Begunkovb97e7362021-08-15 10:40:23 +01005839 }
Jens Axboead8a48a2019-11-15 08:49:11 -07005840 return 0;
5841}
5842
Pavel Begunkov61e98202021-02-10 00:03:08 +00005843static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005844{
Jens Axboead8a48a2019-11-15 08:49:11 -07005845 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005846 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005847 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005848 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005849
Jens Axboe89850fc2021-08-10 15:11:51 -06005850 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005851
Jens Axboe5262f562019-09-17 12:26:57 -06005852 /*
5853 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005854 * timeout event to be satisfied. If it isn't set, then this is
5855 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005856 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005857 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005858 entry = ctx->timeout_list.prev;
5859 goto add;
5860 }
Jens Axboe5262f562019-09-17 12:26:57 -06005861
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005862 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5863 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005864
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005865 /* Update the last seq here in case io_flush_timeouts() hasn't.
5866 * This is safe because ->completion_lock is held, and submissions
5867 * and completions are never mixed in the same ->completion_lock section.
5868 */
5869 ctx->cq_last_tm_flush = tail;
5870
Jens Axboe5262f562019-09-17 12:26:57 -06005871 /*
5872 * Insertion sort, ensuring the first entry in the list is always
5873 * the one we need first.
5874 */
Jens Axboe5262f562019-09-17 12:26:57 -06005875 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005876 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5877 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005878
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005879 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005880 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005881 /* nxt.seq is behind @tail, otherwise would've been completed */
5882 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005883 break;
5884 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005885add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005886 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005887 data->timer.function = io_timeout_fn;
5888 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe89850fc2021-08-10 15:11:51 -06005889 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005890 return 0;
5891}
5892
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005893struct io_cancel_data {
5894 struct io_ring_ctx *ctx;
5895 u64 user_data;
5896};
5897
Jens Axboe62755e32019-10-28 21:49:21 -06005898static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005899{
Jens Axboe62755e32019-10-28 21:49:21 -06005900 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005901 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06005902
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005903 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06005904}
5905
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005906static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
5907 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06005908{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005909 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06005910 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005911 int ret = 0;
5912
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005913 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005914 return -ENOENT;
5915
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005916 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005917 switch (cancel_ret) {
5918 case IO_WQ_CANCEL_OK:
5919 ret = 0;
5920 break;
5921 case IO_WQ_CANCEL_RUNNING:
5922 ret = -EALREADY;
5923 break;
5924 case IO_WQ_CANCEL_NOTFOUND:
5925 ret = -ENOENT;
5926 break;
5927 }
5928
Jens Axboee977d6d2019-11-05 12:39:45 -07005929 return ret;
5930}
5931
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005932static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr)
Jens Axboe47f46762019-11-09 17:43:02 -07005933{
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005934 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005935 int ret;
5936
Pavel Begunkovdadebc32021-08-23 13:30:44 +01005937 WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current);
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005938
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005939 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01005940 if (ret != -ENOENT)
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005941 return ret;
Pavel Begunkov505657b2021-08-17 20:28:09 +01005942
5943 spin_lock(&ctx->completion_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005944 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005945 ret = io_timeout_cancel(ctx, sqe_addr);
Jens Axboe79ebeae2021-08-10 15:18:27 -06005946 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07005947 if (ret != -ENOENT)
Pavel Begunkov505657b2021-08-17 20:28:09 +01005948 goto out;
5949 ret = io_poll_cancel(ctx, sqe_addr, false);
5950out:
5951 spin_unlock(&ctx->completion_lock);
5952 return ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005953}
5954
Jens Axboe3529d8c2019-12-19 18:24:38 -07005955static int io_async_cancel_prep(struct io_kiocb *req,
5956 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005957{
Jens Axboefbf23842019-12-17 18:45:56 -07005958 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005959 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005960 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5961 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01005962 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags ||
5963 sqe->splice_fd_in)
Jens Axboee977d6d2019-11-05 12:39:45 -07005964 return -EINVAL;
5965
Jens Axboefbf23842019-12-17 18:45:56 -07005966 req->cancel.addr = READ_ONCE(sqe->addr);
5967 return 0;
5968}
5969
Pavel Begunkov61e98202021-02-10 00:03:08 +00005970static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005971{
5972 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005973 u64 sqe_addr = req->cancel.addr;
5974 struct io_tctx_node *node;
5975 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07005976
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01005977 ret = io_try_cancel_userdata(req, sqe_addr);
Pavel Begunkov58f99372021-03-12 16:25:55 +00005978 if (ret != -ENOENT)
5979 goto done;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005980
5981 /* slow path, try all io-wq's */
5982 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
5983 ret = -ENOENT;
5984 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
5985 struct io_uring_task *tctx = node->task->io_uring;
5986
Pavel Begunkov58f99372021-03-12 16:25:55 +00005987 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
5988 if (ret != -ENOENT)
5989 break;
5990 }
5991 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
Pavel Begunkov58f99372021-03-12 16:25:55 +00005992done:
Pavel Begunkov58f99372021-03-12 16:25:55 +00005993 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005994 req_set_fail(req);
Pavel Begunkov505657b2021-08-17 20:28:09 +01005995 io_req_complete_post(req, ret, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005996 return 0;
5997}
5998
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005999static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006000 const struct io_uring_sqe *sqe)
6001{
Daniele Albano61710e42020-07-18 14:15:16 -06006002 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6003 return -EINVAL;
Pavel Begunkov26578cd2021-08-20 10:36:37 +01006004 if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006005 return -EINVAL;
6006
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006007 req->rsrc_update.offset = READ_ONCE(sqe->off);
6008 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6009 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006010 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006011 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006012 return 0;
6013}
6014
Pavel Begunkov889fca72021-02-10 00:03:09 +00006015static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006016{
6017 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006018 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006019 int ret;
6020
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006021 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006022 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006023
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006024 up.offset = req->rsrc_update.offset;
6025 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01006026 up.nr = 0;
6027 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01006028 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006029
6030 mutex_lock(&ctx->uring_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01006031 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01006032 &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006033 mutex_unlock(&ctx->uring_lock);
6034
6035 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006036 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006037 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006038 return 0;
6039}
6040
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006041static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006042{
Jens Axboed625c6e2019-12-17 19:53:05 -07006043 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006044 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006045 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006046 case IORING_OP_READV:
6047 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006048 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006049 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006050 case IORING_OP_WRITEV:
6051 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006052 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006053 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006054 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006055 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006056 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006057 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006058 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006059 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006060 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006061 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006062 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006063 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006064 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006065 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006066 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006067 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006068 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006069 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006070 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006071 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006072 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006073 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006074 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006075 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006076 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006077 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006078 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006079 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006080 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006081 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006082 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006083 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006084 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006085 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006086 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006087 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006088 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006089 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006090 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006091 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006092 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006093 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006094 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006095 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006096 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006097 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006098 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006099 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006100 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006101 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006102 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006103 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006104 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006105 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006106 case IORING_OP_SHUTDOWN:
6107 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006108 case IORING_OP_RENAMEAT:
6109 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006110 case IORING_OP_UNLINKAT:
6111 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006112 }
6113
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006114 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6115 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01006116 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006117}
6118
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006119static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006120{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006121 if (!io_op_defs[req->opcode].needs_async_setup)
6122 return 0;
6123 if (WARN_ON_ONCE(req->async_data))
6124 return -EFAULT;
6125 if (io_alloc_async_data(req))
6126 return -EAGAIN;
6127
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006128 switch (req->opcode) {
6129 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006130 return io_rw_prep_async(req, READ);
6131 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006132 return io_rw_prep_async(req, WRITE);
6133 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006134 return io_sendmsg_prep_async(req);
6135 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006136 return io_recvmsg_prep_async(req);
6137 case IORING_OP_CONNECT:
6138 return io_connect_prep_async(req);
6139 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006140 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
6141 req->opcode);
6142 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07006143}
6144
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006145static u32 io_get_sequence(struct io_kiocb *req)
6146{
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006147 u32 seq = req->ctx->cached_sq_head;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006148
Pavel Begunkova3dbdf52021-06-17 18:14:05 +01006149 /* need original cached_sq_head, but it was increased for each req */
6150 io_for_each_link(req, req)
6151 seq--;
6152 return seq;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006153}
6154
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006155static bool io_drain_req(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006156{
Pavel Begunkov3c199662021-06-15 16:47:57 +01006157 struct io_kiocb *pos;
Jens Axboedef596e2019-01-09 08:59:42 -07006158 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006159 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07006160 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006161 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07006162
Pavel Begunkov3c199662021-06-15 16:47:57 +01006163 /*
6164 * If we need to drain a request in the middle of a link, drain the
6165 * head request and the next request/link after the current link.
6166 * Considering sequential execution of links, IOSQE_IO_DRAIN will be
6167 * maintained for every request of our link.
6168 */
6169 if (ctx->drain_next) {
6170 req->flags |= REQ_F_IO_DRAIN;
6171 ctx->drain_next = false;
6172 }
6173 /* not interested in head, start from the first linked */
6174 io_for_each_link(pos, req->link) {
6175 if (pos->flags & REQ_F_IO_DRAIN) {
6176 ctx->drain_next = true;
6177 req->flags |= REQ_F_IO_DRAIN;
6178 break;
6179 }
6180 }
6181
Jens Axboedef596e2019-01-09 08:59:42 -07006182 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006183 if (likely(list_empty_careful(&ctx->defer_list) &&
Pavel Begunkov10c66902021-06-15 16:47:56 +01006184 !(req->flags & REQ_F_IO_DRAIN))) {
6185 ctx->drain_active = false;
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006186 return false;
Pavel Begunkov10c66902021-06-15 16:47:56 +01006187 }
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006188
6189 seq = io_get_sequence(req);
6190 /* Still a chance to pass the sequence check */
6191 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006192 return false;
Jens Axboedef596e2019-01-09 08:59:42 -07006193
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006194 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006195 if (ret)
Pavel Begunkov1b487732021-07-11 22:41:13 +01006196 goto fail;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006197 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006198 de = kmalloc(sizeof(*de), GFP_KERNEL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006199 if (!de) {
Pavel Begunkov1b487732021-07-11 22:41:13 +01006200 ret = -ENOMEM;
6201fail:
6202 io_req_complete_failed(req, ret);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006203 return true;
6204 }
Jens Axboe31b51512019-01-18 22:56:34 -07006205
Jens Axboe79ebeae2021-08-10 15:18:27 -06006206 spin_lock(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006207 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe79ebeae2021-08-10 15:18:27 -06006208 spin_unlock(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006209 kfree(de);
Pavel Begunkovf237c302021-08-18 12:42:46 +01006210 io_queue_async_work(req, NULL);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006211 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006212 }
6213
6214 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006215 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006216 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006217 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006218 spin_unlock(&ctx->completion_lock);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006219 return true;
Jens Axboe31b51512019-01-18 22:56:34 -07006220}
6221
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006222static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006223{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006224 if (req->flags & REQ_F_BUFFER_SELECTED) {
6225 switch (req->opcode) {
6226 case IORING_OP_READV:
6227 case IORING_OP_READ_FIXED:
6228 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006229 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006230 break;
6231 case IORING_OP_RECVMSG:
6232 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006233 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006234 break;
6235 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006236 }
6237
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006238 if (req->flags & REQ_F_NEED_CLEANUP) {
6239 switch (req->opcode) {
6240 case IORING_OP_READV:
6241 case IORING_OP_READ_FIXED:
6242 case IORING_OP_READ:
6243 case IORING_OP_WRITEV:
6244 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006245 case IORING_OP_WRITE: {
6246 struct io_async_rw *io = req->async_data;
Pavel Begunkov1dacb4d2021-06-17 18:14:03 +01006247
6248 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006249 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006250 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006251 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006252 case IORING_OP_SENDMSG: {
6253 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006254
6255 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006256 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006257 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006258 case IORING_OP_SPLICE:
6259 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006260 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6261 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006262 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006263 case IORING_OP_OPENAT:
6264 case IORING_OP_OPENAT2:
6265 if (req->open.filename)
6266 putname(req->open.filename);
6267 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006268 case IORING_OP_RENAMEAT:
6269 putname(req->rename.oldpath);
6270 putname(req->rename.newpath);
6271 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006272 case IORING_OP_UNLINKAT:
6273 putname(req->unlink.filename);
6274 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006275 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006276 }
Jens Axboe75652a302021-04-15 09:52:40 -06006277 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6278 kfree(req->apoll->double_poll);
6279 kfree(req->apoll);
6280 req->apoll = NULL;
6281 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006282 if (req->flags & REQ_F_INFLIGHT) {
6283 struct io_uring_task *tctx = req->task->io_uring;
6284
6285 atomic_dec(&tctx->inflight_tracked);
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006286 }
Pavel Begunkovc8543572021-06-17 18:14:04 +01006287 if (req->flags & REQ_F_CREDS)
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006288 put_cred(req->creds);
Pavel Begunkovc8543572021-06-17 18:14:04 +01006289
6290 req->flags &= ~IO_REQ_CLEAN_FLAGS;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006291}
6292
Pavel Begunkov889fca72021-02-10 00:03:09 +00006293static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006294{
Jens Axboeedafcce2019-01-09 09:16:05 -07006295 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006296 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006297 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006298
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006299 if ((req->flags & REQ_F_CREDS) && req->creds != current_cred())
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006300 creds = override_creds(req->creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006301
Jens Axboed625c6e2019-12-17 19:53:05 -07006302 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006304 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006306 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006307 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006308 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006309 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310 break;
6311 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006312 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006313 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006314 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006315 break;
6316 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006317 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006318 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006319 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006320 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321 break;
6322 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006323 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006324 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006325 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006326 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006327 break;
6328 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006329 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006330 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006331 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006332 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006333 break;
6334 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006335 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006336 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006337 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006338 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006339 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006340 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006341 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006342 break;
6343 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006344 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006345 break;
6346 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006347 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006348 break;
6349 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006350 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006351 break;
6352 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006353 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006354 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006355 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006356 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006357 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006358 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006359 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006360 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006361 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006362 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006363 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006364 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006365 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006366 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006367 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006368 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006369 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006370 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006371 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006372 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006373 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006374 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006375 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006376 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006377 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006378 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006379 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006380 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006381 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006382 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006383 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006384 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006385 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006386 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006387 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006388 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006389 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006391 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006392 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006393 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006394 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006395 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006396 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006397 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006398 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006399 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006400 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006401 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006402 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006403 default:
6404 ret = -EINVAL;
6405 break;
6406 }
Jens Axboe31b51512019-01-18 22:56:34 -07006407
Jens Axboe5730b272021-02-27 15:57:30 -07006408 if (creds)
6409 revert_creds(creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006410 if (ret)
6411 return ret;
Jens Axboeb5325762020-05-19 21:20:27 -06006412 /* If the op doesn't have a file, we're not polling for it */
Pavel Begunkovcb3d8972021-06-14 02:36:14 +01006413 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file)
6414 io_iopoll_req_issued(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006415
6416 return 0;
6417}
6418
Pavel Begunkovebc11b62021-08-09 13:04:05 +01006419static struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
6420{
6421 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6422
6423 req = io_put_req_find_next(req);
6424 return req ? &req->work : NULL;
6425}
6426
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006427static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006428{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006429 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006430 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006431 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006432
Pavel Begunkov48dcd382021-08-15 10:40:18 +01006433 /* one will be dropped by ->io_free_work() after returning to io-wq */
6434 if (!(req->flags & REQ_F_REFCOUNT))
6435 __io_req_set_refcount(req, 2);
6436 else
6437 req_ref_get(req);
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006438
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006439 timeout = io_prep_linked_timeout(req);
6440 if (timeout)
6441 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006442
Pavel Begunkovdadebc32021-08-23 13:30:44 +01006443 /* either cancelled or io-wq is dying, so don't touch tctx->iowq */
Jens Axboe4014d942021-01-19 15:53:54 -07006444 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006445 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006446
Jens Axboe561fb042019-10-24 07:25:42 -06006447 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006448 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006449 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006450 /*
6451 * We can get EAGAIN for polled IO even though we're
6452 * forcing a sync submission from here, since we can't
6453 * wait for request slots on the block side.
6454 */
6455 if (ret != -EAGAIN)
6456 break;
6457 cond_resched();
6458 } while (1);
6459 }
Jens Axboe31b51512019-01-18 22:56:34 -07006460
Pavel Begunkova3df76982021-02-18 22:32:52 +00006461 /* avoid locking problems by failing it from a clean context */
Pavel Begunkov5d5901a2021-08-11 19:28:29 +01006462 if (ret)
Pavel Begunkova3df76982021-02-18 22:32:52 +00006463 io_req_task_queue_fail(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07006464}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006465
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006466static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006467 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006468{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01006469 return &table->files[i];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006470}
6471
Jens Axboe09bb8392019-03-13 12:39:28 -06006472static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6473 int index)
6474{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006475 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006476
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006477 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006478}
6479
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006480static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006481{
6482 unsigned long file_ptr = (unsigned long) file;
6483
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006484 if (__io_file_supports_nowait(file, READ))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006485 file_ptr |= FFS_ASYNC_READ;
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006486 if (__io_file_supports_nowait(file, WRITE))
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006487 file_ptr |= FFS_ASYNC_WRITE;
6488 if (S_ISREG(file_inode(file)->i_mode))
6489 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006490 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006491}
6492
Pavel Begunkovac177052021-08-09 13:04:02 +01006493static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx,
6494 struct io_kiocb *req, int fd)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006495{
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006496 struct file *file;
Pavel Begunkovac177052021-08-09 13:04:02 +01006497 unsigned long file_ptr;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006498
Pavel Begunkovac177052021-08-09 13:04:02 +01006499 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
6500 return NULL;
6501 fd = array_index_nospec(fd, ctx->nr_user_files);
6502 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
6503 file = (struct file *) (file_ptr & FFS_MASK);
6504 file_ptr &= ~FFS_MASK;
6505 /* mask in overlapping REQ_F and FFS bits */
Pavel Begunkovb191e2d2021-08-09 13:04:03 +01006506 req->flags |= (file_ptr << REQ_F_NOWAIT_READ_BIT);
Pavel Begunkovac177052021-08-09 13:04:02 +01006507 io_req_set_rsrc_node(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006508 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006509}
6510
Pavel Begunkovac177052021-08-09 13:04:02 +01006511static struct file *io_file_get_normal(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006512 struct io_kiocb *req, int fd)
6513{
Pavel Begunkov62906e82021-08-10 14:52:47 +01006514 struct file *file = fget(fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006515
6516 trace_io_uring_file_get(ctx, fd);
6517
6518 /* we don't allow fixed io_uring files */
6519 if (file && unlikely(file->f_op == &io_uring_fops))
6520 io_req_track_inflight(req);
6521 return file;
6522}
6523
6524static inline struct file *io_file_get(struct io_ring_ctx *ctx,
Pavel Begunkovac177052021-08-09 13:04:02 +01006525 struct io_kiocb *req, int fd, bool fixed)
6526{
6527 if (fixed)
6528 return io_file_get_fixed(ctx, req, fd);
6529 else
Pavel Begunkov62906e82021-08-10 14:52:47 +01006530 return io_file_get_normal(ctx, req, fd);
Pavel Begunkovac177052021-08-09 13:04:02 +01006531}
6532
Pavel Begunkovf237c302021-08-18 12:42:46 +01006533static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked)
Jens Axboe89b263f2021-08-10 15:14:18 -06006534{
6535 struct io_kiocb *prev = req->timeout.prev;
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006536 int ret;
Jens Axboe89b263f2021-08-10 15:14:18 -06006537
6538 if (prev) {
Pavel Begunkov8cb01fa2021-08-15 10:40:22 +01006539 ret = io_try_cancel_userdata(req, prev->user_data);
Pavel Begunkov505657b2021-08-17 20:28:09 +01006540 io_req_complete_post(req, ret ?: -ETIME, 0);
Jens Axboe89b263f2021-08-10 15:14:18 -06006541 io_put_req(prev);
Jens Axboe89b263f2021-08-10 15:14:18 -06006542 } else {
6543 io_req_complete_post(req, -ETIME, 0);
6544 }
6545}
6546
Jens Axboe2665abf2019-11-05 12:40:47 -07006547static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6548{
Jens Axboead8a48a2019-11-15 08:49:11 -07006549 struct io_timeout_data *data = container_of(timer,
6550 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006551 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006552 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006553 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006554
Jens Axboe89b263f2021-08-10 15:14:18 -06006555 spin_lock_irqsave(&ctx->timeout_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006556 prev = req->timeout.head;
6557 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006558
6559 /*
6560 * We don't expect the list to be empty, that will only happen if we
6561 * race with the completion of the linked work.
6562 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006563 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006564 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006565 if (!req_ref_inc_not_zero(prev))
6566 prev = NULL;
6567 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006568 req->timeout.prev = prev;
6569 spin_unlock_irqrestore(&ctx->timeout_lock, flags);
Jens Axboe2665abf2019-11-05 12:40:47 -07006570
Jens Axboe89b263f2021-08-10 15:14:18 -06006571 req->io_task_work.func = io_req_task_link_timeout;
6572 io_req_task_work_add(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006573 return HRTIMER_NORESTART;
6574}
6575
Pavel Begunkovde968c12021-03-19 17:22:33 +00006576static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006577{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006578 struct io_ring_ctx *ctx = req->ctx;
6579
Jens Axboe89b263f2021-08-10 15:14:18 -06006580 spin_lock_irq(&ctx->timeout_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006581 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006582 * If the back reference is NULL, then our linked request finished
6583 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006584 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006585 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006586 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006587
Jens Axboead8a48a2019-11-15 08:49:11 -07006588 data->timer.function = io_link_timeout_fn;
6589 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6590 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006591 }
Jens Axboe89b263f2021-08-10 15:14:18 -06006592 spin_unlock_irq(&ctx->timeout_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006593 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006594 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006595}
6596
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006597static void __io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006598 __must_hold(&req->ctx->uring_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006599{
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006600 struct io_kiocb *linked_timeout;
Jens Axboee0c5c572019-03-12 10:18:47 -06006601 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006602
Olivier Langlois59b735a2021-06-22 05:17:39 -07006603issue_sqe:
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006604 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006605
6606 /*
6607 * We async punt it if the file wasn't marked NOWAIT, or if the file
6608 * doesn't support non-blocking read/write attempts
6609 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006610 if (likely(!ret)) {
Pavel Begunkove342c802021-01-19 13:32:47 +00006611 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006612 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006613 struct io_submit_state *state = &ctx->submit_state;
Jens Axboee65ef562019-03-12 10:16:44 -06006614
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006615 state->compl_reqs[state->compl_nr++] = req;
6616 if (state->compl_nr == ARRAY_SIZE(state->compl_reqs))
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006617 io_submit_flush_completions(ctx);
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006618 return;
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006619 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006620
6621 linked_timeout = io_prep_linked_timeout(req);
6622 if (linked_timeout)
6623 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov18400382021-03-19 17:22:34 +00006624 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006625 linked_timeout = io_prep_linked_timeout(req);
6626
Olivier Langlois59b735a2021-06-22 05:17:39 -07006627 switch (io_arm_poll_handler(req)) {
6628 case IO_APOLL_READY:
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006629 if (linked_timeout)
6630 io_unprep_linked_timeout(req);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006631 goto issue_sqe;
6632 case IO_APOLL_ABORTED:
Pavel Begunkov18400382021-03-19 17:22:34 +00006633 /*
6634 * Queued up for async execution, worker will release
6635 * submit reference when the iocb is actually submitted.
6636 */
Pavel Begunkovf237c302021-08-18 12:42:46 +01006637 io_queue_async_work(req, NULL);
Olivier Langlois59b735a2021-06-22 05:17:39 -07006638 break;
Pavel Begunkov18400382021-03-19 17:22:34 +00006639 }
Pavel Begunkov906c6ca2021-08-15 10:40:26 +01006640
6641 if (linked_timeout)
6642 io_queue_linked_timeout(linked_timeout);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006643 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006644 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006645 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006646}
6647
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006648static inline void io_queue_sqe(struct io_kiocb *req)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006649 __must_hold(&req->ctx->uring_lock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006650{
Pavel Begunkov10c66902021-06-15 16:47:56 +01006651 if (unlikely(req->ctx->drain_active) && io_drain_req(req))
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006652 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08006653
Hao Xua8295b92021-08-27 17:46:09 +08006654 if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006655 __io_queue_sqe(req);
Hao Xua8295b92021-08-27 17:46:09 +08006656 } else if (req->flags & REQ_F_FAIL) {
6657 io_req_complete_failed(req, req->result);
Pavel Begunkov76cc33d2021-06-14 23:37:30 +01006658 } else {
6659 int ret = io_req_prep_async(req);
6660
6661 if (unlikely(ret))
6662 io_req_complete_failed(req, ret);
6663 else
Pavel Begunkovf237c302021-08-18 12:42:46 +01006664 io_queue_async_work(req, NULL);
Jens Axboece35a472019-12-17 08:04:44 -07006665 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006666}
6667
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006668/*
6669 * Check SQE restrictions (opcode and flags).
6670 *
6671 * Returns 'true' if SQE is allowed, 'false' otherwise.
6672 */
6673static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6674 struct io_kiocb *req,
6675 unsigned int sqe_flags)
6676{
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006677 if (likely(!ctx->restricted))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006678 return true;
6679
6680 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6681 return false;
6682
6683 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6684 ctx->restrictions.sqe_flags_required)
6685 return false;
6686
6687 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6688 ctx->restrictions.sqe_flags_required))
6689 return false;
6690
6691 return true;
6692}
6693
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006694static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006695 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006696 __must_hold(&ctx->uring_lock)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006697{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006698 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006699 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006700 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006701
Pavel Begunkov864ea922021-08-09 13:04:08 +01006702 /* req is partially pre-initialised, see io_preinit_req() */
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006703 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006704 /* same numerical values with corresponding REQ_F_*, safe to copy */
6705 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006706 req->user_data = READ_ONCE(sqe->user_data);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006707 req->file = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006708 req->fixed_rsrc_refs = NULL;
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006709 req->task = current;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006710
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006711 /* enforce forwards compatibility on users */
Pavel Begunkovdddca222021-04-27 16:13:52 +01006712 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006713 return -EINVAL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006714 if (unlikely(req->opcode >= IORING_OP_LAST))
6715 return -EINVAL;
Pavel Begunkov4cfb25b2021-06-26 21:40:47 +01006716 if (!io_check_restriction(ctx, req, sqe_flags))
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006717 return -EACCES;
6718
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006719 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6720 !io_op_defs[req->opcode].buffer_select)
6721 return -EOPNOTSUPP;
Pavel Begunkov3c199662021-06-15 16:47:57 +01006722 if (unlikely(sqe_flags & IOSQE_IO_DRAIN))
6723 ctx->drain_active = true;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006724
Jens Axboe003e8dc2021-03-06 09:22:27 -07006725 personality = READ_ONCE(sqe->personality);
6726 if (personality) {
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006727 req->creds = xa_load(&ctx->personalities, personality);
6728 if (!req->creds)
Jens Axboe003e8dc2021-03-06 09:22:27 -07006729 return -EINVAL;
Pavel Begunkovc10d1f92021-06-17 18:14:01 +01006730 get_cred(req->creds);
Pavel Begunkovb8e64b52021-06-17 18:14:02 +01006731 req->flags |= REQ_F_CREDS;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006732 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006733 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006734
Jens Axboe27926b62020-10-28 09:33:23 -06006735 /*
6736 * Plug now if we have more than 1 IO left after this, and the target
6737 * is potentially a read/write to block based storage.
6738 */
6739 if (!state->plug_started && state->ios_left > 1 &&
6740 io_op_defs[req->opcode].plug) {
6741 blk_start_plug(&state->plug);
6742 state->plug_started = true;
6743 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006744
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006745 if (io_op_defs[req->opcode].needs_file) {
Pavel Begunkov62906e82021-08-10 14:52:47 +01006746 req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd),
Pavel Begunkovac177052021-08-09 13:04:02 +01006747 (sqe_flags & IOSQE_FIXED_FILE));
Pavel Begunkovba13e232021-02-01 18:59:52 +00006748 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006749 ret = -EBADF;
6750 }
6751
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006752 state->ios_left--;
6753 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006754}
6755
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006756static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006757 const struct io_uring_sqe *sqe)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006758 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006759{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006760 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006761 int ret;
6762
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006763 ret = io_init_req(ctx, req, sqe);
6764 if (unlikely(ret)) {
6765fail_req:
Hao Xua8295b92021-08-27 17:46:09 +08006766 /* fail even hard links since we don't submit */
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006767 if (link->head) {
Hao Xua8295b92021-08-27 17:46:09 +08006768 /*
6769 * we can judge a link req is failed or cancelled by if
6770 * REQ_F_FAIL is set, but the head is an exception since
6771 * it may be set REQ_F_FAIL because of other req's failure
6772 * so let's leverage req->result to distinguish if a head
6773 * is set REQ_F_FAIL because of its failure or other req's
6774 * failure so that we can set the correct ret code for it.
6775 * init result here to avoid affecting the normal path.
6776 */
6777 if (!(link->head->flags & REQ_F_FAIL))
6778 req_fail_link_node(link->head, -ECANCELED);
6779 } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
6780 /*
6781 * the current req is a normal req, we should return
6782 * error and thus break the submittion loop.
6783 */
6784 io_req_complete_failed(req, ret);
6785 return ret;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006786 }
Hao Xua8295b92021-08-27 17:46:09 +08006787 req_fail_link_node(req, ret);
6788 } else {
6789 ret = io_req_prep(req, sqe);
6790 if (unlikely(ret))
6791 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006792 }
Pavel Begunkov441b8a72021-06-14 23:37:31 +01006793
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006794 /* don't need @sqe from now on */
Olivier Langlois236daeae2021-05-31 02:36:37 -04006795 trace_io_uring_submit_sqe(ctx, req, req->opcode, req->user_data,
6796 req->flags, true,
6797 ctx->flags & IORING_SETUP_SQPOLL);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006798
Jens Axboe6c271ce2019-01-10 11:22:30 -07006799 /*
6800 * If we already have a head request, queue this one for async
6801 * submittal once the head completes. If we don't have a head but
6802 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6803 * submitted sync once the chain is complete. If none of those
6804 * conditions are true (normal request), then just queue it.
6805 */
6806 if (link->head) {
6807 struct io_kiocb *head = link->head;
6808
Hao Xua8295b92021-08-27 17:46:09 +08006809 if (!(req->flags & REQ_F_FAIL)) {
6810 ret = io_req_prep_async(req);
6811 if (unlikely(ret)) {
6812 req_fail_link_node(req, ret);
6813 if (!(head->flags & REQ_F_FAIL))
6814 req_fail_link_node(head, -ECANCELED);
6815 }
6816 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006817 trace_io_uring_link(ctx, req, head);
6818 link->last->link = req;
6819 link->last = req;
6820
6821 /* last request of a link, enqueue the link */
6822 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
6823 link->head = NULL;
Pavel Begunkov5e159202021-06-14 23:37:26 +01006824 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006825 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006826 } else {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006827 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006828 link->head = req;
6829 link->last = req;
6830 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006831 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006832 }
6833 }
6834
6835 return 0;
6836}
6837
6838/*
6839 * Batched submission is done, ensure local IO is flushed out.
6840 */
6841static void io_submit_state_end(struct io_submit_state *state,
6842 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006843{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006844 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006845 io_queue_sqe(state->link.head);
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01006846 if (state->compl_nr)
Pavel Begunkov2a2758f2021-06-17 18:14:00 +01006847 io_submit_flush_completions(ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006848 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006849 blk_finish_plug(&state->plug);
Jens Axboe9e645e112019-05-10 16:07:28 -06006850}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006851
Jens Axboe9e645e112019-05-10 16:07:28 -06006852/*
6853 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006854 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006855static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006856 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006857{
6858 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006859 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006860 /* set only head, no need to init link_last in advance */
6861 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006862}
6863
Jens Axboe193155c2020-02-22 23:22:19 -07006864static void io_commit_sqring(struct io_ring_ctx *ctx)
6865{
Jens Axboe75c6a032020-01-28 10:15:23 -07006866 struct io_rings *rings = ctx->rings;
6867
6868 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006869 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006870 * since once we write the new head, the application could
6871 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006872 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006873 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006874}
6875
Jens Axboe9e645e112019-05-10 16:07:28 -06006876/*
Fam Zhengdd9ae8a2021-06-04 17:42:56 +01006877 * Fetch an sqe, if one is available. Note this returns a pointer to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006878 * that is mapped by userspace. This means that care needs to be taken to
6879 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006880 * being a good citizen. If members of the sqe are validated and then later
6881 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006882 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006883 */
6884static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006885{
Pavel Begunkovea5ab3b2021-05-16 22:58:09 +01006886 unsigned head, mask = ctx->sq_entries - 1;
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006887 unsigned sq_idx = ctx->cached_sq_head++ & mask;
Jens Axboe9e645e112019-05-10 16:07:28 -06006888
6889 /*
6890 * The cached sq head (or cq tail) serves two purposes:
6891 *
6892 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006893 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006894 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006895 * though the application is the one updating it.
6896 */
Pavel Begunkov17d3aeb2021-06-14 23:37:23 +01006897 head = READ_ONCE(ctx->sq_array[sq_idx]);
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006898 if (likely(head < ctx->sq_entries))
6899 return &ctx->sq_sqes[head];
6900
6901 /* drop invalid entries */
Pavel Begunkov15641e42021-06-14 23:37:24 +01006902 ctx->cq_extra--;
6903 WRITE_ONCE(ctx->rings->sq_dropped,
6904 READ_ONCE(ctx->rings->sq_dropped) + 1);
Pavel Begunkov711be032020-01-17 03:57:59 +03006905 return NULL;
6906}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006907
Jens Axboe0f212202020-09-13 13:09:39 -06006908static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Pavel Begunkov282cdc82021-08-09 13:04:10 +01006909 __must_hold(&ctx->uring_lock)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006910{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006911 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006912
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006913 /* make sure SQ entry isn't read before tail */
6914 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006915 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6916 return -EAGAIN;
Pavel Begunkov9a108672021-08-27 11:55:01 +01006917 io_get_task_refs(nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006918
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006919 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006920 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006921 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006922 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006923
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006924 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006925 if (unlikely(!req)) {
6926 if (!submitted)
6927 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006928 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006929 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006930 sqe = io_get_sqe(ctx);
6931 if (unlikely(!sqe)) {
Hao Xu0c6e1d72021-08-26 01:58:56 +08006932 list_add(&req->inflight_entry, &ctx->submit_state.free_list);
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006933 break;
6934 }
Jens Axboed3656342019-12-18 09:50:26 -07006935 /* will complete beyond this point, count as submitted */
6936 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006937 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006938 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006939 }
6940
Pavel Begunkov9466f432020-01-25 22:34:01 +03006941 if (unlikely(submitted != nr)) {
6942 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006943 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006944
Pavel Begunkov09899b12021-06-14 02:36:22 +01006945 current->io_uring->cached_refs += unused;
Jens Axboed8a6df12020-10-15 16:24:45 -06006946 percpu_ref_put_many(&ctx->refs, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006947 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006948
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006949 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006950 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6951 io_commit_sqring(ctx);
6952
Jens Axboe6c271ce2019-01-10 11:22:30 -07006953 return submitted;
6954}
6955
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006956static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
6957{
6958 return READ_ONCE(sqd->state);
6959}
6960
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006961static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6962{
6963 /* Tell userspace we may need a wakeup call */
Jens Axboe79ebeae2021-08-10 15:18:27 -06006964 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006965 WRITE_ONCE(ctx->rings->sq_flags,
6966 ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006967 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006968}
6969
6970static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6971{
Jens Axboe79ebeae2021-08-10 15:18:27 -06006972 spin_lock(&ctx->completion_lock);
Nadav Amit20c0b382021-08-07 17:13:42 -07006973 WRITE_ONCE(ctx->rings->sq_flags,
6974 ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP);
Jens Axboe79ebeae2021-08-10 15:18:27 -06006975 spin_unlock(&ctx->completion_lock);
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006976}
6977
Xiaoguang Wang08369242020-11-03 14:15:59 +08006978static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006979{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006980 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006981 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006982
Jens Axboec8d1ba52020-09-14 11:07:26 -06006983 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006984 /* if we're handling multiple rings, cap submit size for fairness */
Olivier Langlois4ce8ad92021-06-23 11:50:18 -07006985 if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
6986 to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
Jens Axboee95eee22020-09-08 09:11:32 -06006987
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006988 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6989 unsigned nr_events = 0;
Pavel Begunkov948e1942021-06-24 15:09:55 +01006990 const struct cred *creds = NULL;
6991
6992 if (ctx->sq_creds != current_cred())
6993 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006994
Xiaoguang Wang08369242020-11-03 14:15:59 +08006995 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006996 if (!list_empty(&ctx->iopoll_list))
Pavel Begunkova8576af2021-08-15 10:40:21 +01006997 io_do_iopoll(ctx, &nr_events, 0);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006998
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01006999 /*
7000 * Don't submit if refs are dying, good for io_uring_register(),
7001 * but also it is relied upon by io_ring_exit_work()
7002 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00007003 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
7004 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08007005 ret = io_submit_sqes(ctx, to_submit);
7006 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06007007
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007008 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
7009 wake_up(&ctx->sqo_sq_wait);
Pavel Begunkov948e1942021-06-24 15:09:55 +01007010 if (creds)
7011 revert_creds(creds);
Pavel Begunkovacfb3812021-05-16 22:58:03 +01007012 }
Jens Axboe90554202020-09-03 12:12:41 -06007013
Xiaoguang Wang08369242020-11-03 14:15:59 +08007014 return ret;
7015}
7016
7017static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7018{
7019 struct io_ring_ctx *ctx;
7020 unsigned sq_thread_idle = 0;
7021
Pavel Begunkovc9dca272021-03-10 13:13:55 +00007022 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7023 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007024 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007025}
7026
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007027static bool io_sqd_handle_event(struct io_sq_data *sqd)
7028{
7029 bool did_sig = false;
7030 struct ksignal ksig;
7031
7032 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
7033 signal_pending(current)) {
7034 mutex_unlock(&sqd->lock);
7035 if (signal_pending(current))
7036 did_sig = get_signal(&ksig);
7037 cond_resched();
7038 mutex_lock(&sqd->lock);
7039 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007040 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
7041}
7042
Jens Axboe6c271ce2019-01-10 11:22:30 -07007043static int io_sq_thread(void *data)
7044{
Jens Axboe69fb2132020-09-14 11:16:23 -06007045 struct io_sq_data *sqd = data;
7046 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007047 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007048 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08007049 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007050
Pavel Begunkov696ee882021-04-01 09:55:04 +01007051 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007052 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06007053
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007054 if (sqd->sq_cpu != -1)
7055 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
7056 else
7057 set_cpus_allowed_ptr(current, cpu_online_mask);
7058 current->flags |= PF_NO_SETAFFINITY;
7059
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007060 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007061 while (1) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007062 bool cap_entries, sqt_spin = false;
Jens Axboec1edbf52019-11-10 16:56:04 -07007063
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007064 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
7065 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01007066 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007067 timeout = jiffies + sqd->sq_thread_idle;
7068 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01007069
Jens Axboee95eee22020-09-08 09:11:32 -06007070 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007071 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkov948e1942021-06-24 15:09:55 +01007072 int ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007073
Xiaoguang Wang08369242020-11-03 14:15:59 +08007074 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7075 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007076 }
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007077 if (io_run_task_work())
7078 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007079
Xiaoguang Wang08369242020-11-03 14:15:59 +08007080 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007081 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007082 if (sqt_spin)
7083 timeout = jiffies + sqd->sq_thread_idle;
7084 continue;
7085 }
7086
Xiaoguang Wang08369242020-11-03 14:15:59 +08007087 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkovdd432ea52021-06-26 21:40:45 +01007088 if (!io_sqd_events_pending(sqd) && !current->task_works) {
Pavel Begunkov1a924a82021-06-24 15:09:56 +01007089 bool needs_sched = true;
7090
Hao Xu724cb4f2021-04-21 23:19:11 +08007091 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01007092 io_ring_set_wakeup_flag(ctx);
7093
Hao Xu724cb4f2021-04-21 23:19:11 +08007094 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7095 !list_empty_careful(&ctx->iopoll_list)) {
7096 needs_sched = false;
7097 break;
7098 }
7099 if (io_sqring_entries(ctx)) {
7100 needs_sched = false;
7101 break;
7102 }
7103 }
7104
7105 if (needs_sched) {
7106 mutex_unlock(&sqd->lock);
7107 schedule();
7108 mutex_lock(&sqd->lock);
7109 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007110 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7111 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007112 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007113
7114 finish_wait(&sqd->wait, &wait);
7115 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007116 }
7117
Pavel Begunkov78cc6872021-06-14 02:36:23 +01007118 io_uring_cancel_generic(true, sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007119 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07007120 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07007121 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007122 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01007123 mutex_unlock(&sqd->lock);
7124
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007125 complete(&sqd->exited);
7126 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007127}
7128
Jens Axboebda52162019-09-24 13:47:15 -06007129struct io_wait_queue {
7130 struct wait_queue_entry wq;
7131 struct io_ring_ctx *ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007132 unsigned cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007133 unsigned nr_timeouts;
7134};
7135
Pavel Begunkov6c503152021-01-04 20:36:36 +00007136static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007137{
7138 struct io_ring_ctx *ctx = iowq->ctx;
Jens Axboe5fd46172021-08-06 14:04:31 -06007139 int dist = ctx->cached_cq_tail - (int) iowq->cq_tail;
Jens Axboebda52162019-09-24 13:47:15 -06007140
7141 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007142 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007143 * started waiting. For timeouts, we always want to return to userspace,
7144 * regardless of event count.
7145 */
Jens Axboe5fd46172021-08-06 14:04:31 -06007146 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
Jens Axboebda52162019-09-24 13:47:15 -06007147}
7148
7149static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7150 int wake_flags, void *key)
7151{
7152 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7153 wq);
7154
Pavel Begunkov6c503152021-01-04 20:36:36 +00007155 /*
7156 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7157 * the task, and the next invocation will do it.
7158 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007159 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow))
Pavel Begunkov6c503152021-01-04 20:36:36 +00007160 return autoremove_wake_function(curr, mode, wake_flags, key);
7161 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007162}
7163
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007164static int io_run_task_work_sig(void)
7165{
7166 if (io_run_task_work())
7167 return 1;
7168 if (!signal_pending(current))
7169 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06007170 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06007171 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007172 return -EINTR;
7173}
7174
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007175/* when returns >0, the caller should retry */
7176static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7177 struct io_wait_queue *iowq,
7178 signed long *timeout)
7179{
7180 int ret;
7181
7182 /* make sure we run task_work before checking for signals */
7183 ret = io_run_task_work_sig();
7184 if (ret || io_should_wake(iowq))
7185 return ret;
7186 /* let the caller flush overflows, retry */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01007187 if (test_bit(0, &ctx->check_cq_overflow))
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007188 return 1;
7189
7190 *timeout = schedule_timeout(*timeout);
7191 return !*timeout ? -ETIME : 1;
7192}
7193
Jens Axboe2b188cc2019-01-07 10:46:33 -07007194/*
7195 * Wait until events become available, if we don't already have some. The
7196 * application must reap them itself, as they reside on the shared cq ring.
7197 */
7198static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007199 const sigset_t __user *sig, size_t sigsz,
7200 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007201{
Pavel Begunkov902910992021-08-09 09:07:32 -06007202 struct io_wait_queue iowq;
Hristo Venev75b28af2019-08-26 17:23:46 +00007203 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007204 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7205 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007206
Jens Axboeb41e9852020-02-17 09:52:41 -07007207 do {
Pavel Begunkov90f67362021-08-09 20:18:12 +01007208 io_cqring_overflow_flush(ctx);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007209 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007210 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007211 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007212 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007213 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007214
7215 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007216#ifdef CONFIG_COMPAT
7217 if (in_compat_syscall())
7218 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007219 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007220 else
7221#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007222 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007223
Jens Axboe2b188cc2019-01-07 10:46:33 -07007224 if (ret)
7225 return ret;
7226 }
7227
Hao Xuc73ebb62020-11-03 10:54:37 +08007228 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007229 struct timespec64 ts;
7230
Hao Xuc73ebb62020-11-03 10:54:37 +08007231 if (get_timespec64(&ts, uts))
7232 return -EFAULT;
7233 timeout = timespec64_to_jiffies(&ts);
7234 }
7235
Pavel Begunkov902910992021-08-09 09:07:32 -06007236 init_waitqueue_func_entry(&iowq.wq, io_wake_function);
7237 iowq.wq.private = current;
7238 INIT_LIST_HEAD(&iowq.wq.entry);
7239 iowq.ctx = ctx;
Jens Axboebda52162019-09-24 13:47:15 -06007240 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Jens Axboe5fd46172021-08-06 14:04:31 -06007241 iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events;
Pavel Begunkov902910992021-08-09 09:07:32 -06007242
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007243 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007244 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007245 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov90f67362021-08-09 20:18:12 +01007246 if (!io_cqring_overflow_flush(ctx)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007247 ret = -EBUSY;
7248 break;
7249 }
Pavel Begunkov311997b2021-06-14 23:37:28 +01007250 prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq,
Jens Axboebda52162019-09-24 13:47:15 -06007251 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007252 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
Pavel Begunkov311997b2021-06-14 23:37:28 +01007253 finish_wait(&ctx->cq_wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007254 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007255 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007256
Jens Axboeb7db41c2020-07-04 08:55:50 -06007257 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007258
Hristo Venev75b28af2019-08-26 17:23:46 +00007259 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007260}
7261
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007262static void io_free_page_table(void **table, size_t size)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007263{
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007264 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007265
7266 for (i = 0; i < nr_tables; i++)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007267 kfree(table[i]);
7268 kfree(table);
7269}
7270
7271static void **io_alloc_page_table(size_t size)
7272{
7273 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
7274 size_t init_size = size;
7275 void **table;
7276
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007277 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007278 if (!table)
7279 return NULL;
7280
7281 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov27f6b312021-06-15 13:20:13 +01007282 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007283
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007284 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007285 if (!table[i]) {
7286 io_free_page_table(table, init_size);
7287 return NULL;
7288 }
7289 size -= this_size;
7290 }
7291 return table;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007292}
7293
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007294static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7295{
7296 percpu_ref_exit(&ref_node->refs);
7297 kfree(ref_node);
7298}
7299
Pavel Begunkovb9bd2be2021-08-09 09:09:47 -06007300static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
7301{
7302 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
7303 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
7304 unsigned long flags;
7305 bool first_add = false;
7306
7307 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
7308 node->done = true;
7309
7310 while (!list_empty(&ctx->rsrc_ref_list)) {
7311 node = list_first_entry(&ctx->rsrc_ref_list,
7312 struct io_rsrc_node, node);
7313 /* recycle ref nodes in order */
7314 if (!node->done)
7315 break;
7316 list_del(&node->node);
7317 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
7318 }
7319 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
7320
7321 if (first_add)
7322 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
7323}
7324
7325static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
7326{
7327 struct io_rsrc_node *ref_node;
7328
7329 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7330 if (!ref_node)
7331 return NULL;
7332
7333 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
7334 0, GFP_KERNEL)) {
7335 kfree(ref_node);
7336 return NULL;
7337 }
7338 INIT_LIST_HEAD(&ref_node->node);
7339 INIT_LIST_HEAD(&ref_node->rsrc_list);
7340 ref_node->done = false;
7341 return ref_node;
7342}
7343
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007344static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7345 struct io_rsrc_data *data_to_kill)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007346{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007347 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7348 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007349
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007350 if (data_to_kill) {
7351 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007352
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007353 rsrc_node->rsrc_data = data_to_kill;
Jens Axboe4956b9e2021-08-09 07:49:41 -06007354 spin_lock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007355 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
Jens Axboe4956b9e2021-08-09 07:49:41 -06007356 spin_unlock_irq(&ctx->rsrc_ref_lock);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007357
Pavel Begunkov3e942492021-04-11 01:46:34 +01007358 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007359 percpu_ref_kill(&rsrc_node->refs);
7360 ctx->rsrc_node = NULL;
7361 }
7362
7363 if (!ctx->rsrc_node) {
7364 ctx->rsrc_node = ctx->rsrc_backup_node;
7365 ctx->rsrc_backup_node = NULL;
7366 }
Pavel Begunkov1642b442020-12-30 21:34:14 +00007367}
7368
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007369static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007370{
7371 if (ctx->rsrc_backup_node)
7372 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007373 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007374 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7375}
7376
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007377static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007378{
7379 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007380
Pavel Begunkov215c3902021-04-01 15:43:48 +01007381 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007382 if (data->quiesce)
7383 return -ENXIO;
7384
7385 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007386 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007387 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007388 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007389 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007390 io_rsrc_node_switch(ctx, data);
7391
Pavel Begunkov3e942492021-04-11 01:46:34 +01007392 /* kill initial ref, already quiesced if zero */
7393 if (atomic_dec_and_test(&data->refs))
7394 break;
Jens Axboec018db42021-08-09 08:15:50 -06007395 mutex_unlock(&ctx->uring_lock);
Hao Xu8bad28d2021-02-19 17:19:36 +08007396 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007397 ret = wait_for_completion_interruptible(&data->done);
Jens Axboec018db42021-08-09 08:15:50 -06007398 if (!ret) {
7399 mutex_lock(&ctx->uring_lock);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007400 break;
Jens Axboec018db42021-08-09 08:15:50 -06007401 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007402
Pavel Begunkov3e942492021-04-11 01:46:34 +01007403 atomic_inc(&data->refs);
7404 /* wait for all works potentially completing data->done */
7405 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007406 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007407
Hao Xu8bad28d2021-02-19 17:19:36 +08007408 ret = io_run_task_work_sig();
7409 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007410 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007411 data->quiesce = false;
7412
Hao Xu8bad28d2021-02-19 17:19:36 +08007413 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007414}
7415
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007416static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
7417{
7418 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
7419 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
7420
7421 return &data->tags[table_idx][off];
7422}
7423
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007424static void io_rsrc_data_free(struct io_rsrc_data *data)
7425{
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007426 size_t size = data->nr * sizeof(data->tags[0][0]);
7427
7428 if (data->tags)
7429 io_free_page_table((void **)data->tags, size);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007430 kfree(data);
7431}
7432
Pavel Begunkovd878c812021-06-14 02:36:18 +01007433static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put,
7434 u64 __user *utags, unsigned nr,
7435 struct io_rsrc_data **pdata)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007436{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007437 struct io_rsrc_data *data;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007438 int ret = -ENOMEM;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007439 unsigned i;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007440
7441 data = kzalloc(sizeof(*data), GFP_KERNEL);
7442 if (!data)
Pavel Begunkovd878c812021-06-14 02:36:18 +01007443 return -ENOMEM;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007444 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007445 if (!data->tags) {
7446 kfree(data);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007447 return -ENOMEM;
7448 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007449
7450 data->nr = nr;
7451 data->ctx = ctx;
7452 data->do_put = do_put;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007453 if (utags) {
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007454 ret = -EFAULT;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007455 for (i = 0; i < nr; i++) {
Colin Ian Kingfdd1dc32021-06-15 14:00:11 +01007456 u64 *tag_slot = io_get_tag_slot(data, i);
7457
7458 if (copy_from_user(tag_slot, &utags[i],
7459 sizeof(*tag_slot)))
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007460 goto fail;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007461 }
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007462 }
7463
Pavel Begunkov3e942492021-04-11 01:46:34 +01007464 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007465 init_completion(&data->done);
Pavel Begunkovd878c812021-06-14 02:36:18 +01007466 *pdata = data;
7467 return 0;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007468fail:
7469 io_rsrc_data_free(data);
7470 return ret;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007471}
7472
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007473static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
7474{
Pavel Begunkov0bea96f2021-08-20 10:36:36 +01007475 table->files = kvcalloc(nr_files, sizeof(table->files[0]),
7476 GFP_KERNEL_ACCOUNT);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007477 return !!table->files;
7478}
7479
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007480static void io_free_file_tables(struct io_file_table *table)
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007481{
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007482 kvfree(table->files);
Pavel Begunkov9123c8f2021-06-14 02:36:20 +01007483 table->files = NULL;
7484}
7485
Jens Axboe2b188cc2019-01-07 10:46:33 -07007486static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7487{
7488#if defined(CONFIG_UNIX)
7489 if (ctx->ring_sock) {
7490 struct sock *sock = ctx->ring_sock->sk;
7491 struct sk_buff *skb;
7492
7493 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7494 kfree_skb(skb);
7495 }
7496#else
7497 int i;
7498
7499 for (i = 0; i < ctx->nr_user_files; i++) {
7500 struct file *file;
7501
7502 file = io_file_from_index(ctx, i);
7503 if (file)
7504 fput(file);
7505 }
7506#endif
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007507 io_free_file_tables(&ctx->file_table);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007508 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007509 ctx->file_data = NULL;
7510 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007511}
7512
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007513static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7514{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007515 int ret;
7516
Pavel Begunkov08480402021-04-13 02:58:38 +01007517 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007518 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007519 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7520 if (!ret)
7521 __io_sqe_files_unregister(ctx);
7522 return ret;
Jens Axboe6b063142019-01-10 22:13:58 -07007523}
7524
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007525static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007526 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007527{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007528 WARN_ON_ONCE(sqd->thread == current);
7529
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007530 /*
7531 * Do the dance but not conditional clear_bit() because it'd race with
7532 * other threads incrementing park_pending and setting the bit.
7533 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007534 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007535 if (atomic_dec_return(&sqd->park_pending))
7536 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007537 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007538}
7539
Jens Axboe86e0d672021-03-05 08:44:39 -07007540static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007541 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007542{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007543 WARN_ON_ONCE(sqd->thread == current);
7544
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007545 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007546 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007547 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007548 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007549 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007550}
7551
7552static void io_sq_thread_stop(struct io_sq_data *sqd)
7553{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007554 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007555 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007556
Jens Axboe05962f92021-03-06 13:58:48 -07007557 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007558 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007559 if (sqd->thread)
7560 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007561 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007562 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007563}
7564
Jens Axboe534ca6d2020-09-02 13:52:19 -06007565static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007566{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007567 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007568 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7569
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007570 io_sq_thread_stop(sqd);
7571 kfree(sqd);
7572 }
7573}
7574
7575static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7576{
7577 struct io_sq_data *sqd = ctx->sq_data;
7578
7579 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007580 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007581 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007582 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007583 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007584
7585 io_put_sq_data(sqd);
7586 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007587 }
7588}
7589
Jens Axboeaa061652020-09-02 14:50:27 -06007590static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7591{
7592 struct io_ring_ctx *ctx_attach;
7593 struct io_sq_data *sqd;
7594 struct fd f;
7595
7596 f = fdget(p->wq_fd);
7597 if (!f.file)
7598 return ERR_PTR(-ENXIO);
7599 if (f.file->f_op != &io_uring_fops) {
7600 fdput(f);
7601 return ERR_PTR(-EINVAL);
7602 }
7603
7604 ctx_attach = f.file->private_data;
7605 sqd = ctx_attach->sq_data;
7606 if (!sqd) {
7607 fdput(f);
7608 return ERR_PTR(-EINVAL);
7609 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007610 if (sqd->task_tgid != current->tgid) {
7611 fdput(f);
7612 return ERR_PTR(-EPERM);
7613 }
Jens Axboeaa061652020-09-02 14:50:27 -06007614
7615 refcount_inc(&sqd->refs);
7616 fdput(f);
7617 return sqd;
7618}
7619
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007620static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7621 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007622{
7623 struct io_sq_data *sqd;
7624
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007625 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007626 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7627 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007628 if (!IS_ERR(sqd)) {
7629 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007630 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007631 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007632 /* fall through for EPERM case, setup new sqd/task */
7633 if (PTR_ERR(sqd) != -EPERM)
7634 return sqd;
7635 }
Jens Axboeaa061652020-09-02 14:50:27 -06007636
Jens Axboe534ca6d2020-09-02 13:52:19 -06007637 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7638 if (!sqd)
7639 return ERR_PTR(-ENOMEM);
7640
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007641 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007642 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007643 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007644 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007645 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007646 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007647 return sqd;
7648}
7649
Jens Axboe6b063142019-01-10 22:13:58 -07007650#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007651/*
7652 * Ensure the UNIX gc is aware of our file set, so we are certain that
7653 * the io_uring can be safely unregistered on process exit, even if we have
7654 * loops in the file referencing.
7655 */
7656static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7657{
7658 struct sock *sk = ctx->ring_sock->sk;
7659 struct scm_fp_list *fpl;
7660 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007661 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007662
Jens Axboe6b063142019-01-10 22:13:58 -07007663 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7664 if (!fpl)
7665 return -ENOMEM;
7666
7667 skb = alloc_skb(0, GFP_KERNEL);
7668 if (!skb) {
7669 kfree(fpl);
7670 return -ENOMEM;
7671 }
7672
7673 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007674
Jens Axboe08a45172019-10-03 08:11:03 -06007675 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007676 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007677 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007678 struct file *file = io_file_from_index(ctx, i + offset);
7679
7680 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007681 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007682 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007683 unix_inflight(fpl->user, fpl->fp[nr_files]);
7684 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007685 }
7686
Jens Axboe08a45172019-10-03 08:11:03 -06007687 if (nr_files) {
7688 fpl->max = SCM_MAX_FD;
7689 fpl->count = nr_files;
7690 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007691 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007692 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7693 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007694
Jens Axboe08a45172019-10-03 08:11:03 -06007695 for (i = 0; i < nr_files; i++)
7696 fput(fpl->fp[i]);
7697 } else {
7698 kfree_skb(skb);
7699 kfree(fpl);
7700 }
Jens Axboe6b063142019-01-10 22:13:58 -07007701
7702 return 0;
7703}
7704
7705/*
7706 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7707 * causes regular reference counting to break down. We rely on the UNIX
7708 * garbage collection to take care of this problem for us.
7709 */
7710static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7711{
7712 unsigned left, total;
7713 int ret = 0;
7714
7715 total = 0;
7716 left = ctx->nr_user_files;
7717 while (left) {
7718 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007719
7720 ret = __io_sqe_files_scm(ctx, this_files, total);
7721 if (ret)
7722 break;
7723 left -= this_files;
7724 total += this_files;
7725 }
7726
7727 if (!ret)
7728 return 0;
7729
7730 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007731 struct file *file = io_file_from_index(ctx, total);
7732
7733 if (file)
7734 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007735 total++;
7736 }
7737
7738 return ret;
7739}
7740#else
7741static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7742{
7743 return 0;
7744}
7745#endif
7746
Pavel Begunkov47e90392021-04-01 15:43:56 +01007747static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007748{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007749 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007750#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007751 struct sock *sock = ctx->ring_sock->sk;
7752 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7753 struct sk_buff *skb;
7754 int i;
7755
7756 __skb_queue_head_init(&list);
7757
7758 /*
7759 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7760 * remove this entry and rearrange the file array.
7761 */
7762 skb = skb_dequeue(head);
7763 while (skb) {
7764 struct scm_fp_list *fp;
7765
7766 fp = UNIXCB(skb).fp;
7767 for (i = 0; i < fp->count; i++) {
7768 int left;
7769
7770 if (fp->fp[i] != file)
7771 continue;
7772
7773 unix_notinflight(fp->user, fp->fp[i]);
7774 left = fp->count - 1 - i;
7775 if (left) {
7776 memmove(&fp->fp[i], &fp->fp[i + 1],
7777 left * sizeof(struct file *));
7778 }
7779 fp->count--;
7780 if (!fp->count) {
7781 kfree_skb(skb);
7782 skb = NULL;
7783 } else {
7784 __skb_queue_tail(&list, skb);
7785 }
7786 fput(file);
7787 file = NULL;
7788 break;
7789 }
7790
7791 if (!file)
7792 break;
7793
7794 __skb_queue_tail(&list, skb);
7795
7796 skb = skb_dequeue(head);
7797 }
7798
7799 if (skb_peek(&list)) {
7800 spin_lock_irq(&head->lock);
7801 while ((skb = __skb_dequeue(&list)) != NULL)
7802 __skb_queue_tail(head, skb);
7803 spin_unlock_irq(&head->lock);
7804 }
7805#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007806 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007807#endif
7808}
7809
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007810static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007811{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007812 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007813 struct io_ring_ctx *ctx = rsrc_data->ctx;
7814 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007815
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007816 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7817 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007818
7819 if (prsrc->tag) {
7820 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007821
7822 io_ring_submit_lock(ctx, lock_ring);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007823 spin_lock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007824 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01007825 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007826 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06007827 spin_unlock(&ctx->completion_lock);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007828 io_cqring_ev_posted(ctx);
7829 io_ring_submit_unlock(ctx, lock_ring);
7830 }
7831
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007832 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007833 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007834 }
7835
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007836 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01007837 if (atomic_dec_and_test(&rsrc_data->refs))
7838 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007839}
7840
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007841static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007842{
7843 struct io_ring_ctx *ctx;
7844 struct llist_node *node;
7845
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007846 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7847 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007848
7849 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007850 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007851 struct llist_node *next = node->next;
7852
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007853 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007854 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007855 node = next;
7856 }
7857}
7858
Jens Axboe05f3fb32019-12-09 11:22:50 -07007859static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01007860 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007861{
7862 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007863 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007864 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007865 unsigned i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007866
7867 if (ctx->file_data)
7868 return -EBUSY;
7869 if (!nr_args)
7870 return -EINVAL;
7871 if (nr_args > IORING_MAX_FIXED_FILES)
7872 return -EMFILE;
Pavel Begunkov3a1b8a42021-08-20 10:36:35 +01007873 if (nr_args > rlimit(RLIMIT_NOFILE))
7874 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007875 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007876 if (ret)
7877 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01007878 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
7879 &ctx->file_data);
7880 if (ret)
7881 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007882
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007883 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007884 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007885 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007886
Jens Axboe05f3fb32019-12-09 11:22:50 -07007887 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkovd878c812021-06-14 02:36:18 +01007888 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007889 ret = -EFAULT;
7890 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007891 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007892 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01007893 if (fd == -1) {
7894 ret = -EINVAL;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01007895 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
Pavel Begunkov792e3582021-04-25 14:32:21 +01007896 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007897 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007898 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007899
Jens Axboe05f3fb32019-12-09 11:22:50 -07007900 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007901 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007902 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007903 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007904
7905 /*
7906 * Don't allow io_uring instances to be registered. If UNIX
7907 * isn't enabled, then this causes a reference cycle and this
7908 * instance can never get freed. If UNIX is enabled we'll
7909 * handle it just fine, but there's still no point in allowing
7910 * a ring fd as it doesn't support regular read/write anyway.
7911 */
7912 if (file->f_op == &io_uring_fops) {
7913 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007914 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007915 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007916 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007917 }
7918
Jens Axboe05f3fb32019-12-09 11:22:50 -07007919 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007920 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01007921 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007922 return ret;
7923 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007924
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007925 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007926 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007927out_fput:
7928 for (i = 0; i < ctx->nr_user_files; i++) {
7929 file = io_file_from_index(ctx, i);
7930 if (file)
7931 fput(file);
7932 }
Pavel Begunkov042b0d82021-08-09 13:04:01 +01007933 io_free_file_tables(&ctx->file_table);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007934 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007935out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007936 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007937 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007938 return ret;
7939}
7940
Jens Axboec3a31e62019-10-03 13:59:56 -06007941static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7942 int index)
7943{
7944#if defined(CONFIG_UNIX)
7945 struct sock *sock = ctx->ring_sock->sk;
7946 struct sk_buff_head *head = &sock->sk_receive_queue;
7947 struct sk_buff *skb;
7948
7949 /*
7950 * See if we can merge this file into an existing skb SCM_RIGHTS
7951 * file set. If there's no room, fall back to allocating a new skb
7952 * and filling it in.
7953 */
7954 spin_lock_irq(&head->lock);
7955 skb = skb_peek(head);
7956 if (skb) {
7957 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7958
7959 if (fpl->count < SCM_MAX_FD) {
7960 __skb_unlink(skb, head);
7961 spin_unlock_irq(&head->lock);
7962 fpl->fp[fpl->count] = get_file(file);
7963 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7964 fpl->count++;
7965 spin_lock_irq(&head->lock);
7966 __skb_queue_head(head, skb);
7967 } else {
7968 skb = NULL;
7969 }
7970 }
7971 spin_unlock_irq(&head->lock);
7972
7973 if (skb) {
7974 fput(file);
7975 return 0;
7976 }
7977
7978 return __io_sqe_files_scm(ctx, 1, index);
7979#else
7980 return 0;
7981#endif
7982}
7983
Pavel Begunkovb9445592021-08-25 12:25:45 +01007984static int io_install_fixed_file(struct io_kiocb *req, struct file *file,
7985 unsigned int issue_flags, u32 slot_index)
7986{
7987 struct io_ring_ctx *ctx = req->ctx;
7988 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
7989 struct io_fixed_file *file_slot;
7990 int ret = -EBADF;
7991
7992 io_ring_submit_lock(ctx, !force_nonblock);
7993 if (file->f_op == &io_uring_fops)
7994 goto err;
7995 ret = -ENXIO;
7996 if (!ctx->file_data)
7997 goto err;
7998 ret = -EINVAL;
7999 if (slot_index >= ctx->nr_user_files)
8000 goto err;
8001
8002 slot_index = array_index_nospec(slot_index, ctx->nr_user_files);
8003 file_slot = io_fixed_file_slot(&ctx->file_table, slot_index);
8004 ret = -EBADF;
8005 if (file_slot->file_ptr)
8006 goto err;
8007
8008 *io_get_tag_slot(ctx->file_data, slot_index) = 0;
8009 io_fixed_file_set(file_slot, file);
8010 ret = io_sqe_file_register(ctx, file, slot_index);
8011 if (ret) {
8012 file_slot->file_ptr = 0;
8013 goto err;
8014 }
8015
8016 ret = 0;
8017err:
8018 io_ring_submit_unlock(ctx, !force_nonblock);
8019 if (ret)
8020 fput(file);
8021 return ret;
8022}
8023
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008024static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
Pavel Begunkove7c78372021-04-01 15:43:45 +01008025 struct io_rsrc_node *node, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07008026{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008027 struct io_rsrc_put *prsrc;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008028
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008029 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
8030 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08008031 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008032
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008033 prsrc->tag = *io_get_tag_slot(data, idx);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008034 prsrc->rsrc = rsrc;
Pavel Begunkove7c78372021-04-01 15:43:45 +01008035 list_add(&prsrc->list, &node->rsrc_list);
Hillf Dantona5318d32020-03-23 17:47:15 +08008036 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008037}
8038
8039static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008040 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008041 unsigned nr_args)
8042{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008043 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008044 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008045 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008046 struct io_fixed_file *file_slot;
8047 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008048 int fd, i, err = 0;
8049 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008050 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008051
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01008052 if (!ctx->file_data)
8053 return -ENXIO;
8054 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06008055 return -EINVAL;
8056
Pavel Begunkov67973b92021-01-26 13:51:09 +00008057 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008058 u64 tag = 0;
8059
8060 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
8061 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008062 err = -EFAULT;
8063 break;
8064 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01008065 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
8066 err = -EINVAL;
8067 break;
8068 }
noah4e0377a2021-01-26 15:23:28 -05008069 if (fd == IORING_REGISTER_FILES_SKIP)
8070 continue;
8071
Pavel Begunkov67973b92021-01-26 13:51:09 +00008072 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01008073 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008074
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008075 if (file_slot->file_ptr) {
8076 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01008077 err = io_queue_rsrc_removal(data, up->offset + done,
8078 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08008079 if (err)
8080 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008081 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008082 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008083 }
8084 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008085 file = fget(fd);
8086 if (!file) {
8087 err = -EBADF;
8088 break;
8089 }
8090 /*
8091 * Don't allow io_uring instances to be registered. If
8092 * UNIX isn't enabled, then this causes a reference
8093 * cycle and this instance can never get freed. If UNIX
8094 * is enabled we'll handle it just fine, but there's
8095 * still no point in allowing a ring fd as it doesn't
8096 * support regular read/write anyway.
8097 */
8098 if (file->f_op == &io_uring_fops) {
8099 fput(file);
8100 err = -EBADF;
8101 break;
8102 }
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008103 *io_get_tag_slot(data, up->offset + done) = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01008104 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008105 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008106 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01008107 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008108 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008109 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008110 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008111 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008112 }
8113
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008114 if (needs_switch)
8115 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06008116 return done ? done : err;
8117}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008118
Jens Axboe685fe7f2021-03-08 09:37:51 -07008119static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
8120 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008121{
Jens Axboee9418942021-02-19 12:33:30 -07008122 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008123 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008124 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008125
Yang Yingliang362a9e62021-07-20 16:38:05 +08008126 mutex_lock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008127 hash = ctx->hash_map;
8128 if (!hash) {
8129 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008130 if (!hash) {
8131 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008132 return ERR_PTR(-ENOMEM);
Yang Yingliang362a9e62021-07-20 16:38:05 +08008133 }
Jens Axboee9418942021-02-19 12:33:30 -07008134 refcount_set(&hash->refs, 1);
8135 init_waitqueue_head(&hash->wait);
8136 ctx->hash_map = hash;
8137 }
Yang Yingliang362a9e62021-07-20 16:38:05 +08008138 mutex_unlock(&ctx->uring_lock);
Jens Axboee9418942021-02-19 12:33:30 -07008139
8140 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07008141 data.task = task;
Pavel Begunkovebc11b62021-08-09 13:04:05 +01008142 data.free_work = io_wq_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008143 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008144
Jens Axboed25e3a32021-02-16 11:41:41 -07008145 /* Do QD, or 4 * CPUS, whatever is smallest */
8146 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008147
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008148 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008149}
8150
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008151static int io_uring_alloc_task_context(struct task_struct *task,
8152 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008153{
8154 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008155 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008156
Pavel Begunkov09899b12021-06-14 02:36:22 +01008157 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
Jens Axboe0f212202020-09-13 13:09:39 -06008158 if (unlikely(!tctx))
8159 return -ENOMEM;
8160
Jens Axboed8a6df12020-10-15 16:24:45 -06008161 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8162 if (unlikely(ret)) {
8163 kfree(tctx);
8164 return ret;
8165 }
8166
Jens Axboe685fe7f2021-03-08 09:37:51 -07008167 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008168 if (IS_ERR(tctx->io_wq)) {
8169 ret = PTR_ERR(tctx->io_wq);
8170 percpu_counter_destroy(&tctx->inflight);
8171 kfree(tctx);
8172 return ret;
8173 }
8174
Jens Axboe0f212202020-09-13 13:09:39 -06008175 xa_init(&tctx->xa);
8176 init_waitqueue_head(&tctx->wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008177 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01008178 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06008179 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008180 spin_lock_init(&tctx->task_lock);
8181 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe7cbf1722021-02-10 00:03:20 +00008182 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008183 return 0;
8184}
8185
8186void __io_uring_free(struct task_struct *tsk)
8187{
8188 struct io_uring_task *tctx = tsk->io_uring;
8189
8190 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008191 WARN_ON_ONCE(tctx->io_wq);
Pavel Begunkov09899b12021-06-14 02:36:22 +01008192 WARN_ON_ONCE(tctx->cached_refs);
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00008193
Jens Axboed8a6df12020-10-15 16:24:45 -06008194 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008195 kfree(tctx);
8196 tsk->io_uring = NULL;
8197}
8198
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008199static int io_sq_offload_create(struct io_ring_ctx *ctx,
8200 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008201{
8202 int ret;
8203
Jens Axboed25e3a32021-02-16 11:41:41 -07008204 /* Retain compatibility with failing for an invalid attach attempt */
8205 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8206 IORING_SETUP_ATTACH_WQ) {
8207 struct fd f;
8208
8209 f = fdget(p->wq_fd);
8210 if (!f.file)
8211 return -ENXIO;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008212 if (f.file->f_op != &io_uring_fops) {
8213 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008214 return -EINVAL;
Jens Axboe0cc936f2021-07-22 17:08:07 -06008215 }
8216 fdput(f);
Jens Axboed25e3a32021-02-16 11:41:41 -07008217 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008218 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07008219 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008220 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008221 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06008222
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008223 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008224 if (IS_ERR(sqd)) {
8225 ret = PTR_ERR(sqd);
8226 goto err;
8227 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008228
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01008229 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06008230 ctx->sq_data = sqd;
Jens Axboe917257d2019-04-13 09:28:55 -06008231 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8232 if (!ctx->sq_thread_idle)
8233 ctx->sq_thread_idle = HZ;
8234
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008235 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008236 list_add(&ctx->sqd_list, &sqd->ctx_list);
8237 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00008238 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008239 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00008240 io_sq_thread_unpark(sqd);
8241
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00008242 if (ret < 0)
8243 goto err;
8244 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008245 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008246
Jens Axboe6c271ce2019-01-10 11:22:30 -07008247 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008248 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008249
Jens Axboe917257d2019-04-13 09:28:55 -06008250 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008251 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07008252 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008253 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008254 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008255 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008256 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008257
8258 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07008259 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07008260 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
8261 if (IS_ERR(tsk)) {
8262 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008263 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008264 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008265
Jens Axboe46fe18b2021-03-04 12:39:36 -07008266 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008267 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008268 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008269 if (ret)
8270 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008271 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8272 /* Can't have SQ_AFF without SQPOLL */
8273 ret = -EINVAL;
8274 goto err;
8275 }
8276
Jens Axboe2b188cc2019-01-07 10:46:33 -07008277 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008278err_sqpoll:
8279 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008280err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008281 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008282 return ret;
8283}
8284
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008285static inline void __io_unaccount_mem(struct user_struct *user,
8286 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008287{
8288 atomic_long_sub(nr_pages, &user->locked_vm);
8289}
8290
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008291static inline int __io_account_mem(struct user_struct *user,
8292 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008293{
8294 unsigned long page_limit, cur_pages, new_pages;
8295
8296 /* Don't allow more pages than we can safely lock */
8297 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8298
8299 do {
8300 cur_pages = atomic_long_read(&user->locked_vm);
8301 new_pages = cur_pages + nr_pages;
8302 if (new_pages > page_limit)
8303 return -ENOMEM;
8304 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8305 new_pages) != cur_pages);
8306
8307 return 0;
8308}
8309
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008310static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008311{
Jens Axboe62e398b2021-02-21 16:19:37 -07008312 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008313 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008314
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008315 if (ctx->mm_account)
8316 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008317}
8318
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008319static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008320{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008321 int ret;
8322
Jens Axboe62e398b2021-02-21 16:19:37 -07008323 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008324 ret = __io_account_mem(ctx->user, nr_pages);
8325 if (ret)
8326 return ret;
8327 }
8328
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008329 if (ctx->mm_account)
8330 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008331
8332 return 0;
8333}
8334
Jens Axboe2b188cc2019-01-07 10:46:33 -07008335static void io_mem_free(void *ptr)
8336{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008337 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008338
Mark Rutland52e04ef2019-04-30 17:30:21 +01008339 if (!ptr)
8340 return;
8341
8342 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008343 if (put_page_testzero(page))
8344 free_compound_page(page);
8345}
8346
8347static void *io_mem_alloc(size_t size)
8348{
8349 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008350 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008351
8352 return (void *) __get_free_pages(gfp_flags, get_order(size));
8353}
8354
Hristo Venev75b28af2019-08-26 17:23:46 +00008355static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8356 size_t *sq_offset)
8357{
8358 struct io_rings *rings;
8359 size_t off, sq_array_size;
8360
8361 off = struct_size(rings, cqes, cq_entries);
8362 if (off == SIZE_MAX)
8363 return SIZE_MAX;
8364
8365#ifdef CONFIG_SMP
8366 off = ALIGN(off, SMP_CACHE_BYTES);
8367 if (off == 0)
8368 return SIZE_MAX;
8369#endif
8370
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008371 if (sq_offset)
8372 *sq_offset = off;
8373
Hristo Venev75b28af2019-08-26 17:23:46 +00008374 sq_array_size = array_size(sizeof(u32), sq_entries);
8375 if (sq_array_size == SIZE_MAX)
8376 return SIZE_MAX;
8377
8378 if (check_add_overflow(off, sq_array_size, &off))
8379 return SIZE_MAX;
8380
Hristo Venev75b28af2019-08-26 17:23:46 +00008381 return off;
8382}
8383
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008384static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008385{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008386 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008387 unsigned int i;
8388
Pavel Begunkov62248432021-04-28 13:11:29 +01008389 if (imu != ctx->dummy_ubuf) {
8390 for (i = 0; i < imu->nr_bvecs; i++)
8391 unpin_user_page(imu->bvec[i].bv_page);
8392 if (imu->acct_pages)
8393 io_unaccount_mem(ctx, imu->acct_pages);
8394 kvfree(imu);
8395 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008396 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008397}
8398
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008399static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8400{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008401 io_buffer_unmap(ctx, &prsrc->buf);
8402 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008403}
8404
8405static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008406{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008407 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008408
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008409 for (i = 0; i < ctx->nr_user_bufs; i++)
8410 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008411 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008412 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008413 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008414 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008415 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008416}
8417
Jens Axboeedafcce2019-01-09 09:16:05 -07008418static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8419{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008420 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008421
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008422 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008423 return -ENXIO;
8424
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008425 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8426 if (!ret)
8427 __io_sqe_buffers_unregister(ctx);
8428 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008429}
8430
8431static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8432 void __user *arg, unsigned index)
8433{
8434 struct iovec __user *src;
8435
8436#ifdef CONFIG_COMPAT
8437 if (ctx->compat) {
8438 struct compat_iovec __user *ciovs;
8439 struct compat_iovec ciov;
8440
8441 ciovs = (struct compat_iovec __user *) arg;
8442 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8443 return -EFAULT;
8444
Jens Axboed55e5f52019-12-11 16:12:15 -07008445 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008446 dst->iov_len = ciov.iov_len;
8447 return 0;
8448 }
8449#endif
8450 src = (struct iovec __user *) arg;
8451 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8452 return -EFAULT;
8453 return 0;
8454}
8455
Jens Axboede293932020-09-17 16:19:16 -06008456/*
8457 * Not super efficient, but this is just a registration time. And we do cache
8458 * the last compound head, so generally we'll only do a full search if we don't
8459 * match that one.
8460 *
8461 * We check if the given compound head page has already been accounted, to
8462 * avoid double accounting it. This allows us to account the full size of the
8463 * page, not just the constituent pages of a huge page.
8464 */
8465static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8466 int nr_pages, struct page *hpage)
8467{
8468 int i, j;
8469
8470 /* check current page array */
8471 for (i = 0; i < nr_pages; i++) {
8472 if (!PageCompound(pages[i]))
8473 continue;
8474 if (compound_head(pages[i]) == hpage)
8475 return true;
8476 }
8477
8478 /* check previously registered pages */
8479 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008480 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008481
8482 for (j = 0; j < imu->nr_bvecs; j++) {
8483 if (!PageCompound(imu->bvec[j].bv_page))
8484 continue;
8485 if (compound_head(imu->bvec[j].bv_page) == hpage)
8486 return true;
8487 }
8488 }
8489
8490 return false;
8491}
8492
8493static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8494 int nr_pages, struct io_mapped_ubuf *imu,
8495 struct page **last_hpage)
8496{
8497 int i, ret;
8498
Pavel Begunkov216e5832021-05-29 12:01:02 +01008499 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008500 for (i = 0; i < nr_pages; i++) {
8501 if (!PageCompound(pages[i])) {
8502 imu->acct_pages++;
8503 } else {
8504 struct page *hpage;
8505
8506 hpage = compound_head(pages[i]);
8507 if (hpage == *last_hpage)
8508 continue;
8509 *last_hpage = hpage;
8510 if (headpage_already_acct(ctx, pages, i, hpage))
8511 continue;
8512 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8513 }
8514 }
8515
8516 if (!imu->acct_pages)
8517 return 0;
8518
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008519 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008520 if (ret)
8521 imu->acct_pages = 0;
8522 return ret;
8523}
8524
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008525static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008526 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008527 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008528{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008529 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008530 struct vm_area_struct **vmas = NULL;
8531 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008532 unsigned long off, start, end, ubuf;
8533 size_t size;
8534 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008535
Pavel Begunkov62248432021-04-28 13:11:29 +01008536 if (!iov->iov_base) {
8537 *pimu = ctx->dummy_ubuf;
8538 return 0;
8539 }
8540
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008541 ubuf = (unsigned long) iov->iov_base;
8542 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8543 start = ubuf >> PAGE_SHIFT;
8544 nr_pages = end - start;
8545
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008546 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008547 ret = -ENOMEM;
8548
8549 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8550 if (!pages)
8551 goto done;
8552
8553 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8554 GFP_KERNEL);
8555 if (!vmas)
8556 goto done;
8557
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008558 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008559 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008560 goto done;
8561
8562 ret = 0;
8563 mmap_read_lock(current->mm);
8564 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8565 pages, vmas);
8566 if (pret == nr_pages) {
8567 /* don't support file backed memory */
8568 for (i = 0; i < nr_pages; i++) {
8569 struct vm_area_struct *vma = vmas[i];
8570
Pavel Begunkov40dad762021-06-09 15:26:54 +01008571 if (vma_is_shmem(vma))
8572 continue;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008573 if (vma->vm_file &&
8574 !is_file_hugepages(vma->vm_file)) {
8575 ret = -EOPNOTSUPP;
8576 break;
8577 }
8578 }
8579 } else {
8580 ret = pret < 0 ? pret : -EFAULT;
8581 }
8582 mmap_read_unlock(current->mm);
8583 if (ret) {
8584 /*
8585 * if we did partial map, or found file backed vmas,
8586 * release any pages we did get
8587 */
8588 if (pret > 0)
8589 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008590 goto done;
8591 }
8592
8593 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8594 if (ret) {
8595 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008596 goto done;
8597 }
8598
8599 off = ubuf & ~PAGE_MASK;
8600 size = iov->iov_len;
8601 for (i = 0; i < nr_pages; i++) {
8602 size_t vec_len;
8603
8604 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8605 imu->bvec[i].bv_page = pages[i];
8606 imu->bvec[i].bv_len = vec_len;
8607 imu->bvec[i].bv_offset = off;
8608 off = 0;
8609 size -= vec_len;
8610 }
8611 /* store original address for later verification */
8612 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008613 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008614 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008615 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008616 ret = 0;
8617done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008618 if (ret)
8619 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008620 kvfree(pages);
8621 kvfree(vmas);
8622 return ret;
8623}
8624
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008625static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008626{
Pavel Begunkov87094462021-04-11 01:46:36 +01008627 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8628 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008629}
8630
8631static int io_buffer_validate(struct iovec *iov)
8632{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008633 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8634
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008635 /*
8636 * Don't impose further limits on the size and buffer
8637 * constraints here, we'll -EINVAL later when IO is
8638 * submitted if they are wrong.
8639 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008640 if (!iov->iov_base)
8641 return iov->iov_len ? -EFAULT : 0;
8642 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008643 return -EFAULT;
8644
8645 /* arbitrary limit, but we need something */
8646 if (iov->iov_len > SZ_1G)
8647 return -EFAULT;
8648
Pavel Begunkov50e96982021-03-24 22:59:01 +00008649 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8650 return -EOVERFLOW;
8651
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008652 return 0;
8653}
8654
8655static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008656 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008657{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008658 struct page *last_hpage = NULL;
8659 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008660 int i, ret;
8661 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008662
Pavel Begunkov87094462021-04-11 01:46:36 +01008663 if (ctx->user_bufs)
8664 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01008665 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01008666 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008667 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008668 if (ret)
8669 return ret;
Pavel Begunkovd878c812021-06-14 02:36:18 +01008670 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
8671 if (ret)
8672 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008673 ret = io_buffers_map_alloc(ctx, nr_args);
8674 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08008675 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008676 return ret;
8677 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008678
Pavel Begunkov87094462021-04-11 01:46:36 +01008679 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Jens Axboeedafcce2019-01-09 09:16:05 -07008680 ret = io_copy_iov(ctx, &iov, arg, i);
8681 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008682 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008683 ret = io_buffer_validate(&iov);
8684 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008685 break;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008686 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008687 ret = -EINVAL;
8688 break;
8689 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008690
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008691 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
8692 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008693 if (ret)
8694 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008695 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008696
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008697 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008698
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008699 ctx->buf_data = data;
8700 if (ret)
8701 __io_sqe_buffers_unregister(ctx);
8702 else
8703 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07008704 return ret;
8705}
8706
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008707static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
8708 struct io_uring_rsrc_update2 *up,
8709 unsigned int nr_args)
8710{
8711 u64 __user *tags = u64_to_user_ptr(up->tags);
8712 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008713 struct page *last_hpage = NULL;
8714 bool needs_switch = false;
8715 __u32 done;
8716 int i, err;
8717
8718 if (!ctx->buf_data)
8719 return -ENXIO;
8720 if (up->offset + nr_args > ctx->nr_user_bufs)
8721 return -EINVAL;
8722
8723 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008724 struct io_mapped_ubuf *imu;
8725 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008726 u64 tag = 0;
8727
8728 err = io_copy_iov(ctx, &iov, iovs, done);
8729 if (err)
8730 break;
8731 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
8732 err = -EFAULT;
8733 break;
8734 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008735 err = io_buffer_validate(&iov);
8736 if (err)
8737 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008738 if (!iov.iov_base && tag) {
8739 err = -EINVAL;
8740 break;
8741 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008742 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
8743 if (err)
8744 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008745
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008746 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01008747 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008748 err = io_queue_rsrc_removal(ctx->buf_data, offset,
8749 ctx->rsrc_node, ctx->user_bufs[i]);
8750 if (unlikely(err)) {
8751 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008752 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008753 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008754 ctx->user_bufs[i] = NULL;
8755 needs_switch = true;
8756 }
8757
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008758 ctx->user_bufs[i] = imu;
Pavel Begunkov2d091d62021-06-14 02:36:21 +01008759 *io_get_tag_slot(ctx->buf_data, offset) = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008760 }
8761
8762 if (needs_switch)
8763 io_rsrc_node_switch(ctx, ctx->buf_data);
8764 return done ? done : err;
8765}
8766
Jens Axboe9b402842019-04-11 11:45:41 -06008767static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8768{
8769 __s32 __user *fds = arg;
8770 int fd;
8771
8772 if (ctx->cq_ev_fd)
8773 return -EBUSY;
8774
8775 if (copy_from_user(&fd, fds, sizeof(*fds)))
8776 return -EFAULT;
8777
8778 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8779 if (IS_ERR(ctx->cq_ev_fd)) {
8780 int ret = PTR_ERR(ctx->cq_ev_fd);
Pavel Begunkovfe7e3252021-06-24 15:09:57 +01008781
Jens Axboe9b402842019-04-11 11:45:41 -06008782 ctx->cq_ev_fd = NULL;
8783 return ret;
8784 }
8785
8786 return 0;
8787}
8788
8789static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8790{
8791 if (ctx->cq_ev_fd) {
8792 eventfd_ctx_put(ctx->cq_ev_fd);
8793 ctx->cq_ev_fd = NULL;
8794 return 0;
8795 }
8796
8797 return -ENXIO;
8798}
8799
Jens Axboe5a2e7452020-02-23 16:23:11 -07008800static void io_destroy_buffers(struct io_ring_ctx *ctx)
8801{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07008802 struct io_buffer *buf;
8803 unsigned long index;
8804
8805 xa_for_each(&ctx->io_buffers, index, buf)
8806 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008807}
8808
Pavel Begunkov72558342021-08-09 20:18:09 +01008809static void io_req_cache_free(struct list_head *list)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008810{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008811 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008812
Pavel Begunkovbb943b82021-08-09 20:18:10 +01008813 list_for_each_entry_safe(req, nxt, list, inflight_entry) {
8814 list_del(&req->inflight_entry);
Jens Axboe1b4c3512021-02-10 00:03:19 +00008815 kmem_cache_free(req_cachep, req);
8816 }
8817}
8818
Jens Axboe4010fec2021-02-27 15:04:18 -07008819static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008820{
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008821 struct io_submit_state *state = &ctx->submit_state;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008822
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008823 mutex_lock(&ctx->uring_lock);
8824
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008825 if (state->free_reqs) {
8826 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
8827 state->free_reqs = 0;
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008828 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008829
Pavel Begunkovcd0ca2e2021-08-09 20:18:11 +01008830 io_flush_cached_locked_reqs(ctx, state);
8831 io_req_cache_free(&state->free_list);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008832 mutex_unlock(&ctx->uring_lock);
8833}
8834
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008835static void io_wait_rsrc_data(struct io_rsrc_data *data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008836{
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008837 if (data && !atomic_dec_and_test(&data->refs))
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008838 wait_for_completion(&data->done);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008839}
8840
Jens Axboe2b188cc2019-01-07 10:46:33 -07008841static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8842{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008843 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008844
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008845 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008846 mmdrop(ctx->mm_account);
8847 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008848 }
Jens Axboedef596e2019-01-09 08:59:42 -07008849
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008850 /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */
8851 io_wait_rsrc_data(ctx->buf_data);
8852 io_wait_rsrc_data(ctx->file_data);
8853
Hao Xu8bad28d2021-02-19 17:19:36 +08008854 mutex_lock(&ctx->uring_lock);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008855 if (ctx->buf_data)
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008856 __io_sqe_buffers_unregister(ctx);
Pavel Begunkov43597aa2021-08-10 02:44:23 +01008857 if (ctx->file_data)
Pavel Begunkov08480402021-04-13 02:58:38 +01008858 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01008859 if (ctx->rings)
8860 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08008861 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008862 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008863 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01008864 if (ctx->sq_creds)
8865 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07008866
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008867 /* there are no registered resources left, nobody uses it */
8868 if (ctx->rsrc_node)
8869 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00008870 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008871 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008872 flush_delayed_work(&ctx->rsrc_put_work);
8873
8874 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
8875 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008876
8877#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008878 if (ctx->ring_sock) {
8879 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008880 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008881 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008882#endif
8883
Hristo Venev75b28af2019-08-26 17:23:46 +00008884 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008885 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008886
8887 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008888 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008889 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008890 if (ctx->hash_map)
8891 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008892 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01008893 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008894 kfree(ctx);
8895}
8896
8897static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8898{
8899 struct io_ring_ctx *ctx = file->private_data;
8900 __poll_t mask = 0;
8901
Pavel Begunkov311997b2021-06-14 23:37:28 +01008902 poll_wait(file, &ctx->poll_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008903 /*
8904 * synchronizes with barrier from wq_has_sleeper call in
8905 * io_commit_cqring
8906 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008907 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008908 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008909 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008910
8911 /*
8912 * Don't flush cqring overflow list here, just do a simple check.
8913 * Otherwise there could possible be ABBA deadlock:
8914 * CPU0 CPU1
8915 * ---- ----
8916 * lock(&ctx->uring_lock);
8917 * lock(&ep->mtx);
8918 * lock(&ctx->uring_lock);
8919 * lock(&ep->mtx);
8920 *
8921 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8922 * pushs them to do the flush.
8923 */
Pavel Begunkov5ed7a372021-06-14 23:37:27 +01008924 if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008925 mask |= EPOLLIN | EPOLLRDNORM;
8926
8927 return mask;
8928}
8929
8930static int io_uring_fasync(int fd, struct file *file, int on)
8931{
8932 struct io_ring_ctx *ctx = file->private_data;
8933
8934 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8935}
8936
Yejune Deng0bead8c2020-12-24 11:02:20 +08008937static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008938{
Jens Axboe4379bf82021-02-15 13:40:22 -07008939 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008940
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008941 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07008942 if (creds) {
8943 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008944 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008945 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008946
8947 return -EINVAL;
8948}
8949
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008950struct io_tctx_exit {
8951 struct callback_head task_work;
8952 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00008953 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008954};
8955
8956static void io_tctx_exit_cb(struct callback_head *cb)
8957{
8958 struct io_uring_task *tctx = current->io_uring;
8959 struct io_tctx_exit *work;
8960
8961 work = container_of(cb, struct io_tctx_exit, task_work);
8962 /*
8963 * When @in_idle, we're in cancellation and it's racy to remove the
8964 * node. It'll be removed by the end of cancellation, just ignore it.
8965 */
8966 if (!atomic_read(&tctx->in_idle))
Pavel Begunkoveef51da2021-06-14 02:36:15 +01008967 io_uring_del_tctx_node((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008968 complete(&work->completion);
8969}
8970
Pavel Begunkov28090c12021-04-25 23:34:45 +01008971static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8972{
8973 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8974
8975 return req->ctx == data;
8976}
8977
Jens Axboe85faa7b2020-04-09 18:14:00 -06008978static void io_ring_exit_work(struct work_struct *work)
8979{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008980 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008981 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkov58d3be22021-08-09 13:04:17 +01008982 unsigned long interval = HZ / 20;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008983 struct io_tctx_exit exit;
8984 struct io_tctx_node *node;
8985 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06008986
Jens Axboe56952e92020-06-17 15:00:04 -06008987 /*
8988 * If we're doing polled IO and end up having requests being
8989 * submitted async (out-of-line), then completions can come in while
8990 * we're waiting for refs to drop. We need to reap these manually,
8991 * as nobody else will be looking for them.
8992 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008993 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008994 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01008995 if (ctx->sq_data) {
8996 struct io_sq_data *sqd = ctx->sq_data;
8997 struct task_struct *tsk;
8998
8999 io_sq_thread_park(sqd);
9000 tsk = sqd->thread;
9001 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
9002 io_wq_cancel_cb(tsk->io_uring->io_wq,
9003 io_cancel_ctx_cb, ctx, true);
9004 io_sq_thread_unpark(sqd);
9005 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009006
Pavel Begunkov58d3be22021-08-09 13:04:17 +01009007 if (WARN_ON_ONCE(time_after(jiffies, timeout))) {
9008 /* there is little hope left, don't run it too often */
9009 interval = HZ * 60;
9010 }
9011 } while (!wait_for_completion_timeout(&ctx->ref_comp, interval));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009012
Pavel Begunkov7f006512021-04-14 13:38:34 +01009013 init_completion(&exit.completion);
9014 init_task_work(&exit.task_work, io_tctx_exit_cb);
9015 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01009016 /*
9017 * Some may use context even when all refs and requests have been put,
9018 * and they are free to do so while still holding uring_lock or
Pavel Begunkov5b0a6ac2021-06-30 21:54:04 +01009019 * completion_lock, see io_req_task_submit(). Apart from other work,
Pavel Begunkov89b50662021-04-01 15:43:50 +01009020 * this lock/unlock section also waits them to finish.
9021 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009022 mutex_lock(&ctx->uring_lock);
9023 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00009024 WARN_ON_ONCE(time_after(jiffies, timeout));
9025
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009026 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
9027 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01009028 /* don't spin on a single task if cancellation failed */
9029 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009030 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
9031 if (WARN_ON_ONCE(ret))
9032 continue;
9033 wake_up_process(node->task);
9034
9035 mutex_unlock(&ctx->uring_lock);
9036 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009037 mutex_lock(&ctx->uring_lock);
9038 }
9039 mutex_unlock(&ctx->uring_lock);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009040 spin_lock(&ctx->completion_lock);
9041 spin_unlock(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00009042
Jens Axboe85faa7b2020-04-09 18:14:00 -06009043 io_ring_ctx_free(ctx);
9044}
9045
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009046/* Returns true if we found and killed one or more timeouts */
9047static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009048 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009049{
9050 struct io_kiocb *req, *tmp;
9051 int canceled = 0;
9052
Jens Axboe79ebeae2021-08-10 15:18:27 -06009053 spin_lock(&ctx->completion_lock);
9054 spin_lock_irq(&ctx->timeout_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009055 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009056 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009057 io_kill_timeout(req, -ECANCELED);
9058 canceled++;
9059 }
9060 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009061 spin_unlock_irq(&ctx->timeout_lock);
Pavel Begunkov51520422021-03-29 11:39:29 +01009062 if (canceled != 0)
9063 io_commit_cqring(ctx);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009064 spin_unlock(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00009065 if (canceled != 0)
9066 io_cqring_ev_posted(ctx);
9067 return canceled != 0;
9068}
9069
Jens Axboe2b188cc2019-01-07 10:46:33 -07009070static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
9071{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009072 unsigned long index;
9073 struct creds *creds;
9074
Jens Axboe2b188cc2019-01-07 10:46:33 -07009075 mutex_lock(&ctx->uring_lock);
9076 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00009077 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009078 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009079 xa_for_each(&ctx->personalities, index, creds)
9080 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009081 mutex_unlock(&ctx->uring_lock);
9082
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009083 io_kill_timeouts(ctx, NULL, true);
9084 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06009085
Jens Axboe15dff282019-11-13 09:09:23 -07009086 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03009087 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06009088
Jens Axboe85faa7b2020-04-09 18:14:00 -06009089 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06009090 /*
9091 * Use system_unbound_wq to avoid spawning tons of event kworkers
9092 * if we're exiting a ton of rings at the same time. It just adds
9093 * noise and overhead, there's no discernable change in runtime
9094 * over using system_wq.
9095 */
9096 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009097}
9098
9099static int io_uring_release(struct inode *inode, struct file *file)
9100{
9101 struct io_ring_ctx *ctx = file->private_data;
9102
9103 file->private_data = NULL;
9104 io_ring_ctx_wait_and_kill(ctx);
9105 return 0;
9106}
9107
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009108struct io_task_cancel {
9109 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009110 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009111};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03009112
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009113static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07009114{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009115 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009116 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009117 bool ret;
9118
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009119 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009120 struct io_ring_ctx *ctx = req->ctx;
9121
9122 /* protect against races with linked timeouts */
Jens Axboe79ebeae2021-08-10 15:18:27 -06009123 spin_lock(&ctx->completion_lock);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009124 ret = io_match_task(req, cancel->task, cancel->all);
Jens Axboe79ebeae2021-08-10 15:18:27 -06009125 spin_unlock(&ctx->completion_lock);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009126 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009127 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00009128 }
9129 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07009130}
9131
Pavel Begunkove1915f72021-03-11 23:29:35 +00009132static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009133 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009134{
Pavel Begunkove1915f72021-03-11 23:29:35 +00009135 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009136 LIST_HEAD(list);
9137
Jens Axboe79ebeae2021-08-10 15:18:27 -06009138 spin_lock(&ctx->completion_lock);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009139 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009140 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009141 list_cut_position(&list, &ctx->defer_list, &de->list);
9142 break;
9143 }
9144 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009145 spin_unlock(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00009146 if (list_empty(&list))
9147 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009148
9149 while (!list_empty(&list)) {
9150 de = list_first_entry(&list, struct io_defer_entry, list);
9151 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00009152 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009153 kfree(de);
9154 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00009155 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03009156}
9157
Pavel Begunkov1b007642021-03-06 11:02:17 +00009158static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
9159{
9160 struct io_tctx_node *node;
9161 enum io_wq_cancel cret;
9162 bool ret = false;
9163
9164 mutex_lock(&ctx->uring_lock);
9165 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
9166 struct io_uring_task *tctx = node->task->io_uring;
9167
9168 /*
9169 * io_wq will stay alive while we hold uring_lock, because it's
9170 * killed after ctx nodes, which requires to take the lock.
9171 */
9172 if (!tctx || !tctx->io_wq)
9173 continue;
9174 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
9175 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9176 }
9177 mutex_unlock(&ctx->uring_lock);
9178
9179 return ret;
9180}
9181
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009182static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
9183 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009184 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009185{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009186 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00009187 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009188
9189 while (1) {
9190 enum io_wq_cancel cret;
9191 bool ret = false;
9192
Pavel Begunkov1b007642021-03-06 11:02:17 +00009193 if (!task) {
9194 ret |= io_uring_try_cancel_iowq(ctx);
9195 } else if (tctx && tctx->io_wq) {
9196 /*
9197 * Cancels requests of all rings, not only @ctx, but
9198 * it's fine as the task is in exit/exec.
9199 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009200 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009201 &cancel, true);
9202 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
9203 }
9204
9205 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009206 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07009207 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009208 while (!list_empty_careful(&ctx->iopoll_list)) {
9209 io_iopoll_try_reap_events(ctx);
9210 ret = true;
9211 }
9212 }
9213
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009214 ret |= io_cancel_defer_files(ctx, task, cancel_all);
9215 ret |= io_poll_remove_all(ctx, task, cancel_all);
9216 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkove5dc4802021-06-26 21:40:46 +01009217 if (task)
9218 ret |= io_run_task_work();
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009219 if (!ret)
9220 break;
9221 cond_resched();
9222 }
9223}
9224
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009225static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009226{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009227 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009228 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00009229 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009230
9231 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009232 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009233 if (unlikely(ret))
9234 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009235 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009236 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009237 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
9238 node = kmalloc(sizeof(*node), GFP_KERNEL);
9239 if (!node)
9240 return -ENOMEM;
9241 node->ctx = ctx;
9242 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06009243
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009244 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
9245 node, GFP_KERNEL));
9246 if (ret) {
9247 kfree(node);
9248 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06009249 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009250
9251 mutex_lock(&ctx->uring_lock);
9252 list_add(&node->ctx_node, &ctx->tctx_list);
9253 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009254 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009255 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009256 return 0;
9257}
9258
9259/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009260 * Note that this task has used io_uring. We use it for cancelation purposes.
9261 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009262static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx)
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009263{
9264 struct io_uring_task *tctx = current->io_uring;
9265
9266 if (likely(tctx && tctx->last == ctx))
9267 return 0;
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009268 return __io_uring_add_tctx_node(ctx);
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009269}
9270
9271/*
Jens Axboe0f212202020-09-13 13:09:39 -06009272 * Remove this io_uring_file -> task mapping.
9273 */
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009274static void io_uring_del_tctx_node(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009275{
9276 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009277 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009278
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009279 if (!tctx)
9280 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009281 node = xa_erase(&tctx->xa, index);
9282 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009283 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009284
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009285 WARN_ON_ONCE(current != node->task);
9286 WARN_ON_ONCE(list_empty(&node->ctx_node));
9287
9288 mutex_lock(&node->ctx->uring_lock);
9289 list_del(&node->ctx_node);
9290 mutex_unlock(&node->ctx->uring_lock);
9291
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009292 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009293 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009294 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009295}
9296
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009297static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009298{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009299 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009300 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009301 unsigned long index;
9302
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009303 xa_for_each(&tctx->xa, index, node)
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009304 io_uring_del_tctx_node(index);
Marco Elverb16ef422021-05-27 11:25:48 +02009305 if (wq) {
9306 /*
9307 * Must be after io_uring_del_task_file() (removes nodes under
9308 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9309 */
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009310 io_wq_put_and_exit(wq);
Pavel Begunkovdadebc32021-08-23 13:30:44 +01009311 tctx->io_wq = NULL;
Marco Elverb16ef422021-05-27 11:25:48 +02009312 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009313}
9314
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009315static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009316{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009317 if (tracked)
9318 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009319 return percpu_counter_sum(&tctx->inflight);
9320}
9321
Pavel Begunkov09899b12021-06-14 02:36:22 +01009322static void io_uring_drop_tctx_refs(struct task_struct *task)
9323{
9324 struct io_uring_task *tctx = task->io_uring;
9325 unsigned int refs = tctx->cached_refs;
9326
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009327 if (refs) {
9328 tctx->cached_refs = 0;
9329 percpu_counter_sub(&tctx->inflight, refs);
9330 put_task_struct_many(task, refs);
9331 }
Pavel Begunkov09899b12021-06-14 02:36:22 +01009332}
9333
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009334/*
9335 * Find any io_uring ctx that this task has registered or done IO on, and cancel
9336 * requests. @sqd should be not-null IIF it's an SQPOLL thread cancellation.
9337 */
9338static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009339{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009340 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009341 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009342 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009343 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009344
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009345 WARN_ON_ONCE(sqd && sqd->thread != current);
9346
Palash Oswal6d042ff2021-04-27 18:21:49 +05309347 if (!current->io_uring)
9348 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009349 if (tctx->io_wq)
9350 io_wq_exit_start(tctx->io_wq);
9351
Jens Axboefdaf0832020-10-30 09:37:30 -06009352 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009353 do {
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009354 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009355 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009356 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009357 if (!inflight)
9358 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009359
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009360 if (!sqd) {
9361 struct io_tctx_node *node;
9362 unsigned long index;
9363
9364 xa_for_each(&tctx->xa, index, node) {
9365 /* sqpoll task will cancel all its requests */
9366 if (node->ctx->sq_data)
9367 continue;
9368 io_uring_try_cancel_requests(node->ctx, current,
9369 cancel_all);
9370 }
9371 } else {
9372 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
9373 io_uring_try_cancel_requests(ctx, current,
9374 cancel_all);
9375 }
9376
9377 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
Pavel Begunkove9dbe222021-08-09 13:04:20 +01009378 io_uring_drop_tctx_refs(current);
Jens Axboe0f212202020-09-13 13:09:39 -06009379 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009380 * If we've seen completions, retry without waiting. This
9381 * avoids a race where a completion comes in before we did
9382 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009383 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009384 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009385 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009386 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009387 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009388 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009389
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009390 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009391 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009392 /* for exec all current's requests should be gone, kill tctx */
9393 __io_uring_free(current);
9394 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009395}
9396
Hao Xuf552a272021-08-12 12:14:35 +08009397void __io_uring_cancel(bool cancel_all)
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009398{
Hao Xuf552a272021-08-12 12:14:35 +08009399 io_uring_cancel_generic(cancel_all, NULL);
Pavel Begunkov78cc6872021-06-14 02:36:23 +01009400}
9401
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009402static void *io_uring_validate_mmap_request(struct file *file,
9403 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009404{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009405 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009406 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009407 struct page *page;
9408 void *ptr;
9409
9410 switch (offset) {
9411 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009412 case IORING_OFF_CQ_RING:
9413 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009414 break;
9415 case IORING_OFF_SQES:
9416 ptr = ctx->sq_sqes;
9417 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009418 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009419 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009420 }
9421
9422 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009423 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009424 return ERR_PTR(-EINVAL);
9425
9426 return ptr;
9427}
9428
9429#ifdef CONFIG_MMU
9430
9431static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9432{
9433 size_t sz = vma->vm_end - vma->vm_start;
9434 unsigned long pfn;
9435 void *ptr;
9436
9437 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9438 if (IS_ERR(ptr))
9439 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009440
9441 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9442 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9443}
9444
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009445#else /* !CONFIG_MMU */
9446
9447static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9448{
9449 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9450}
9451
9452static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9453{
9454 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9455}
9456
9457static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9458 unsigned long addr, unsigned long len,
9459 unsigned long pgoff, unsigned long flags)
9460{
9461 void *ptr;
9462
9463 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9464 if (IS_ERR(ptr))
9465 return PTR_ERR(ptr);
9466
9467 return (unsigned long) ptr;
9468}
9469
9470#endif /* !CONFIG_MMU */
9471
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009472static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009473{
9474 DEFINE_WAIT(wait);
9475
9476 do {
9477 if (!io_sqring_full(ctx))
9478 break;
Jens Axboe90554202020-09-03 12:12:41 -06009479 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9480
9481 if (!io_sqring_full(ctx))
9482 break;
Jens Axboe90554202020-09-03 12:12:41 -06009483 schedule();
9484 } while (!signal_pending(current));
9485
9486 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009487 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009488}
9489
Hao Xuc73ebb62020-11-03 10:54:37 +08009490static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9491 struct __kernel_timespec __user **ts,
9492 const sigset_t __user **sig)
9493{
9494 struct io_uring_getevents_arg arg;
9495
9496 /*
9497 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9498 * is just a pointer to the sigset_t.
9499 */
9500 if (!(flags & IORING_ENTER_EXT_ARG)) {
9501 *sig = (const sigset_t __user *) argp;
9502 *ts = NULL;
9503 return 0;
9504 }
9505
9506 /*
9507 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9508 * timespec and sigset_t pointers if good.
9509 */
9510 if (*argsz != sizeof(arg))
9511 return -EINVAL;
9512 if (copy_from_user(&arg, argp, sizeof(arg)))
9513 return -EFAULT;
9514 *sig = u64_to_user_ptr(arg.sigmask);
9515 *argsz = arg.sigmask_sz;
9516 *ts = u64_to_user_ptr(arg.ts);
9517 return 0;
9518}
9519
Jens Axboe2b188cc2019-01-07 10:46:33 -07009520SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009521 u32, min_complete, u32, flags, const void __user *, argp,
9522 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009523{
9524 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009525 int submitted = 0;
9526 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009527 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009528
Jens Axboe4c6e2772020-07-01 11:29:10 -06009529 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009530
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009531 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9532 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009533 return -EINVAL;
9534
9535 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009536 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009537 return -EBADF;
9538
9539 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009540 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009541 goto out_fput;
9542
9543 ret = -ENXIO;
9544 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009545 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009546 goto out_fput;
9547
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009548 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009549 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009550 goto out;
9551
Jens Axboe6c271ce2019-01-10 11:22:30 -07009552 /*
9553 * For SQ polling, the thread will do all submissions and completions.
9554 * Just return the requested submit count, and wake the thread if
9555 * we were asked to.
9556 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009557 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009558 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov90f67362021-08-09 20:18:12 +01009559 io_cqring_overflow_flush(ctx);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009560
Jens Axboe21f96522021-08-14 09:04:40 -06009561 if (unlikely(ctx->sq_data->thread == NULL)) {
9562 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009563 goto out;
Jens Axboe21f96522021-08-14 09:04:40 -06009564 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009565 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009566 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009567 if (flags & IORING_ENTER_SQ_WAIT) {
9568 ret = io_sqpoll_wait_sq(ctx);
9569 if (ret)
9570 goto out;
9571 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009572 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009573 } else if (to_submit) {
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009574 ret = io_uring_add_tctx_node(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009575 if (unlikely(ret))
9576 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009577 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009578 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009579 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009580
9581 if (submitted != to_submit)
9582 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009583 }
9584 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009585 const sigset_t __user *sig;
9586 struct __kernel_timespec __user *ts;
9587
9588 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9589 if (unlikely(ret))
9590 goto out;
9591
Jens Axboe2b188cc2019-01-07 10:46:33 -07009592 min_complete = min(min_complete, ctx->cq_entries);
9593
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009594 /*
9595 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9596 * space applications don't need to do io completion events
9597 * polling again, they can rely on io_sq_thread to do polling
9598 * work, which can reduce cpu usage and uring_lock contention.
9599 */
9600 if (ctx->flags & IORING_SETUP_IOPOLL &&
9601 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009602 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009603 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009604 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009605 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009606 }
9607
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009608out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009609 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009610out_fput:
9611 fdput(f);
9612 return submitted ? submitted : ret;
9613}
9614
Tobias Klauserbebdb652020-02-26 18:38:32 +01009615#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009616static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9617 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009618{
Jens Axboe87ce9552020-01-30 08:25:34 -07009619 struct user_namespace *uns = seq_user_ns(m);
9620 struct group_info *gi;
9621 kernel_cap_t cap;
9622 unsigned __capi;
9623 int g;
9624
9625 seq_printf(m, "%5d\n", id);
9626 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9627 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9628 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9629 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9630 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9631 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9632 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9633 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9634 seq_puts(m, "\n\tGroups:\t");
9635 gi = cred->group_info;
9636 for (g = 0; g < gi->ngroups; g++) {
9637 seq_put_decimal_ull(m, g ? " " : "",
9638 from_kgid_munged(uns, gi->gid[g]));
9639 }
9640 seq_puts(m, "\n\tCapEff:\t");
9641 cap = cred->cap_effective;
9642 CAP_FOR_EACH_U32(__capi)
9643 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9644 seq_putc(m, '\n');
9645 return 0;
9646}
9647
9648static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9649{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009650 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009651 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009652 int i;
9653
Jens Axboefad8e0d2020-09-28 08:57:48 -06009654 /*
9655 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9656 * since fdinfo case grabs it in the opposite direction of normal use
9657 * cases. If we fail to get the lock, we just don't iterate any
9658 * structures that could be going away outside the io_uring mutex.
9659 */
9660 has_lock = mutex_trylock(&ctx->uring_lock);
9661
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009662 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009663 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009664 if (!sq->thread)
9665 sq = NULL;
9666 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009667
9668 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9669 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009670 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009671 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -07009672 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009673
Jens Axboe87ce9552020-01-30 08:25:34 -07009674 if (f)
9675 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9676 else
9677 seq_printf(m, "%5u: <none>\n", i);
9678 }
9679 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009680 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009681 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +01009682 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -07009683
Pavel Begunkov4751f532021-04-01 15:43:55 +01009684 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -07009685 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009686 if (has_lock && !xa_empty(&ctx->personalities)) {
9687 unsigned long index;
9688 const struct cred *cred;
9689
Jens Axboe87ce9552020-01-30 08:25:34 -07009690 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009691 xa_for_each(&ctx->personalities, index, cred)
9692 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -07009693 }
Jens Axboed7718a92020-02-14 22:23:12 -07009694 seq_printf(m, "PollList:\n");
Jens Axboe79ebeae2021-08-10 15:18:27 -06009695 spin_lock(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -07009696 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9697 struct hlist_head *list = &ctx->cancel_hash[i];
9698 struct io_kiocb *req;
9699
9700 hlist_for_each_entry(req, list, hash_node)
9701 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9702 req->task->task_works != NULL);
9703 }
Jens Axboe79ebeae2021-08-10 15:18:27 -06009704 spin_unlock(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009705 if (has_lock)
9706 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009707}
9708
9709static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9710{
9711 struct io_ring_ctx *ctx = f->private_data;
9712
9713 if (percpu_ref_tryget(&ctx->refs)) {
9714 __io_uring_show_fdinfo(ctx, m);
9715 percpu_ref_put(&ctx->refs);
9716 }
9717}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009718#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009719
Jens Axboe2b188cc2019-01-07 10:46:33 -07009720static const struct file_operations io_uring_fops = {
9721 .release = io_uring_release,
9722 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009723#ifndef CONFIG_MMU
9724 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9725 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9726#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009727 .poll = io_uring_poll,
9728 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009729#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009730 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009731#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009732};
9733
9734static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9735 struct io_uring_params *p)
9736{
Hristo Venev75b28af2019-08-26 17:23:46 +00009737 struct io_rings *rings;
9738 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009739
Jens Axboebd740482020-08-05 12:58:23 -06009740 /* make sure these are sane, as we already accounted them */
9741 ctx->sq_entries = p->sq_entries;
9742 ctx->cq_entries = p->cq_entries;
9743
Hristo Venev75b28af2019-08-26 17:23:46 +00009744 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9745 if (size == SIZE_MAX)
9746 return -EOVERFLOW;
9747
9748 rings = io_mem_alloc(size);
9749 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009750 return -ENOMEM;
9751
Hristo Venev75b28af2019-08-26 17:23:46 +00009752 ctx->rings = rings;
9753 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9754 rings->sq_ring_mask = p->sq_entries - 1;
9755 rings->cq_ring_mask = p->cq_entries - 1;
9756 rings->sq_ring_entries = p->sq_entries;
9757 rings->cq_ring_entries = p->cq_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009758
9759 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009760 if (size == SIZE_MAX) {
9761 io_mem_free(ctx->rings);
9762 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009763 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009764 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009765
9766 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009767 if (!ctx->sq_sqes) {
9768 io_mem_free(ctx->rings);
9769 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009770 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009771 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009772
Jens Axboe2b188cc2019-01-07 10:46:33 -07009773 return 0;
9774}
9775
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009776static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9777{
9778 int ret, fd;
9779
9780 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9781 if (fd < 0)
9782 return fd;
9783
Pavel Begunkoveef51da2021-06-14 02:36:15 +01009784 ret = io_uring_add_tctx_node(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009785 if (ret) {
9786 put_unused_fd(fd);
9787 return ret;
9788 }
9789 fd_install(fd, file);
9790 return fd;
9791}
9792
Jens Axboe2b188cc2019-01-07 10:46:33 -07009793/*
9794 * Allocate an anonymous fd, this is what constitutes the application
9795 * visible backing of an io_uring instance. The application mmaps this
9796 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9797 * we have to tie this fd to a socket for file garbage collection purposes.
9798 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009799static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009800{
9801 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009802#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009803 int ret;
9804
Jens Axboe2b188cc2019-01-07 10:46:33 -07009805 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9806 &ctx->ring_sock);
9807 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009808 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009809#endif
9810
Jens Axboe2b188cc2019-01-07 10:46:33 -07009811 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9812 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009813#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009814 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009815 sock_release(ctx->ring_sock);
9816 ctx->ring_sock = NULL;
9817 } else {
9818 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009819 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009820#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009821 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009822}
9823
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009824static int io_uring_create(unsigned entries, struct io_uring_params *p,
9825 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009826{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009827 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009828 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009829 int ret;
9830
Jens Axboe8110c1a2019-12-28 15:39:54 -07009831 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009832 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009833 if (entries > IORING_MAX_ENTRIES) {
9834 if (!(p->flags & IORING_SETUP_CLAMP))
9835 return -EINVAL;
9836 entries = IORING_MAX_ENTRIES;
9837 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009838
9839 /*
9840 * Use twice as many entries for the CQ ring. It's possible for the
9841 * application to drive a higher depth than the size of the SQ ring,
9842 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009843 * some flexibility in overcommitting a bit. If the application has
9844 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9845 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009846 */
9847 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009848 if (p->flags & IORING_SETUP_CQSIZE) {
9849 /*
9850 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9851 * to a power-of-two, if it isn't already. We do NOT impose
9852 * any cq vs sq ring sizing.
9853 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009854 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009855 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009856 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9857 if (!(p->flags & IORING_SETUP_CLAMP))
9858 return -EINVAL;
9859 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9860 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009861 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9862 if (p->cq_entries < p->sq_entries)
9863 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009864 } else {
9865 p->cq_entries = 2 * p->sq_entries;
9866 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009867
Jens Axboe2b188cc2019-01-07 10:46:33 -07009868 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009869 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009870 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009871 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009872 if (!capable(CAP_IPC_LOCK))
9873 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -06009874
9875 /*
9876 * This is just grabbed for accounting purposes. When a process exits,
9877 * the mm is exited and dropped before the files, hence we need to hang
9878 * on to this mm purely for the purposes of being able to unaccount
9879 * memory (locked/pinned vm). It's not used for anything else.
9880 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009881 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009882 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009883
Jens Axboe2b188cc2019-01-07 10:46:33 -07009884 ret = io_allocate_scq_urings(ctx, p);
9885 if (ret)
9886 goto err;
9887
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009888 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009889 if (ret)
9890 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009891 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +01009892 ret = io_rsrc_node_switch_start(ctx);
9893 if (ret)
9894 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009895 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009896
Jens Axboe2b188cc2019-01-07 10:46:33 -07009897 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009898 p->sq_off.head = offsetof(struct io_rings, sq.head);
9899 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9900 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9901 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9902 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9903 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9904 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009905
9906 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009907 p->cq_off.head = offsetof(struct io_rings, cq.head);
9908 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9909 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9910 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9911 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9912 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009913 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009914
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009915 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9916 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009917 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009918 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +01009919 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
9920 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009921
9922 if (copy_to_user(params, p, sizeof(*p))) {
9923 ret = -EFAULT;
9924 goto err;
9925 }
Jens Axboed1719f72020-07-30 13:43:53 -06009926
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009927 file = io_uring_get_file(ctx);
9928 if (IS_ERR(file)) {
9929 ret = PTR_ERR(file);
9930 goto err;
9931 }
9932
Jens Axboed1719f72020-07-30 13:43:53 -06009933 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009934 * Install ring fd as the very last thing, so we don't risk someone
9935 * having closed it before we finish setup
9936 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009937 ret = io_uring_install_fd(ctx, file);
9938 if (ret < 0) {
9939 /* fput will clean it up */
9940 fput(file);
9941 return ret;
9942 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009943
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009944 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009945 return ret;
9946err:
9947 io_ring_ctx_wait_and_kill(ctx);
9948 return ret;
9949}
9950
9951/*
9952 * Sets up an aio uring context, and returns the fd. Applications asks for a
9953 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9954 * params structure passed in.
9955 */
9956static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9957{
9958 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009959 int i;
9960
9961 if (copy_from_user(&p, params, sizeof(p)))
9962 return -EFAULT;
9963 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9964 if (p.resv[i])
9965 return -EINVAL;
9966 }
9967
Jens Axboe6c271ce2019-01-10 11:22:30 -07009968 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009969 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009970 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9971 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009972 return -EINVAL;
9973
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009974 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009975}
9976
9977SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9978 struct io_uring_params __user *, params)
9979{
9980 return io_uring_setup(entries, params);
9981}
9982
Jens Axboe66f4af92020-01-16 15:36:52 -07009983static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9984{
9985 struct io_uring_probe *p;
9986 size_t size;
9987 int i, ret;
9988
9989 size = struct_size(p, ops, nr_args);
9990 if (size == SIZE_MAX)
9991 return -EOVERFLOW;
9992 p = kzalloc(size, GFP_KERNEL);
9993 if (!p)
9994 return -ENOMEM;
9995
9996 ret = -EFAULT;
9997 if (copy_from_user(p, arg, size))
9998 goto out;
9999 ret = -EINVAL;
10000 if (memchr_inv(p, 0, size))
10001 goto out;
10002
10003 p->last_op = IORING_OP_LAST - 1;
10004 if (nr_args > IORING_OP_LAST)
10005 nr_args = IORING_OP_LAST;
10006
10007 for (i = 0; i < nr_args; i++) {
10008 p->ops[i].op = i;
10009 if (!io_op_defs[i].not_supported)
10010 p->ops[i].flags = IO_URING_OP_SUPPORTED;
10011 }
10012 p->ops_len = i;
10013
10014 ret = 0;
10015 if (copy_to_user(arg, p, size))
10016 ret = -EFAULT;
10017out:
10018 kfree(p);
10019 return ret;
10020}
10021
Jens Axboe071698e2020-01-28 10:04:42 -070010022static int io_register_personality(struct io_ring_ctx *ctx)
10023{
Jens Axboe4379bf82021-02-15 13:40:22 -070010024 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010025 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -060010026 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -070010027
Jens Axboe4379bf82021-02-15 13:40:22 -070010028 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -060010029
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +000010030 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
10031 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
Jens Axboea30f8952021-08-20 14:53:59 -060010032 if (ret < 0) {
10033 put_cred(creds);
10034 return ret;
10035 }
10036 return id;
Jens Axboe071698e2020-01-28 10:04:42 -070010037}
10038
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010039static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
10040 unsigned int nr_args)
10041{
10042 struct io_uring_restriction *res;
10043 size_t size;
10044 int i, ret;
10045
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010046 /* Restrictions allowed only if rings started disabled */
10047 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10048 return -EBADFD;
10049
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010050 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010051 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010052 return -EBUSY;
10053
10054 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
10055 return -EINVAL;
10056
10057 size = array_size(nr_args, sizeof(*res));
10058 if (size == SIZE_MAX)
10059 return -EOVERFLOW;
10060
10061 res = memdup_user(arg, size);
10062 if (IS_ERR(res))
10063 return PTR_ERR(res);
10064
10065 ret = 0;
10066
10067 for (i = 0; i < nr_args; i++) {
10068 switch (res[i].opcode) {
10069 case IORING_RESTRICTION_REGISTER_OP:
10070 if (res[i].register_op >= IORING_REGISTER_LAST) {
10071 ret = -EINVAL;
10072 goto out;
10073 }
10074
10075 __set_bit(res[i].register_op,
10076 ctx->restrictions.register_op);
10077 break;
10078 case IORING_RESTRICTION_SQE_OP:
10079 if (res[i].sqe_op >= IORING_OP_LAST) {
10080 ret = -EINVAL;
10081 goto out;
10082 }
10083
10084 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10085 break;
10086 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10087 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10088 break;
10089 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10090 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10091 break;
10092 default:
10093 ret = -EINVAL;
10094 goto out;
10095 }
10096 }
10097
10098out:
10099 /* Reset all restrictions if an error happened */
10100 if (ret != 0)
10101 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10102 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010103 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010104
10105 kfree(res);
10106 return ret;
10107}
10108
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010109static int io_register_enable_rings(struct io_ring_ctx *ctx)
10110{
10111 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10112 return -EBADFD;
10113
10114 if (ctx->restrictions.registered)
10115 ctx->restricted = 1;
10116
Pavel Begunkov0298ef92021-03-08 13:20:57 +000010117 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10118 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
10119 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010120 return 0;
10121}
10122
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010123static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010124 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010125 unsigned nr_args)
10126{
10127 __u32 tmp;
10128 int err;
10129
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010130 if (up->resv)
10131 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010132 if (check_add_overflow(up->offset, nr_args, &tmp))
10133 return -EOVERFLOW;
10134 err = io_rsrc_node_switch_start(ctx);
10135 if (err)
10136 return err;
10137
Pavel Begunkovfdecb662021-04-25 14:32:20 +010010138 switch (type) {
10139 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010140 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010141 case IORING_RSRC_BUFFER:
10142 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010143 }
10144 return -EINVAL;
10145}
10146
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010147static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
10148 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010149{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010150 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010151
10152 if (!nr_args)
10153 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010154 memset(&up, 0, sizeof(up));
10155 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
10156 return -EFAULT;
10157 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
10158}
10159
10160static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010161 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010162{
10163 struct io_uring_rsrc_update2 up;
10164
10165 if (size != sizeof(up))
10166 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010167 if (copy_from_user(&up, arg, sizeof(up)))
10168 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010169 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010170 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +010010171 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +010010172}
10173
Pavel Begunkov792e3582021-04-25 14:32:21 +010010174static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +010010175 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010176{
10177 struct io_uring_rsrc_register rr;
10178
10179 /* keep it extendible */
10180 if (size != sizeof(rr))
10181 return -EINVAL;
10182
10183 memset(&rr, 0, sizeof(rr));
10184 if (copy_from_user(&rr, arg, size))
10185 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +010010186 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +010010187 return -EINVAL;
10188
Pavel Begunkov992da012021-06-10 16:37:37 +010010189 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +010010190 case IORING_RSRC_FILE:
10191 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
10192 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010193 case IORING_RSRC_BUFFER:
10194 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
10195 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +010010196 }
10197 return -EINVAL;
10198}
10199
Jens Axboefe764212021-06-17 10:19:54 -060010200static int io_register_iowq_aff(struct io_ring_ctx *ctx, void __user *arg,
10201 unsigned len)
10202{
10203 struct io_uring_task *tctx = current->io_uring;
10204 cpumask_var_t new_mask;
10205 int ret;
10206
10207 if (!tctx || !tctx->io_wq)
10208 return -EINVAL;
10209
10210 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
10211 return -ENOMEM;
10212
10213 cpumask_clear(new_mask);
10214 if (len > cpumask_size())
10215 len = cpumask_size();
10216
10217 if (copy_from_user(new_mask, arg, len)) {
10218 free_cpumask_var(new_mask);
10219 return -EFAULT;
10220 }
10221
10222 ret = io_wq_cpu_affinity(tctx->io_wq, new_mask);
10223 free_cpumask_var(new_mask);
10224 return ret;
10225}
10226
10227static int io_unregister_iowq_aff(struct io_ring_ctx *ctx)
10228{
10229 struct io_uring_task *tctx = current->io_uring;
10230
10231 if (!tctx || !tctx->io_wq)
10232 return -EINVAL;
10233
10234 return io_wq_cpu_affinity(tctx->io_wq, NULL);
10235}
10236
Jens Axboe071698e2020-01-28 10:04:42 -070010237static bool io_register_op_must_quiesce(int op)
10238{
10239 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +010010240 case IORING_REGISTER_BUFFERS:
10241 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +010010242 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -070010243 case IORING_UNREGISTER_FILES:
10244 case IORING_REGISTER_FILES_UPDATE:
10245 case IORING_REGISTER_PROBE:
10246 case IORING_REGISTER_PERSONALITY:
10247 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +010010248 case IORING_REGISTER_FILES2:
10249 case IORING_REGISTER_FILES_UPDATE2:
10250 case IORING_REGISTER_BUFFERS2:
10251 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboefe764212021-06-17 10:19:54 -060010252 case IORING_REGISTER_IOWQ_AFF:
10253 case IORING_UNREGISTER_IOWQ_AFF:
Jens Axboe071698e2020-01-28 10:04:42 -070010254 return false;
10255 default:
10256 return true;
10257 }
10258}
10259
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010260static int io_ctx_quiesce(struct io_ring_ctx *ctx)
10261{
10262 long ret;
10263
10264 percpu_ref_kill(&ctx->refs);
10265
10266 /*
10267 * Drop uring mutex before waiting for references to exit. If another
10268 * thread is currently inside io_uring_enter() it might need to grab the
10269 * uring_lock to make progress. If we hold it here across the drain
10270 * wait, then we can deadlock. It's safe to drop the mutex here, since
10271 * no new references will come in after we've killed the percpu ref.
10272 */
10273 mutex_unlock(&ctx->uring_lock);
10274 do {
10275 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10276 if (!ret)
10277 break;
10278 ret = io_run_task_work_sig();
10279 } while (ret >= 0);
10280 mutex_lock(&ctx->uring_lock);
10281
10282 if (ret)
10283 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10284 return ret;
10285}
10286
Jens Axboeedafcce2019-01-09 09:16:05 -070010287static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10288 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010289 __releases(ctx->uring_lock)
10290 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010291{
10292 int ret;
10293
Jens Axboe35fa71a2019-04-22 10:23:23 -060010294 /*
10295 * We're inside the ring mutex, if the ref is already dying, then
10296 * someone else killed the ctx or is already going through
10297 * io_uring_register().
10298 */
10299 if (percpu_ref_is_dying(&ctx->refs))
10300 return -ENXIO;
10301
Pavel Begunkov75c40212021-04-15 13:07:40 +010010302 if (ctx->restricted) {
10303 if (opcode >= IORING_REGISTER_LAST)
10304 return -EINVAL;
10305 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
10306 if (!test_bit(opcode, ctx->restrictions.register_op))
10307 return -EACCES;
10308 }
10309
Jens Axboe071698e2020-01-28 10:04:42 -070010310 if (io_register_op_must_quiesce(opcode)) {
Pavel Begunkove73c5c72021-08-09 13:04:12 +010010311 ret = io_ctx_quiesce(ctx);
10312 if (ret)
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010313 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -070010314 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010315
10316 switch (opcode) {
10317 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010318 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010319 break;
10320 case IORING_UNREGISTER_BUFFERS:
10321 ret = -EINVAL;
10322 if (arg || nr_args)
10323 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010324 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010325 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010326 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010327 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010328 break;
10329 case IORING_UNREGISTER_FILES:
10330 ret = -EINVAL;
10331 if (arg || nr_args)
10332 break;
10333 ret = io_sqe_files_unregister(ctx);
10334 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010335 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010336 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010337 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010338 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010339 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010340 ret = -EINVAL;
10341 if (nr_args != 1)
10342 break;
10343 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010344 if (ret)
10345 break;
10346 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10347 ctx->eventfd_async = 1;
10348 else
10349 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010350 break;
10351 case IORING_UNREGISTER_EVENTFD:
10352 ret = -EINVAL;
10353 if (arg || nr_args)
10354 break;
10355 ret = io_eventfd_unregister(ctx);
10356 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010357 case IORING_REGISTER_PROBE:
10358 ret = -EINVAL;
10359 if (!arg || nr_args > 256)
10360 break;
10361 ret = io_probe(ctx, arg, nr_args);
10362 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010363 case IORING_REGISTER_PERSONALITY:
10364 ret = -EINVAL;
10365 if (arg || nr_args)
10366 break;
10367 ret = io_register_personality(ctx);
10368 break;
10369 case IORING_UNREGISTER_PERSONALITY:
10370 ret = -EINVAL;
10371 if (arg)
10372 break;
10373 ret = io_unregister_personality(ctx, nr_args);
10374 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010375 case IORING_REGISTER_ENABLE_RINGS:
10376 ret = -EINVAL;
10377 if (arg || nr_args)
10378 break;
10379 ret = io_register_enable_rings(ctx);
10380 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010381 case IORING_REGISTER_RESTRICTIONS:
10382 ret = io_register_restrictions(ctx, arg, nr_args);
10383 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010384 case IORING_REGISTER_FILES2:
10385 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010386 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010387 case IORING_REGISTER_FILES_UPDATE2:
10388 ret = io_register_rsrc_update(ctx, arg, nr_args,
10389 IORING_RSRC_FILE);
10390 break;
10391 case IORING_REGISTER_BUFFERS2:
10392 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10393 break;
10394 case IORING_REGISTER_BUFFERS_UPDATE:
10395 ret = io_register_rsrc_update(ctx, arg, nr_args,
10396 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010397 break;
Jens Axboefe764212021-06-17 10:19:54 -060010398 case IORING_REGISTER_IOWQ_AFF:
10399 ret = -EINVAL;
10400 if (!arg || !nr_args)
10401 break;
10402 ret = io_register_iowq_aff(ctx, arg, nr_args);
10403 break;
10404 case IORING_UNREGISTER_IOWQ_AFF:
10405 ret = -EINVAL;
10406 if (arg || nr_args)
10407 break;
10408 ret = io_unregister_iowq_aff(ctx);
10409 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010410 default:
10411 ret = -EINVAL;
10412 break;
10413 }
10414
Jens Axboe071698e2020-01-28 10:04:42 -070010415 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010416 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010417 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010418 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010419 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010420 return ret;
10421}
10422
10423SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10424 void __user *, arg, unsigned int, nr_args)
10425{
10426 struct io_ring_ctx *ctx;
10427 long ret = -EBADF;
10428 struct fd f;
10429
10430 f = fdget(fd);
10431 if (!f.file)
10432 return -EBADF;
10433
10434 ret = -EOPNOTSUPP;
10435 if (f.file->f_op != &io_uring_fops)
10436 goto out_fput;
10437
10438 ctx = f.file->private_data;
10439
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010440 io_run_task_work();
10441
Jens Axboeedafcce2019-01-09 09:16:05 -070010442 mutex_lock(&ctx->uring_lock);
10443 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10444 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010445 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10446 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010447out_fput:
10448 fdput(f);
10449 return ret;
10450}
10451
Jens Axboe2b188cc2019-01-07 10:46:33 -070010452static int __init io_uring_init(void)
10453{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010454#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10455 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10456 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10457} while (0)
10458
10459#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10460 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10461 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10462 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10463 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10464 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10465 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10466 BUILD_BUG_SQE_ELEM(8, __u64, off);
10467 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10468 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010469 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010470 BUILD_BUG_SQE_ELEM(24, __u32, len);
10471 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10472 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10473 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10474 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010475 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10476 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010477 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10478 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10479 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10480 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10481 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10482 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10483 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10484 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010485 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010486 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10487 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010488 BUILD_BUG_SQE_ELEM(40, __u16, buf_group);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010489 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010490 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Pavel Begunkovb9445592021-08-25 12:25:45 +010010491 BUILD_BUG_SQE_ELEM(44, __u32, file_index);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010492
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010493 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10494 sizeof(struct io_uring_rsrc_update));
10495 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10496 sizeof(struct io_uring_rsrc_update2));
10497 /* should fit into one byte */
10498 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
10499
Jens Axboed3656342019-12-18 09:50:26 -070010500 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010501 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Pavel Begunkov16340ea2021-06-24 15:09:58 +010010502
Jens Axboe91f245d2021-02-09 13:48:50 -070010503 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10504 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010505 return 0;
10506};
10507__initcall(io_uring_init);