blob: f628af3a33685fdc63e00a9a19069dc9ef355507 [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
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * 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>
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020082#define CREATE_TRACE_POINTS
83#include <trace/events/io_uring.h>
84
Jens Axboe2b188cc2019-01-07 10:46:33 -070085#include <uapi/linux/io_uring.h>
86
87#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060088#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070089
Daniel Xu5277dea2019-09-14 14:23:45 -070090#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060091#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060092
93/*
94 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
95 */
96#define IORING_FILE_TABLE_SHIFT 9
97#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
98#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
99#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200100#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
101 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700102
Pavel Begunkov489809e2021-05-14 12:06:44 +0100103#define IORING_MAX_REG_BUFFERS (1U << 14)
104
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000105#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
106 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
107 IOSQE_BUFFER_SELECT)
108
Jens Axboe2b188cc2019-01-07 10:46:33 -0700109struct io_uring {
110 u32 head ____cacheline_aligned_in_smp;
111 u32 tail ____cacheline_aligned_in_smp;
112};
113
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * This data is shared with the application through the mmap at offsets
116 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 *
118 * The offsets to the member fields are published through struct
119 * io_sqring_offsets when calling io_uring_setup.
120 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000121struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 /*
123 * Head and tail offsets into the ring; the offsets need to be
124 * masked to get valid indices.
125 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 * The kernel controls head of the sq ring and the tail of the cq ring,
127 * and the application controls tail of the sq ring and the head of the
128 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000130 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 * ring_entries - 1)
134 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000135 u32 sq_ring_mask, cq_ring_mask;
136 /* Ring sizes (constant, power of 2) */
137 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 /*
139 * Number of invalid entries dropped by the kernel due to
140 * invalid index stored in array
141 *
142 * Written by the kernel, shouldn't be modified by the
143 * application (i.e. get number of "new events" by comparing to
144 * cached value).
145 *
146 * After a new SQ head value was read by the application this
147 * counter includes all submissions that were dropped reaching
148 * the new SQ head (and possibly more).
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200152 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 *
154 * Written by the kernel, shouldn't be modified by the
155 * application.
156 *
157 * The application needs a full memory barrier before checking
158 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
159 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000160 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200162 * Runtime CQ flags
163 *
164 * Written by the application, shouldn't be modified by the
165 * kernel.
166 */
167 u32 cq_flags;
168 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200169 * Number of completion events lost because the queue was full;
170 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800171 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200172 * the completion queue.
173 *
174 * Written by the kernel, shouldn't be modified by the
175 * application (i.e. get number of "new events" by comparing to
176 * cached value).
177 *
178 * As completion events come in out of order this counter is not
179 * ordered with any other data.
180 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000181 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200182 /*
183 * Ring buffer of completion events.
184 *
185 * The kernel writes completion events fresh every time they are
186 * produced, so the application is allowed to modify pending
187 * entries.
188 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000189 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700190};
191
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000192enum io_uring_cmd_flags {
193 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000194 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000195};
196
Jens Axboeedafcce2019-01-09 09:16:05 -0700197struct io_mapped_ubuf {
198 u64 ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +0100199 u64 ubuf_end;
Jens Axboeedafcce2019-01-09 09:16:05 -0700200 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600201 unsigned long acct_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100202 struct bio_vec bvec[];
Jens Axboeedafcce2019-01-09 09:16:05 -0700203};
204
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000205struct io_ring_ctx;
206
Pavel Begunkov6c2450a2021-02-23 12:40:22 +0000207struct io_overflow_cqe {
208 struct io_uring_cqe cqe;
209 struct list_head list;
210};
211
Pavel Begunkova04b0ac2021-04-01 15:44:04 +0100212struct io_fixed_file {
213 /* file * with additional FFS_* flags */
214 unsigned long file_ptr;
215};
216
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000217struct io_rsrc_put {
218 struct list_head list;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100219 u64 tag;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000220 union {
221 void *rsrc;
222 struct file *file;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +0100223 struct io_mapped_ubuf *buf;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000224 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000225};
226
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100227struct io_file_table {
228 /* two level table */
229 struct io_fixed_file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700230};
231
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100232struct io_rsrc_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800233 struct percpu_ref refs;
234 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000235 struct list_head rsrc_list;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100236 struct io_rsrc_data *rsrc_data;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600237 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000238 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800239};
240
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100241typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
242
Pavel Begunkovb895c9a2021-04-01 15:43:40 +0100243struct io_rsrc_data {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700244 struct io_ring_ctx *ctx;
245
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +0100246 u64 *tags;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +0100247 rsrc_put_fn *do_put;
Pavel Begunkov3e942492021-04-11 01:46:34 +0100248 atomic_t refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700249 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800250 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700251};
252
Jens Axboe5a2e7452020-02-23 16:23:11 -0700253struct io_buffer {
254 struct list_head list;
255 __u64 addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -0300256 __u32 len;
Jens Axboe5a2e7452020-02-23 16:23:11 -0700257 __u16 bid;
258};
259
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200260struct io_restriction {
261 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
262 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
263 u8 sqe_flags_allowed;
264 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200265 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200266};
267
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700268enum {
269 IO_SQ_THREAD_SHOULD_STOP = 0,
270 IO_SQ_THREAD_SHOULD_PARK,
271};
272
Jens Axboe534ca6d2020-09-02 13:52:19 -0600273struct io_sq_data {
274 refcount_t refs;
Pavel Begunkov9e138a42021-03-14 20:57:12 +0000275 atomic_t park_pending;
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +0000276 struct mutex lock;
Jens Axboe69fb2132020-09-14 11:16:23 -0600277
278 /* ctx's that are using this sqd */
279 struct list_head ctx_list;
Jens Axboe69fb2132020-09-14 11:16:23 -0600280
Jens Axboe534ca6d2020-09-02 13:52:19 -0600281 struct task_struct *thread;
282 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800283
284 unsigned sq_thread_idle;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700285 int sq_cpu;
286 pid_t task_pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -0700287 pid_t task_tgid;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700288
289 unsigned long state;
Jens Axboe37d1e2e2021-02-17 21:03:43 -0700290 struct completion exited;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600291};
292
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000293#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000294#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000295#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000296#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000297
298struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000299 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000300 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700301 unsigned int locked_free_nr;
302 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000303 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700304 /* IRQ completion list, under ->completion_lock */
305 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000306};
307
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000308struct io_submit_link {
309 struct io_kiocb *head;
310 struct io_kiocb *last;
311};
312
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000313struct io_submit_state {
314 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000315 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000316
317 /*
318 * io_kiocb alloc cache
319 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000320 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000321 unsigned int free_reqs;
322
323 bool plug_started;
324
325 /*
326 * Batch completion logic
327 */
328 struct io_comp_state comp;
329
330 /*
331 * File reference cache
332 */
333 struct file *file;
334 unsigned int fd;
335 unsigned int file_refs;
336 unsigned int ios_left;
337};
338
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339struct io_ring_ctx {
340 struct {
341 struct percpu_ref refs;
342 } ____cacheline_aligned_in_smp;
343
344 struct {
345 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800346 unsigned int compat: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800347 unsigned int drain_next: 1;
348 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200349 unsigned int restricted: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700350
Hristo Venev75b28af2019-08-26 17:23:46 +0000351 /*
352 * Ring buffer of indices into array of io_uring_sqe, which is
353 * mmapped by the application using the IORING_OFF_SQES offset.
354 *
355 * This indirection could e.g. be used to assign fixed
356 * io_uring_sqe entries to operations and only submit them to
357 * the queue when needed.
358 *
359 * The kernel modifies neither the indices array nor the entries
360 * array.
361 */
362 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363 unsigned cached_sq_head;
364 unsigned sq_entries;
365 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700366 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600367 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100368 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700369 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600370
371 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600372 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700373 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700374
Jens Axboead3eb2c2019-12-18 17:12:20 -0700375 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376 } ____cacheline_aligned_in_smp;
377
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700378 struct {
379 struct mutex uring_lock;
380 wait_queue_head_t wait;
381 } ____cacheline_aligned_in_smp;
382
383 struct io_submit_state submit_state;
384
Hristo Venev75b28af2019-08-26 17:23:46 +0000385 struct io_rings *rings;
386
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +0100387 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
Jens Axboe534ca6d2020-09-02 13:52:19 -0600388 struct io_sq_data *sq_data; /* if using sq thread polling */
389
Jens Axboe90554202020-09-03 12:12:41 -0600390 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600391 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700392
Jens Axboe6b063142019-01-10 22:13:58 -0700393 /*
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100394 * Fixed resources fast path, should be accessed only under uring_lock,
395 * and updated through io_uring_register(2)
Jens Axboe6b063142019-01-10 22:13:58 -0700396 */
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100397 struct io_rsrc_node *rsrc_node;
398
Pavel Begunkovaeca2412021-04-11 01:46:37 +0100399 struct io_file_table file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700400 unsigned nr_user_files;
Jens Axboeedafcce2019-01-09 09:16:05 -0700401 unsigned nr_user_bufs;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +0100402 struct io_mapped_ubuf **user_bufs;
Jens Axboeedafcce2019-01-09 09:16:05 -0700403
Jens Axboe9e15c3a2021-03-13 12:29:43 -0700404 struct xarray io_buffers;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +0000405 struct xarray personalities;
406 u32 pers_next;
Jens Axboe071698e2020-01-28 10:04:42 -0700407
Jens Axboe206aefd2019-11-07 18:27:42 -0700408 struct {
409 unsigned cached_cq_tail;
410 unsigned cq_entries;
411 unsigned cq_mask;
412 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500413 unsigned cq_last_tm_flush;
Hao Xu7b289c32021-04-13 15:20:39 +0800414 unsigned cq_extra;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700415 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700416 struct wait_queue_head cq_wait;
417 struct fasync_struct *cq_fasync;
418 struct eventfd_ctx *cq_ev_fd;
419 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420
421 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700422 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700423
Jens Axboedef596e2019-01-09 08:59:42 -0700424 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300425 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700426 * io_uring instances that don't use IORING_SETUP_SQPOLL.
427 * For SQPOLL, only the single threaded io_sq_thread() will
428 * manipulate the list, hence no extra locking is needed there.
429 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300430 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700431 struct hlist_head *cancel_hash;
432 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700433 bool poll_multi_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700434 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600435
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200436 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700437
Pavel Begunkovb13a8912021-05-16 22:58:07 +0100438 /* slow path rsrc auxilary data, used by update/register */
439 struct {
440 struct io_rsrc_node *rsrc_backup_node;
441 struct io_mapped_ubuf *dummy_ubuf;
442 struct io_rsrc_data *file_data;
443 struct io_rsrc_data *buf_data;
444
445 struct delayed_work rsrc_put_work;
446 struct llist_head rsrc_put_llist;
447 struct list_head rsrc_ref_list;
448 spinlock_t rsrc_ref_lock;
449 };
450
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700451 /* Keep this last, we don't need it for the fast path */
Pavel Begunkovb986af72021-05-16 22:58:06 +0100452 struct {
453 #if defined(CONFIG_UNIX)
454 struct socket *ring_sock;
455 #endif
456 /* hashed buffered write serialization */
457 struct io_wq_hash *hash_map;
458
459 /* Only used for accounting purposes */
460 struct user_struct *user;
461 struct mm_struct *mm_account;
462
463 /* ctx exit and cancelation */
464 struct callback_head *exit_task_work;
465 struct work_struct exit_work;
466 struct list_head tctx_list;
467 struct completion ref_comp;
468 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469};
470
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100471struct io_uring_task {
472 /* submission side */
473 struct xarray xa;
474 struct wait_queue_head wait;
Stefan Metzmacheree53fb22021-03-15 12:56:57 +0100475 const struct io_ring_ctx *last;
476 struct io_wq *io_wq;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100477 struct percpu_counter inflight;
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100478 atomic_t inflight_tracked;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100479 atomic_t in_idle;
Stefan Metzmacher53e043b2021-03-15 12:56:56 +0100480
481 spinlock_t task_lock;
482 struct io_wq_work_list task_list;
483 unsigned long task_state;
484 struct callback_head task_work;
485};
486
Jens Axboe09bb8392019-03-13 12:39:28 -0600487/*
488 * First field must be the file pointer in all the
489 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
490 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700491struct io_poll_iocb {
492 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000493 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700494 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600495 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700496 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700497 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700498};
499
Pavel Begunkov9d805892021-04-13 02:58:40 +0100500struct io_poll_update {
Pavel Begunkov018043b2020-10-27 23:17:18 +0000501 struct file *file;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100502 u64 old_user_data;
503 u64 new_user_data;
504 __poll_t events;
Jens Axboeb69de282021-03-17 08:37:41 -0600505 bool update_events;
506 bool update_user_data;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000507};
508
Jens Axboeb5dba592019-12-11 14:02:38 -0700509struct io_close {
510 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700511 int fd;
512};
513
Jens Axboead8a48a2019-11-15 08:49:11 -0700514struct io_timeout_data {
515 struct io_kiocb *req;
516 struct hrtimer timer;
517 struct timespec64 ts;
518 enum hrtimer_mode mode;
519};
520
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700521struct io_accept {
522 struct file *file;
523 struct sockaddr __user *addr;
524 int __user *addr_len;
525 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600526 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700527};
528
529struct io_sync {
530 struct file *file;
531 loff_t len;
532 loff_t off;
533 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700534 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700535};
536
Jens Axboefbf23842019-12-17 18:45:56 -0700537struct io_cancel {
538 struct file *file;
539 u64 addr;
540};
541
Jens Axboeb29472e2019-12-17 18:50:29 -0700542struct io_timeout {
543 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300544 u32 off;
545 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300546 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000547 /* head of the link, used by linked timeouts only */
548 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700549};
550
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100551struct io_timeout_rem {
552 struct file *file;
553 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000554
555 /* timeout update */
556 struct timespec64 ts;
557 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100558};
559
Jens Axboe9adbd452019-12-20 08:45:55 -0700560struct io_rw {
561 /* NOTE: kiocb has the file as the first member, so don't do it here */
562 struct kiocb kiocb;
563 u64 addr;
564 u64 len;
565};
566
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700567struct io_connect {
568 struct file *file;
569 struct sockaddr __user *addr;
570 int addr_len;
571};
572
Jens Axboee47293f2019-12-20 08:58:21 -0700573struct io_sr_msg {
574 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700575 union {
Pavel Begunkov4af34172021-04-11 01:46:30 +0100576 struct compat_msghdr __user *umsg_compat;
577 struct user_msghdr __user *umsg;
578 void __user *buf;
Jens Axboefddafac2020-01-04 20:19:44 -0700579 };
Jens Axboee47293f2019-12-20 08:58:21 -0700580 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700581 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700582 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700583 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700584};
585
Jens Axboe15b71ab2019-12-11 11:20:36 -0700586struct io_open {
587 struct file *file;
588 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700589 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700590 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600591 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700592};
593
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000594struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700595 struct file *file;
596 u64 arg;
597 u32 nr_args;
598 u32 offset;
599};
600
Jens Axboe4840e412019-12-25 22:03:45 -0700601struct io_fadvise {
602 struct file *file;
603 u64 offset;
604 u32 len;
605 u32 advice;
606};
607
Jens Axboec1ca7572019-12-25 22:18:28 -0700608struct io_madvise {
609 struct file *file;
610 u64 addr;
611 u32 len;
612 u32 advice;
613};
614
Jens Axboe3e4827b2020-01-08 15:18:09 -0700615struct io_epoll {
616 struct file *file;
617 int epfd;
618 int op;
619 int fd;
620 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621};
622
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300623struct io_splice {
624 struct file *file_out;
625 struct file *file_in;
626 loff_t off_out;
627 loff_t off_in;
628 u64 len;
629 unsigned int flags;
630};
631
Jens Axboeddf0322d2020-02-23 16:41:33 -0700632struct io_provide_buf {
633 struct file *file;
634 __u64 addr;
Pavel Begunkov38134ad2021-04-15 13:07:39 +0100635 __u32 len;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700636 __u32 bgid;
637 __u16 nbufs;
638 __u16 bid;
639};
640
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700641struct io_statx {
642 struct file *file;
643 int dfd;
644 unsigned int mask;
645 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700646 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700647 struct statx __user *buffer;
648};
649
Jens Axboe36f4fa62020-09-05 11:14:22 -0600650struct io_shutdown {
651 struct file *file;
652 int how;
653};
654
Jens Axboe80a261f2020-09-28 14:23:58 -0600655struct io_rename {
656 struct file *file;
657 int old_dfd;
658 int new_dfd;
659 struct filename *oldpath;
660 struct filename *newpath;
661 int flags;
662};
663
Jens Axboe14a11432020-09-28 14:27:37 -0600664struct io_unlink {
665 struct file *file;
666 int dfd;
667 int flags;
668 struct filename *filename;
669};
670
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300671struct io_completion {
672 struct file *file;
673 struct list_head list;
Pavel Begunkov8c3f9cd2021-02-28 22:35:15 +0000674 u32 cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300675};
676
Jens Axboef499a022019-12-02 16:28:46 -0700677struct io_async_connect {
678 struct sockaddr_storage address;
679};
680
Jens Axboe03b12302019-12-02 18:50:25 -0700681struct io_async_msghdr {
682 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000683 /* points to an allocated iov, if NULL we use fast_iov instead */
684 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700685 struct sockaddr __user *uaddr;
686 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700687 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700688};
689
Jens Axboef67676d2019-12-02 11:03:47 -0700690struct io_async_rw {
691 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600692 const struct iovec *free_iovec;
693 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600694 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600695 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700696};
697
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300698enum {
699 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
700 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
701 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
702 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
703 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700704 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300705
Pavel Begunkovdddca222021-04-27 16:13:52 +0100706 /* first byte is taken by user flags, shift it to not overlap */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100707 REQ_F_FAIL_BIT = 8,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300708 REQ_F_INFLIGHT_BIT,
709 REQ_F_CUR_POS_BIT,
710 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300711 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300712 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700713 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700714 REQ_F_BUFFER_SELECTED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100715 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000716 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe230d50d2021-04-01 20:41:15 -0600717 REQ_F_REISSUE_BIT,
Pavel Begunkov8c130822021-03-22 01:58:32 +0000718 REQ_F_DONT_REISSUE_BIT,
Jens Axboe7b29f922021-03-12 08:30:14 -0700719 /* keep async read/write and isreg together and in order */
720 REQ_F_ASYNC_READ_BIT,
721 REQ_F_ASYNC_WRITE_BIT,
722 REQ_F_ISREG_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700723
724 /* not a real bit, just to check we're not overflowing the space */
725 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300726};
727
728enum {
729 /* ctx owns file */
730 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
731 /* drain existing IO first */
732 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
733 /* linked sqes */
734 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
735 /* doesn't sever on completion < 0 */
736 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
737 /* IOSQE_ASYNC */
738 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700739 /* IOSQE_BUFFER_SELECT */
740 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300741
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300742 /* fail rest of links */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +0100743 REQ_F_FAIL = BIT(REQ_F_FAIL_BIT),
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +0000744 /* on inflight list, should be cancelled and waited on exit reliably */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300745 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
746 /* read/write uses file position */
747 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
748 /* must not punt to workers */
749 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100750 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300751 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300752 /* needs cleanup */
753 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700754 /* already went through poll handler */
755 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700756 /* buffer already selected */
757 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100758 /* linked timeout is active, i.e. prepared by link's head */
759 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000760 /* completion is deferred through io_comp_state */
761 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboe230d50d2021-04-01 20:41:15 -0600762 /* caller should reissue async */
763 REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT),
Pavel Begunkov8c130822021-03-22 01:58:32 +0000764 /* don't attempt request reissue, see io_rw_reissue() */
765 REQ_F_DONT_REISSUE = BIT(REQ_F_DONT_REISSUE_BIT),
Jens Axboe7b29f922021-03-12 08:30:14 -0700766 /* supports async reads */
767 REQ_F_ASYNC_READ = BIT(REQ_F_ASYNC_READ_BIT),
768 /* supports async writes */
769 REQ_F_ASYNC_WRITE = BIT(REQ_F_ASYNC_WRITE_BIT),
770 /* regular file */
771 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700772};
773
774struct async_poll {
775 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600776 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300777};
778
Jens Axboe7cbf1722021-02-10 00:03:20 +0000779struct io_task_work {
780 struct io_wq_work_node node;
781 task_work_func_t func;
782};
783
Pavel Begunkov992da012021-06-10 16:37:37 +0100784enum {
785 IORING_RSRC_FILE = 0,
786 IORING_RSRC_BUFFER = 1,
787};
788
Jens Axboe09bb8392019-03-13 12:39:28 -0600789/*
790 * NOTE! Each of the iocb union members has the file pointer
791 * as the first entry in their struct definition. So you can
792 * access the file pointer through any of the sub-structs,
793 * or directly as just 'ki_filp' in this struct.
794 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700795struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700796 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600797 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700798 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700799 struct io_poll_iocb poll;
Pavel Begunkov9d805892021-04-13 02:58:40 +0100800 struct io_poll_update poll_update;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700801 struct io_accept accept;
802 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700803 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700804 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100805 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700806 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700807 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700808 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700809 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000810 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700811 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700812 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700813 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300814 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700815 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700816 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600817 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600818 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600819 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300820 /* use only after cleaning per-op data, see io_clean_op() */
821 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700822 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823
Jens Axboee8c2bc12020-08-15 18:44:09 -0700824 /* opcode allocated if it needs to store data for async defer */
825 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700826 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800827 /* polled IO has completed */
828 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700829
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700830 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300831 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700832
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300833 struct io_ring_ctx *ctx;
834 unsigned int flags;
Jens Axboeabc54d62021-02-24 13:32:30 -0700835 atomic_t refs;
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300836 struct task_struct *task;
837 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700838
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000839 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000840 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700841
Pavel Begunkovb303fe22021-04-11 01:46:26 +0100842 /* used with ctx->iopoll_list with reads/writes */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300843 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000844 union {
845 struct io_task_work io_task_work;
846 struct callback_head task_work;
847 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300848 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
849 struct hlist_node hash_node;
850 struct async_poll *apoll;
851 struct io_wq_work work;
Pavel Begunkoveae071c2021-04-25 14:32:24 +0100852 /* store used ubuf, so we can prevent reloading */
853 struct io_mapped_ubuf *imu;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700854};
855
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000856struct io_tctx_node {
857 struct list_head ctx_node;
858 struct task_struct *task;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +0000859 struct io_ring_ctx *ctx;
860};
861
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300862struct io_defer_entry {
863 struct list_head list;
864 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300865 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300866};
867
Jens Axboed3656342019-12-18 09:50:26 -0700868struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700869 /* needs req->file assigned */
870 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700871 /* hash wq insertion if file is a regular file */
872 unsigned hash_reg_file : 1;
873 /* unbound wq insertion if file is a non-regular file */
874 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700875 /* opcode is not supported by this kernel */
876 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700877 /* set if opcode supports polled "wait" */
878 unsigned pollin : 1;
879 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700880 /* op supports buffer selection */
881 unsigned buffer_select : 1;
Pavel Begunkov26f05052021-02-28 22:35:18 +0000882 /* do prep async if is going to be punted */
883 unsigned needs_async_setup : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600884 /* should block plug */
885 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700886 /* size of async data needed, if any */
887 unsigned short async_size;
Jens Axboed3656342019-12-18 09:50:26 -0700888};
889
Jens Axboe09186822020-10-13 15:01:40 -0600890static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300891 [IORING_OP_NOP] = {},
892 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700893 .needs_file = 1,
894 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700895 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700896 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000897 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600898 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700899 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700900 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300901 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700902 .needs_file = 1,
903 .hash_reg_file = 1,
904 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700905 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000906 .needs_async_setup = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600907 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700908 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700911 .needs_file = 1,
912 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300913 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700914 .needs_file = 1,
915 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700916 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600917 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700918 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700919 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300920 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700921 .needs_file = 1,
922 .hash_reg_file = 1,
923 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700924 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600925 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700926 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700927 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700929 .needs_file = 1,
930 .unbound_nonreg_file = 1,
931 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300932 [IORING_OP_POLL_REMOVE] = {},
933 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700934 .needs_file = 1,
935 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300936 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700937 .needs_file = 1,
938 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700939 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000940 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700944 .needs_file = 1,
945 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700946 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700947 .buffer_select = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000948 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700949 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700950 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300951 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700952 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700953 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000954 [IORING_OP_TIMEOUT_REMOVE] = {
955 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700958 .needs_file = 1,
959 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700960 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_ASYNC_CANCEL] = {},
963 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700964 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700965 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300966 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700967 .needs_file = 1,
968 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700969 .pollout = 1,
Pavel Begunkov26f05052021-02-28 22:35:18 +0000970 .needs_async_setup = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700971 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700974 .needs_file = 1,
975 },
Jens Axboe44526be2021-02-15 13:32:18 -0700976 [IORING_OP_OPENAT] = {},
977 [IORING_OP_CLOSE] = {},
978 [IORING_OP_FILES_UPDATE] = {},
979 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300980 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700981 .needs_file = 1,
982 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700983 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700984 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600985 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700986 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700987 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300988 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700989 .needs_file = 1,
990 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700991 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600992 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700993 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700994 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300995 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700996 .needs_file = 1,
997 },
Jens Axboe44526be2021-02-15 13:32:18 -0700998 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300999 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001000 .needs_file = 1,
1001 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001002 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001003 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001004 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -07001005 .needs_file = 1,
1006 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001007 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001008 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -07001009 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001010 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -07001011 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001012 [IORING_OP_EPOLL_CTL] = {
1013 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001014 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001015 [IORING_OP_SPLICE] = {
1016 .needs_file = 1,
1017 .hash_reg_file = 1,
1018 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001019 },
1020 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001021 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001022 [IORING_OP_TEE] = {
1023 .needs_file = 1,
1024 .hash_reg_file = 1,
1025 .unbound_nonreg_file = 1,
1026 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001027 [IORING_OP_SHUTDOWN] = {
1028 .needs_file = 1,
1029 },
Jens Axboe44526be2021-02-15 13:32:18 -07001030 [IORING_OP_RENAMEAT] = {},
1031 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -07001032};
1033
Pavel Begunkov7a612352021-03-09 00:37:59 +00001034static bool io_disarm_next(struct io_kiocb *req);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00001035static void io_uring_del_task_file(unsigned long index);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001036static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1037 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001038 bool cancel_all);
Pavel Begunkov734551d2021-04-18 14:52:09 +01001039static void io_uring_cancel_sqpoll(struct io_sq_data *sqd);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001040static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001041
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001042static bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1043 long res, unsigned int cflags);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001044static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001045static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001046static void io_dismantle_req(struct io_kiocb *req);
1047static void io_put_task(struct task_struct *task, int nr);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001048static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
1049static void io_queue_linked_timeout(struct io_kiocb *req);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01001050static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01001051 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01001052 unsigned nr_args);
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001053static void io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001054static struct file *io_file_get(struct io_submit_state *state,
1055 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001056static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001057static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001058
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001059static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001060static void io_submit_flush_completions(struct io_comp_state *cs,
1061 struct io_ring_ctx *ctx);
Jens Axboe50826202021-02-23 09:02:26 -07001062static bool io_poll_remove_waitqs(struct io_kiocb *req);
Pavel Begunkov179ae0d2021-02-28 22:35:20 +00001063static int io_req_prep_async(struct io_kiocb *req);
Jens Axboe9a56a232019-01-09 09:06:50 -07001064
Jens Axboe2b188cc2019-01-07 10:46:33 -07001065static struct kmem_cache *req_cachep;
1066
Jens Axboe09186822020-10-13 15:01:40 -06001067static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001068
1069struct sock *io_uring_get_socket(struct file *file)
1070{
1071#if defined(CONFIG_UNIX)
1072 if (file->f_op == &io_uring_fops) {
1073 struct io_ring_ctx *ctx = file->private_data;
1074
1075 return ctx->ring_sock->sk;
1076 }
1077#endif
1078 return NULL;
1079}
1080EXPORT_SYMBOL(io_uring_get_socket);
1081
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001082#define io_for_each_link(pos, head) \
1083 for (pos = (head); pos; pos = pos->link)
1084
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01001085static inline void io_req_set_rsrc_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001086{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001087 struct io_ring_ctx *ctx = req->ctx;
1088
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001089 if (!req->fixed_rsrc_refs) {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01001090 req->fixed_rsrc_refs = &ctx->rsrc_node->refs;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001091 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001092 }
1093}
1094
Pavel Begunkovf70865d2021-04-11 01:46:40 +01001095static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1096{
1097 bool got = percpu_ref_tryget(ref);
1098
1099 /* already at zero, wait for ->release() */
1100 if (!got)
1101 wait_for_completion(compl);
1102 percpu_ref_resurrect(ref);
1103 if (got)
1104 percpu_ref_put(ref);
1105}
1106
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001107static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
1108 bool cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001109{
1110 struct io_kiocb *req;
1111
Pavel Begunkov68207682021-03-22 01:58:25 +00001112 if (task && head->task != task)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001113 return false;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01001114 if (cancel_all)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001115 return true;
1116
1117 io_for_each_link(req, head) {
Pavel Begunkovb05a1bc2021-03-04 13:59:24 +00001118 if (req->flags & REQ_F_INFLIGHT)
Jens Axboe02a13672021-01-23 15:49:31 -07001119 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001120 }
1121 return false;
1122}
1123
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001124static inline void req_set_fail(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001125{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001126 req->flags |= REQ_F_FAIL;
Jens Axboec40f6372020-06-25 15:39:59 -06001127}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001128
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1130{
1131 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1132
Jens Axboe0f158b42020-05-14 17:18:39 -06001133 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134}
1135
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001136static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1137{
1138 return !req->timeout.off;
1139}
1140
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1142{
1143 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001144 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145
1146 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1147 if (!ctx)
1148 return NULL;
1149
Jens Axboe78076bb2019-12-04 19:56:40 -07001150 /*
1151 * Use 5 bits less than the max cq entries, that should give us around
1152 * 32 entries per hash list if totally full and uniformly spread.
1153 */
1154 hash_bits = ilog2(p->cq_entries);
1155 hash_bits -= 5;
1156 if (hash_bits <= 0)
1157 hash_bits = 1;
1158 ctx->cancel_hash_bits = hash_bits;
1159 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1160 GFP_KERNEL);
1161 if (!ctx->cancel_hash)
1162 goto err;
1163 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1164
Pavel Begunkov62248432021-04-28 13:11:29 +01001165 ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL);
1166 if (!ctx->dummy_ubuf)
1167 goto err;
1168 /* set invalid range, so io_import_fixed() fails meeting it */
1169 ctx->dummy_ubuf->ubuf = -1UL;
1170
Roman Gushchin21482892019-05-07 10:01:48 -07001171 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001172 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1173 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001174
1175 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001176 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001177 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001178 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001179 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001180 init_completion(&ctx->ref_comp);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07001181 xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00001182 xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001183 mutex_init(&ctx->uring_lock);
1184 init_waitqueue_head(&ctx->wait);
1185 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001186 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001187 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001188 INIT_LIST_HEAD(&ctx->timeout_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001189 spin_lock_init(&ctx->rsrc_ref_lock);
1190 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001191 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1192 init_llist_head(&ctx->rsrc_put_llist);
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00001193 INIT_LIST_HEAD(&ctx->tctx_list);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001194 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001195 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001196 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001197err:
Pavel Begunkov62248432021-04-28 13:11:29 +01001198 kfree(ctx->dummy_ubuf);
Jens Axboe78076bb2019-12-04 19:56:40 -07001199 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001200 kfree(ctx);
1201 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001202}
1203
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001204static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001205{
Jens Axboe2bc99302020-07-09 09:43:27 -06001206 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1207 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001208
Hao Xu7b289c32021-04-13 15:20:39 +08001209 return seq + ctx->cq_extra != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001210 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001211 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001212
Bob Liu9d858b22019-11-13 18:06:25 +08001213 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001214}
1215
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001216static void io_req_track_inflight(struct io_kiocb *req)
1217{
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001218 if (!(req->flags & REQ_F_INFLIGHT)) {
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001219 req->flags |= REQ_F_INFLIGHT;
Pavel Begunkovb303fe22021-04-11 01:46:26 +01001220 atomic_inc(&current->io_uring->inflight_tracked);
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001221 }
1222}
1223
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001224static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001225{
Jens Axboed3656342019-12-18 09:50:26 -07001226 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001227 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001228
Jens Axboe003e8dc2021-03-06 09:22:27 -07001229 if (!req->work.creds)
1230 req->work.creds = get_current_cred();
1231
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001232 req->work.list.next = NULL;
1233 req->work.flags = 0;
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001234 if (req->flags & REQ_F_FORCE_ASYNC)
1235 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1236
Jens Axboed3656342019-12-18 09:50:26 -07001237 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001238 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001239 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboe4b982bd2021-04-01 08:38:34 -06001240 } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
Jens Axboed3656342019-12-18 09:50:26 -07001241 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001242 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001243 }
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001244
1245 switch (req->opcode) {
1246 case IORING_OP_SPLICE:
1247 case IORING_OP_TEE:
Pavel Begunkove1d675d2021-03-22 01:58:29 +00001248 if (!S_ISREG(file_inode(req->splice.file_in)->i_mode))
1249 req->work.flags |= IO_WQ_WORK_UNBOUND;
1250 break;
1251 }
Jens Axboe561fb042019-10-24 07:25:42 -06001252}
1253
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001254static void io_prep_async_link(struct io_kiocb *req)
1255{
1256 struct io_kiocb *cur;
1257
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001258 io_for_each_link(cur, req)
1259 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001260}
1261
Pavel Begunkovebf93662021-03-01 18:20:47 +00001262static void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001263{
Jackie Liua197f662019-11-08 08:09:12 -07001264 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001265 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001266 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001267
Jens Axboe3bfe6102021-02-16 14:15:30 -07001268 BUG_ON(!tctx);
1269 BUG_ON(!tctx->io_wq);
Jens Axboe561fb042019-10-24 07:25:42 -06001270
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001271 /* init ->work of the whole link before punting */
1272 io_prep_async_link(req);
Pavel Begunkovd07f1e8a2021-03-22 01:45:58 +00001273 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1274 &req->work, req->flags);
Pavel Begunkovebf93662021-03-01 18:20:47 +00001275 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001276 if (link)
1277 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001278}
1279
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001280static void io_kill_timeout(struct io_kiocb *req, int status)
Pavel Begunkov8c855882021-04-13 02:58:41 +01001281 __must_hold(&req->ctx->completion_lock)
Jens Axboe5262f562019-09-17 12:26:57 -06001282{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001283 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001284
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001285 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001286 atomic_set(&req->ctx->cq_timeouts,
1287 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001288 list_del_init(&req->timeout.list);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001289 io_cqring_fill_event(req->ctx, req->user_data, status, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001290 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001291 }
1292}
1293
Pavel Begunkov04518942020-05-26 20:34:05 +03001294static void __io_queue_deferred(struct io_ring_ctx *ctx)
1295{
1296 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001297 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1298 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001299
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001300 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001301 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001302 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001303 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001304 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001305 } while (!list_empty(&ctx->defer_list));
1306}
1307
Pavel Begunkov360428f2020-05-30 14:54:17 +03001308static void io_flush_timeouts(struct io_ring_ctx *ctx)
1309{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001310 u32 seq;
1311
1312 if (list_empty(&ctx->timeout_list))
1313 return;
1314
1315 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1316
1317 do {
1318 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001319 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001320 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001321
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001322 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001323 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001324
1325 /*
1326 * Since seq can easily wrap around over time, subtract
1327 * the last seq at which timeouts were flushed before comparing.
1328 * Assuming not more than 2^31-1 events have happened since,
1329 * these subtractions won't have wrapped, so we can check if
1330 * target is in [last_seq, current_seq] by comparing the two.
1331 */
1332 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1333 events_got = seq - ctx->cq_last_tm_flush;
1334 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001335 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001336
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001337 list_del_init(&req->timeout.list);
Pavel Begunkov1ee41602021-03-25 18:32:42 +00001338 io_kill_timeout(req, 0);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001339 } while (!list_empty(&ctx->timeout_list));
1340
1341 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001342}
1343
Jens Axboede0617e2019-04-06 21:51:27 -06001344static void io_commit_cqring(struct io_ring_ctx *ctx)
1345{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001346 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001347
1348 /* order cqe stores with ring update */
1349 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001350
Pavel Begunkov04518942020-05-26 20:34:05 +03001351 if (unlikely(!list_empty(&ctx->defer_list)))
1352 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001353}
1354
Jens Axboe90554202020-09-03 12:12:41 -06001355static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1356{
1357 struct io_rings *r = ctx->rings;
1358
1359 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1360}
1361
Pavel Begunkov888aae22021-01-19 13:32:39 +00001362static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1363{
1364 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1365}
1366
Pavel Begunkov8d133262021-04-11 01:46:33 +01001367static inline struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001368{
Hristo Venev75b28af2019-08-26 17:23:46 +00001369 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001370 unsigned tail;
1371
Stefan Bühler115e12e2019-04-24 23:54:18 +02001372 /*
1373 * writes to the cq entry need to come after reading head; the
1374 * control dependency is enough as we're using WRITE_ONCE to
1375 * fill the cq entry
1376 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001377 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001378 return NULL;
1379
Pavel Begunkov888aae22021-01-19 13:32:39 +00001380 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001381 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382}
1383
Jens Axboef2842ab2020-01-08 11:04:00 -07001384static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1385{
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001386 if (likely(!ctx->cq_ev_fd))
Jens Axboef0b493e2020-02-01 21:30:11 -07001387 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001388 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1389 return false;
Pavel Begunkov44c769d2021-04-11 01:46:31 +01001390 return !ctx->eventfd_async || io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001391}
1392
Jens Axboeb41e9852020-02-17 09:52:41 -07001393static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001394{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001395 /* see waitqueue_active() comment */
1396 smp_mb();
1397
Jens Axboe8c838782019-03-12 15:48:16 -06001398 if (waitqueue_active(&ctx->wait))
1399 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001400 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1401 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001402 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001403 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001404 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001405 wake_up_interruptible(&ctx->cq_wait);
1406 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1407 }
Jens Axboe8c838782019-03-12 15:48:16 -06001408}
1409
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001410static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1411{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001412 /* see waitqueue_active() comment */
1413 smp_mb();
1414
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001415 if (ctx->flags & IORING_SETUP_SQPOLL) {
1416 if (waitqueue_active(&ctx->wait))
1417 wake_up(&ctx->wait);
1418 }
1419 if (io_should_trigger_evfd(ctx))
1420 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001421 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001422 wake_up_interruptible(&ctx->cq_wait);
1423 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1424 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001425}
1426
Jens Axboec4a2ed72019-11-21 21:01:26 -07001427/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001428static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001430 struct io_rings *rings = ctx->rings;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001431 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001432 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001433
Pavel Begunkove23de152020-12-17 00:24:37 +00001434 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1435 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001436
Jens Axboeb18032b2021-01-24 16:58:56 -07001437 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001438 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001439 while (!list_empty(&ctx->cq_overflow_list)) {
1440 struct io_uring_cqe *cqe = io_get_cqring(ctx);
1441 struct io_overflow_cqe *ocqe;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001442
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001443 if (!cqe && !force)
1444 break;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001445 ocqe = list_first_entry(&ctx->cq_overflow_list,
1446 struct io_overflow_cqe, list);
1447 if (cqe)
1448 memcpy(cqe, &ocqe->cqe, sizeof(*cqe));
1449 else
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001450 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001451 ++ctx->cached_cq_overflow);
Jens Axboeb18032b2021-01-24 16:58:56 -07001452 posted = true;
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001453 list_del(&ocqe->list);
1454 kfree(ocqe);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001455 }
1456
Pavel Begunkov09e88402020-12-17 00:24:38 +00001457 all_flushed = list_empty(&ctx->cq_overflow_list);
1458 if (all_flushed) {
1459 clear_bit(0, &ctx->sq_check_overflow);
1460 clear_bit(0, &ctx->cq_check_overflow);
1461 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1462 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001463
Jens Axboeb18032b2021-01-24 16:58:56 -07001464 if (posted)
1465 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001466 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001467 if (posted)
1468 io_cqring_ev_posted(ctx);
Pavel Begunkov09e88402020-12-17 00:24:38 +00001469 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001470}
1471
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001472static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Pavel Begunkov6c503152021-01-04 20:36:36 +00001473{
Jens Axboeca0a2652021-03-04 17:15:48 -07001474 bool ret = true;
1475
Pavel Begunkov6c503152021-01-04 20:36:36 +00001476 if (test_bit(0, &ctx->cq_check_overflow)) {
1477 /* iopoll syncs against uring_lock, not completion_lock */
1478 if (ctx->flags & IORING_SETUP_IOPOLL)
1479 mutex_lock(&ctx->uring_lock);
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00001480 ret = __io_cqring_overflow_flush(ctx, force);
Pavel Begunkov6c503152021-01-04 20:36:36 +00001481 if (ctx->flags & IORING_SETUP_IOPOLL)
1482 mutex_unlock(&ctx->uring_lock);
1483 }
Jens Axboeca0a2652021-03-04 17:15:48 -07001484
1485 return ret;
Pavel Begunkov6c503152021-01-04 20:36:36 +00001486}
1487
Jens Axboeabc54d62021-02-24 13:32:30 -07001488/*
1489 * Shamelessly stolen from the mm implementation of page reference checking,
1490 * see commit f958d7b528b1 for details.
1491 */
1492#define req_ref_zero_or_close_to_overflow(req) \
1493 ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u)
1494
Jens Axboede9b4cc2021-02-24 13:28:27 -07001495static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001496{
Jens Axboeabc54d62021-02-24 13:32:30 -07001497 return atomic_inc_not_zero(&req->refs);
Jens Axboede9b4cc2021-02-24 13:28:27 -07001498}
1499
1500static inline bool req_ref_sub_and_test(struct io_kiocb *req, int refs)
1501{
Jens Axboeabc54d62021-02-24 13:32:30 -07001502 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1503 return atomic_sub_and_test(refs, &req->refs);
Jens Axboede9b4cc2021-02-24 13:28:27 -07001504}
1505
1506static inline bool req_ref_put_and_test(struct io_kiocb *req)
1507{
Jens Axboeabc54d62021-02-24 13:32:30 -07001508 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1509 return atomic_dec_and_test(&req->refs);
Jens Axboede9b4cc2021-02-24 13:28:27 -07001510}
1511
1512static inline void req_ref_put(struct io_kiocb *req)
1513{
Jens Axboeabc54d62021-02-24 13:32:30 -07001514 WARN_ON_ONCE(req_ref_put_and_test(req));
Jens Axboede9b4cc2021-02-24 13:28:27 -07001515}
1516
1517static inline void req_ref_get(struct io_kiocb *req)
1518{
Jens Axboeabc54d62021-02-24 13:32:30 -07001519 WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
1520 atomic_inc(&req->refs);
Jens Axboede9b4cc2021-02-24 13:28:27 -07001521}
1522
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001523static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data,
1524 long res, unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001525{
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001526 struct io_overflow_cqe *ocqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001528 ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT);
1529 if (!ocqe) {
1530 /*
1531 * If we're in ring overflow flush mode, or in task cancel mode,
1532 * or cannot allocate an overflow entry, then we need to drop it
1533 * on the floor.
1534 */
1535 WRITE_ONCE(ctx->rings->cq_overflow, ++ctx->cached_cq_overflow);
1536 return false;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001537 }
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001538 if (list_empty(&ctx->cq_overflow_list)) {
1539 set_bit(0, &ctx->sq_check_overflow);
1540 set_bit(0, &ctx->cq_check_overflow);
1541 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
1542 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001543 ocqe->cqe.user_data = user_data;
Pavel Begunkovcce4b8b2021-04-13 02:58:44 +01001544 ocqe->cqe.res = res;
1545 ocqe->cqe.flags = cflags;
1546 list_add_tail(&ocqe->list, &ctx->cq_overflow_list);
1547 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001548}
1549
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001550static inline bool __io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1551 long res, unsigned int cflags)
Pavel Begunkov8d133262021-04-11 01:46:33 +01001552{
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553 struct io_uring_cqe *cqe;
1554
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001555 trace_io_uring_complete(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001556
1557 /*
1558 * If we can't get a cq entry, userspace overflowed the
1559 * submission (by quite a lot). Increment the overflow count in
1560 * the ring.
1561 */
1562 cqe = io_get_cqring(ctx);
1563 if (likely(cqe)) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001564 WRITE_ONCE(cqe->user_data, user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565 WRITE_ONCE(cqe->res, res);
1566 WRITE_ONCE(cqe->flags, cflags);
Pavel Begunkov8d133262021-04-11 01:46:33 +01001567 return true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001568 }
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001569 return io_cqring_event_overflow(ctx, user_data, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001570}
1571
Pavel Begunkov8d133262021-04-11 01:46:33 +01001572/* not as hot to bloat with inlining */
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001573static noinline bool io_cqring_fill_event(struct io_ring_ctx *ctx, u64 user_data,
1574 long res, unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001575{
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001576 return __io_cqring_fill_event(ctx, user_data, res, cflags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001577}
1578
Pavel Begunkov7a612352021-03-09 00:37:59 +00001579static void io_req_complete_post(struct io_kiocb *req, long res,
1580 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001581{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001582 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001583 unsigned long flags;
1584
1585 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001586 __io_cqring_fill_event(ctx, req->user_data, res, cflags);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001587 /*
1588 * If we're the last reference to this request, add to our locked
1589 * free_list cache.
1590 */
Jens Axboede9b4cc2021-02-24 13:28:27 -07001591 if (req_ref_put_and_test(req)) {
Jens Axboec7dae4b2021-02-09 19:53:37 -07001592 struct io_comp_state *cs = &ctx->submit_state.comp;
1593
Pavel Begunkov7a612352021-03-09 00:37:59 +00001594 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001595 if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL))
Pavel Begunkov7a612352021-03-09 00:37:59 +00001596 io_disarm_next(req);
1597 if (req->link) {
1598 io_req_task_queue(req->link);
1599 req->link = NULL;
1600 }
1601 }
Jens Axboec7dae4b2021-02-09 19:53:37 -07001602 io_dismantle_req(req);
1603 io_put_task(req->task, 1);
1604 list_add(&req->compl.list, &cs->locked_free_list);
1605 cs->locked_free_nr++;
Pavel Begunkov180f8292021-03-14 20:57:09 +00001606 } else {
1607 if (!percpu_ref_tryget(&ctx->refs))
1608 req = NULL;
1609 }
Pavel Begunkov7a612352021-03-09 00:37:59 +00001610 io_commit_cqring(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7a612352021-03-09 00:37:59 +00001612
Pavel Begunkov180f8292021-03-14 20:57:09 +00001613 if (req) {
1614 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001615 percpu_ref_put(&ctx->refs);
Pavel Begunkov180f8292021-03-14 20:57:09 +00001616 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001617}
1618
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001619static inline bool io_req_needs_clean(struct io_kiocb *req)
1620{
Jens Axboe75652a302021-04-15 09:52:40 -06001621 return req->flags & (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP |
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001622 REQ_F_POLLED | REQ_F_INFLIGHT);
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001623}
1624
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001625static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001626 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001627{
Jens Axboe4e3d9ff2021-04-15 17:44:34 -06001628 if (io_req_needs_clean(req))
Pavel Begunkov68fb8972021-03-19 17:22:41 +00001629 io_clean_op(req);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001630 req->result = res;
1631 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001632 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001633}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001634
Pavel Begunkov889fca72021-02-10 00:03:09 +00001635static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1636 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001637{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001638 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1639 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001640 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001641 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001642}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001643
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001644static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001645{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001646 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001647}
1648
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001649static void io_req_complete_failed(struct io_kiocb *req, long res)
1650{
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001651 req_set_fail(req);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00001652 io_put_req(req);
1653 io_req_complete_post(req, res, 0);
1654}
1655
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001656static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx,
1657 struct io_comp_state *cs)
1658{
1659 spin_lock_irq(&ctx->completion_lock);
1660 list_splice_init(&cs->locked_free_list, &cs->free_list);
1661 cs->locked_free_nr = 0;
1662 spin_unlock_irq(&ctx->completion_lock);
1663}
1664
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001665/* Returns true IFF there are requests in the cache */
Jens Axboec7dae4b2021-02-09 19:53:37 -07001666static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001667{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001668 struct io_submit_state *state = &ctx->submit_state;
1669 struct io_comp_state *cs = &state->comp;
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001670 int nr;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001671
Jens Axboec7dae4b2021-02-09 19:53:37 -07001672 /*
1673 * If we have more than a batch's worth of requests in our IRQ side
1674 * locked cache, grab the lock and move them over to our submission
1675 * side cache.
1676 */
Pavel Begunkovdac7a092021-03-19 17:22:39 +00001677 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH)
1678 io_flush_cached_locked_reqs(ctx, cs);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001679
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001680 nr = state->free_reqs;
Jens Axboec7dae4b2021-02-09 19:53:37 -07001681 while (!list_empty(&cs->free_list)) {
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001682 struct io_kiocb *req = list_first_entry(&cs->free_list,
1683 struct io_kiocb, compl.list);
1684
Jens Axboe2b188cc2019-01-07 10:46:33 -07001685 list_del(&req->compl.list);
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001686 state->reqs[nr++] = req;
1687 if (nr == ARRAY_SIZE(state->reqs))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001688 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001689 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690
Pavel Begunkovdd78f492021-03-19 17:22:35 +00001691 state->free_reqs = nr;
1692 return nr != 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693}
1694
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001695static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001696{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001697 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001698
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001699 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07001700
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001701 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001702 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001703 int ret;
1704
Jens Axboec7dae4b2021-02-09 19:53:37 -07001705 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001706 goto got_req;
1707
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001708 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1709 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001710
1711 /*
1712 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1713 * retry single alloc to be on the safe side.
1714 */
1715 if (unlikely(ret <= 0)) {
1716 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1717 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001718 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001719 ret = 1;
1720 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001721 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001723got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001724 state->free_reqs--;
1725 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726}
1727
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001728static inline void io_put_file(struct file *file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001729{
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001730 if (file)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001731 fput(file);
1732}
1733
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001734static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735{
Pavel Begunkov094bae42021-03-19 17:22:42 +00001736 unsigned int flags = req->flags;
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001737
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01001738 if (io_req_needs_clean(req))
1739 io_clean_op(req);
Pavel Begunkove1d767f2021-03-19 17:22:43 +00001740 if (!(flags & REQ_F_FIXED_FILE))
1741 io_put_file(req->file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001742 if (req->fixed_rsrc_refs)
1743 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov094bae42021-03-19 17:22:42 +00001744 if (req->async_data)
1745 kfree(req->async_data);
Jens Axboe003e8dc2021-03-06 09:22:27 -07001746 if (req->work.creds) {
1747 put_cred(req->work.creds);
1748 req->work.creds = NULL;
1749 }
Pavel Begunkove6543a82020-06-28 12:52:30 +03001750}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001751
Pavel Begunkovb23fcf42021-03-01 18:20:48 +00001752/* must to be called somewhat shortly after putting a request */
Pavel Begunkov7c660732021-01-25 11:42:21 +00001753static inline void io_put_task(struct task_struct *task, int nr)
1754{
1755 struct io_uring_task *tctx = task->io_uring;
1756
1757 percpu_counter_sub(&tctx->inflight, nr);
1758 if (unlikely(atomic_read(&tctx->in_idle)))
1759 wake_up(&tctx->wait);
1760 put_task_struct_many(task, nr);
1761}
1762
Pavel Begunkov216578e2020-10-13 09:44:00 +01001763static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001764{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001765 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001766
Pavel Begunkov216578e2020-10-13 09:44:00 +01001767 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001768 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001769
Pavel Begunkov3893f392021-02-10 00:03:15 +00001770 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001771 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001772}
1773
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001774static inline void io_remove_next_linked(struct io_kiocb *req)
1775{
1776 struct io_kiocb *nxt = req->link;
1777
1778 req->link = nxt->link;
1779 nxt->link = NULL;
1780}
1781
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001782static bool io_kill_linked_timeout(struct io_kiocb *req)
1783 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001784{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001785 struct io_kiocb *link = req->link;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001786
Pavel Begunkov900fad42020-10-19 16:39:16 +01001787 /*
1788 * Can happen if a linked timeout fired and link had been like
1789 * req -> link t-out -> link t-out [-> ...]
1790 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001791 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1792 struct io_timeout_data *io = link->async_data;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001793
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001794 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001795 link->timeout.head = NULL;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01001796 if (hrtimer_try_to_cancel(&io->timer) != -1) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001797 io_cqring_fill_event(link->ctx, link->user_data,
1798 -ECANCELED, 0);
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001799 io_put_req_deferred(link, 1);
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001800 return true;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001801 }
1802 }
Pavel Begunkovd4729fb2021-03-22 01:58:24 +00001803 return false;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001804}
1805
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001806static void io_fail_links(struct io_kiocb *req)
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001807 __must_hold(&req->ctx->completion_lock)
Jens Axboe9e645e112019-05-10 16:07:28 -06001808{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001809 struct io_kiocb *nxt, *link = req->link;
Jens Axboe9e645e112019-05-10 16:07:28 -06001810
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001811 req->link = NULL;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001812 while (link) {
1813 nxt = link->link;
1814 link->link = NULL;
1815
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001816 trace_io_uring_fail_link(req, link);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01001817 io_cqring_fill_event(link->ctx, link->user_data, -ECANCELED, 0);
Jens Axboe1575f212021-02-27 15:20:49 -07001818 io_put_req_deferred(link, 2);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001819 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001820 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001821}
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001822
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001823static bool io_disarm_next(struct io_kiocb *req)
1824 __must_hold(&req->ctx->completion_lock)
1825{
1826 bool posted = false;
1827
1828 if (likely(req->flags & REQ_F_LINK_TIMEOUT))
1829 posted = io_kill_linked_timeout(req);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001830 if (unlikely((req->flags & REQ_F_FAIL) &&
Pavel Begunkove4335ed2021-04-11 01:46:39 +01001831 !(req->flags & REQ_F_HARDLINK))) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001832 posted |= (req->link != NULL);
1833 io_fail_links(req);
1834 }
1835 return posted;
Jens Axboe9e645e112019-05-10 16:07:28 -06001836}
1837
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001838static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001839{
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001840 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001841
Jens Axboe9e645e112019-05-10 16:07:28 -06001842 /*
1843 * If LINK is set, we have dependent requests in this chain. If we
1844 * didn't fail this request, queue the first one up, moving any other
1845 * dependencies to the next request. In case of failure, fail the rest
1846 * of the chain.
1847 */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01001848 if (req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_FAIL)) {
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001849 struct io_ring_ctx *ctx = req->ctx;
1850 unsigned long flags;
1851 bool posted;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001852
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001853 spin_lock_irqsave(&ctx->completion_lock, flags);
1854 posted = io_disarm_next(req);
1855 if (posted)
1856 io_commit_cqring(req->ctx);
1857 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1858 if (posted)
1859 io_cqring_ev_posted(ctx);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001860 }
Pavel Begunkov33cc89a2021-03-09 00:37:58 +00001861 nxt = req->link;
1862 req->link = NULL;
1863 return nxt;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001864}
Jens Axboe2665abf2019-11-05 12:40:47 -07001865
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001866static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001867{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00001868 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001869 return NULL;
1870 return __io_req_find_next(req);
1871}
1872
Pavel Begunkov2c323952021-02-28 22:04:53 +00001873static void ctx_flush_and_put(struct io_ring_ctx *ctx)
1874{
1875 if (!ctx)
1876 return;
1877 if (ctx->submit_state.comp.nr) {
1878 mutex_lock(&ctx->uring_lock);
1879 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
1880 mutex_unlock(&ctx->uring_lock);
1881 }
1882 percpu_ref_put(&ctx->refs);
1883}
1884
Jens Axboe7cbf1722021-02-10 00:03:20 +00001885static bool __tctx_task_work(struct io_uring_task *tctx)
1886{
Jens Axboe65453d12021-02-10 00:03:21 +00001887 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001888 struct io_wq_work_list list;
1889 struct io_wq_work_node *node;
1890
1891 if (wq_list_empty(&tctx->task_list))
1892 return false;
1893
Jens Axboe0b81e802021-02-16 10:33:53 -07001894 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001895 list = tctx->task_list;
1896 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001897 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001898
1899 node = list.first;
1900 while (node) {
1901 struct io_wq_work_node *next = node->next;
1902 struct io_kiocb *req;
1903
1904 req = container_of(node, struct io_kiocb, io_task_work.node);
Pavel Begunkov2c323952021-02-28 22:04:53 +00001905 if (req->ctx != ctx) {
1906 ctx_flush_and_put(ctx);
1907 ctx = req->ctx;
1908 percpu_ref_get(&ctx->refs);
1909 }
1910
Jens Axboe7cbf1722021-02-10 00:03:20 +00001911 req->task_work.func(&req->task_work);
1912 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00001913 }
1914
Pavel Begunkov2c323952021-02-28 22:04:53 +00001915 ctx_flush_and_put(ctx);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001916 return list.first != NULL;
1917}
1918
1919static void tctx_task_work(struct callback_head *cb)
1920{
1921 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
1922
Jens Axboe1d5f3602021-02-26 14:54:16 -07001923 clear_bit(0, &tctx->task_state);
1924
Jens Axboe7cbf1722021-02-10 00:03:20 +00001925 while (__tctx_task_work(tctx))
1926 cond_resched();
Jens Axboe7cbf1722021-02-10 00:03:20 +00001927}
1928
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00001929static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboe7cbf1722021-02-10 00:03:20 +00001930{
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00001931 struct task_struct *tsk = req->task;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001932 struct io_uring_task *tctx = tsk->io_uring;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00001933 enum task_work_notify_mode notify;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001934 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07001935 unsigned long flags;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00001936 int ret = 0;
1937
1938 if (unlikely(tsk->flags & PF_EXITING))
1939 return -ESRCH;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001940
1941 WARN_ON_ONCE(!tctx);
1942
Jens Axboe0b81e802021-02-16 10:33:53 -07001943 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001944 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001945 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001946
1947 /* task_work already pending, we're done */
1948 if (test_bit(0, &tctx->task_state) ||
1949 test_and_set_bit(0, &tctx->task_state))
1950 return 0;
1951
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00001952 /*
1953 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
1954 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
1955 * processing task_work. There's no reliable way to tell if TWA_RESUME
1956 * will do the job.
1957 */
1958 notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL;
1959
1960 if (!task_work_add(tsk, &tctx->task_work, notify)) {
1961 wake_up_process(tsk);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001962 return 0;
Pavel Begunkovc15b79d2021-03-19 17:22:44 +00001963 }
Jens Axboe7cbf1722021-02-10 00:03:20 +00001964
1965 /*
1966 * Slow path - we failed, find and delete work. if the work is not
1967 * in the list, it got run and we're fine.
1968 */
Jens Axboe0b81e802021-02-16 10:33:53 -07001969 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001970 wq_list_for_each(node, prev, &tctx->task_list) {
1971 if (&req->io_task_work.node == node) {
1972 wq_list_del(&tctx->task_list, node, prev);
1973 ret = 1;
1974 break;
1975 }
1976 }
Jens Axboe0b81e802021-02-16 10:33:53 -07001977 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001978 clear_bit(0, &tctx->task_state);
1979 return ret;
1980}
1981
Pavel Begunkov9b465712021-03-15 14:23:07 +00001982static bool io_run_task_work_head(struct callback_head **work_head)
1983{
1984 struct callback_head *work, *next;
1985 bool executed = false;
1986
1987 do {
1988 work = xchg(work_head, NULL);
1989 if (!work)
1990 break;
1991
1992 do {
1993 next = work->next;
1994 work->func(work);
1995 work = next;
1996 cond_resched();
1997 } while (work);
1998 executed = true;
1999 } while (1);
2000
2001 return executed;
2002}
2003
2004static void io_task_work_add_head(struct callback_head **work_head,
2005 struct callback_head *task_work)
2006{
2007 struct callback_head *head;
2008
2009 do {
2010 head = READ_ONCE(*work_head);
2011 task_work->next = head;
2012 } while (cmpxchg(work_head, head, task_work) != head);
2013}
2014
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002015static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002016 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002017{
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002018 init_task_work(&req->task_work, cb);
Pavel Begunkov9b465712021-03-15 14:23:07 +00002019 io_task_work_add_head(&req->ctx->exit_task_work, &req->task_work);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002020}
2021
Jens Axboec40f6372020-06-25 15:39:59 -06002022static void io_req_task_cancel(struct callback_head *cb)
2023{
2024 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002025 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002026
Pavel Begunkove83acd72021-02-28 22:35:09 +00002027 /* ctx is guaranteed to stay alive while we hold uring_lock */
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002028 mutex_lock(&ctx->uring_lock);
Pavel Begunkov25935532021-03-19 17:22:40 +00002029 io_req_complete_failed(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002030 mutex_unlock(&ctx->uring_lock);
Jens Axboec40f6372020-06-25 15:39:59 -06002031}
2032
2033static void __io_req_task_submit(struct io_kiocb *req)
2034{
2035 struct io_ring_ctx *ctx = req->ctx;
2036
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002037 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002038 mutex_lock(&ctx->uring_lock);
Pavel Begunkov70aacfe2021-03-01 13:02:15 +00002039 if (!(current->flags & PF_EXITING) && !current->in_execve)
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002040 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002041 else
Pavel Begunkov25935532021-03-19 17:22:40 +00002042 io_req_complete_failed(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002043 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002044}
2045
Jens Axboec40f6372020-06-25 15:39:59 -06002046static void io_req_task_submit(struct callback_head *cb)
2047{
2048 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2049
2050 __io_req_task_submit(req);
2051}
2052
Pavel Begunkova3df76982021-02-18 22:32:52 +00002053static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2054{
Pavel Begunkova3df76982021-02-18 22:32:52 +00002055 req->result = ret;
2056 req->task_work.func = io_req_task_cancel;
2057
2058 if (unlikely(io_req_task_work_add(req)))
2059 io_req_task_work_add_fallback(req, io_req_task_cancel);
2060}
2061
Pavel Begunkov2c4b8eb2021-02-28 22:35:10 +00002062static void io_req_task_queue(struct io_kiocb *req)
2063{
2064 req->task_work.func = io_req_task_submit;
2065
2066 if (unlikely(io_req_task_work_add(req)))
2067 io_req_task_queue_fail(req, -ECANCELED);
2068}
2069
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002070static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002071{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002072 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002073
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002074 if (nxt)
2075 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002076}
2077
Jens Axboe9e645e112019-05-10 16:07:28 -06002078static void io_free_req(struct io_kiocb *req)
2079{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002080 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002081 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002082}
2083
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002084struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002085 struct task_struct *task;
2086 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002087 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002088};
2089
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002090static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002091{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002092 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002093 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002094 rb->task = NULL;
2095}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002096
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002097static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2098 struct req_batch *rb)
2099{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002100 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002101 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002102 if (rb->ctx_refs)
2103 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002104}
2105
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002106static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2107 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002108{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002109 io_queue_next(req);
Pavel Begunkov96670652021-03-19 17:22:32 +00002110 io_dismantle_req(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002111
Jens Axboee3bc8e92020-09-24 08:45:57 -06002112 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002113 if (rb->task)
2114 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002115 rb->task = req->task;
2116 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002117 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002118 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002119 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002120
Pavel Begunkovbd759042021-02-12 03:23:50 +00002121 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002122 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002123 else
2124 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002125}
2126
Pavel Begunkov905c1722021-02-10 00:03:14 +00002127static void io_submit_flush_completions(struct io_comp_state *cs,
2128 struct io_ring_ctx *ctx)
2129{
2130 int i, nr = cs->nr;
2131 struct io_kiocb *req;
2132 struct req_batch rb;
2133
2134 io_init_req_batch(&rb);
2135 spin_lock_irq(&ctx->completion_lock);
2136 for (i = 0; i < nr; i++) {
2137 req = cs->reqs[i];
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002138 __io_cqring_fill_event(ctx, req->user_data, req->result,
2139 req->compl.cflags);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002140 }
2141 io_commit_cqring(ctx);
2142 spin_unlock_irq(&ctx->completion_lock);
2143
2144 io_cqring_ev_posted(ctx);
2145 for (i = 0; i < nr; i++) {
2146 req = cs->reqs[i];
2147
2148 /* submission and completion refs */
Jens Axboede9b4cc2021-02-24 13:28:27 -07002149 if (req_ref_sub_and_test(req, 2))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002150 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002151 }
2152
2153 io_req_free_batch_finish(ctx, &rb);
2154 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002155}
2156
Jens Axboeba816ad2019-09-28 11:36:45 -06002157/*
2158 * Drop reference to request, return next in chain (if there is one) if this
2159 * was the last reference to this request.
2160 */
Pavel Begunkov0d850352021-03-19 17:22:37 +00002161static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002162{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002163 struct io_kiocb *nxt = NULL;
2164
Jens Axboede9b4cc2021-02-24 13:28:27 -07002165 if (req_ref_put_and_test(req)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002166 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002167 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002168 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002169 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002170}
2171
Pavel Begunkov0d850352021-03-19 17:22:37 +00002172static inline void io_put_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002173{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002174 if (req_ref_put_and_test(req))
Jens Axboedef596e2019-01-09 08:59:42 -07002175 io_free_req(req);
2176}
2177
Pavel Begunkov216578e2020-10-13 09:44:00 +01002178static void io_put_req_deferred_cb(struct callback_head *cb)
2179{
2180 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2181
2182 io_free_req(req);
2183}
2184
2185static void io_free_req_deferred(struct io_kiocb *req)
2186{
Jens Axboe7cbf1722021-02-10 00:03:20 +00002187 req->task_work.func = io_put_req_deferred_cb;
Pavel Begunkova05432f2021-03-19 17:22:38 +00002188 if (unlikely(io_req_task_work_add(req)))
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002189 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002190}
2191
2192static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2193{
Jens Axboede9b4cc2021-02-24 13:28:27 -07002194 if (req_ref_sub_and_test(req, refs))
Pavel Begunkov216578e2020-10-13 09:44:00 +01002195 io_free_req_deferred(req);
2196}
2197
Pavel Begunkov6c503152021-01-04 20:36:36 +00002198static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002199{
2200 /* See comment at the top of this file */
2201 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002202 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002203}
2204
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002205static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2206{
2207 struct io_rings *rings = ctx->rings;
2208
2209 /* make sure SQ entry isn't read before tail */
2210 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2211}
2212
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002213static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002214{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002215 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002216
Jens Axboebcda7ba2020-02-23 16:42:51 -07002217 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2218 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002219 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002220 kfree(kbuf);
2221 return cflags;
2222}
2223
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002224static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2225{
2226 struct io_buffer *kbuf;
2227
2228 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2229 return io_put_kbuf(req, kbuf);
2230}
2231
Jens Axboe4c6e2772020-07-01 11:29:10 -06002232static inline bool io_run_task_work(void)
2233{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002234 /*
2235 * Not safe to run on exiting task, and the task_work handling will
2236 * not add work to such a task.
2237 */
2238 if (unlikely(current->flags & PF_EXITING))
2239 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002240 if (current->task_works) {
2241 __set_current_state(TASK_RUNNING);
2242 task_work_run();
2243 return true;
2244 }
2245
2246 return false;
2247}
2248
Jens Axboedef596e2019-01-09 08:59:42 -07002249/*
2250 * Find and free completed poll iocbs
2251 */
2252static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2253 struct list_head *done)
2254{
Jens Axboe8237e042019-12-28 10:48:22 -07002255 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002256 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002257
2258 /* order with ->result store in io_complete_rw_iopoll() */
2259 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002260
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002261 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002262 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002263 int cflags = 0;
2264
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002265 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002266 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002267
Pavel Begunkov8c130822021-03-22 01:58:32 +00002268 if (READ_ONCE(req->result) == -EAGAIN &&
2269 !(req->flags & REQ_F_DONT_REISSUE)) {
Pavel Begunkovf1613402021-02-11 18:28:21 +00002270 req->iopoll_completed = 0;
Pavel Begunkov8c130822021-03-22 01:58:32 +00002271 req_ref_get(req);
2272 io_queue_async_work(req);
2273 continue;
Pavel Begunkovf1613402021-02-11 18:28:21 +00002274 }
2275
Jens Axboebcda7ba2020-02-23 16:42:51 -07002276 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002277 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002278
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01002279 __io_cqring_fill_event(ctx, req->user_data, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002280 (*nr_events)++;
2281
Jens Axboede9b4cc2021-02-24 13:28:27 -07002282 if (req_ref_put_and_test(req))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002283 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002284 }
Jens Axboedef596e2019-01-09 08:59:42 -07002285
Jens Axboe09bb8392019-03-13 12:39:28 -06002286 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002287 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002288 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002289}
2290
Jens Axboedef596e2019-01-09 08:59:42 -07002291static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2292 long min)
2293{
2294 struct io_kiocb *req, *tmp;
2295 LIST_HEAD(done);
2296 bool spin;
2297 int ret;
2298
2299 /*
2300 * Only spin for completions if we don't have multiple devices hanging
2301 * off our complete list, and we're under the requested amount.
2302 */
2303 spin = !ctx->poll_multi_file && *nr_events < min;
2304
2305 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002306 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002307 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002308
2309 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002310 * Move completed and retryable entries to our local lists.
2311 * If we find a request that requires polling, break out
2312 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002313 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002314 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002315 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002316 continue;
2317 }
2318 if (!list_empty(&done))
2319 break;
2320
2321 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2322 if (ret < 0)
2323 break;
2324
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002325 /* iopoll may have completed current req */
2326 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002327 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002328
Jens Axboedef596e2019-01-09 08:59:42 -07002329 if (ret && spin)
2330 spin = false;
2331 ret = 0;
2332 }
2333
2334 if (!list_empty(&done))
2335 io_iopoll_complete(ctx, nr_events, &done);
2336
2337 return ret;
2338}
2339
2340/*
Jens Axboedef596e2019-01-09 08:59:42 -07002341 * We can't just wait for polled events to come to us, we have to actively
2342 * find and complete them.
2343 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002344static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002345{
2346 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2347 return;
2348
2349 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002350 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002351 unsigned int nr_events = 0;
2352
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002353 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002354
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002355 /* let it sleep and repeat later if can't complete a request */
2356 if (nr_events == 0)
2357 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002358 /*
2359 * Ensure we allow local-to-the-cpu processing to take place,
2360 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002361 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002362 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002363 if (need_resched()) {
2364 mutex_unlock(&ctx->uring_lock);
2365 cond_resched();
2366 mutex_lock(&ctx->uring_lock);
2367 }
Jens Axboedef596e2019-01-09 08:59:42 -07002368 }
2369 mutex_unlock(&ctx->uring_lock);
2370}
2371
Pavel Begunkov7668b922020-07-07 16:36:21 +03002372static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002373{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002374 unsigned int nr_events = 0;
Pavel Begunkove9979b32021-04-13 02:58:45 +01002375 int ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002376
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002377 /*
2378 * We disallow the app entering submit/complete with polling, but we
2379 * still need to lock the ring to prevent racing with polled issue
2380 * that got punted to a workqueue.
2381 */
2382 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002383 /*
2384 * Don't enter poll loop if we already have events pending.
2385 * If we do, we can potentially be spinning for commands that
2386 * already triggered a CQE (eg in error).
2387 */
2388 if (test_bit(0, &ctx->cq_check_overflow))
2389 __io_cqring_overflow_flush(ctx, false);
2390 if (io_cqring_events(ctx))
2391 goto out;
Jens Axboedef596e2019-01-09 08:59:42 -07002392 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002393 /*
2394 * If a submit got punted to a workqueue, we can have the
2395 * application entering polling for a command before it gets
2396 * issued. That app will hold the uring_lock for the duration
2397 * of the poll right here, so we need to take a breather every
2398 * now and then to ensure that the issue has a chance to add
2399 * the poll to the issued list. Otherwise we can spin here
2400 * forever, while the workqueue is stuck trying to acquire the
2401 * very same mutex.
2402 */
Pavel Begunkove9979b32021-04-13 02:58:45 +01002403 if (list_empty(&ctx->iopoll_list)) {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002404 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002405 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002406 mutex_lock(&ctx->uring_lock);
Pavel Begunkove9979b32021-04-13 02:58:45 +01002407
2408 if (list_empty(&ctx->iopoll_list))
2409 break;
Jens Axboe500f9fb2019-08-19 12:15:59 -06002410 }
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002411 ret = io_do_iopoll(ctx, &nr_events, min);
2412 } while (!ret && nr_events < min && !need_resched());
2413out:
Jens Axboe500f9fb2019-08-19 12:15:59 -06002414 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002415 return ret;
2416}
2417
Jens Axboe491381ce2019-10-17 09:20:46 -06002418static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002419{
Jens Axboe491381ce2019-10-17 09:20:46 -06002420 /*
2421 * Tell lockdep we inherited freeze protection from submission
2422 * thread.
2423 */
2424 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov1c986792021-03-22 01:58:31 +00002425 struct super_block *sb = file_inode(req->file)->i_sb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002426
Pavel Begunkov1c986792021-03-22 01:58:31 +00002427 __sb_writers_acquired(sb, SB_FREEZE_WRITE);
2428 sb_end_write(sb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002429 }
2430}
2431
Jens Axboeb63534c2020-06-04 11:28:00 -06002432#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002433static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002434{
Pavel Begunkovab454432021-03-22 01:58:33 +00002435 struct io_async_rw *rw = req->async_data;
Jens Axboeb63534c2020-06-04 11:28:00 -06002436
Pavel Begunkovab454432021-03-22 01:58:33 +00002437 if (!rw)
2438 return !io_req_prep_async(req);
2439 /* may have left rw->iter inconsistent on -EIOCBQUEUED */
2440 iov_iter_revert(&rw->iter, req->result - iov_iter_count(&rw->iter));
2441 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002442}
Jens Axboeb63534c2020-06-04 11:28:00 -06002443
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002444static bool io_rw_should_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002445{
Jens Axboe355afae2020-09-02 09:30:31 -06002446 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002447 struct io_ring_ctx *ctx = req->ctx;
Jens Axboeb63534c2020-06-04 11:28:00 -06002448
Jens Axboe355afae2020-09-02 09:30:31 -06002449 if (!S_ISBLK(mode) && !S_ISREG(mode))
2450 return false;
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002451 if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
2452 !(ctx->flags & IORING_SETUP_IOPOLL)))
Jens Axboeb63534c2020-06-04 11:28:00 -06002453 return false;
Jens Axboe7c977a52021-02-23 19:17:35 -07002454 /*
2455 * If ref is dying, we might be running poll reap from the exit work.
2456 * Don't attempt to reissue from that path, just let it fail with
2457 * -EAGAIN.
2458 */
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002459 if (percpu_ref_is_dying(&ctx->refs))
2460 return false;
2461 return true;
2462}
Jens Axboee82ad482021-04-02 19:45:34 -06002463#else
Jens Axboea1ff1e32021-04-12 06:40:02 -06002464static bool io_resubmit_prep(struct io_kiocb *req)
2465{
2466 return false;
2467}
Jens Axboee82ad482021-04-02 19:45:34 -06002468static bool io_rw_should_reissue(struct io_kiocb *req)
2469{
2470 return false;
2471}
Jens Axboe3e6a0d32021-03-01 13:56:00 -07002472#endif
2473
Jens Axboea1d7c392020-06-22 11:09:46 -06002474static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002475 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002476{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002477 int cflags = 0;
2478
Pavel Begunkovb65c1282021-03-22 01:45:59 +00002479 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2480 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002481 if (res != req->result) {
2482 if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
2483 io_rw_should_reissue(req)) {
2484 req->flags |= REQ_F_REISSUE;
2485 return;
2486 }
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002487 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002488 }
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002489 if (req->flags & REQ_F_BUFFER_SELECTED)
2490 cflags = io_put_rw_kbuf(req);
2491 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002492}
2493
2494static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2495{
Jens Axboe9adbd452019-12-20 08:45:55 -07002496 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002497
Pavel Begunkov889fca72021-02-10 00:03:09 +00002498 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002499}
2500
Jens Axboedef596e2019-01-09 08:59:42 -07002501static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2502{
Jens Axboe9adbd452019-12-20 08:45:55 -07002503 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002504
Jens Axboe491381ce2019-10-17 09:20:46 -06002505 if (kiocb->ki_flags & IOCB_WRITE)
2506 kiocb_end_write(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002507 if (unlikely(res != req->result)) {
Jens Axboea1ff1e32021-04-12 06:40:02 -06002508 if (!(res == -EAGAIN && io_rw_should_reissue(req) &&
2509 io_resubmit_prep(req))) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002510 req_set_fail(req);
Pavel Begunkov9532b992021-03-22 01:58:34 +00002511 req->flags |= REQ_F_DONT_REISSUE;
2512 }
Pavel Begunkov8c130822021-03-22 01:58:32 +00002513 }
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002514
2515 WRITE_ONCE(req->result, res);
Jens Axboeb9b0e0d2021-02-23 08:18:36 -07002516 /* order with io_iopoll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002517 smp_wmb();
2518 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002519}
2520
2521/*
2522 * After the iocb has been issued, it's safe to be found on the poll list.
2523 * Adding the kiocb to the list AFTER submission ensures that we don't
Pavel Begunkovf39c8a52021-04-13 02:58:46 +01002524 * find it from a io_do_iopoll() thread before the issuer is done
Jens Axboedef596e2019-01-09 08:59:42 -07002525 * accessing the kiocb cookie.
2526 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002527static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002528{
2529 struct io_ring_ctx *ctx = req->ctx;
2530
2531 /*
2532 * Track whether we have multiple files in our lists. This will impact
2533 * how we do polling eventually, not spinning if we're on potentially
2534 * different devices.
2535 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002536 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002537 ctx->poll_multi_file = false;
2538 } else if (!ctx->poll_multi_file) {
2539 struct io_kiocb *list_req;
2540
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002541 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002542 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002543 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002544 ctx->poll_multi_file = true;
2545 }
2546
2547 /*
2548 * For fast devices, IO may have already completed. If it has, add
2549 * it to the front so we find it first.
2550 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002551 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002552 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002553 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002554 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002555
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002556 /*
2557 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2558 * task context or in io worker task context. If current task context is
2559 * sq thread, we don't need to check whether should wake up sq thread.
2560 */
2561 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002562 wq_has_sleeper(&ctx->sq_data->wait))
2563 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002564}
2565
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002566static inline void io_state_file_put(struct io_submit_state *state)
2567{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002568 if (state->file_refs) {
2569 fput_many(state->file, state->file_refs);
2570 state->file_refs = 0;
2571 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002572}
2573
2574/*
2575 * Get as many references to a file as we have IOs left in this submission,
2576 * assuming most submissions are for one file, or at least that each file
2577 * has more than one submission.
2578 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002579static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002580{
2581 if (!state)
2582 return fget(fd);
2583
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002584 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002585 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002586 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002587 return state->file;
2588 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002589 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002590 }
2591 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002592 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002593 return NULL;
2594
2595 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002596 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002597 return state->file;
2598}
2599
Jens Axboe4503b762020-06-01 10:00:27 -06002600static bool io_bdev_nowait(struct block_device *bdev)
2601{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002602 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002603}
2604
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605/*
2606 * If we tracked the file through the SCM inflight mechanism, we could support
2607 * any file. For now, just ensure that anything potentially problematic is done
2608 * inline.
2609 */
Jens Axboe7b29f922021-03-12 08:30:14 -07002610static bool __io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002611{
2612 umode_t mode = file_inode(file)->i_mode;
2613
Jens Axboe4503b762020-06-01 10:00:27 -06002614 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002615 if (IS_ENABLED(CONFIG_BLOCK) &&
2616 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002617 return true;
2618 return false;
2619 }
2620 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002621 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002622 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002623 if (IS_ENABLED(CONFIG_BLOCK) &&
2624 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002625 file->f_op != &io_uring_fops)
2626 return true;
2627 return false;
2628 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002629
Jens Axboec5b85622020-06-09 19:23:05 -06002630 /* any ->read/write should understand O_NONBLOCK */
2631 if (file->f_flags & O_NONBLOCK)
2632 return true;
2633
Jens Axboeaf197f52020-04-28 13:15:06 -06002634 if (!(file->f_mode & FMODE_NOWAIT))
2635 return false;
2636
2637 if (rw == READ)
2638 return file->f_op->read_iter != NULL;
2639
2640 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641}
2642
Jens Axboe7b29f922021-03-12 08:30:14 -07002643static bool io_file_supports_async(struct io_kiocb *req, int rw)
2644{
2645 if (rw == READ && (req->flags & REQ_F_ASYNC_READ))
2646 return true;
2647 else if (rw == WRITE && (req->flags & REQ_F_ASYNC_WRITE))
2648 return true;
2649
2650 return __io_file_supports_async(req->file, rw);
2651}
2652
Pavel Begunkova88fc402020-09-30 22:57:53 +03002653static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654{
Jens Axboedef596e2019-01-09 08:59:42 -07002655 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002656 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002657 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002658 unsigned ioprio;
2659 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660
Jens Axboe7b29f922021-03-12 08:30:14 -07002661 if (!(req->flags & REQ_F_ISREG) && S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002662 req->flags |= REQ_F_ISREG;
2663
Jens Axboe2b188cc2019-01-07 10:46:33 -07002664 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002665 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002666 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002667 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002668 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002669 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002670 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2671 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2672 if (unlikely(ret))
2673 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002674
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002675 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2676 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2677 req->flags |= REQ_F_NOWAIT;
2678
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679 ioprio = READ_ONCE(sqe->ioprio);
2680 if (ioprio) {
2681 ret = ioprio_check_cap(ioprio);
2682 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002683 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002684
2685 kiocb->ki_ioprio = ioprio;
2686 } else
2687 kiocb->ki_ioprio = get_current_ioprio();
2688
Jens Axboedef596e2019-01-09 08:59:42 -07002689 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002690 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2691 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002692 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002693
Jens Axboedef596e2019-01-09 08:59:42 -07002694 kiocb->ki_flags |= IOCB_HIPRI;
2695 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002696 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002697 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002698 if (kiocb->ki_flags & IOCB_HIPRI)
2699 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002700 kiocb->ki_complete = io_complete_rw;
2701 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002702
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002703 if (req->opcode == IORING_OP_READ_FIXED ||
2704 req->opcode == IORING_OP_WRITE_FIXED) {
2705 req->imu = NULL;
2706 io_req_set_rsrc_node(req);
2707 }
2708
Jens Axboe3529d8c2019-12-19 18:24:38 -07002709 req->rw.addr = READ_ONCE(sqe->addr);
2710 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002711 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002712 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002713}
2714
2715static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2716{
2717 switch (ret) {
2718 case -EIOCBQUEUED:
2719 break;
2720 case -ERESTARTSYS:
2721 case -ERESTARTNOINTR:
2722 case -ERESTARTNOHAND:
2723 case -ERESTART_RESTARTBLOCK:
2724 /*
2725 * We can't just restart the syscall, since previously
2726 * submitted sqes may already be in progress. Just fail this
2727 * IO with EINTR.
2728 */
2729 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002730 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002731 default:
2732 kiocb->ki_complete(kiocb, ret, 0);
2733 }
2734}
2735
Jens Axboea1d7c392020-06-22 11:09:46 -06002736static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002737 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002738{
Jens Axboeba042912019-12-25 16:33:42 -07002739 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002740 struct io_async_rw *io = req->async_data;
Pavel Begunkov97284632021-04-08 19:28:03 +01002741 bool check_reissue = kiocb->ki_complete == io_complete_rw;
Jens Axboeba042912019-12-25 16:33:42 -07002742
Jens Axboe227c0c92020-08-13 11:51:40 -06002743 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002744 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002745 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002746 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002747 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002748 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002749 }
2750
Jens Axboeba042912019-12-25 16:33:42 -07002751 if (req->flags & REQ_F_CUR_POS)
2752 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002753 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002754 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002755 else
2756 io_rw_done(kiocb, ret);
Pavel Begunkov97284632021-04-08 19:28:03 +01002757
2758 if (check_reissue && req->flags & REQ_F_REISSUE) {
2759 req->flags &= ~REQ_F_REISSUE;
Jens Axboea7be7c22021-04-15 11:31:14 -06002760 if (io_resubmit_prep(req)) {
Pavel Begunkov8c130822021-03-22 01:58:32 +00002761 req_ref_get(req);
2762 io_queue_async_work(req);
2763 } else {
Pavel Begunkov97284632021-04-08 19:28:03 +01002764 int cflags = 0;
2765
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01002766 req_set_fail(req);
Pavel Begunkov97284632021-04-08 19:28:03 +01002767 if (req->flags & REQ_F_BUFFER_SELECTED)
2768 cflags = io_put_rw_kbuf(req);
2769 __io_req_complete(req, issue_flags, ret, cflags);
2770 }
2771 }
Jens Axboeba816ad2019-09-28 11:36:45 -06002772}
2773
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002774static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter,
2775 struct io_mapped_ubuf *imu)
Jens Axboeedafcce2019-01-09 09:16:05 -07002776{
Jens Axboe9adbd452019-12-20 08:45:55 -07002777 size_t len = req->rw.len;
Pavel Begunkov75769e32021-04-01 15:43:54 +01002778 u64 buf_end, buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002779 size_t offset;
Jens Axboeedafcce2019-01-09 09:16:05 -07002780
Pavel Begunkov75769e32021-04-01 15:43:54 +01002781 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
Jens Axboeedafcce2019-01-09 09:16:05 -07002782 return -EFAULT;
2783 /* not inside the mapped region */
Pavel Begunkov4751f532021-04-01 15:43:55 +01002784 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
Jens Axboeedafcce2019-01-09 09:16:05 -07002785 return -EFAULT;
2786
2787 /*
2788 * May not be a start of buffer, set size appropriately
2789 * and advance us to the beginning.
2790 */
2791 offset = buf_addr - imu->ubuf;
2792 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002793
2794 if (offset) {
2795 /*
2796 * Don't use iov_iter_advance() here, as it's really slow for
2797 * using the latter parts of a big fixed buffer - it iterates
2798 * over each segment manually. We can cheat a bit here, because
2799 * we know that:
2800 *
2801 * 1) it's a BVEC iter, we set it up
2802 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2803 * first and last bvec
2804 *
2805 * So just find our index, and adjust the iterator afterwards.
2806 * If the offset is within the first bvec (or the whole first
2807 * bvec, just use iov_iter_advance(). This makes it easier
2808 * since we can just skip the first segment, which may not
2809 * be PAGE_SIZE aligned.
2810 */
2811 const struct bio_vec *bvec = imu->bvec;
2812
2813 if (offset <= bvec->bv_len) {
2814 iov_iter_advance(iter, offset);
2815 } else {
2816 unsigned long seg_skip;
2817
2818 /* skip first vec */
2819 offset -= bvec->bv_len;
2820 seg_skip = 1 + (offset >> PAGE_SHIFT);
2821
2822 iter->bvec = bvec + seg_skip;
2823 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002824 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002825 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002826 }
2827 }
2828
Pavel Begunkov847595d2021-02-04 13:52:06 +00002829 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002830}
2831
Pavel Begunkoveae071c2021-04-25 14:32:24 +01002832static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
2833{
2834 struct io_ring_ctx *ctx = req->ctx;
2835 struct io_mapped_ubuf *imu = req->imu;
2836 u16 index, buf_index = req->buf_index;
2837
2838 if (likely(!imu)) {
2839 if (unlikely(buf_index >= ctx->nr_user_bufs))
2840 return -EFAULT;
2841 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2842 imu = READ_ONCE(ctx->user_bufs[index]);
2843 req->imu = imu;
2844 }
2845 return __io_import_fixed(req, rw, iter, imu);
2846}
2847
Jens Axboebcda7ba2020-02-23 16:42:51 -07002848static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2849{
2850 if (needs_lock)
2851 mutex_unlock(&ctx->uring_lock);
2852}
2853
2854static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2855{
2856 /*
2857 * "Normal" inline submissions always hold the uring_lock, since we
2858 * grab it from the system call. Same is true for the SQPOLL offload.
2859 * The only exception is when we've detached the request and issue it
2860 * from an async worker thread, grab the lock for that case.
2861 */
2862 if (needs_lock)
2863 mutex_lock(&ctx->uring_lock);
2864}
2865
2866static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2867 int bgid, struct io_buffer *kbuf,
2868 bool needs_lock)
2869{
2870 struct io_buffer *head;
2871
2872 if (req->flags & REQ_F_BUFFER_SELECTED)
2873 return kbuf;
2874
2875 io_ring_submit_lock(req->ctx, needs_lock);
2876
2877 lockdep_assert_held(&req->ctx->uring_lock);
2878
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002879 head = xa_load(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002880 if (head) {
2881 if (!list_empty(&head->list)) {
2882 kbuf = list_last_entry(&head->list, struct io_buffer,
2883 list);
2884 list_del(&kbuf->list);
2885 } else {
2886 kbuf = head;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07002887 xa_erase(&req->ctx->io_buffers, bgid);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002888 }
2889 if (*len > kbuf->len)
2890 *len = kbuf->len;
2891 } else {
2892 kbuf = ERR_PTR(-ENOBUFS);
2893 }
2894
2895 io_ring_submit_unlock(req->ctx, needs_lock);
2896
2897 return kbuf;
2898}
2899
Jens Axboe4d954c22020-02-27 07:31:19 -07002900static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
2901 bool needs_lock)
2902{
2903 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002904 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07002905
2906 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002907 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07002908 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
2909 if (IS_ERR(kbuf))
2910 return kbuf;
2911 req->rw.addr = (u64) (unsigned long) kbuf;
2912 req->flags |= REQ_F_BUFFER_SELECTED;
2913 return u64_to_user_ptr(kbuf->addr);
2914}
2915
2916#ifdef CONFIG_COMPAT
2917static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
2918 bool needs_lock)
2919{
2920 struct compat_iovec __user *uiov;
2921 compat_ssize_t clen;
2922 void __user *buf;
2923 ssize_t len;
2924
2925 uiov = u64_to_user_ptr(req->rw.addr);
2926 if (!access_ok(uiov, sizeof(*uiov)))
2927 return -EFAULT;
2928 if (__get_user(clen, &uiov->iov_len))
2929 return -EFAULT;
2930 if (clen < 0)
2931 return -EINVAL;
2932
2933 len = clen;
2934 buf = io_rw_buffer_select(req, &len, needs_lock);
2935 if (IS_ERR(buf))
2936 return PTR_ERR(buf);
2937 iov[0].iov_base = buf;
2938 iov[0].iov_len = (compat_size_t) len;
2939 return 0;
2940}
2941#endif
2942
2943static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2944 bool needs_lock)
2945{
2946 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
2947 void __user *buf;
2948 ssize_t len;
2949
2950 if (copy_from_user(iov, uiov, sizeof(*uiov)))
2951 return -EFAULT;
2952
2953 len = iov[0].iov_len;
2954 if (len < 0)
2955 return -EINVAL;
2956 buf = io_rw_buffer_select(req, &len, needs_lock);
2957 if (IS_ERR(buf))
2958 return PTR_ERR(buf);
2959 iov[0].iov_base = buf;
2960 iov[0].iov_len = len;
2961 return 0;
2962}
2963
2964static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
2965 bool needs_lock)
2966{
Jens Axboedddb3e22020-06-04 11:27:01 -06002967 if (req->flags & REQ_F_BUFFER_SELECTED) {
2968 struct io_buffer *kbuf;
2969
2970 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2971 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
2972 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07002973 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06002974 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00002975 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07002976 return -EINVAL;
2977
2978#ifdef CONFIG_COMPAT
2979 if (req->ctx->compat)
2980 return io_compat_import(req, iov, needs_lock);
2981#endif
2982
2983 return __io_iov_buffer_select(req, iov, needs_lock);
2984}
2985
Pavel Begunkov847595d2021-02-04 13:52:06 +00002986static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
2987 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002988{
Jens Axboe9adbd452019-12-20 08:45:55 -07002989 void __user *buf = u64_to_user_ptr(req->rw.addr);
2990 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00002991 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07002992 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07002993
Pavel Begunkov7d009162019-11-25 23:14:40 +03002994 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002995 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002996 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002997 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002998
Jens Axboebcda7ba2020-02-23 16:42:51 -07002999 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003000 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003001 return -EINVAL;
3002
Jens Axboe3a6820f2019-12-22 15:19:35 -07003003 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003004 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003005 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003006 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003007 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003008 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003009 }
3010
Jens Axboe3a6820f2019-12-22 15:19:35 -07003011 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3012 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003013 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003014 }
3015
Jens Axboe4d954c22020-02-27 07:31:19 -07003016 if (req->flags & REQ_F_BUFFER_SELECT) {
3017 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003018 if (!ret)
3019 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003020 *iovec = NULL;
3021 return ret;
3022 }
3023
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003024 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3025 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026}
3027
Jens Axboe0fef9482020-08-26 10:36:20 -06003028static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3029{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003030 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003031}
3032
Jens Axboe32960612019-09-23 11:05:34 -06003033/*
3034 * For files that don't have ->read_iter() and ->write_iter(), handle them
3035 * by looping over ->read() or ->write() manually.
3036 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003037static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003038{
Jens Axboe4017eb92020-10-22 14:14:12 -06003039 struct kiocb *kiocb = &req->rw.kiocb;
3040 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003041 ssize_t ret = 0;
3042
3043 /*
3044 * Don't support polled IO through this interface, and we can't
3045 * support non-blocking either. For the latter, this just causes
3046 * the kiocb to be handled from an async context.
3047 */
3048 if (kiocb->ki_flags & IOCB_HIPRI)
3049 return -EOPNOTSUPP;
3050 if (kiocb->ki_flags & IOCB_NOWAIT)
3051 return -EAGAIN;
3052
3053 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003054 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003055 ssize_t nr;
3056
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003057 if (!iov_iter_is_bvec(iter)) {
3058 iovec = iov_iter_iovec(iter);
3059 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003060 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3061 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003062 }
3063
Jens Axboe32960612019-09-23 11:05:34 -06003064 if (rw == READ) {
3065 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003066 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003067 } else {
3068 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003069 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003070 }
3071
3072 if (nr < 0) {
3073 if (!ret)
3074 ret = nr;
3075 break;
3076 }
3077 ret += nr;
3078 if (nr != iovec.iov_len)
3079 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003080 req->rw.len -= nr;
3081 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003082 iov_iter_advance(iter, nr);
3083 }
3084
3085 return ret;
3086}
3087
Jens Axboeff6165b2020-08-13 09:47:43 -06003088static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3089 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003090{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003091 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003092
Jens Axboeff6165b2020-08-13 09:47:43 -06003093 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003094 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003095 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003096 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003097 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003098 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003099 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003100 unsigned iov_off = 0;
3101
3102 rw->iter.iov = rw->fast_iov;
3103 if (iter->iov != fast_iov) {
3104 iov_off = iter->iov - fast_iov;
3105 rw->iter.iov += iov_off;
3106 }
3107 if (rw->fast_iov != fast_iov)
3108 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003109 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003110 } else {
3111 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003112 }
3113}
3114
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003115static inline int io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003116{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003117 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3118 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3119 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003120}
3121
Jens Axboeff6165b2020-08-13 09:47:43 -06003122static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3123 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003124 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003125{
Pavel Begunkov26f05052021-02-28 22:35:18 +00003126 if (!force && !io_op_defs[req->opcode].needs_async_setup)
Jens Axboe74566df2020-01-13 19:23:24 -07003127 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003128 if (!req->async_data) {
Pavel Begunkov6cb78682021-02-28 22:35:17 +00003129 if (io_alloc_async_data(req)) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003130 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003131 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003132 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003133
Jens Axboeff6165b2020-08-13 09:47:43 -06003134 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003135 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003136 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003137}
3138
Pavel Begunkov73debe62020-09-30 22:57:54 +03003139static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003140{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003141 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003142 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003143 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003144
Pavel Begunkov2846c482020-11-07 13:16:27 +00003145 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003146 if (unlikely(ret < 0))
3147 return ret;
3148
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003149 iorw->bytes_done = 0;
3150 iorw->free_iovec = iov;
3151 if (iov)
3152 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003153 return 0;
3154}
3155
Pavel Begunkov73debe62020-09-30 22:57:54 +03003156static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003157{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003158 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3159 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003160 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003161}
3162
Jens Axboec1dd91d2020-08-03 16:43:59 -06003163/*
3164 * This is our waitqueue callback handler, registered through lock_page_async()
3165 * when we initially tried to do the IO with the iocb armed our waitqueue.
3166 * This gets called when the page is unlocked, and we generally expect that to
3167 * happen when the page IO is completed and the page is now uptodate. This will
3168 * queue a task_work based retry of the operation, attempting to copy the data
3169 * again. If the latter fails because the page was NOT uptodate, then we will
3170 * do a thread based blocking retry of the operation. That's the unexpected
3171 * slow path.
3172 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003173static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3174 int sync, void *arg)
3175{
3176 struct wait_page_queue *wpq;
3177 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003178 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003179
3180 wpq = container_of(wait, struct wait_page_queue, wait);
3181
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003182 if (!wake_page_match(wpq, key))
3183 return 0;
3184
Hao Xuc8d317a2020-09-29 20:00:45 +08003185 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003186 list_del_init(&wait->entry);
3187
Jens Axboebcf5a062020-05-22 09:24:42 -06003188 /* submit ref gets dropped, acquire a new one */
Jens Axboede9b4cc2021-02-24 13:28:27 -07003189 req_ref_get(req);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003190 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003191 return 1;
3192}
3193
Jens Axboec1dd91d2020-08-03 16:43:59 -06003194/*
3195 * This controls whether a given IO request should be armed for async page
3196 * based retry. If we return false here, the request is handed to the async
3197 * worker threads for retry. If we're doing buffered reads on a regular file,
3198 * we prepare a private wait_page_queue entry and retry the operation. This
3199 * will either succeed because the page is now uptodate and unlocked, or it
3200 * will register a callback when the page is unlocked at IO completion. Through
3201 * that callback, io_uring uses task_work to setup a retry of the operation.
3202 * That retry will attempt the buffered read again. The retry will generally
3203 * succeed, or in rare cases where it fails, we then fall back to using the
3204 * async worker threads for a blocking retry.
3205 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003206static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003207{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003208 struct io_async_rw *rw = req->async_data;
3209 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003210 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003211
3212 /* never retry for NOWAIT, we just complete with -EAGAIN */
3213 if (req->flags & REQ_F_NOWAIT)
3214 return false;
3215
Jens Axboe227c0c92020-08-13 11:51:40 -06003216 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003217 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003218 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003219
Jens Axboebcf5a062020-05-22 09:24:42 -06003220 /*
3221 * just use poll if we can, and don't attempt if the fs doesn't
3222 * support callback based unlocks
3223 */
3224 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3225 return false;
3226
Jens Axboe3b2a4432020-08-16 10:58:43 -07003227 wait->wait.func = io_async_buf_func;
3228 wait->wait.private = req;
3229 wait->wait.flags = 0;
3230 INIT_LIST_HEAD(&wait->wait.entry);
3231 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003232 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003233 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003234 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003235}
3236
3237static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3238{
3239 if (req->file->f_op->read_iter)
3240 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003241 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003242 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003243 else
3244 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003245}
3246
Pavel Begunkov889fca72021-02-10 00:03:09 +00003247static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003248{
3249 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003250 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003251 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003252 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003253 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003254 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003255
Pavel Begunkov2846c482020-11-07 13:16:27 +00003256 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003257 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003258 iovec = NULL;
3259 } else {
3260 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3261 if (ret < 0)
3262 return ret;
3263 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003264 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003265 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266
Jens Axboefd6c2e42019-12-18 12:19:41 -07003267 /* Ensure we clear previously set non-block flag */
3268 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003269 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003270 else
3271 kiocb->ki_flags |= IOCB_NOWAIT;
3272
Pavel Begunkov24c74672020-06-21 13:09:51 +03003273 /* If the file doesn't support async, just async punt */
Jens Axboe7b29f922021-03-12 08:30:14 -07003274 if (force_nonblock && !io_file_supports_async(req, READ)) {
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003275 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003276 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003277 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003278
Pavel Begunkov632546c2020-11-07 13:16:26 +00003279 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003280 if (unlikely(ret)) {
3281 kfree(iovec);
3282 return ret;
3283 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003284
Jens Axboe227c0c92020-08-13 11:51:40 -06003285 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003286
Jens Axboe230d50d2021-04-01 20:41:15 -06003287 if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003288 req->flags &= ~REQ_F_REISSUE;
Jens Axboeeefdf302020-08-27 16:40:19 -06003289 /* IOPOLL retry should happen for io-wq threads */
3290 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003291 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003292 /* no retry on NONBLOCK nor RWF_NOWAIT */
3293 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003294 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003295 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003296 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003297 ret = 0;
Jens Axboe230d50d2021-04-01 20:41:15 -06003298 } else if (ret == -EIOCBQUEUED) {
3299 goto out_free;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003300 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003301 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003302 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003303 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003304 }
3305
Jens Axboe227c0c92020-08-13 11:51:40 -06003306 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003307 if (ret2)
3308 return ret2;
3309
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003310 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003311 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003312 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003313 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003314
Pavel Begunkovb23df912021-02-04 13:52:04 +00003315 do {
3316 io_size -= ret;
3317 rw->bytes_done += ret;
3318 /* if we can retry, do so with the callbacks armed */
3319 if (!io_rw_should_retry(req)) {
3320 kiocb->ki_flags &= ~IOCB_WAITQ;
3321 return -EAGAIN;
3322 }
3323
3324 /*
3325 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3326 * we get -EIOCBQUEUED, then we'll get a notification when the
3327 * desired page gets unlocked. We can also get a partial read
3328 * here, and if we do, then just retry at the new offset.
3329 */
3330 ret = io_iter_do_read(req, iter);
3331 if (ret == -EIOCBQUEUED)
3332 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003333 /* we got some bytes, but not all. retry. */
Jens Axboeb5b0ecb2021-03-04 21:02:58 -07003334 kiocb->ki_flags &= ~IOCB_WAITQ;
Pavel Begunkovb23df912021-02-04 13:52:04 +00003335 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003336done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003337 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003338out_free:
3339 /* it's faster to check here then delegate to kfree */
3340 if (iovec)
3341 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003342 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003343}
3344
Pavel Begunkov73debe62020-09-30 22:57:54 +03003345static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003346{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003347 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3348 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003349 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003350}
3351
Pavel Begunkov889fca72021-02-10 00:03:09 +00003352static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003353{
3354 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003355 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003356 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003357 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003358 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003359 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003360
Pavel Begunkov2846c482020-11-07 13:16:27 +00003361 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003362 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003363 iovec = NULL;
3364 } else {
3365 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3366 if (ret < 0)
3367 return ret;
3368 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003369 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003370 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003371
Jens Axboefd6c2e42019-12-18 12:19:41 -07003372 /* Ensure we clear previously set non-block flag */
3373 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003374 kiocb->ki_flags &= ~IOCB_NOWAIT;
3375 else
3376 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003377
Pavel Begunkov24c74672020-06-21 13:09:51 +03003378 /* If the file doesn't support async, just async punt */
Jens Axboe7b29f922021-03-12 08:30:14 -07003379 if (force_nonblock && !io_file_supports_async(req, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003380 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003381
Jens Axboe10d59342019-12-09 20:16:22 -07003382 /* file path doesn't support NOWAIT for non-direct_IO */
3383 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3384 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003385 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003386
Pavel Begunkov632546c2020-11-07 13:16:26 +00003387 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003388 if (unlikely(ret))
3389 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003390
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003391 /*
3392 * Open-code file_start_write here to grab freeze protection,
3393 * which will be released by another thread in
3394 * io_complete_rw(). Fool lockdep by telling it the lock got
3395 * released so that it doesn't complain about the held lock when
3396 * we return to userspace.
3397 */
3398 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003399 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003400 __sb_writers_release(file_inode(req->file)->i_sb,
3401 SB_FREEZE_WRITE);
3402 }
3403 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003404
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003405 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003406 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003407 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003408 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003409 else
3410 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003411
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003412 if (req->flags & REQ_F_REISSUE) {
3413 req->flags &= ~REQ_F_REISSUE;
Jens Axboe230d50d2021-04-01 20:41:15 -06003414 ret2 = -EAGAIN;
Pavel Begunkov6ad7f232021-04-08 01:54:39 +01003415 }
Jens Axboe230d50d2021-04-01 20:41:15 -06003416
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003417 /*
3418 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3419 * retry them without IOCB_NOWAIT.
3420 */
3421 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3422 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003423 /* no retry on NONBLOCK nor RWF_NOWAIT */
3424 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003425 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003426 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003427 /* IOPOLL retry should happen for io-wq threads */
3428 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3429 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003430done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003431 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003432 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003433copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003434 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003435 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003436 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003437 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003438 }
Jens Axboe31b51512019-01-18 22:56:34 -07003439out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003440 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003441 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003442 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003443 return ret;
3444}
3445
Jens Axboe80a261f2020-09-28 14:23:58 -06003446static int io_renameat_prep(struct io_kiocb *req,
3447 const struct io_uring_sqe *sqe)
3448{
3449 struct io_rename *ren = &req->rename;
3450 const char __user *oldf, *newf;
3451
3452 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3453 return -EBADF;
3454
3455 ren->old_dfd = READ_ONCE(sqe->fd);
3456 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3457 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3458 ren->new_dfd = READ_ONCE(sqe->len);
3459 ren->flags = READ_ONCE(sqe->rename_flags);
3460
3461 ren->oldpath = getname(oldf);
3462 if (IS_ERR(ren->oldpath))
3463 return PTR_ERR(ren->oldpath);
3464
3465 ren->newpath = getname(newf);
3466 if (IS_ERR(ren->newpath)) {
3467 putname(ren->oldpath);
3468 return PTR_ERR(ren->newpath);
3469 }
3470
3471 req->flags |= REQ_F_NEED_CLEANUP;
3472 return 0;
3473}
3474
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003475static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003476{
3477 struct io_rename *ren = &req->rename;
3478 int ret;
3479
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003480 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003481 return -EAGAIN;
3482
3483 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3484 ren->newpath, ren->flags);
3485
3486 req->flags &= ~REQ_F_NEED_CLEANUP;
3487 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003488 req_set_fail(req);
Jens Axboe80a261f2020-09-28 14:23:58 -06003489 io_req_complete(req, ret);
3490 return 0;
3491}
3492
Jens Axboe14a11432020-09-28 14:27:37 -06003493static int io_unlinkat_prep(struct io_kiocb *req,
3494 const struct io_uring_sqe *sqe)
3495{
3496 struct io_unlink *un = &req->unlink;
3497 const char __user *fname;
3498
3499 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3500 return -EBADF;
3501
3502 un->dfd = READ_ONCE(sqe->fd);
3503
3504 un->flags = READ_ONCE(sqe->unlink_flags);
3505 if (un->flags & ~AT_REMOVEDIR)
3506 return -EINVAL;
3507
3508 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3509 un->filename = getname(fname);
3510 if (IS_ERR(un->filename))
3511 return PTR_ERR(un->filename);
3512
3513 req->flags |= REQ_F_NEED_CLEANUP;
3514 return 0;
3515}
3516
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003517static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003518{
3519 struct io_unlink *un = &req->unlink;
3520 int ret;
3521
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003522 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003523 return -EAGAIN;
3524
3525 if (un->flags & AT_REMOVEDIR)
3526 ret = do_rmdir(un->dfd, un->filename);
3527 else
3528 ret = do_unlinkat(un->dfd, un->filename);
3529
3530 req->flags &= ~REQ_F_NEED_CLEANUP;
3531 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003532 req_set_fail(req);
Jens Axboe14a11432020-09-28 14:27:37 -06003533 io_req_complete(req, ret);
3534 return 0;
3535}
3536
Jens Axboe36f4fa62020-09-05 11:14:22 -06003537static int io_shutdown_prep(struct io_kiocb *req,
3538 const struct io_uring_sqe *sqe)
3539{
3540#if defined(CONFIG_NET)
3541 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3542 return -EINVAL;
3543 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3544 sqe->buf_index)
3545 return -EINVAL;
3546
3547 req->shutdown.how = READ_ONCE(sqe->len);
3548 return 0;
3549#else
3550 return -EOPNOTSUPP;
3551#endif
3552}
3553
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003554static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003555{
3556#if defined(CONFIG_NET)
3557 struct socket *sock;
3558 int ret;
3559
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003560 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003561 return -EAGAIN;
3562
Linus Torvalds48aba792020-12-16 12:44:05 -08003563 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003564 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003565 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003566
3567 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003568 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003569 req_set_fail(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003570 io_req_complete(req, ret);
3571 return 0;
3572#else
3573 return -EOPNOTSUPP;
3574#endif
3575}
3576
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003577static int __io_splice_prep(struct io_kiocb *req,
3578 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003579{
3580 struct io_splice* sp = &req->splice;
3581 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003582
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003583 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3584 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003585
3586 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003587 sp->len = READ_ONCE(sqe->len);
3588 sp->flags = READ_ONCE(sqe->splice_flags);
3589
3590 if (unlikely(sp->flags & ~valid_flags))
3591 return -EINVAL;
3592
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003593 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3594 (sp->flags & SPLICE_F_FD_IN_FIXED));
3595 if (!sp->file_in)
3596 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003597 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003598 return 0;
3599}
3600
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003601static int io_tee_prep(struct io_kiocb *req,
3602 const struct io_uring_sqe *sqe)
3603{
3604 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3605 return -EINVAL;
3606 return __io_splice_prep(req, sqe);
3607}
3608
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003609static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003610{
3611 struct io_splice *sp = &req->splice;
3612 struct file *in = sp->file_in;
3613 struct file *out = sp->file_out;
3614 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3615 long ret = 0;
3616
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003617 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003618 return -EAGAIN;
3619 if (sp->len)
3620 ret = do_tee(in, out, sp->len, flags);
3621
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003622 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3623 io_put_file(in);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003624 req->flags &= ~REQ_F_NEED_CLEANUP;
3625
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003626 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003627 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003628 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003629 return 0;
3630}
3631
3632static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3633{
3634 struct io_splice* sp = &req->splice;
3635
3636 sp->off_in = READ_ONCE(sqe->splice_off_in);
3637 sp->off_out = READ_ONCE(sqe->off);
3638 return __io_splice_prep(req, sqe);
3639}
3640
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003641static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003642{
3643 struct io_splice *sp = &req->splice;
3644 struct file *in = sp->file_in;
3645 struct file *out = sp->file_out;
3646 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3647 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003648 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003649
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003650 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003651 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003652
3653 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3654 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003655
Jens Axboe948a7742020-05-17 14:21:38 -06003656 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003657 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003658
Pavel Begunkove1d767f2021-03-19 17:22:43 +00003659 if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
3660 io_put_file(in);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003661 req->flags &= ~REQ_F_NEED_CLEANUP;
3662
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003663 if (ret != sp->len)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003664 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003665 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003666 return 0;
3667}
3668
Jens Axboe2b188cc2019-01-07 10:46:33 -07003669/*
3670 * IORING_OP_NOP just posts a completion event, nothing else.
3671 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003672static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003673{
3674 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003675
Jens Axboedef596e2019-01-09 08:59:42 -07003676 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3677 return -EINVAL;
3678
Pavel Begunkov889fca72021-02-10 00:03:09 +00003679 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003680 return 0;
3681}
3682
Pavel Begunkov1155c762021-02-18 18:29:38 +00003683static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003684{
Jens Axboe6b063142019-01-10 22:13:58 -07003685 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003686
Jens Axboe09bb8392019-03-13 12:39:28 -06003687 if (!req->file)
3688 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003689
Jens Axboe6b063142019-01-10 22:13:58 -07003690 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003691 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003692 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003693 return -EINVAL;
3694
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003695 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3696 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3697 return -EINVAL;
3698
3699 req->sync.off = READ_ONCE(sqe->off);
3700 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003701 return 0;
3702}
3703
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003704static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003705{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003706 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003707 int ret;
3708
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003709 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003710 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003711 return -EAGAIN;
3712
Jens Axboe9adbd452019-12-20 08:45:55 -07003713 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003714 end > 0 ? end : LLONG_MAX,
3715 req->sync.flags & IORING_FSYNC_DATASYNC);
3716 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003717 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003718 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003719 return 0;
3720}
3721
Jens Axboed63d1b52019-12-10 10:38:56 -07003722static int io_fallocate_prep(struct io_kiocb *req,
3723 const struct io_uring_sqe *sqe)
3724{
3725 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3726 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003727 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3728 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003729
3730 req->sync.off = READ_ONCE(sqe->off);
3731 req->sync.len = READ_ONCE(sqe->addr);
3732 req->sync.mode = READ_ONCE(sqe->len);
3733 return 0;
3734}
3735
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003736static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003737{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003738 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003739
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003740 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003741 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003742 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003743 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3744 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003745 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003746 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003747 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003748 return 0;
3749}
3750
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003751static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003752{
Jens Axboef8748882020-01-08 17:47:02 -07003753 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003754 int ret;
3755
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003756 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003757 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003758 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003759 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003760
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003761 /* open.how should be already initialised */
3762 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003763 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003764
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003765 req->open.dfd = READ_ONCE(sqe->fd);
3766 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003767 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003768 if (IS_ERR(req->open.filename)) {
3769 ret = PTR_ERR(req->open.filename);
3770 req->open.filename = NULL;
3771 return ret;
3772 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003773 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003774 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003775 return 0;
3776}
3777
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003778static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3779{
3780 u64 flags, mode;
3781
Jens Axboe14587a462020-09-05 11:36:08 -06003782 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003783 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003784 mode = READ_ONCE(sqe->len);
3785 flags = READ_ONCE(sqe->open_flags);
3786 req->open.how = build_open_how(flags, mode);
3787 return __io_openat_prep(req, sqe);
3788}
3789
Jens Axboecebdb982020-01-08 17:59:24 -07003790static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3791{
3792 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003793 size_t len;
3794 int ret;
3795
Jens Axboe14587a462020-09-05 11:36:08 -06003796 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003797 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003798 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3799 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003800 if (len < OPEN_HOW_SIZE_VER0)
3801 return -EINVAL;
3802
3803 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3804 len);
3805 if (ret)
3806 return ret;
3807
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003808 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003809}
3810
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003811static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003812{
3813 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003814 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003815 bool nonblock_set;
3816 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003817 int ret;
3818
Jens Axboecebdb982020-01-08 17:59:24 -07003819 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003820 if (ret)
3821 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003822 nonblock_set = op.open_flag & O_NONBLOCK;
3823 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003824 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003825 /*
3826 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3827 * it'll always -EAGAIN
3828 */
3829 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3830 return -EAGAIN;
3831 op.lookup_flags |= LOOKUP_CACHED;
3832 op.open_flag |= O_NONBLOCK;
3833 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003834
Jens Axboe4022e7a2020-03-19 19:23:18 -06003835 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003836 if (ret < 0)
3837 goto err;
3838
3839 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07003840 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003841 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
3842 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003843 /*
3844 * We could hang on to this 'fd', but seems like marginal
3845 * gain for something that is now known to be a slower path.
3846 * So just put it, and we'll get a new one when we retry.
3847 */
3848 put_unused_fd(ret);
3849 return -EAGAIN;
3850 }
3851
Jens Axboe15b71ab2019-12-11 11:20:36 -07003852 if (IS_ERR(file)) {
3853 put_unused_fd(ret);
3854 ret = PTR_ERR(file);
3855 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003856 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07003857 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003858 fsnotify_open(file);
3859 fd_install(ret, file);
3860 }
3861err:
3862 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003863 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003864 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003865 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01003866 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003867 return 0;
3868}
3869
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003870static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003871{
Pavel Begunkove45cff52021-02-28 22:35:14 +00003872 return io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07003873}
3874
Jens Axboe067524e2020-03-02 16:32:28 -07003875static int io_remove_buffers_prep(struct io_kiocb *req,
3876 const struct io_uring_sqe *sqe)
3877{
3878 struct io_provide_buf *p = &req->pbuf;
3879 u64 tmp;
3880
3881 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3882 return -EINVAL;
3883
3884 tmp = READ_ONCE(sqe->fd);
3885 if (!tmp || tmp > USHRT_MAX)
3886 return -EINVAL;
3887
3888 memset(p, 0, sizeof(*p));
3889 p->nbufs = tmp;
3890 p->bgid = READ_ONCE(sqe->buf_group);
3891 return 0;
3892}
3893
3894static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
3895 int bgid, unsigned nbufs)
3896{
3897 unsigned i = 0;
3898
3899 /* shouldn't happen */
3900 if (!nbufs)
3901 return 0;
3902
3903 /* the head kbuf is the list itself */
3904 while (!list_empty(&buf->list)) {
3905 struct io_buffer *nxt;
3906
3907 nxt = list_first_entry(&buf->list, struct io_buffer, list);
3908 list_del(&nxt->list);
3909 kfree(nxt);
3910 if (++i == nbufs)
3911 return i;
3912 }
3913 i++;
3914 kfree(buf);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003915 xa_erase(&ctx->io_buffers, bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07003916
3917 return i;
3918}
3919
Pavel Begunkov889fca72021-02-10 00:03:09 +00003920static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07003921{
3922 struct io_provide_buf *p = &req->pbuf;
3923 struct io_ring_ctx *ctx = req->ctx;
3924 struct io_buffer *head;
3925 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003926 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07003927
3928 io_ring_submit_lock(ctx, !force_nonblock);
3929
3930 lockdep_assert_held(&ctx->uring_lock);
3931
3932 ret = -ENOENT;
Jens Axboe9e15c3a2021-03-13 12:29:43 -07003933 head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboe067524e2020-03-02 16:32:28 -07003934 if (head)
3935 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07003936 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01003937 req_set_fail(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00003938
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00003939 /* complete before unlock, IOPOLL may need the lock */
3940 __io_req_complete(req, issue_flags, ret, 0);
3941 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboe067524e2020-03-02 16:32:28 -07003942 return 0;
3943}
3944
Jens Axboeddf0322d2020-02-23 16:41:33 -07003945static int io_provide_buffers_prep(struct io_kiocb *req,
3946 const struct io_uring_sqe *sqe)
3947{
Pavel Begunkov38134ad2021-04-15 13:07:39 +01003948 unsigned long size, tmp_check;
Jens Axboeddf0322d2020-02-23 16:41:33 -07003949 struct io_provide_buf *p = &req->pbuf;
3950 u64 tmp;
3951
3952 if (sqe->ioprio || sqe->rw_flags)
3953 return -EINVAL;
3954
3955 tmp = READ_ONCE(sqe->fd);
3956 if (!tmp || tmp > USHRT_MAX)
3957 return -E2BIG;
3958 p->nbufs = tmp;
3959 p->addr = READ_ONCE(sqe->addr);
3960 p->len = READ_ONCE(sqe->len);
3961
Pavel Begunkov38134ad2021-04-15 13:07:39 +01003962 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
3963 &size))
3964 return -EOVERFLOW;
3965 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
3966 return -EOVERFLOW;
3967
Pavel Begunkovd81269f2021-03-19 10:21:19 +00003968 size = (unsigned long)p->len * p->nbufs;
3969 if (!access_ok(u64_to_user_ptr(p->addr), size))
Jens Axboeddf0322d2020-02-23 16:41:33 -07003970 return -EFAULT;
3971
3972 p->bgid = READ_ONCE(sqe->buf_group);
3973 tmp = READ_ONCE(sqe->off);
3974 if (tmp > USHRT_MAX)
3975 return -E2BIG;
3976 p->bid = tmp;
3977 return 0;
3978}
3979
3980static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
3981{
3982 struct io_buffer *buf;
3983 u64 addr = pbuf->addr;
3984 int i, bid = pbuf->bid;
3985
3986 for (i = 0; i < pbuf->nbufs; i++) {
3987 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
3988 if (!buf)
3989 break;
3990
3991 buf->addr = addr;
Thadeu Lima de Souza Cascardod1f82802021-05-05 09:47:06 -03003992 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
Jens Axboeddf0322d2020-02-23 16:41:33 -07003993 buf->bid = bid;
3994 addr += pbuf->len;
3995 bid++;
3996 if (!*head) {
3997 INIT_LIST_HEAD(&buf->list);
3998 *head = buf;
3999 } else {
4000 list_add_tail(&buf->list, &(*head)->list);
4001 }
4002 }
4003
4004 return i ? i : -ENOMEM;
4005}
4006
Pavel Begunkov889fca72021-02-10 00:03:09 +00004007static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004008{
4009 struct io_provide_buf *p = &req->pbuf;
4010 struct io_ring_ctx *ctx = req->ctx;
4011 struct io_buffer *head, *list;
4012 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004013 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004014
4015 io_ring_submit_lock(ctx, !force_nonblock);
4016
4017 lockdep_assert_held(&ctx->uring_lock);
4018
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004019 list = head = xa_load(&ctx->io_buffers, p->bgid);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004020
4021 ret = io_add_buffers(p, &head);
Jens Axboe9e15c3a2021-03-13 12:29:43 -07004022 if (ret >= 0 && !list) {
4023 ret = xa_insert(&ctx->io_buffers, p->bgid, head, GFP_KERNEL);
4024 if (ret < 0)
Jens Axboe067524e2020-03-02 16:32:28 -07004025 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004026 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004027 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004028 req_set_fail(req);
Pavel Begunkov9fb8cb42021-02-28 22:35:13 +00004029 /* complete before unlock, IOPOLL may need the lock */
4030 __io_req_complete(req, issue_flags, ret, 0);
4031 io_ring_submit_unlock(ctx, !force_nonblock);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004032 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004033}
4034
Jens Axboe3e4827b2020-01-08 15:18:09 -07004035static int io_epoll_ctl_prep(struct io_kiocb *req,
4036 const struct io_uring_sqe *sqe)
4037{
4038#if defined(CONFIG_EPOLL)
4039 if (sqe->ioprio || sqe->buf_index)
4040 return -EINVAL;
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004041 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004042 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004043
4044 req->epoll.epfd = READ_ONCE(sqe->fd);
4045 req->epoll.op = READ_ONCE(sqe->len);
4046 req->epoll.fd = READ_ONCE(sqe->off);
4047
4048 if (ep_op_has_event(req->epoll.op)) {
4049 struct epoll_event __user *ev;
4050
4051 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4052 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4053 return -EFAULT;
4054 }
4055
4056 return 0;
4057#else
4058 return -EOPNOTSUPP;
4059#endif
4060}
4061
Pavel Begunkov889fca72021-02-10 00:03:09 +00004062static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004063{
4064#if defined(CONFIG_EPOLL)
4065 struct io_epoll *ie = &req->epoll;
4066 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004067 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004068
4069 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4070 if (force_nonblock && ret == -EAGAIN)
4071 return -EAGAIN;
4072
4073 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004074 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004075 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004076 return 0;
4077#else
4078 return -EOPNOTSUPP;
4079#endif
4080}
4081
Jens Axboec1ca7572019-12-25 22:18:28 -07004082static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4083{
4084#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4085 if (sqe->ioprio || sqe->buf_index || sqe->off)
4086 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004087 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4088 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004089
4090 req->madvise.addr = READ_ONCE(sqe->addr);
4091 req->madvise.len = READ_ONCE(sqe->len);
4092 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4093 return 0;
4094#else
4095 return -EOPNOTSUPP;
4096#endif
4097}
4098
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004099static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004100{
4101#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4102 struct io_madvise *ma = &req->madvise;
4103 int ret;
4104
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004105 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004106 return -EAGAIN;
4107
Minchan Kim0726b012020-10-17 16:14:50 -07004108 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004109 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004110 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004111 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004112 return 0;
4113#else
4114 return -EOPNOTSUPP;
4115#endif
4116}
4117
Jens Axboe4840e412019-12-25 22:03:45 -07004118static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4119{
4120 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4121 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004122 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4123 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004124
4125 req->fadvise.offset = READ_ONCE(sqe->off);
4126 req->fadvise.len = READ_ONCE(sqe->len);
4127 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4128 return 0;
4129}
4130
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004131static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004132{
4133 struct io_fadvise *fa = &req->fadvise;
4134 int ret;
4135
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004136 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004137 switch (fa->advice) {
4138 case POSIX_FADV_NORMAL:
4139 case POSIX_FADV_RANDOM:
4140 case POSIX_FADV_SEQUENTIAL:
4141 break;
4142 default:
4143 return -EAGAIN;
4144 }
4145 }
Jens Axboe4840e412019-12-25 22:03:45 -07004146
4147 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4148 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004149 req_set_fail(req);
Pavel Begunkov0bdf3392021-04-11 01:46:29 +01004150 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe4840e412019-12-25 22:03:45 -07004151 return 0;
4152}
4153
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004154static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4155{
Pavel Begunkov2d74d042021-05-14 12:05:46 +01004156 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004157 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004158 if (sqe->ioprio || sqe->buf_index)
4159 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004160 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004161 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004162
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004163 req->statx.dfd = READ_ONCE(sqe->fd);
4164 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004165 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004166 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4167 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004168
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004169 return 0;
4170}
4171
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004172static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004173{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004174 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004175 int ret;
4176
Pavel Begunkov59d70012021-03-22 01:58:30 +00004177 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004178 return -EAGAIN;
4179
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004180 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4181 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004182
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004183 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004184 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004185 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004186 return 0;
4187}
4188
Jens Axboeb5dba592019-12-11 14:02:38 -07004189static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4190{
Jens Axboe14587a462020-09-05 11:36:08 -06004191 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004192 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004193 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4194 sqe->rw_flags || sqe->buf_index)
4195 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004196 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004197 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004198
4199 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004200 return 0;
4201}
4202
Pavel Begunkov889fca72021-02-10 00:03:09 +00004203static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004204{
Jens Axboe9eac1902021-01-19 15:50:37 -07004205 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004206 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004207 struct fdtable *fdt;
Pavel Begunkova1fde922021-04-11 01:46:28 +01004208 struct file *file = NULL;
4209 int ret = -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004210
Jens Axboe9eac1902021-01-19 15:50:37 -07004211 spin_lock(&files->file_lock);
4212 fdt = files_fdtable(files);
4213 if (close->fd >= fdt->max_fds) {
4214 spin_unlock(&files->file_lock);
4215 goto err;
4216 }
4217 file = fdt->fd[close->fd];
Pavel Begunkova1fde922021-04-11 01:46:28 +01004218 if (!file || file->f_op == &io_uring_fops) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004219 spin_unlock(&files->file_lock);
4220 file = NULL;
4221 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004222 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004223
4224 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004225 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004226 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004227 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004228 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004229
Jens Axboe9eac1902021-01-19 15:50:37 -07004230 ret = __close_fd_get_file(close->fd, &file);
4231 spin_unlock(&files->file_lock);
4232 if (ret < 0) {
4233 if (ret == -ENOENT)
4234 ret = -EBADF;
4235 goto err;
4236 }
4237
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004238 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004239 ret = filp_close(file, current->files);
4240err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004241 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004242 req_set_fail(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004243 if (file)
4244 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004245 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004246 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004247}
4248
Pavel Begunkov1155c762021-02-18 18:29:38 +00004249static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004250{
4251 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004252
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004253 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4254 return -EINVAL;
4255 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4256 return -EINVAL;
4257
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004258 req->sync.off = READ_ONCE(sqe->off);
4259 req->sync.len = READ_ONCE(sqe->len);
4260 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004261 return 0;
4262}
4263
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004264static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004265{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004266 int ret;
4267
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004268 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004269 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004270 return -EAGAIN;
4271
Jens Axboe9adbd452019-12-20 08:45:55 -07004272 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004273 req->sync.flags);
4274 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004275 req_set_fail(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004276 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004277 return 0;
4278}
4279
YueHaibing469956e2020-03-04 15:53:52 +08004280#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004281static int io_setup_async_msg(struct io_kiocb *req,
4282 struct io_async_msghdr *kmsg)
4283{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004284 struct io_async_msghdr *async_msg = req->async_data;
4285
4286 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004287 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004288 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004289 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004290 return -ENOMEM;
4291 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004292 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004293 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004294 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004295 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004296 /* if were using fast_iov, set it to the new one */
4297 if (!async_msg->free_iov)
4298 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4299
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004300 return -EAGAIN;
4301}
4302
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004303static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4304 struct io_async_msghdr *iomsg)
4305{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004306 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004307 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004308 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004309 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004310}
4311
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004312static int io_sendmsg_prep_async(struct io_kiocb *req)
4313{
4314 int ret;
4315
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004316 ret = io_sendmsg_copy_hdr(req, req->async_data);
4317 if (!ret)
4318 req->flags |= REQ_F_NEED_CLEANUP;
4319 return ret;
4320}
4321
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004323{
Jens Axboee47293f2019-12-20 08:58:21 -07004324 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004325
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004326 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4327 return -EINVAL;
4328
Pavel Begunkov270a5942020-07-12 20:41:04 +03004329 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004330 sr->len = READ_ONCE(sqe->len);
Pavel Begunkov04411802021-04-01 15:44:00 +01004331 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4332 if (sr->msg_flags & MSG_DONTWAIT)
4333 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004334
Jens Axboed8768362020-02-27 14:17:49 -07004335#ifdef CONFIG_COMPAT
4336 if (req->ctx->compat)
4337 sr->msg_flags |= MSG_CMSG_COMPAT;
4338#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004339 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004340}
4341
Pavel Begunkov889fca72021-02-10 00:03:09 +00004342static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004343{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004344 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004345 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004346 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004347 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004348 int ret;
4349
Florent Revestdba4a922020-12-04 12:36:04 +01004350 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004351 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004352 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004353
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004354 kmsg = req->async_data;
4355 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004356 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004357 if (ret)
4358 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004359 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004360 }
4361
Pavel Begunkov04411802021-04-01 15:44:00 +01004362 flags = req->sr_msg.msg_flags;
4363 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004364 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004365 if (flags & MSG_WAITALL)
4366 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4367
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004368 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004369 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004370 return io_setup_async_msg(req, kmsg);
4371 if (ret == -ERESTARTSYS)
4372 ret = -EINTR;
4373
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004374 /* fast path, check for non-NULL to avoid function call */
4375 if (kmsg->free_iov)
4376 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004377 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004378 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004379 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004380 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004381 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004382}
4383
Pavel Begunkov889fca72021-02-10 00:03:09 +00004384static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004385{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004386 struct io_sr_msg *sr = &req->sr_msg;
4387 struct msghdr msg;
4388 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004389 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004390 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004391 int min_ret = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004392 int ret;
4393
Florent Revestdba4a922020-12-04 12:36:04 +01004394 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004395 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004396 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004397
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004398 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4399 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004400 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004401
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004402 msg.msg_name = NULL;
4403 msg.msg_control = NULL;
4404 msg.msg_controllen = 0;
4405 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004406
Pavel Begunkov04411802021-04-01 15:44:00 +01004407 flags = req->sr_msg.msg_flags;
4408 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004409 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004410 if (flags & MSG_WAITALL)
4411 min_ret = iov_iter_count(&msg.msg_iter);
4412
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004413 msg.msg_flags = flags;
4414 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004415 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004416 return -EAGAIN;
4417 if (ret == -ERESTARTSYS)
4418 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004419
Stefan Metzmacher00312752021-03-20 20:33:36 +01004420 if (ret < min_ret)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004421 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004422 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004423 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004424}
4425
Pavel Begunkov1400e692020-07-12 20:41:05 +03004426static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4427 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004428{
4429 struct io_sr_msg *sr = &req->sr_msg;
4430 struct iovec __user *uiov;
4431 size_t iov_len;
4432 int ret;
4433
Pavel Begunkov1400e692020-07-12 20:41:05 +03004434 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4435 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004436 if (ret)
4437 return ret;
4438
4439 if (req->flags & REQ_F_BUFFER_SELECT) {
4440 if (iov_len > 1)
4441 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004442 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004443 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004444 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004445 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004446 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004447 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004448 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004449 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004450 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004451 if (ret > 0)
4452 ret = 0;
4453 }
4454
4455 return ret;
4456}
4457
4458#ifdef CONFIG_COMPAT
4459static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004460 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004461{
Jens Axboe52de1fe2020-02-27 10:15:42 -07004462 struct io_sr_msg *sr = &req->sr_msg;
4463 struct compat_iovec __user *uiov;
4464 compat_uptr_t ptr;
4465 compat_size_t len;
4466 int ret;
4467
Pavel Begunkov4af34172021-04-11 01:46:30 +01004468 ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr,
4469 &ptr, &len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004470 if (ret)
4471 return ret;
4472
4473 uiov = compat_ptr(ptr);
4474 if (req->flags & REQ_F_BUFFER_SELECT) {
4475 compat_ssize_t clen;
4476
4477 if (len > 1)
4478 return -EINVAL;
4479 if (!access_ok(uiov, sizeof(*uiov)))
4480 return -EFAULT;
4481 if (__get_user(clen, &uiov->iov_len))
4482 return -EFAULT;
4483 if (clen < 0)
4484 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004485 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004486 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004487 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004488 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004489 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004490 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004491 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004492 if (ret < 0)
4493 return ret;
4494 }
4495
4496 return 0;
4497}
Jens Axboe03b12302019-12-02 18:50:25 -07004498#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004499
Pavel Begunkov1400e692020-07-12 20:41:05 +03004500static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4501 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004502{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004503 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004504
4505#ifdef CONFIG_COMPAT
4506 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004507 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004508#endif
4509
Pavel Begunkov1400e692020-07-12 20:41:05 +03004510 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004511}
4512
Jens Axboebcda7ba2020-02-23 16:42:51 -07004513static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004514 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004515{
4516 struct io_sr_msg *sr = &req->sr_msg;
4517 struct io_buffer *kbuf;
4518
Jens Axboebcda7ba2020-02-23 16:42:51 -07004519 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4520 if (IS_ERR(kbuf))
4521 return kbuf;
4522
4523 sr->kbuf = kbuf;
4524 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004525 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004526}
4527
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004528static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4529{
4530 return io_put_kbuf(req, req->sr_msg.kbuf);
4531}
4532
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004533static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004534{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004535 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004536
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004537 ret = io_recvmsg_copy_hdr(req, req->async_data);
4538 if (!ret)
4539 req->flags |= REQ_F_NEED_CLEANUP;
4540 return ret;
4541}
4542
4543static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4544{
4545 struct io_sr_msg *sr = &req->sr_msg;
4546
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004547 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4548 return -EINVAL;
4549
Pavel Begunkov270a5942020-07-12 20:41:04 +03004550 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004551 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004552 sr->bgid = READ_ONCE(sqe->buf_group);
Pavel Begunkov04411802021-04-01 15:44:00 +01004553 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
4554 if (sr->msg_flags & MSG_DONTWAIT)
4555 req->flags |= REQ_F_NOWAIT;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004556
Jens Axboed8768362020-02-27 14:17:49 -07004557#ifdef CONFIG_COMPAT
4558 if (req->ctx->compat)
4559 sr->msg_flags |= MSG_CMSG_COMPAT;
4560#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004561 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004562}
4563
Pavel Begunkov889fca72021-02-10 00:03:09 +00004564static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004565{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004566 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004567 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004568 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004569 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004570 int min_ret = 0;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004571 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004572 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004573
Florent Revestdba4a922020-12-04 12:36:04 +01004574 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004575 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004576 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004577
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004578 kmsg = req->async_data;
4579 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004580 ret = io_recvmsg_copy_hdr(req, &iomsg);
4581 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004582 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004583 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004584 }
4585
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004586 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004587 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004588 if (IS_ERR(kbuf))
4589 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004590 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004591 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4592 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004593 1, req->sr_msg.len);
4594 }
4595
Pavel Begunkov04411802021-04-01 15:44:00 +01004596 flags = req->sr_msg.msg_flags;
4597 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004598 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004599 if (flags & MSG_WAITALL)
4600 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4601
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004602 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4603 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004604 if (force_nonblock && ret == -EAGAIN)
4605 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004606 if (ret == -ERESTARTSYS)
4607 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004608
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004609 if (req->flags & REQ_F_BUFFER_SELECTED)
4610 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004611 /* fast path, check for non-NULL to avoid function call */
4612 if (kmsg->free_iov)
4613 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004614 req->flags &= ~REQ_F_NEED_CLEANUP;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004615 if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004616 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004617 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004618 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004619}
4620
Pavel Begunkov889fca72021-02-10 00:03:09 +00004621static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004622{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004623 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004624 struct io_sr_msg *sr = &req->sr_msg;
4625 struct msghdr msg;
4626 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004627 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004628 struct iovec iov;
4629 unsigned flags;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004630 int min_ret = 0;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004631 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004632 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004633
Florent Revestdba4a922020-12-04 12:36:04 +01004634 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004635 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004636 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004637
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004638 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004639 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004640 if (IS_ERR(kbuf))
4641 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004642 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004643 }
4644
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004645 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004646 if (unlikely(ret))
4647 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004648
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004649 msg.msg_name = NULL;
4650 msg.msg_control = NULL;
4651 msg.msg_controllen = 0;
4652 msg.msg_namelen = 0;
4653 msg.msg_iocb = NULL;
4654 msg.msg_flags = 0;
4655
Pavel Begunkov04411802021-04-01 15:44:00 +01004656 flags = req->sr_msg.msg_flags;
4657 if (force_nonblock)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004658 flags |= MSG_DONTWAIT;
Stefan Metzmacher00312752021-03-20 20:33:36 +01004659 if (flags & MSG_WAITALL)
4660 min_ret = iov_iter_count(&msg.msg_iter);
4661
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004662 ret = sock_recvmsg(sock, &msg, flags);
4663 if (force_nonblock && ret == -EAGAIN)
4664 return -EAGAIN;
4665 if (ret == -ERESTARTSYS)
4666 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004667out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004668 if (req->flags & REQ_F_BUFFER_SELECTED)
4669 cflags = io_put_recv_kbuf(req);
Stefan Metzmacher00312752021-03-20 20:33:36 +01004670 if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004671 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004672 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004673 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004674}
4675
Jens Axboe3529d8c2019-12-19 18:24:38 -07004676static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004677{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004678 struct io_accept *accept = &req->accept;
4679
Jens Axboe14587a462020-09-05 11:36:08 -06004680 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004681 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004682 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004683 return -EINVAL;
4684
Jens Axboed55e5f52019-12-11 16:12:15 -07004685 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4686 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004687 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004688 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004689 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004690}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004691
Pavel Begunkov889fca72021-02-10 00:03:09 +00004692static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004693{
4694 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004695 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004696 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004697 int ret;
4698
Jiufei Xuee697dee2020-06-10 13:41:59 +08004699 if (req->file->f_flags & O_NONBLOCK)
4700 req->flags |= REQ_F_NOWAIT;
4701
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004702 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004703 accept->addr_len, accept->flags,
4704 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004705 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004706 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004707 if (ret < 0) {
4708 if (ret == -ERESTARTSYS)
4709 ret = -EINTR;
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004710 req_set_fail(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004711 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004712 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004713 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004714}
4715
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004716static int io_connect_prep_async(struct io_kiocb *req)
4717{
4718 struct io_async_connect *io = req->async_data;
4719 struct io_connect *conn = &req->connect;
4720
4721 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4722}
4723
Jens Axboe3529d8c2019-12-19 18:24:38 -07004724static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004725{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004726 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004727
Jens Axboe14587a462020-09-05 11:36:08 -06004728 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004729 return -EINVAL;
4730 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4731 return -EINVAL;
4732
Jens Axboe3529d8c2019-12-19 18:24:38 -07004733 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4734 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004735 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004736}
4737
Pavel Begunkov889fca72021-02-10 00:03:09 +00004738static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004739{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004740 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004741 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004742 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004743 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004744
Jens Axboee8c2bc12020-08-15 18:44:09 -07004745 if (req->async_data) {
4746 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004747 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004748 ret = move_addr_to_kernel(req->connect.addr,
4749 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004750 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004751 if (ret)
4752 goto out;
4753 io = &__io;
4754 }
4755
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004756 file_flags = force_nonblock ? O_NONBLOCK : 0;
4757
Jens Axboee8c2bc12020-08-15 18:44:09 -07004758 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004759 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004760 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004761 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004762 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004763 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004764 ret = -ENOMEM;
4765 goto out;
4766 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004767 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004768 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004769 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004770 if (ret == -ERESTARTSYS)
4771 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004772out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004773 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01004774 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004775 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004776 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004777}
YueHaibing469956e2020-03-04 15:53:52 +08004778#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004779#define IO_NETOP_FN(op) \
4780static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4781{ \
4782 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004783}
4784
Jens Axboe99a10082021-02-19 09:35:19 -07004785#define IO_NETOP_PREP(op) \
4786IO_NETOP_FN(op) \
4787static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4788{ \
4789 return -EOPNOTSUPP; \
4790} \
4791
4792#define IO_NETOP_PREP_ASYNC(op) \
4793IO_NETOP_PREP(op) \
4794static int io_##op##_prep_async(struct io_kiocb *req) \
4795{ \
4796 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004797}
4798
Jens Axboe99a10082021-02-19 09:35:19 -07004799IO_NETOP_PREP_ASYNC(sendmsg);
4800IO_NETOP_PREP_ASYNC(recvmsg);
4801IO_NETOP_PREP_ASYNC(connect);
4802IO_NETOP_PREP(accept);
4803IO_NETOP_FN(send);
4804IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004805#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004806
Jens Axboed7718a92020-02-14 22:23:12 -07004807struct io_poll_table {
4808 struct poll_table_struct pt;
4809 struct io_kiocb *req;
4810 int error;
4811};
4812
Jens Axboed7718a92020-02-14 22:23:12 -07004813static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4814 __poll_t mask, task_work_func_t func)
4815{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004816 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004817
4818 /* for instances that support it check for an event match first: */
4819 if (mask && !(mask & poll->events))
4820 return 0;
4821
4822 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4823
4824 list_del_init(&poll->wait.entry);
4825
Jens Axboed7718a92020-02-14 22:23:12 -07004826 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00004827 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004828
Jens Axboed7718a92020-02-14 22:23:12 -07004829 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004830 * If this fails, then the task is exiting. When a task exits, the
4831 * work gets canceled, so just cancel this request as well instead
4832 * of executing it. We can't safely execute it anyway, as we may not
4833 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004834 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06004835 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004836 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004837 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00004838 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004839 }
Jens Axboed7718a92020-02-14 22:23:12 -07004840 return 1;
4841}
4842
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004843static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4844 __acquires(&req->ctx->completion_lock)
4845{
4846 struct io_ring_ctx *ctx = req->ctx;
4847
4848 if (!req->result && !READ_ONCE(poll->canceled)) {
4849 struct poll_table_struct pt = { ._key = poll->events };
4850
4851 req->result = vfs_poll(req->file, &pt) & poll->events;
4852 }
4853
4854 spin_lock_irq(&ctx->completion_lock);
4855 if (!req->result && !READ_ONCE(poll->canceled)) {
4856 add_wait_queue(poll->head, &poll->wait);
4857 return true;
4858 }
4859
4860 return false;
4861}
4862
Jens Axboed4e7cd32020-08-15 11:44:50 -07004863static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06004864{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004865 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07004866 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07004867 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004868 return req->apoll->double_poll;
4869}
4870
4871static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
4872{
4873 if (req->opcode == IORING_OP_POLL_ADD)
4874 return &req->poll;
4875 return &req->apoll->poll;
4876}
4877
4878static void io_poll_remove_double(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01004879 __must_hold(&req->ctx->completion_lock)
Jens Axboed4e7cd32020-08-15 11:44:50 -07004880{
4881 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004882
4883 lockdep_assert_held(&req->ctx->completion_lock);
4884
4885 if (poll && poll->head) {
4886 struct wait_queue_head *head = poll->head;
4887
4888 spin_lock(&head->lock);
4889 list_del_init(&poll->wait.entry);
4890 if (poll->wait.private)
Jens Axboede9b4cc2021-02-24 13:28:27 -07004891 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004892 poll->head = NULL;
4893 spin_unlock(&head->lock);
4894 }
4895}
4896
Pavel Begunkove27414b2021-04-09 09:13:20 +01004897static bool io_poll_complete(struct io_kiocb *req, __poll_t mask)
Pavel Begunkove07785b2021-04-01 15:43:57 +01004898 __must_hold(&req->ctx->completion_lock)
Jens Axboe18bceab2020-05-15 11:56:54 -06004899{
4900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe88e41cf2021-02-22 22:08:01 -07004901 unsigned flags = IORING_CQE_F_MORE;
Pavel Begunkove27414b2021-04-09 09:13:20 +01004902 int error;
Jens Axboe18bceab2020-05-15 11:56:54 -06004903
Pavel Begunkove27414b2021-04-09 09:13:20 +01004904 if (READ_ONCE(req->poll.canceled)) {
Jens Axboe45ab03b2021-02-23 08:19:33 -07004905 error = -ECANCELED;
Jens Axboe88e41cf2021-02-22 22:08:01 -07004906 req->poll.events |= EPOLLONESHOT;
Pavel Begunkove27414b2021-04-09 09:13:20 +01004907 } else {
Jens Axboe50826202021-02-23 09:02:26 -07004908 error = mangle_poll(mask);
Pavel Begunkove27414b2021-04-09 09:13:20 +01004909 }
Jens Axboeb69de282021-03-17 08:37:41 -06004910 if (req->poll.events & EPOLLONESHOT)
4911 flags = 0;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01004912 if (!io_cqring_fill_event(ctx, req->user_data, error, flags)) {
Jens Axboe50826202021-02-23 09:02:26 -07004913 io_poll_remove_waitqs(req);
Jens Axboe88e41cf2021-02-22 22:08:01 -07004914 req->poll.done = true;
4915 flags = 0;
4916 }
Hao Xu7b289c32021-04-13 15:20:39 +08004917 if (flags & IORING_CQE_F_MORE)
4918 ctx->cq_extra++;
4919
Jens Axboe18bceab2020-05-15 11:56:54 -06004920 io_commit_cqring(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07004921 return !(flags & IORING_CQE_F_MORE);
Jens Axboe18bceab2020-05-15 11:56:54 -06004922}
4923
Jens Axboe18bceab2020-05-15 11:56:54 -06004924static void io_poll_task_func(struct callback_head *cb)
4925{
4926 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06004927 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004928 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06004929
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004930 if (io_poll_rewait(req, &req->poll)) {
4931 spin_unlock_irq(&ctx->completion_lock);
4932 } else {
Pavel Begunkovf40b9642021-04-09 09:13:19 +01004933 bool done;
Jens Axboe88e41cf2021-02-22 22:08:01 -07004934
Pavel Begunkove27414b2021-04-09 09:13:20 +01004935 done = io_poll_complete(req, req->result);
Jens Axboe88e41cf2021-02-22 22:08:01 -07004936 if (done) {
4937 hash_del(&req->hash_node);
Pavel Begunkovf40b9642021-04-09 09:13:19 +01004938 } else {
Jens Axboe88e41cf2021-02-22 22:08:01 -07004939 req->result = 0;
4940 add_wait_queue(req->poll.head, &req->poll.wait);
4941 }
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004942 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004943 io_cqring_ev_posted(ctx);
Pavel Begunkovdd221f462020-10-18 10:17:42 +01004944
Jens Axboe88e41cf2021-02-22 22:08:01 -07004945 if (done) {
4946 nxt = io_put_req_find_next(req);
4947 if (nxt)
4948 __io_req_task_submit(nxt);
4949 }
Pavel Begunkovea1164e2020-06-30 15:20:41 +03004950 }
Jens Axboe18bceab2020-05-15 11:56:54 -06004951}
4952
4953static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
4954 int sync, void *key)
4955{
4956 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07004957 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004958 __poll_t mask = key_to_poll(key);
4959
4960 /* for instances that support it check for an event match first: */
4961 if (mask && !(mask & poll->events))
4962 return 0;
Jens Axboe88e41cf2021-02-22 22:08:01 -07004963 if (!(poll->events & EPOLLONESHOT))
4964 return poll->wait.func(&poll->wait, mode, sync, key);
Jens Axboe18bceab2020-05-15 11:56:54 -06004965
Jens Axboe8706e042020-09-28 08:38:54 -06004966 list_del_init(&wait->entry);
4967
Jens Axboe807abcb2020-07-17 17:09:27 -06004968 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06004969 bool done;
4970
Jens Axboe807abcb2020-07-17 17:09:27 -06004971 spin_lock(&poll->head->lock);
4972 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06004973 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06004974 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07004975 /* make sure double remove sees this as being gone */
4976 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06004977 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06004978 if (!done) {
4979 /* use wait func handler, so it matches the rq type */
4980 poll->wait.func(&poll->wait, mode, sync, key);
4981 }
Jens Axboe18bceab2020-05-15 11:56:54 -06004982 }
Jens Axboede9b4cc2021-02-24 13:28:27 -07004983 req_ref_put(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06004984 return 1;
4985}
4986
4987static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
4988 wait_queue_func_t wake_func)
4989{
4990 poll->head = NULL;
4991 poll->done = false;
4992 poll->canceled = false;
Jens Axboe464dca62021-03-19 14:06:24 -06004993#define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP)
4994 /* mask in events that we always want/need */
4995 poll->events = events | IO_POLL_UNMASK;
Jens Axboe18bceab2020-05-15 11:56:54 -06004996 INIT_LIST_HEAD(&poll->wait.entry);
4997 init_waitqueue_func_entry(&poll->wait, wake_func);
4998}
4999
5000static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005001 struct wait_queue_head *head,
5002 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005003{
5004 struct io_kiocb *req = pt->req;
5005
5006 /*
5007 * If poll->head is already set, it's because the file being polled
5008 * uses multiple waitqueues for poll handling (eg one for read, one
5009 * for write). Setup a separate io_poll_iocb if this happens.
5010 */
5011 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005012 struct io_poll_iocb *poll_one = poll;
5013
Jens Axboe18bceab2020-05-15 11:56:54 -06005014 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005015 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005016 pt->error = -EINVAL;
5017 return;
5018 }
Jens Axboeea6a693d2021-04-15 09:47:13 -06005019 /*
5020 * Can't handle multishot for double wait for now, turn it
5021 * into one-shot mode.
5022 */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005023 if (!(poll_one->events & EPOLLONESHOT))
5024 poll_one->events |= EPOLLONESHOT;
Jens Axboe1c3b3e62021-02-28 16:07:30 -07005025 /* double add on the same waitqueue head, ignore */
Pavel Begunkov7a274722021-05-17 12:43:34 +01005026 if (poll_one->head == head)
Jens Axboe1c3b3e62021-02-28 16:07:30 -07005027 return;
Jens Axboe18bceab2020-05-15 11:56:54 -06005028 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5029 if (!poll) {
5030 pt->error = -ENOMEM;
5031 return;
5032 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005033 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboede9b4cc2021-02-24 13:28:27 -07005034 req_ref_get(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005035 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005036 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005037 }
5038
5039 pt->error = 0;
5040 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005041
5042 if (poll->events & EPOLLEXCLUSIVE)
5043 add_wait_queue_exclusive(head, &poll->wait);
5044 else
5045 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005046}
5047
5048static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5049 struct poll_table_struct *p)
5050{
5051 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005052 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005053
Jens Axboe807abcb2020-07-17 17:09:27 -06005054 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005055}
5056
Jens Axboed7718a92020-02-14 22:23:12 -07005057static void io_async_task_func(struct callback_head *cb)
5058{
5059 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5060 struct async_poll *apoll = req->apoll;
5061 struct io_ring_ctx *ctx = req->ctx;
5062
5063 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5064
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005065 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005066 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005067 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005068 }
5069
Pavel Begunkov0ea13b42021-04-09 09:13:21 +01005070 hash_del(&req->hash_node);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005071 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005072 spin_unlock_irq(&ctx->completion_lock);
5073
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005074 if (!READ_ONCE(apoll->poll.canceled))
5075 __io_req_task_submit(req);
5076 else
Pavel Begunkov25935532021-03-19 17:22:40 +00005077 io_req_complete_failed(req, -ECANCELED);
Jens Axboed7718a92020-02-14 22:23:12 -07005078}
5079
5080static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5081 void *key)
5082{
5083 struct io_kiocb *req = wait->private;
5084 struct io_poll_iocb *poll = &req->apoll->poll;
5085
5086 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5087 key_to_poll(key));
5088
5089 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5090}
5091
5092static void io_poll_req_insert(struct io_kiocb *req)
5093{
5094 struct io_ring_ctx *ctx = req->ctx;
5095 struct hlist_head *list;
5096
5097 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5098 hlist_add_head(&req->hash_node, list);
5099}
5100
5101static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5102 struct io_poll_iocb *poll,
5103 struct io_poll_table *ipt, __poll_t mask,
5104 wait_queue_func_t wake_func)
5105 __acquires(&ctx->completion_lock)
5106{
5107 struct io_ring_ctx *ctx = req->ctx;
5108 bool cancel = false;
5109
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005110 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005111 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005112 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005113 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005114
5115 ipt->pt._key = mask;
5116 ipt->req = req;
5117 ipt->error = -EINVAL;
5118
Jens Axboed7718a92020-02-14 22:23:12 -07005119 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5120
5121 spin_lock_irq(&ctx->completion_lock);
5122 if (likely(poll->head)) {
5123 spin_lock(&poll->head->lock);
5124 if (unlikely(list_empty(&poll->wait.entry))) {
5125 if (ipt->error)
5126 cancel = true;
5127 ipt->error = 0;
5128 mask = 0;
5129 }
Jens Axboe88e41cf2021-02-22 22:08:01 -07005130 if ((mask && (poll->events & EPOLLONESHOT)) || ipt->error)
Jens Axboed7718a92020-02-14 22:23:12 -07005131 list_del_init(&poll->wait.entry);
5132 else if (cancel)
5133 WRITE_ONCE(poll->canceled, true);
5134 else if (!poll->done) /* actually waiting for an event */
5135 io_poll_req_insert(req);
5136 spin_unlock(&poll->head->lock);
5137 }
5138
5139 return mask;
5140}
5141
5142static bool io_arm_poll_handler(struct io_kiocb *req)
5143{
5144 const struct io_op_def *def = &io_op_defs[req->opcode];
5145 struct io_ring_ctx *ctx = req->ctx;
5146 struct async_poll *apoll;
5147 struct io_poll_table ipt;
5148 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005149 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005150
5151 if (!req->file || !file_can_poll(req->file))
5152 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005153 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005154 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005155 if (def->pollin)
5156 rw = READ;
5157 else if (def->pollout)
5158 rw = WRITE;
5159 else
5160 return false;
5161 /* if we can't nonblock try, then no point in arming a poll handler */
Jens Axboe7b29f922021-03-12 08:30:14 -07005162 if (!io_file_supports_async(req, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005163 return false;
5164
5165 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5166 if (unlikely(!apoll))
5167 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005168 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005169
5170 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005171 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005172
Jens Axboe88e41cf2021-02-22 22:08:01 -07005173 mask = EPOLLONESHOT;
Jens Axboed7718a92020-02-14 22:23:12 -07005174 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005175 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005176 if (def->pollout)
5177 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005178
5179 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5180 if ((req->opcode == IORING_OP_RECVMSG) &&
5181 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5182 mask &= ~POLLIN;
5183
Jens Axboed7718a92020-02-14 22:23:12 -07005184 mask |= POLLERR | POLLPRI;
5185
5186 ipt.pt._qproc = io_async_queue_proc;
5187
5188 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5189 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005190 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005191 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005192 spin_unlock_irq(&ctx->completion_lock);
Jens Axboed7718a92020-02-14 22:23:12 -07005193 return false;
5194 }
5195 spin_unlock_irq(&ctx->completion_lock);
5196 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5197 apoll->poll.events);
5198 return true;
5199}
5200
5201static bool __io_poll_remove_one(struct io_kiocb *req,
Jens Axboeb2e720a2021-03-31 09:03:03 -06005202 struct io_poll_iocb *poll, bool do_cancel)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005203 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005204{
Jens Axboeb41e9852020-02-17 09:52:41 -07005205 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005206
Jens Axboe50826202021-02-23 09:02:26 -07005207 if (!poll->head)
5208 return false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005209 spin_lock(&poll->head->lock);
Jens Axboeb2e720a2021-03-31 09:03:03 -06005210 if (do_cancel)
5211 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005212 if (!list_empty(&poll->wait.entry)) {
5213 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005214 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005215 }
5216 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005217 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005218 return do_complete;
5219}
5220
Jens Axboeb2c3f7e2021-02-23 08:58:04 -07005221static bool io_poll_remove_waitqs(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005222 __must_hold(&req->ctx->completion_lock)
Jens Axboed7718a92020-02-14 22:23:12 -07005223{
5224 bool do_complete;
5225
Jens Axboed4e7cd32020-08-15 11:44:50 -07005226 io_poll_remove_double(req);
Pavel Begunkove31001a2021-04-13 02:58:43 +01005227 do_complete = __io_poll_remove_one(req, io_poll_get_single(req), true);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005228
Pavel Begunkove31001a2021-04-13 02:58:43 +01005229 if (req->opcode != IORING_OP_POLL_ADD && do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005230 /* non-poll requests have submit ref still */
Pavel Begunkove31001a2021-04-13 02:58:43 +01005231 req_ref_put(req);
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005232 }
Jens Axboeb2c3f7e2021-02-23 08:58:04 -07005233 return do_complete;
5234}
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005235
Jens Axboeb2c3f7e2021-02-23 08:58:04 -07005236static bool io_poll_remove_one(struct io_kiocb *req)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005237 __must_hold(&req->ctx->completion_lock)
Jens Axboeb2c3f7e2021-02-23 08:58:04 -07005238{
5239 bool do_complete;
5240
5241 do_complete = io_poll_remove_waitqs(req);
Jens Axboeb41e9852020-02-17 09:52:41 -07005242 if (do_complete) {
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005243 io_cqring_fill_event(req->ctx, req->user_data, -ECANCELED, 0);
Jens Axboeb41e9852020-02-17 09:52:41 -07005244 io_commit_cqring(req->ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005245 req_set_fail(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005246 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005247 }
5248
5249 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005250}
5251
Jens Axboe76e1b642020-09-26 15:05:03 -06005252/*
5253 * Returns true if we found and killed one or more poll requests
5254 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005255static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005256 bool cancel_all)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005257{
Jens Axboe78076bb2019-12-04 19:56:40 -07005258 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005259 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005260 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005261
5262 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005263 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5264 struct hlist_head *list;
5265
5266 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005267 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01005268 if (io_match_task(req, tsk, cancel_all))
Jens Axboef3606e32020-09-22 08:18:24 -06005269 posted += io_poll_remove_one(req);
5270 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005271 }
5272 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005273
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005274 if (posted)
5275 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005276
5277 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005278}
5279
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005280static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr,
5281 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005282 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005283{
Jens Axboe78076bb2019-12-04 19:56:40 -07005284 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005285 struct io_kiocb *req;
5286
Jens Axboe78076bb2019-12-04 19:56:40 -07005287 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5288 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005289 if (sqe_addr != req->user_data)
5290 continue;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005291 if (poll_only && req->opcode != IORING_OP_POLL_ADD)
5292 continue;
Jens Axboeb2cb8052021-03-17 08:17:19 -06005293 return req;
Jens Axboe47f46762019-11-09 17:43:02 -07005294 }
Jens Axboeb2cb8052021-03-17 08:17:19 -06005295 return NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07005296}
5297
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005298static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr,
5299 bool poll_only)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005300 __must_hold(&ctx->completion_lock)
Jens Axboeb2cb8052021-03-17 08:17:19 -06005301{
5302 struct io_kiocb *req;
5303
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005304 req = io_poll_find(ctx, sqe_addr, poll_only);
Jens Axboeb2cb8052021-03-17 08:17:19 -06005305 if (!req)
5306 return -ENOENT;
5307 if (io_poll_remove_one(req))
5308 return 0;
5309
5310 return -EALREADY;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005311}
5312
Pavel Begunkov9096af32021-04-14 13:38:36 +01005313static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe,
5314 unsigned int flags)
5315{
5316 u32 events;
5317
5318 events = READ_ONCE(sqe->poll32_events);
5319#ifdef __BIG_ENDIAN
5320 events = swahw32(events);
5321#endif
5322 if (!(flags & IORING_POLL_ADD_MULTI))
5323 events |= EPOLLONESHOT;
5324 return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT));
5325}
5326
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005327static int io_poll_update_prep(struct io_kiocb *req,
Jens Axboe3529d8c2019-12-19 18:24:38 -07005328 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005329{
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005330 struct io_poll_update *upd = &req->poll_update;
5331 u32 flags;
5332
Jens Axboe221c5eb2019-01-17 09:41:58 -07005333 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5334 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005335 if (sqe->ioprio || sqe->buf_index)
5336 return -EINVAL;
5337 flags = READ_ONCE(sqe->len);
5338 if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA |
5339 IORING_POLL_ADD_MULTI))
5340 return -EINVAL;
5341 /* meaningless without update */
5342 if (flags == IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005343 return -EINVAL;
5344
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005345 upd->old_user_data = READ_ONCE(sqe->addr);
5346 upd->update_events = flags & IORING_POLL_UPDATE_EVENTS;
5347 upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA;
Jens Axboe0969e782019-12-17 18:40:57 -07005348
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005349 upd->new_user_data = READ_ONCE(sqe->off);
5350 if (!upd->update_user_data && upd->new_user_data)
5351 return -EINVAL;
5352 if (upd->update_events)
5353 upd->events = io_poll_parse_events(sqe, flags);
5354 else if (sqe->poll32_events)
5355 return -EINVAL;
Jens Axboe0969e782019-12-17 18:40:57 -07005356
Jens Axboe221c5eb2019-01-17 09:41:58 -07005357 return 0;
5358}
5359
Jens Axboe221c5eb2019-01-17 09:41:58 -07005360static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5361 void *key)
5362{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005363 struct io_kiocb *req = wait->private;
5364 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005365
Jens Axboed7718a92020-02-14 22:23:12 -07005366 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005367}
5368
Jens Axboe221c5eb2019-01-17 09:41:58 -07005369static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5370 struct poll_table_struct *p)
5371{
5372 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5373
Jens Axboee8c2bc12020-08-15 18:44:09 -07005374 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005375}
5376
Jens Axboe3529d8c2019-12-19 18:24:38 -07005377static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005378{
5379 struct io_poll_iocb *poll = &req->poll;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005380 u32 flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005381
5382 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5383 return -EINVAL;
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005384 if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr)
Jens Axboe88e41cf2021-02-22 22:08:01 -07005385 return -EINVAL;
5386 flags = READ_ONCE(sqe->len);
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005387 if (flags & ~IORING_POLL_ADD_MULTI)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005388 return -EINVAL;
5389
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005390 poll->events = io_poll_parse_events(sqe, flags);
Jens Axboe0969e782019-12-17 18:40:57 -07005391 return 0;
5392}
5393
Pavel Begunkov61e98202021-02-10 00:03:08 +00005394static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005395{
5396 struct io_poll_iocb *poll = &req->poll;
5397 struct io_ring_ctx *ctx = req->ctx;
5398 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005399 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005400
Jens Axboed7718a92020-02-14 22:23:12 -07005401 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005402
Jens Axboed7718a92020-02-14 22:23:12 -07005403 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5404 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005405
Jens Axboe8c838782019-03-12 15:48:16 -06005406 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005407 ipt.error = 0;
Pavel Begunkove27414b2021-04-09 09:13:20 +01005408 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06005409 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005410 spin_unlock_irq(&ctx->completion_lock);
5411
Jens Axboe8c838782019-03-12 15:48:16 -06005412 if (mask) {
5413 io_cqring_ev_posted(ctx);
Jens Axboe88e41cf2021-02-22 22:08:01 -07005414 if (poll->events & EPOLLONESHOT)
5415 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005416 }
Jens Axboe8c838782019-03-12 15:48:16 -06005417 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005418}
5419
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005420static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb69de282021-03-17 08:37:41 -06005421{
5422 struct io_ring_ctx *ctx = req->ctx;
5423 struct io_kiocb *preq;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005424 bool completing;
Jens Axboeb69de282021-03-17 08:37:41 -06005425 int ret;
5426
5427 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005428 preq = io_poll_find(ctx, req->poll_update.old_user_data, true);
Jens Axboeb69de282021-03-17 08:37:41 -06005429 if (!preq) {
5430 ret = -ENOENT;
5431 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005432 }
Jens Axboecb3b200e2021-04-06 09:49:31 -06005433
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005434 if (!req->poll_update.update_events && !req->poll_update.update_user_data) {
5435 completing = true;
5436 ret = io_poll_remove_one(preq) ? 0 : -EALREADY;
5437 goto err;
5438 }
5439
Jens Axboecb3b200e2021-04-06 09:49:31 -06005440 /*
5441 * Don't allow racy completion with singleshot, as we cannot safely
5442 * update those. For multishot, if we're racing with completion, just
5443 * let completion re-add it.
5444 */
5445 completing = !__io_poll_remove_one(preq, &preq->poll, false);
5446 if (completing && (preq->poll.events & EPOLLONESHOT)) {
5447 ret = -EALREADY;
5448 goto err;
Jens Axboeb69de282021-03-17 08:37:41 -06005449 }
5450 /* we now have a detached poll request. reissue. */
5451 ret = 0;
5452err:
Jens Axboeb69de282021-03-17 08:37:41 -06005453 if (ret < 0) {
Jens Axboecb3b200e2021-04-06 09:49:31 -06005454 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005455 req_set_fail(req);
Jens Axboeb69de282021-03-17 08:37:41 -06005456 io_req_complete(req, ret);
5457 return 0;
5458 }
5459 /* only mask one event flags, keep behavior flags */
Pavel Begunkov9d805892021-04-13 02:58:40 +01005460 if (req->poll_update.update_events) {
Jens Axboeb69de282021-03-17 08:37:41 -06005461 preq->poll.events &= ~0xffff;
Pavel Begunkov9d805892021-04-13 02:58:40 +01005462 preq->poll.events |= req->poll_update.events & 0xffff;
Jens Axboeb69de282021-03-17 08:37:41 -06005463 preq->poll.events |= IO_POLL_UNMASK;
5464 }
Pavel Begunkov9d805892021-04-13 02:58:40 +01005465 if (req->poll_update.update_user_data)
5466 preq->user_data = req->poll_update.new_user_data;
Jens Axboecb3b200e2021-04-06 09:49:31 -06005467 spin_unlock_irq(&ctx->completion_lock);
5468
Jens Axboeb69de282021-03-17 08:37:41 -06005469 /* complete update request, we're done with it */
5470 io_req_complete(req, ret);
5471
Jens Axboecb3b200e2021-04-06 09:49:31 -06005472 if (!completing) {
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005473 ret = io_poll_add(preq, issue_flags);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005474 if (ret < 0) {
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005475 req_set_fail(preq);
Jens Axboecb3b200e2021-04-06 09:49:31 -06005476 io_req_complete(preq, ret);
5477 }
Jens Axboeb69de282021-03-17 08:37:41 -06005478 }
5479 return 0;
5480}
5481
Jens Axboe5262f562019-09-17 12:26:57 -06005482static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5483{
Jens Axboead8a48a2019-11-15 08:49:11 -07005484 struct io_timeout_data *data = container_of(timer,
5485 struct io_timeout_data, timer);
5486 struct io_kiocb *req = data->req;
5487 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005488 unsigned long flags;
5489
Jens Axboe5262f562019-09-17 12:26:57 -06005490 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005491 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005492 atomic_set(&req->ctx->cq_timeouts,
5493 atomic_read(&req->ctx->cq_timeouts) + 1);
5494
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005495 io_cqring_fill_event(ctx, req->user_data, -ETIME, 0);
Jens Axboe5262f562019-09-17 12:26:57 -06005496 io_commit_cqring(ctx);
5497 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5498
5499 io_cqring_ev_posted(ctx);
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005500 req_set_fail(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005501 io_put_req(req);
5502 return HRTIMER_NORESTART;
5503}
5504
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005505static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5506 __u64 user_data)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005507 __must_hold(&ctx->completion_lock)
Jens Axboe47f46762019-11-09 17:43:02 -07005508{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005509 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005510 struct io_kiocb *req;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005511 bool found = false;
Jens Axboef254ac02020-08-12 17:33:30 -06005512
5513 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005514 found = user_data == req->user_data;
5515 if (found)
Jens Axboef254ac02020-08-12 17:33:30 -06005516 break;
Jens Axboef254ac02020-08-12 17:33:30 -06005517 }
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005518 if (!found)
5519 return ERR_PTR(-ENOENT);
Jens Axboef254ac02020-08-12 17:33:30 -06005520
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005521 io = req->async_data;
Pavel Begunkovfd9c7bc2021-04-13 02:58:42 +01005522 if (hrtimer_try_to_cancel(&io->timer) == -1)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005523 return ERR_PTR(-EALREADY);
5524 list_del_init(&req->timeout.list);
5525 return req;
5526}
5527
5528static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005529 __must_hold(&ctx->completion_lock)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005530{
5531 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5532
5533 if (IS_ERR(req))
5534 return PTR_ERR(req);
5535
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005536 req_set_fail(req);
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005537 io_cqring_fill_event(ctx, req->user_data, -ECANCELED, 0);
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005538 io_put_req_deferred(req, 1);
5539 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005540}
5541
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005542static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5543 struct timespec64 *ts, enum hrtimer_mode mode)
Pavel Begunkove07785b2021-04-01 15:43:57 +01005544 __must_hold(&ctx->completion_lock)
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005545{
5546 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5547 struct io_timeout_data *data;
5548
5549 if (IS_ERR(req))
5550 return PTR_ERR(req);
5551
5552 req->timeout.off = 0; /* noseq */
5553 data = req->async_data;
5554 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5555 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5556 data->timer.function = io_timeout_fn;
5557 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5558 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005559}
5560
Jens Axboe3529d8c2019-12-19 18:24:38 -07005561static int io_timeout_remove_prep(struct io_kiocb *req,
5562 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005563{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005564 struct io_timeout_rem *tr = &req->timeout_rem;
5565
Jens Axboeb29472e2019-12-17 18:50:29 -07005566 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5567 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005568 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5569 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005570 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005571 return -EINVAL;
5572
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005573 tr->addr = READ_ONCE(sqe->addr);
5574 tr->flags = READ_ONCE(sqe->timeout_flags);
5575 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5576 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5577 return -EINVAL;
5578 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5579 return -EFAULT;
5580 } else if (tr->flags) {
5581 /* timeout removal doesn't support flags */
5582 return -EINVAL;
5583 }
5584
Jens Axboeb29472e2019-12-17 18:50:29 -07005585 return 0;
5586}
5587
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005588static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5589{
5590 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5591 : HRTIMER_MODE_REL;
5592}
5593
Jens Axboe11365042019-10-16 09:08:32 -06005594/*
5595 * Remove or update an existing timeout command
5596 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005597static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005598{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005599 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005600 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005601 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005602
Jens Axboe11365042019-10-16 09:08:32 -06005603 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005604 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005605 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005606 else
5607 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5608 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005609
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005610 io_cqring_fill_event(ctx, req->user_data, ret, 0);
Jens Axboe11365042019-10-16 09:08:32 -06005611 io_commit_cqring(ctx);
5612 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005613 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005614 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005615 req_set_fail(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005616 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005617 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005618}
5619
Jens Axboe3529d8c2019-12-19 18:24:38 -07005620static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005621 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005622{
Jens Axboead8a48a2019-11-15 08:49:11 -07005623 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005624 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005625 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005626
Jens Axboead8a48a2019-11-15 08:49:11 -07005627 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005628 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005629 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005630 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005631 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005632 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005633 flags = READ_ONCE(sqe->timeout_flags);
5634 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005635 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005636
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005637 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005638
Jens Axboee8c2bc12020-08-15 18:44:09 -07005639 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005640 return -ENOMEM;
5641
Jens Axboee8c2bc12020-08-15 18:44:09 -07005642 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005643 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005644
5645 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005646 return -EFAULT;
5647
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005648 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005649 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
Pavel Begunkov2482b582021-03-25 18:32:44 +00005650 if (is_timeout_link)
5651 io_req_track_inflight(req);
Jens Axboead8a48a2019-11-15 08:49:11 -07005652 return 0;
5653}
5654
Pavel Begunkov61e98202021-02-10 00:03:08 +00005655static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005656{
Jens Axboead8a48a2019-11-15 08:49:11 -07005657 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005658 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005659 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005660 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005661
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005662 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005663
Jens Axboe5262f562019-09-17 12:26:57 -06005664 /*
5665 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005666 * timeout event to be satisfied. If it isn't set, then this is
5667 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005668 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005669 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005670 entry = ctx->timeout_list.prev;
5671 goto add;
5672 }
Jens Axboe5262f562019-09-17 12:26:57 -06005673
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005674 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5675 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005676
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005677 /* Update the last seq here in case io_flush_timeouts() hasn't.
5678 * This is safe because ->completion_lock is held, and submissions
5679 * and completions are never mixed in the same ->completion_lock section.
5680 */
5681 ctx->cq_last_tm_flush = tail;
5682
Jens Axboe5262f562019-09-17 12:26:57 -06005683 /*
5684 * Insertion sort, ensuring the first entry in the list is always
5685 * the one we need first.
5686 */
Jens Axboe5262f562019-09-17 12:26:57 -06005687 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005688 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5689 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005690
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005691 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005692 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005693 /* nxt.seq is behind @tail, otherwise would've been completed */
5694 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005695 break;
5696 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005697add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005698 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005699 data->timer.function = io_timeout_fn;
5700 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005701 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005702 return 0;
5703}
5704
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005705struct io_cancel_data {
5706 struct io_ring_ctx *ctx;
5707 u64 user_data;
5708};
5709
Jens Axboe62755e32019-10-28 21:49:21 -06005710static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005711{
Jens Axboe62755e32019-10-28 21:49:21 -06005712 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005713 struct io_cancel_data *cd = data;
Jens Axboede0617e2019-04-06 21:51:27 -06005714
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005715 return req->ctx == cd->ctx && req->user_data == cd->user_data;
Jens Axboe62755e32019-10-28 21:49:21 -06005716}
5717
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005718static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data,
5719 struct io_ring_ctx *ctx)
Jens Axboe62755e32019-10-28 21:49:21 -06005720{
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005721 struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, };
Jens Axboe62755e32019-10-28 21:49:21 -06005722 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005723 int ret = 0;
5724
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005725 if (!tctx || !tctx->io_wq)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005726 return -ENOENT;
5727
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005728 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005729 switch (cancel_ret) {
5730 case IO_WQ_CANCEL_OK:
5731 ret = 0;
5732 break;
5733 case IO_WQ_CANCEL_RUNNING:
5734 ret = -EALREADY;
5735 break;
5736 case IO_WQ_CANCEL_NOTFOUND:
5737 ret = -ENOENT;
5738 break;
5739 }
5740
Jens Axboee977d6d2019-11-05 12:39:45 -07005741 return ret;
5742}
5743
Jens Axboe47f46762019-11-09 17:43:02 -07005744static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5745 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005746 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005747{
5748 unsigned long flags;
5749 int ret;
5750
Pavel Begunkovf458dd842021-03-08 12:14:14 +00005751 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07005752 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovdf9727a2021-04-01 15:43:59 +01005753 if (ret != -ENOENT)
5754 goto done;
Jens Axboe47f46762019-11-09 17:43:02 -07005755 ret = io_timeout_cancel(ctx, sqe_addr);
5756 if (ret != -ENOENT)
5757 goto done;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005758 ret = io_poll_cancel(ctx, sqe_addr, false);
Jens Axboe47f46762019-11-09 17:43:02 -07005759done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005760 if (!ret)
5761 ret = success_ret;
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005762 io_cqring_fill_event(ctx, req->user_data, ret, 0);
Jens Axboe47f46762019-11-09 17:43:02 -07005763 io_commit_cqring(ctx);
5764 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5765 io_cqring_ev_posted(ctx);
5766
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005767 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005768 req_set_fail(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005769}
5770
Jens Axboe3529d8c2019-12-19 18:24:38 -07005771static int io_async_cancel_prep(struct io_kiocb *req,
5772 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005773{
Jens Axboefbf23842019-12-17 18:45:56 -07005774 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005775 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005776 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5777 return -EINVAL;
5778 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005779 return -EINVAL;
5780
Jens Axboefbf23842019-12-17 18:45:56 -07005781 req->cancel.addr = READ_ONCE(sqe->addr);
5782 return 0;
5783}
5784
Pavel Begunkov61e98202021-02-10 00:03:08 +00005785static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005786{
5787 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov58f99372021-03-12 16:25:55 +00005788 u64 sqe_addr = req->cancel.addr;
5789 struct io_tctx_node *node;
5790 int ret;
Jens Axboefbf23842019-12-17 18:45:56 -07005791
Pavel Begunkov58f99372021-03-12 16:25:55 +00005792 /* tasks should wait for their io-wq threads, so safe w/o sync */
5793 ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx);
5794 spin_lock_irq(&ctx->completion_lock);
5795 if (ret != -ENOENT)
5796 goto done;
5797 ret = io_timeout_cancel(ctx, sqe_addr);
5798 if (ret != -ENOENT)
5799 goto done;
Pavel Begunkov9ba5fac2021-04-14 13:38:35 +01005800 ret = io_poll_cancel(ctx, sqe_addr, false);
Pavel Begunkov58f99372021-03-12 16:25:55 +00005801 if (ret != -ENOENT)
5802 goto done;
5803 spin_unlock_irq(&ctx->completion_lock);
5804
5805 /* slow path, try all io-wq's */
5806 io_ring_submit_lock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
5807 ret = -ENOENT;
5808 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
5809 struct io_uring_task *tctx = node->task->io_uring;
5810
Pavel Begunkov58f99372021-03-12 16:25:55 +00005811 ret = io_async_cancel_one(tctx, req->cancel.addr, ctx);
5812 if (ret != -ENOENT)
5813 break;
5814 }
5815 io_ring_submit_unlock(ctx, !(issue_flags & IO_URING_F_NONBLOCK));
5816
5817 spin_lock_irq(&ctx->completion_lock);
5818done:
Pavel Begunkovd4d19c12021-04-25 14:32:17 +01005819 io_cqring_fill_event(ctx, req->user_data, ret, 0);
Pavel Begunkov58f99372021-03-12 16:25:55 +00005820 io_commit_cqring(ctx);
5821 spin_unlock_irq(&ctx->completion_lock);
5822 io_cqring_ev_posted(ctx);
5823
5824 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005825 req_set_fail(req);
Pavel Begunkov58f99372021-03-12 16:25:55 +00005826 io_put_req(req);
Jens Axboe62755e32019-10-28 21:49:21 -06005827 return 0;
5828}
5829
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005830static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005831 const struct io_uring_sqe *sqe)
5832{
Daniele Albano61710e42020-07-18 14:15:16 -06005833 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5834 return -EINVAL;
5835 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005836 return -EINVAL;
5837
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005838 req->rsrc_update.offset = READ_ONCE(sqe->off);
5839 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5840 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005841 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005842 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005843 return 0;
5844}
5845
Pavel Begunkov889fca72021-02-10 00:03:09 +00005846static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005847{
5848 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005849 struct io_uring_rsrc_update2 up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005850 int ret;
5851
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005852 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005853 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005854
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005855 up.offset = req->rsrc_update.offset;
5856 up.data = req->rsrc_update.arg;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01005857 up.nr = 0;
5858 up.tags = 0;
Colin Ian King615cee42021-04-26 10:47:35 +01005859 up.resv = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005860
5861 mutex_lock(&ctx->uring_lock);
Pavel Begunkovfdecb662021-04-25 14:32:20 +01005862 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01005863 &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005864 mutex_unlock(&ctx->uring_lock);
5865
5866 if (ret < 0)
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01005867 req_set_fail(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005868 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005869 return 0;
5870}
5871
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005872static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005873{
Jens Axboed625c6e2019-12-17 19:53:05 -07005874 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005875 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005876 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005877 case IORING_OP_READV:
5878 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005879 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005880 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005881 case IORING_OP_WRITEV:
5882 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005883 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005884 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005885 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005886 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005887 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01005888 return io_poll_update_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005889 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005890 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005891 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005892 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005893 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005894 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005895 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005896 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005897 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005898 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005899 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005900 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005901 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005902 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005903 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005904 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005905 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005906 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005907 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005908 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005909 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005910 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005911 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005912 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005913 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005914 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005915 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005916 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005917 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005918 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005919 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005920 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005921 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005922 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005923 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005924 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005925 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005926 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005927 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005928 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005929 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005930 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005931 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005932 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005933 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005934 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005935 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005936 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005937 case IORING_OP_SHUTDOWN:
5938 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06005939 case IORING_OP_RENAMEAT:
5940 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06005941 case IORING_OP_UNLINKAT:
5942 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005943 }
5944
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005945 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5946 req->opcode);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01005947 return -EINVAL;
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005948}
5949
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005950static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005951{
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00005952 if (!io_op_defs[req->opcode].needs_async_setup)
5953 return 0;
5954 if (WARN_ON_ONCE(req->async_data))
5955 return -EFAULT;
5956 if (io_alloc_async_data(req))
5957 return -EAGAIN;
5958
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005959 switch (req->opcode) {
5960 case IORING_OP_READV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005961 return io_rw_prep_async(req, READ);
5962 case IORING_OP_WRITEV:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005963 return io_rw_prep_async(req, WRITE);
5964 case IORING_OP_SENDMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005965 return io_sendmsg_prep_async(req);
5966 case IORING_OP_RECVMSG:
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005967 return io_recvmsg_prep_async(req);
5968 case IORING_OP_CONNECT:
5969 return io_connect_prep_async(req);
5970 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00005971 printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n",
5972 req->opcode);
5973 return -EFAULT;
Jens Axboedef596e2019-01-09 08:59:42 -07005974}
5975
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005976static u32 io_get_sequence(struct io_kiocb *req)
5977{
5978 struct io_kiocb *pos;
5979 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005980 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005981
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005982 io_for_each_link(pos, req)
5983 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005984
5985 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5986 return total_submitted - nr_reqs;
5987}
5988
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005989static int io_req_defer(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005990{
5991 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005992 struct io_defer_entry *de;
Jens Axboedef596e2019-01-09 08:59:42 -07005993 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005994 u32 seq;
Jens Axboedef596e2019-01-09 08:59:42 -07005995
5996 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005997 if (likely(list_empty_careful(&ctx->defer_list) &&
5998 !(req->flags & REQ_F_IO_DRAIN)))
5999 return 0;
6000
6001 seq = io_get_sequence(req);
6002 /* Still a chance to pass the sequence check */
6003 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboedef596e2019-01-09 08:59:42 -07006004 return 0;
6005
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006006 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006007 if (ret)
6008 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006009 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006010 de = kmalloc(sizeof(*de), GFP_KERNEL);
6011 if (!de)
6012 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006013
6014 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006015 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006016 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006017 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006018 io_queue_async_work(req);
6019 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006020 }
6021
6022 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006023 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006024 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006025 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006026 spin_unlock_irq(&ctx->completion_lock);
6027 return -EIOCBQUEUED;
6028}
6029
Pavel Begunkov68fb8972021-03-19 17:22:41 +00006030static void io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006031{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006032 if (req->flags & REQ_F_BUFFER_SELECTED) {
6033 switch (req->opcode) {
6034 case IORING_OP_READV:
6035 case IORING_OP_READ_FIXED:
6036 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006037 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006038 break;
6039 case IORING_OP_RECVMSG:
6040 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006041 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006042 break;
6043 }
6044 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006045 }
6046
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006047 if (req->flags & REQ_F_NEED_CLEANUP) {
6048 switch (req->opcode) {
6049 case IORING_OP_READV:
6050 case IORING_OP_READ_FIXED:
6051 case IORING_OP_READ:
6052 case IORING_OP_WRITEV:
6053 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006054 case IORING_OP_WRITE: {
6055 struct io_async_rw *io = req->async_data;
6056 if (io->free_iovec)
6057 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006058 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006059 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006060 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006061 case IORING_OP_SENDMSG: {
6062 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006063
6064 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006065 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006066 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006067 case IORING_OP_SPLICE:
6068 case IORING_OP_TEE:
Pavel Begunkove1d767f2021-03-19 17:22:43 +00006069 if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED))
6070 io_put_file(req->splice.file_in);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006071 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006072 case IORING_OP_OPENAT:
6073 case IORING_OP_OPENAT2:
6074 if (req->open.filename)
6075 putname(req->open.filename);
6076 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006077 case IORING_OP_RENAMEAT:
6078 putname(req->rename.oldpath);
6079 putname(req->rename.newpath);
6080 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006081 case IORING_OP_UNLINKAT:
6082 putname(req->unlink.filename);
6083 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006084 }
6085 req->flags &= ~REQ_F_NEED_CLEANUP;
6086 }
Jens Axboe75652a302021-04-15 09:52:40 -06006087 if ((req->flags & REQ_F_POLLED) && req->apoll) {
6088 kfree(req->apoll->double_poll);
6089 kfree(req->apoll);
6090 req->apoll = NULL;
6091 }
Pavel Begunkov3a0a6902021-04-20 12:03:31 +01006092 if (req->flags & REQ_F_INFLIGHT) {
6093 struct io_uring_task *tctx = req->task->io_uring;
6094
6095 atomic_dec(&tctx->inflight_tracked);
6096 req->flags &= ~REQ_F_INFLIGHT;
6097 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006098}
6099
Pavel Begunkov889fca72021-02-10 00:03:09 +00006100static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006101{
Jens Axboeedafcce2019-01-09 09:16:05 -07006102 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5730b272021-02-27 15:57:30 -07006103 const struct cred *creds = NULL;
Jens Axboed625c6e2019-12-17 19:53:05 -07006104 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006105
Jens Axboe003e8dc2021-03-06 09:22:27 -07006106 if (req->work.creds && req->work.creds != current_cred())
6107 creds = override_creds(req->work.creds);
Jens Axboe5730b272021-02-27 15:57:30 -07006108
Jens Axboed625c6e2019-12-17 19:53:05 -07006109 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006111 ret = io_nop(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006112 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006114 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006115 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006116 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117 break;
6118 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006119 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006120 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006121 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006122 break;
6123 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006124 ret = io_fsync(req, issue_flags);
Jackie Liuba5290c2019-10-09 09:19:59 +08006125 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006127 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006128 break;
6129 case IORING_OP_POLL_REMOVE:
Pavel Begunkovc5de0032021-04-14 13:38:37 +01006130 ret = io_poll_update(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006131 break;
Jens Axboeb76da702019-11-20 13:05:32 -07006132 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006133 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006134 break;
6135 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006136 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006137 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006138 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006139 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006140 break;
6141 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006142 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006143 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006144 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006145 ret = io_recv(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006146 break;
Jens Axboe561fb042019-10-24 07:25:42 -06006147 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006148 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006149 break;
6150 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006151 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006152 break;
6153 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006154 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006155 break;
6156 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006157 ret = io_connect(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006158 break;
6159 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006160 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006161 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006162 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006163 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006164 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006165 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006166 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006167 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006168 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006169 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006170 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006171 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006172 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006173 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006174 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006175 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006176 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006177 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006178 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006179 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006180 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006181 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006182 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006183 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006184 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006185 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006186 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006187 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006188 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006189 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006190 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006191 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006192 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006193 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006194 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006195 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006196 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006198 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006199 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006200 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006201 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006202 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006203 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006204 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006205 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006206 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006207 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006208 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006209 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006210 default:
6211 ret = -EINVAL;
6212 break;
6213 }
Jens Axboe31b51512019-01-18 22:56:34 -07006214
Jens Axboe5730b272021-02-27 15:57:30 -07006215 if (creds)
6216 revert_creds(creds);
6217
Jens Axboe2b188cc2019-01-07 10:46:33 -07006218 if (ret)
6219 return ret;
6220
Jens Axboeb5325762020-05-19 21:20:27 -06006221 /* If the op doesn't have a file, we're not polling for it */
6222 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006223 const bool in_async = io_wq_current_is_worker();
6224
Jens Axboe11ba8202020-01-15 21:51:17 -07006225 /* workqueue context doesn't hold uring_lock, grab it now */
6226 if (in_async)
6227 mutex_lock(&ctx->uring_lock);
6228
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006229 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006230
6231 if (in_async)
6232 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006233 }
6234
6235 return 0;
6236}
6237
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006238static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006239{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006240 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006241 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006242 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006243
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006244 timeout = io_prep_linked_timeout(req);
6245 if (timeout)
6246 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006247
Jens Axboe4014d942021-01-19 15:53:54 -07006248 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006249 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006250
Jens Axboe561fb042019-10-24 07:25:42 -06006251 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006252 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006253 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006254 /*
6255 * We can get EAGAIN for polled IO even though we're
6256 * forcing a sync submission from here, since we can't
6257 * wait for request slots on the block side.
6258 */
6259 if (ret != -EAGAIN)
6260 break;
6261 cond_resched();
6262 } while (1);
6263 }
Jens Axboe31b51512019-01-18 22:56:34 -07006264
Pavel Begunkova3df76982021-02-18 22:32:52 +00006265 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006266 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006267 /* io-wq is going to take one down */
Jens Axboede9b4cc2021-02-24 13:28:27 -07006268 req_ref_get(req);
Pavel Begunkova3df76982021-02-18 22:32:52 +00006269 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006270 }
Jens Axboe31b51512019-01-18 22:56:34 -07006271}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006272
Jens Axboe7b29f922021-03-12 08:30:14 -07006273#define FFS_ASYNC_READ 0x1UL
6274#define FFS_ASYNC_WRITE 0x2UL
6275#ifdef CONFIG_64BIT
6276#define FFS_ISREG 0x4UL
6277#else
6278#define FFS_ISREG 0x0UL
6279#endif
6280#define FFS_MASK ~(FFS_ASYNC_READ|FFS_ASYNC_WRITE|FFS_ISREG)
6281
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006282static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006283 unsigned i)
Jens Axboe09bb8392019-03-13 12:39:28 -06006284{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006285 struct io_fixed_file *table_l2;
Jens Axboe65e19f52019-10-26 07:20:21 -06006286
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006287 table_l2 = table->files[i >> IORING_FILE_TABLE_SHIFT];
6288 return &table_l2[i & IORING_FILE_TABLE_MASK];
Pavel Begunkovdafecf12021-02-28 22:35:11 +00006289}
6290
Jens Axboe09bb8392019-03-13 12:39:28 -06006291static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6292 int index)
6293{
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006294 struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06006295
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006296 return (struct file *) (slot->file_ptr & FFS_MASK);
Jens Axboe65e19f52019-10-26 07:20:21 -06006297}
6298
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006299static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file)
Pavel Begunkov9a321c92021-04-01 15:44:01 +01006300{
6301 unsigned long file_ptr = (unsigned long) file;
6302
6303 if (__io_file_supports_async(file, READ))
6304 file_ptr |= FFS_ASYNC_READ;
6305 if (__io_file_supports_async(file, WRITE))
6306 file_ptr |= FFS_ASYNC_WRITE;
6307 if (S_ISREG(file_inode(file)->i_mode))
6308 file_ptr |= FFS_ISREG;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01006309 file_slot->file_ptr = file_ptr;
Jens Axboe09bb8392019-03-13 12:39:28 -06006310}
6311
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006312static struct file *io_file_get(struct io_submit_state *state,
6313 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006314{
6315 struct io_ring_ctx *ctx = req->ctx;
6316 struct file *file;
6317
6318 if (fixed) {
Jens Axboe7b29f922021-03-12 08:30:14 -07006319 unsigned long file_ptr;
6320
Pavel Begunkov479f5172020-10-10 18:34:07 +01006321 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006322 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006323 fd = array_index_nospec(fd, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01006324 file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr;
Jens Axboe7b29f922021-03-12 08:30:14 -07006325 file = (struct file *) (file_ptr & FFS_MASK);
6326 file_ptr &= ~FFS_MASK;
6327 /* mask in overlapping REQ_F and FFS bits */
6328 req->flags |= (file_ptr << REQ_F_ASYNC_READ_BIT);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01006329 io_req_set_rsrc_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006330 } else {
6331 trace_io_uring_file_get(ctx, fd);
6332 file = __io_file_get(state, fd);
Jens Axboed44f5542021-03-12 08:27:05 -07006333
6334 /* we don't allow fixed io_uring files */
6335 if (file && unlikely(file->f_op == &io_uring_fops))
6336 io_req_track_inflight(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006337 }
6338
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006339 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006340}
6341
Jens Axboe2665abf2019-11-05 12:40:47 -07006342static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6343{
Jens Axboead8a48a2019-11-15 08:49:11 -07006344 struct io_timeout_data *data = container_of(timer,
6345 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006346 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006347 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006348 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006349
6350 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006351 prev = req->timeout.head;
6352 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006353
6354 /*
6355 * We don't expect the list to be empty, that will only happen if we
6356 * race with the completion of the linked work.
6357 */
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006358 if (prev) {
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006359 io_remove_next_linked(prev);
Pavel Begunkov447c19f2021-05-14 12:02:50 +01006360 if (!req_ref_inc_not_zero(prev))
6361 prev = NULL;
6362 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006363 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6364
6365 if (prev) {
Pavel Begunkov014db002020-03-03 21:33:12 +03006366 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006367 io_put_req_deferred(prev, 1);
Pavel Begunkova2982322021-05-07 21:06:38 +01006368 io_put_req_deferred(req, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006369 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006370 io_req_complete_post(req, -ETIME, 0);
Jens Axboe2665abf2019-11-05 12:40:47 -07006371 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006372 return HRTIMER_NORESTART;
6373}
6374
Pavel Begunkovde968c12021-03-19 17:22:33 +00006375static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006376{
Pavel Begunkovde968c12021-03-19 17:22:33 +00006377 struct io_ring_ctx *ctx = req->ctx;
6378
6379 spin_lock_irq(&ctx->completion_lock);
Jens Axboe76a46e02019-11-10 23:34:16 -07006380 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006381 * If the back reference is NULL, then our linked request finished
6382 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006383 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006384 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006385 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006386
Jens Axboead8a48a2019-11-15 08:49:11 -07006387 data->timer.function = io_link_timeout_fn;
6388 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6389 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006390 }
Jens Axboe76a46e02019-11-10 23:34:16 -07006391 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006392 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006393 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006394}
6395
Jens Axboead8a48a2019-11-15 08:49:11 -07006396static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006397{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006398 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006400 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6401 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006402 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006403
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006404 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006405 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006406 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006407 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006408}
6409
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006410static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006411{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006412 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006413 int ret;
6414
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006415 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006416
6417 /*
6418 * We async punt it if the file wasn't marked NOWAIT, or if the file
6419 * doesn't support non-blocking read/write attempts
6420 */
Pavel Begunkov18400382021-03-19 17:22:34 +00006421 if (likely(!ret)) {
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006422 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006423 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006424 struct io_ring_ctx *ctx = req->ctx;
6425 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006426
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006427 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006428 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006429 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006430 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006431 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006432 }
Pavel Begunkov18400382021-03-19 17:22:34 +00006433 } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
6434 if (!io_arm_poll_handler(req)) {
6435 /*
6436 * Queued up for async execution, worker will release
6437 * submit reference when the iocb is actually submitted.
6438 */
6439 io_queue_async_work(req);
6440 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006441 } else {
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006442 io_req_complete_failed(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006443 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006444 if (linked_timeout)
6445 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006446}
6447
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006448static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006449{
6450 int ret;
6451
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006452 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006453 if (ret) {
6454 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006455fail_req:
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006456 io_req_complete_failed(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006457 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006458 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006459 ret = io_req_prep_async(req);
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006460 if (unlikely(ret))
6461 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006462 io_queue_async_work(req);
6463 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006464 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006465 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006466}
6467
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006468/*
6469 * Check SQE restrictions (opcode and flags).
6470 *
6471 * Returns 'true' if SQE is allowed, 'false' otherwise.
6472 */
6473static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6474 struct io_kiocb *req,
6475 unsigned int sqe_flags)
6476{
6477 if (!ctx->restricted)
6478 return true;
6479
6480 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6481 return false;
6482
6483 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6484 ctx->restrictions.sqe_flags_required)
6485 return false;
6486
6487 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6488 ctx->restrictions.sqe_flags_required))
6489 return false;
6490
6491 return true;
6492}
6493
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006494static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006495 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006496{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006497 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006498 unsigned int sqe_flags;
Jens Axboe003e8dc2021-03-06 09:22:27 -07006499 int personality, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006500
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006501 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006502 /* same numerical values with corresponding REQ_F_*, safe to copy */
6503 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006504 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006505 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006506 req->file = NULL;
6507 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006508 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006509 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006510 /* one is dropped after submission, the other at completion */
Jens Axboeabc54d62021-02-24 13:32:30 -07006511 atomic_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006512 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006513 req->result = 0;
Jens Axboe93e68e02021-03-09 07:02:21 -07006514 req->work.creds = NULL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006515
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006516 /* enforce forwards compatibility on users */
Pavel Begunkovdddca222021-04-27 16:13:52 +01006517 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS))
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006518 return -EINVAL;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006519 if (unlikely(req->opcode >= IORING_OP_LAST))
6520 return -EINVAL;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006521 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6522 return -EACCES;
6523
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006524 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6525 !io_op_defs[req->opcode].buffer_select)
6526 return -EOPNOTSUPP;
6527
Jens Axboe003e8dc2021-03-06 09:22:27 -07006528 personality = READ_ONCE(sqe->personality);
6529 if (personality) {
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00006530 req->work.creds = xa_load(&ctx->personalities, personality);
Jens Axboe003e8dc2021-03-06 09:22:27 -07006531 if (!req->work.creds)
6532 return -EINVAL;
6533 get_cred(req->work.creds);
Jens Axboe003e8dc2021-03-06 09:22:27 -07006534 }
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006535 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006536
Jens Axboe27926b62020-10-28 09:33:23 -06006537 /*
6538 * Plug now if we have more than 1 IO left after this, and the target
6539 * is potentially a read/write to block based storage.
6540 */
6541 if (!state->plug_started && state->ios_left > 1 &&
6542 io_op_defs[req->opcode].plug) {
6543 blk_start_plug(&state->plug);
6544 state->plug_started = true;
6545 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006546
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006547 if (io_op_defs[req->opcode].needs_file) {
6548 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006549
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006550 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006551 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006552 ret = -EBADF;
6553 }
6554
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006555 state->ios_left--;
6556 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006557}
6558
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006559static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006560 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006561{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006562 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006563 int ret;
6564
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006565 ret = io_init_req(ctx, req, sqe);
6566 if (unlikely(ret)) {
6567fail_req:
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006568 if (link->head) {
6569 /* fail even hard links since we don't submit */
Pavel Begunkov93d2bcd2021-05-16 22:58:05 +01006570 req_set_fail(link->head);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006571 io_req_complete_failed(link->head, -ECANCELED);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006572 link->head = NULL;
6573 }
Pavel Begunkovf41db2732021-02-28 22:35:12 +00006574 io_req_complete_failed(req, ret);
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006575 return ret;
6576 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006577 ret = io_req_prep(req, sqe);
6578 if (unlikely(ret))
6579 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006580
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006581 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006582 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6583 true, ctx->flags & IORING_SETUP_SQPOLL);
6584
Jens Axboe6c271ce2019-01-10 11:22:30 -07006585 /*
6586 * If we already have a head request, queue this one for async
6587 * submittal once the head completes. If we don't have a head but
6588 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6589 * submitted sync once the chain is complete. If none of those
6590 * conditions are true (normal request), then just queue it.
6591 */
6592 if (link->head) {
6593 struct io_kiocb *head = link->head;
6594
6595 /*
6596 * Taking sequential execution of a link, draining both sides
6597 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6598 * requests in the link. So, it drains the head and the
6599 * next after the link request. The last one is done via
6600 * drain_next flag to persist the effect across calls.
6601 */
6602 if (req->flags & REQ_F_IO_DRAIN) {
6603 head->flags |= REQ_F_IO_DRAIN;
6604 ctx->drain_next = 1;
6605 }
Pavel Begunkovb7e298d2021-02-28 22:35:19 +00006606 ret = io_req_prep_async(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006607 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006608 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006609 trace_io_uring_link(ctx, req, head);
6610 link->last->link = req;
6611 link->last = req;
6612
6613 /* last request of a link, enqueue the link */
6614 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006615 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006616 link->head = NULL;
6617 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006618 } else {
6619 if (unlikely(ctx->drain_next)) {
6620 req->flags |= REQ_F_IO_DRAIN;
6621 ctx->drain_next = 0;
6622 }
6623 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006624 link->head = req;
6625 link->last = req;
6626 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006627 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006628 }
6629 }
6630
6631 return 0;
6632}
6633
6634/*
6635 * Batched submission is done, ensure local IO is flushed out.
6636 */
6637static void io_submit_state_end(struct io_submit_state *state,
6638 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006639{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006640 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006641 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006642 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006643 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006644 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006645 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006646 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006647}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006648
Jens Axboe9e645e112019-05-10 16:07:28 -06006649/*
6650 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006651 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006652static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006653 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006654{
6655 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006656 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006657 /* set only head, no need to init link_last in advance */
6658 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006659}
6660
Jens Axboe193155c2020-02-22 23:22:19 -07006661static void io_commit_sqring(struct io_ring_ctx *ctx)
6662{
Jens Axboe75c6a032020-01-28 10:15:23 -07006663 struct io_rings *rings = ctx->rings;
6664
6665 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006666 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006667 * since once we write the new head, the application could
6668 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006669 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006670 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006671}
6672
Jens Axboe9e645e112019-05-10 16:07:28 -06006673/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006674 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006675 * that is mapped by userspace. This means that care needs to be taken to
6676 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006677 * being a good citizen. If members of the sqe are validated and then later
6678 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006679 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006680 */
6681static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006682{
6683 u32 *sq_array = ctx->sq_array;
6684 unsigned head;
6685
6686 /*
6687 * The cached sq head (or cq tail) serves two purposes:
6688 *
6689 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006690 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006691 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006692 * though the application is the one updating it.
6693 */
6694 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6695 if (likely(head < ctx->sq_entries))
6696 return &ctx->sq_sqes[head];
6697
6698 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006699 ctx->cached_sq_dropped++;
6700 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6701 return NULL;
6702}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006703
Jens Axboe0f212202020-09-13 13:09:39 -06006704static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006705{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006706 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006707
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006708 /* make sure SQ entry isn't read before tail */
6709 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006710
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006711 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6712 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006713
Jens Axboed8a6df12020-10-15 16:24:45 -06006714 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006715 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006716 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006717
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006718 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006719 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006720 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006721
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006722 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006723 if (unlikely(!req)) {
6724 if (!submitted)
6725 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006726 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006727 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006728 sqe = io_get_sqe(ctx);
6729 if (unlikely(!sqe)) {
6730 kmem_cache_free(req_cachep, req);
6731 break;
6732 }
Jens Axboed3656342019-12-18 09:50:26 -07006733 /* will complete beyond this point, count as submitted */
6734 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006735 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006736 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006737 }
6738
Pavel Begunkov9466f432020-01-25 22:34:01 +03006739 if (unlikely(submitted != nr)) {
6740 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006741 struct io_uring_task *tctx = current->io_uring;
6742 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006743
Jens Axboed8a6df12020-10-15 16:24:45 -06006744 percpu_ref_put_many(&ctx->refs, unused);
6745 percpu_counter_sub(&tctx->inflight, unused);
6746 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006747 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006748
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006749 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006750 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6751 io_commit_sqring(ctx);
6752
Jens Axboe6c271ce2019-01-10 11:22:30 -07006753 return submitted;
6754}
6755
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006756static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
6757{
6758 return READ_ONCE(sqd->state);
6759}
6760
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006761static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6762{
6763 /* Tell userspace we may need a wakeup call */
6764 spin_lock_irq(&ctx->completion_lock);
6765 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6766 spin_unlock_irq(&ctx->completion_lock);
6767}
6768
6769static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6770{
6771 spin_lock_irq(&ctx->completion_lock);
6772 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6773 spin_unlock_irq(&ctx->completion_lock);
6774}
6775
Xiaoguang Wang08369242020-11-03 14:15:59 +08006776static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006777{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006778 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006779 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006780
Jens Axboec8d1ba52020-09-14 11:07:26 -06006781 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006782 /* if we're handling multiple rings, cap submit size for fairness */
6783 if (cap_entries && to_submit > 8)
6784 to_submit = 8;
6785
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006786 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6787 unsigned nr_events = 0;
6788
Xiaoguang Wang08369242020-11-03 14:15:59 +08006789 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006790 if (!list_empty(&ctx->iopoll_list))
6791 io_do_iopoll(ctx, &nr_events, 0);
6792
Pavel Begunkov3b763ba2021-04-18 14:52:08 +01006793 /*
6794 * Don't submit if refs are dying, good for io_uring_register(),
6795 * but also it is relied upon by io_ring_exit_work()
6796 */
Pavel Begunkov0298ef92021-03-08 13:20:57 +00006797 if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
6798 !(ctx->flags & IORING_SETUP_R_DISABLED))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006799 ret = io_submit_sqes(ctx, to_submit);
6800 mutex_unlock(&ctx->uring_lock);
Jens Axboe90554202020-09-03 12:12:41 -06006801
Pavel Begunkovacfb3812021-05-16 22:58:03 +01006802 if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
6803 wake_up(&ctx->sqo_sq_wait);
6804 }
Jens Axboe90554202020-09-03 12:12:41 -06006805
Xiaoguang Wang08369242020-11-03 14:15:59 +08006806 return ret;
6807}
6808
6809static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6810{
6811 struct io_ring_ctx *ctx;
6812 unsigned sq_thread_idle = 0;
6813
Pavel Begunkovc9dca272021-03-10 13:13:55 +00006814 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6815 sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006816 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006817}
6818
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006819static bool io_sqd_handle_event(struct io_sq_data *sqd)
6820{
6821 bool did_sig = false;
6822 struct ksignal ksig;
6823
6824 if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
6825 signal_pending(current)) {
6826 mutex_unlock(&sqd->lock);
6827 if (signal_pending(current))
6828 did_sig = get_signal(&ksig);
6829 cond_resched();
6830 mutex_lock(&sqd->lock);
6831 }
6832 io_run_task_work();
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006833 return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
6834}
6835
Jens Axboe6c271ce2019-01-10 11:22:30 -07006836static int io_sq_thread(void *data)
6837{
Jens Axboe69fb2132020-09-14 11:16:23 -06006838 struct io_sq_data *sqd = data;
6839 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006840 unsigned long timeout = 0;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006841 char buf[TASK_COMM_LEN];
Xiaoguang Wang08369242020-11-03 14:15:59 +08006842 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006843
Pavel Begunkov696ee882021-04-01 09:55:04 +01006844 snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006845 set_task_comm(current, buf);
Jens Axboe28cea78a2020-09-14 10:51:17 -06006846
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006847 if (sqd->sq_cpu != -1)
6848 set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
6849 else
6850 set_cpus_allowed_ptr(current, cpu_online_mask);
6851 current->flags |= PF_NO_SETAFFINITY;
6852
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00006853 mutex_lock(&sqd->lock);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006854 while (1) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006855 int ret;
6856 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006857
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006858 if (io_sqd_events_pending(sqd) || signal_pending(current)) {
6859 if (io_sqd_handle_event(sqd))
Pavel Begunkovc7d95612021-04-13 11:43:00 +01006860 break;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006861 timeout = jiffies + sqd->sq_thread_idle;
Pavel Begunkov7d41e852021-03-10 13:13:54 +00006862 continue;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006863 }
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006864
Xiaoguang Wang08369242020-11-03 14:15:59 +08006865 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006866 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006867 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01006868 const struct cred *creds = NULL;
6869
6870 if (ctx->sq_creds != current_cred())
6871 creds = override_creds(ctx->sq_creds);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006872 ret = __io_sq_thread(ctx, cap_entries);
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01006873 if (creds)
6874 revert_creds(creds);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006875 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
6876 sqt_spin = true;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006877 }
6878
Xiaoguang Wang08369242020-11-03 14:15:59 +08006879 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006880 io_run_task_work();
6881 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08006882 if (sqt_spin)
6883 timeout = jiffies + sqd->sq_thread_idle;
6884 continue;
6885 }
6886
Xiaoguang Wang08369242020-11-03 14:15:59 +08006887 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
Pavel Begunkove4b6d902021-05-16 22:58:00 +01006888 if (!io_sqd_events_pending(sqd)) {
Hao Xu724cb4f2021-04-21 23:19:11 +08006889 needs_sched = true;
6890 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
Pavel Begunkovaaa9f0f2021-05-16 22:58:01 +01006891 io_ring_set_wakeup_flag(ctx);
6892
Hao Xu724cb4f2021-04-21 23:19:11 +08006893 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6894 !list_empty_careful(&ctx->iopoll_list)) {
6895 needs_sched = false;
6896 break;
6897 }
6898 if (io_sqring_entries(ctx)) {
6899 needs_sched = false;
6900 break;
6901 }
6902 }
6903
6904 if (needs_sched) {
6905 mutex_unlock(&sqd->lock);
6906 schedule();
6907 mutex_lock(&sqd->lock);
6908 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006909 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6910 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006911 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006912
6913 finish_wait(&sqd->wait, &wait);
6914 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006915 }
6916
Pavel Begunkov734551d2021-04-18 14:52:09 +01006917 io_uring_cancel_sqpoll(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006918 sqd->thread = NULL;
Jens Axboe05962f92021-03-06 13:58:48 -07006919 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Jens Axboe5f3f26f2021-02-25 10:17:46 -07006920 io_ring_set_wakeup_flag(ctx);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00006921 io_run_task_work();
Pavel Begunkov734551d2021-04-18 14:52:09 +01006922 mutex_unlock(&sqd->lock);
6923
Jens Axboe37d1e2e2021-02-17 21:03:43 -07006924 complete(&sqd->exited);
6925 do_exit(0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006926}
6927
Jens Axboebda52162019-09-24 13:47:15 -06006928struct io_wait_queue {
6929 struct wait_queue_entry wq;
6930 struct io_ring_ctx *ctx;
6931 unsigned to_wait;
6932 unsigned nr_timeouts;
6933};
6934
Pavel Begunkov6c503152021-01-04 20:36:36 +00006935static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006936{
6937 struct io_ring_ctx *ctx = iowq->ctx;
6938
6939 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006940 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006941 * started waiting. For timeouts, we always want to return to userspace,
6942 * regardless of event count.
6943 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00006944 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006945 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6946}
6947
6948static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6949 int wake_flags, void *key)
6950{
6951 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6952 wq);
6953
Pavel Begunkov6c503152021-01-04 20:36:36 +00006954 /*
6955 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6956 * the task, and the next invocation will do it.
6957 */
6958 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6959 return autoremove_wake_function(curr, mode, wake_flags, key);
6960 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006961}
6962
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006963static int io_run_task_work_sig(void)
6964{
6965 if (io_run_task_work())
6966 return 1;
6967 if (!signal_pending(current))
6968 return 0;
Jens Axboe0b8cfa92021-03-21 14:16:08 -06006969 if (test_thread_flag(TIF_NOTIFY_SIGNAL))
Jens Axboe792ee0f62020-10-22 20:17:18 -06006970 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006971 return -EINTR;
6972}
6973
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006974/* when returns >0, the caller should retry */
6975static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
6976 struct io_wait_queue *iowq,
6977 signed long *timeout)
6978{
6979 int ret;
6980
6981 /* make sure we run task_work before checking for signals */
6982 ret = io_run_task_work_sig();
6983 if (ret || io_should_wake(iowq))
6984 return ret;
6985 /* let the caller flush overflows, retry */
6986 if (test_bit(0, &ctx->cq_check_overflow))
6987 return 1;
6988
6989 *timeout = schedule_timeout(*timeout);
6990 return !*timeout ? -ETIME : 1;
6991}
6992
Jens Axboe2b188cc2019-01-07 10:46:33 -07006993/*
6994 * Wait until events become available, if we don't already have some. The
6995 * application must reap them itself, as they reside on the shared cq ring.
6996 */
6997static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08006998 const sigset_t __user *sig, size_t sigsz,
6999 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007000{
Jens Axboebda52162019-09-24 13:47:15 -06007001 struct io_wait_queue iowq = {
7002 .wq = {
7003 .private = current,
7004 .func = io_wake_function,
7005 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7006 },
7007 .ctx = ctx,
7008 .to_wait = min_events,
7009 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007010 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007011 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7012 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007013
Jens Axboeb41e9852020-02-17 09:52:41 -07007014 do {
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00007015 io_cqring_overflow_flush(ctx, false);
Pavel Begunkov6c503152021-01-04 20:36:36 +00007016 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007017 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007018 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007019 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007020 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007021
7022 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007023#ifdef CONFIG_COMPAT
7024 if (in_compat_syscall())
7025 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007026 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007027 else
7028#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007029 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007030
Jens Axboe2b188cc2019-01-07 10:46:33 -07007031 if (ret)
7032 return ret;
7033 }
7034
Hao Xuc73ebb62020-11-03 10:54:37 +08007035 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007036 struct timespec64 ts;
7037
Hao Xuc73ebb62020-11-03 10:54:37 +08007038 if (get_timespec64(&ts, uts))
7039 return -EFAULT;
7040 timeout = timespec64_to_jiffies(&ts);
7041 }
7042
Jens Axboebda52162019-09-24 13:47:15 -06007043 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007044 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007045 do {
Jens Axboeca0a2652021-03-04 17:15:48 -07007046 /* if we can't even flush overflow, don't wait for more */
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00007047 if (!io_cqring_overflow_flush(ctx, false)) {
Jens Axboeca0a2652021-03-04 17:15:48 -07007048 ret = -EBUSY;
7049 break;
7050 }
Jens Axboebda52162019-09-24 13:47:15 -06007051 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7052 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007053 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7054 finish_wait(&ctx->wait, &iowq.wq);
Jens Axboeca0a2652021-03-04 17:15:48 -07007055 cond_resched();
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007056 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007057
Jens Axboeb7db41c2020-07-04 08:55:50 -06007058 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007059
Hristo Venev75b28af2019-08-26 17:23:46 +00007060 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007061}
7062
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007063static void io_free_file_tables(struct io_file_table *table, unsigned nr_files)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007064{
7065 unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE);
7066
7067 for (i = 0; i < nr_tables; i++)
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007068 kfree(table->files[i]);
7069 kfree(table->files);
7070 table->files = NULL;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007071}
7072
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007073static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007074{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007075 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007076}
7077
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007078static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07007079{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007080 spin_unlock_bh(&ctx->rsrc_ref_lock);
7081}
7082
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007083static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
7084{
7085 percpu_ref_exit(&ref_node->refs);
7086 kfree(ref_node);
7087}
7088
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007089static void io_rsrc_node_switch(struct io_ring_ctx *ctx,
7090 struct io_rsrc_data *data_to_kill)
Jens Axboe6b063142019-01-10 22:13:58 -07007091{
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007092 WARN_ON_ONCE(!ctx->rsrc_backup_node);
7093 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007094
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007095 if (data_to_kill) {
7096 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007097
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007098 rsrc_node->rsrc_data = data_to_kill;
7099 io_rsrc_ref_lock(ctx);
7100 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
7101 io_rsrc_ref_unlock(ctx);
Pavel Begunkov82fbcfa2021-04-01 15:43:43 +01007102
Pavel Begunkov3e942492021-04-11 01:46:34 +01007103 atomic_inc(&data_to_kill->refs);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007104 percpu_ref_kill(&rsrc_node->refs);
7105 ctx->rsrc_node = NULL;
7106 }
7107
7108 if (!ctx->rsrc_node) {
7109 ctx->rsrc_node = ctx->rsrc_backup_node;
7110 ctx->rsrc_backup_node = NULL;
7111 }
Jens Axboe6b063142019-01-10 22:13:58 -07007112}
7113
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007114static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007115{
7116 if (ctx->rsrc_backup_node)
7117 return 0;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007118 ctx->rsrc_backup_node = io_rsrc_node_alloc(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007119 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
7120}
7121
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007122static int io_rsrc_ref_quiesce(struct io_rsrc_data *data, struct io_ring_ctx *ctx)
Hao Xu8bad28d2021-02-19 17:19:36 +08007123{
7124 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007125
Pavel Begunkov215c3902021-04-01 15:43:48 +01007126 /* As we may drop ->uring_lock, other task may have started quiesce */
Hao Xu8bad28d2021-02-19 17:19:36 +08007127 if (data->quiesce)
7128 return -ENXIO;
7129
7130 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007131 do {
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007132 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007133 if (ret)
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007134 break;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007135 io_rsrc_node_switch(ctx, data);
7136
Pavel Begunkov3e942492021-04-11 01:46:34 +01007137 /* kill initial ref, already quiesced if zero */
7138 if (atomic_dec_and_test(&data->refs))
7139 break;
Hao Xu8bad28d2021-02-19 17:19:36 +08007140 flush_delayed_work(&ctx->rsrc_put_work);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007141 ret = wait_for_completion_interruptible(&data->done);
7142 if (!ret)
7143 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007144
Pavel Begunkov3e942492021-04-11 01:46:34 +01007145 atomic_inc(&data->refs);
7146 /* wait for all works potentially completing data->done */
7147 flush_delayed_work(&ctx->rsrc_put_work);
Jens Axboecb5e1b82021-02-25 07:37:35 -07007148 reinit_completion(&data->done);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00007149
Hao Xu8bad28d2021-02-19 17:19:36 +08007150 mutex_unlock(&ctx->uring_lock);
7151 ret = io_run_task_work_sig();
7152 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007153 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007154 data->quiesce = false;
7155
Hao Xu8bad28d2021-02-19 17:19:36 +08007156 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007157}
7158
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007159static void io_rsrc_data_free(struct io_rsrc_data *data)
7160{
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007161 kvfree(data->tags);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007162 kfree(data);
7163}
7164
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007165static struct io_rsrc_data *io_rsrc_data_alloc(struct io_ring_ctx *ctx,
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007166 rsrc_put_fn *do_put,
7167 unsigned nr)
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007168{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007169 struct io_rsrc_data *data;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007170
7171 data = kzalloc(sizeof(*data), GFP_KERNEL);
7172 if (!data)
7173 return NULL;
7174
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007175 data->tags = kvcalloc(nr, sizeof(*data->tags), GFP_KERNEL);
7176 if (!data->tags) {
7177 kfree(data);
7178 return NULL;
7179 }
7180
Pavel Begunkov3e942492021-04-11 01:46:34 +01007181 atomic_set(&data->refs, 1);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007182 data->ctx = ctx;
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007183 data->do_put = do_put;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007184 init_completion(&data->done);
7185 return data;
7186}
7187
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007188static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7189{
Jens Axboe06058632019-04-13 09:26:03 -06007190#if defined(CONFIG_UNIX)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007191 if (ctx->ring_sock) {
7192 struct sock *sock = ctx->ring_sock->sk;
7193 struct sk_buff *skb;
7194
7195 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
Jens Axboe6b063142019-01-10 22:13:58 -07007196 kfree_skb(skb);
7197 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007198#else
7199 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07007200
7201 for (i = 0; i < ctx->nr_user_files; i++) {
7202 struct file *file;
7203
7204 file = io_file_from_index(ctx, i);
7205 if (file)
7206 fput(file);
7207 }
7208#endif
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007209 io_free_file_tables(&ctx->file_table, ctx->nr_user_files);
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007210 io_rsrc_data_free(ctx->file_data);
Pavel Begunkovfff4db72021-04-25 14:32:15 +01007211 ctx->file_data = NULL;
7212 ctx->nr_user_files = 0;
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007213}
7214
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007215static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7216{
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007217 int ret;
7218
Pavel Begunkov08480402021-04-13 02:58:38 +01007219 if (!ctx->file_data)
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007220 return -ENXIO;
Pavel Begunkov08480402021-04-13 02:58:38 +01007221 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
7222 if (!ret)
7223 __io_sqe_files_unregister(ctx);
7224 return ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007225}
7226
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007227static void io_sq_thread_unpark(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007228 __releases(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007229{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007230 WARN_ON_ONCE(sqd->thread == current);
7231
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007232 /*
7233 * Do the dance but not conditional clear_bit() because it'd race with
7234 * other threads incrementing park_pending and setting the bit.
7235 */
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007236 clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007237 if (atomic_dec_return(&sqd->park_pending))
7238 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007239 mutex_unlock(&sqd->lock);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007240}
7241
Jens Axboe86e0d672021-03-05 08:44:39 -07007242static void io_sq_thread_park(struct io_sq_data *sqd)
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007243 __acquires(&sqd->lock)
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007244{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007245 WARN_ON_ONCE(sqd->thread == current);
7246
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007247 atomic_inc(&sqd->park_pending);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007248 set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007249 mutex_lock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007250 if (sqd->thread)
Jens Axboe86e0d672021-03-05 08:44:39 -07007251 wake_up_process(sqd->thread);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007252}
7253
7254static void io_sq_thread_stop(struct io_sq_data *sqd)
7255{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007256 WARN_ON_ONCE(sqd->thread == current);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007257 WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007258
Jens Axboe05962f92021-03-06 13:58:48 -07007259 set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
Pavel Begunkov88885f62021-04-11 01:46:38 +01007260 mutex_lock(&sqd->lock);
Jens Axboee8f98f242021-03-09 16:32:13 -07007261 if (sqd->thread)
7262 wake_up_process(sqd->thread);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007263 mutex_unlock(&sqd->lock);
Jens Axboe05962f92021-03-06 13:58:48 -07007264 wait_for_completion(&sqd->exited);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007265}
7266
Jens Axboe534ca6d2020-09-02 13:52:19 -06007267static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007268{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007269 if (refcount_dec_and_test(&sqd->refs)) {
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007270 WARN_ON_ONCE(atomic_read(&sqd->park_pending));
7271
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007272 io_sq_thread_stop(sqd);
7273 kfree(sqd);
7274 }
7275}
7276
7277static void io_sq_thread_finish(struct io_ring_ctx *ctx)
7278{
7279 struct io_sq_data *sqd = ctx->sq_data;
7280
7281 if (sqd) {
Jens Axboe05962f92021-03-06 13:58:48 -07007282 io_sq_thread_park(sqd);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00007283 list_del_init(&ctx->sqd_list);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007284 io_sqd_update_thread_idle(sqd);
Jens Axboe05962f92021-03-06 13:58:48 -07007285 io_sq_thread_unpark(sqd);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007286
7287 io_put_sq_data(sqd);
7288 ctx->sq_data = NULL;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007289 }
7290}
7291
Jens Axboeaa061652020-09-02 14:50:27 -06007292static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7293{
7294 struct io_ring_ctx *ctx_attach;
7295 struct io_sq_data *sqd;
7296 struct fd f;
7297
7298 f = fdget(p->wq_fd);
7299 if (!f.file)
7300 return ERR_PTR(-ENXIO);
7301 if (f.file->f_op != &io_uring_fops) {
7302 fdput(f);
7303 return ERR_PTR(-EINVAL);
7304 }
7305
7306 ctx_attach = f.file->private_data;
7307 sqd = ctx_attach->sq_data;
7308 if (!sqd) {
7309 fdput(f);
7310 return ERR_PTR(-EINVAL);
7311 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007312 if (sqd->task_tgid != current->tgid) {
7313 fdput(f);
7314 return ERR_PTR(-EPERM);
7315 }
Jens Axboeaa061652020-09-02 14:50:27 -06007316
7317 refcount_inc(&sqd->refs);
7318 fdput(f);
7319 return sqd;
7320}
7321
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007322static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
7323 bool *attached)
Jens Axboe534ca6d2020-09-02 13:52:19 -06007324{
7325 struct io_sq_data *sqd;
7326
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007327 *attached = false;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007328 if (p->flags & IORING_SETUP_ATTACH_WQ) {
7329 sqd = io_attach_sq_data(p);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007330 if (!IS_ERR(sqd)) {
7331 *attached = true;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007332 return sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007333 }
Jens Axboe5c2469e2021-03-11 10:17:56 -07007334 /* fall through for EPERM case, setup new sqd/task */
7335 if (PTR_ERR(sqd) != -EPERM)
7336 return sqd;
7337 }
Jens Axboeaa061652020-09-02 14:50:27 -06007338
Jens Axboe534ca6d2020-09-02 13:52:19 -06007339 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7340 if (!sqd)
7341 return ERR_PTR(-ENOMEM);
7342
Pavel Begunkov9e138a42021-03-14 20:57:12 +00007343 atomic_set(&sqd->park_pending, 0);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007344 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007345 INIT_LIST_HEAD(&sqd->ctx_list);
Pavel Begunkov09a6f4e2021-03-14 20:57:10 +00007346 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007347 init_waitqueue_head(&sqd->wait);
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007348 init_completion(&sqd->exited);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007349 return sqd;
7350}
7351
Jens Axboe6b063142019-01-10 22:13:58 -07007352#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007353/*
7354 * Ensure the UNIX gc is aware of our file set, so we are certain that
7355 * the io_uring can be safely unregistered on process exit, even if we have
7356 * loops in the file referencing.
7357 */
7358static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7359{
7360 struct sock *sk = ctx->ring_sock->sk;
7361 struct scm_fp_list *fpl;
7362 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007363 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007364
Jens Axboe6b063142019-01-10 22:13:58 -07007365 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7366 if (!fpl)
7367 return -ENOMEM;
7368
7369 skb = alloc_skb(0, GFP_KERNEL);
7370 if (!skb) {
7371 kfree(fpl);
7372 return -ENOMEM;
7373 }
7374
7375 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007376
Jens Axboe08a45172019-10-03 08:11:03 -06007377 nr_files = 0;
Jens Axboe62e398b2021-02-21 16:19:37 -07007378 fpl->user = get_uid(current_user());
Jens Axboe6b063142019-01-10 22:13:58 -07007379 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007380 struct file *file = io_file_from_index(ctx, i + offset);
7381
7382 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007383 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007384 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007385 unix_inflight(fpl->user, fpl->fp[nr_files]);
7386 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007387 }
7388
Jens Axboe08a45172019-10-03 08:11:03 -06007389 if (nr_files) {
7390 fpl->max = SCM_MAX_FD;
7391 fpl->count = nr_files;
7392 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007393 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007394 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7395 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007396
Jens Axboe08a45172019-10-03 08:11:03 -06007397 for (i = 0; i < nr_files; i++)
7398 fput(fpl->fp[i]);
7399 } else {
7400 kfree_skb(skb);
7401 kfree(fpl);
7402 }
Jens Axboe6b063142019-01-10 22:13:58 -07007403
7404 return 0;
7405}
7406
7407/*
7408 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7409 * causes regular reference counting to break down. We rely on the UNIX
7410 * garbage collection to take care of this problem for us.
7411 */
7412static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7413{
7414 unsigned left, total;
7415 int ret = 0;
7416
7417 total = 0;
7418 left = ctx->nr_user_files;
7419 while (left) {
7420 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
7421
7422 ret = __io_sqe_files_scm(ctx, this_files, total);
7423 if (ret)
7424 break;
7425 left -= this_files;
7426 total += this_files;
7427 }
7428
7429 if (!ret)
7430 return 0;
7431
7432 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007433 struct file *file = io_file_from_index(ctx, total);
7434
7435 if (file)
7436 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007437 total++;
7438 }
7439
7440 return ret;
7441}
7442#else
7443static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7444{
7445 return 0;
7446}
7447#endif
7448
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007449static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007450{
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007451 unsigned i, nr_tables = DIV_ROUND_UP(nr_files, IORING_MAX_FILES_TABLE);
7452
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007453 table->files = kcalloc(nr_tables, sizeof(*table->files), GFP_KERNEL);
7454 if (!table->files)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007455 return false;
Jens Axboe65e19f52019-10-26 07:20:21 -06007456
7457 for (i = 0; i < nr_tables; i++) {
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007458 unsigned int this_files = min(nr_files, IORING_MAX_FILES_TABLE);
Jens Axboe65e19f52019-10-26 07:20:21 -06007459
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007460 table->files[i] = kcalloc(this_files, sizeof(*table->files[i]),
Jens Axboe65e19f52019-10-26 07:20:21 -06007461 GFP_KERNEL);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007462 if (!table->files[i])
Jens Axboe65e19f52019-10-26 07:20:21 -06007463 break;
7464 nr_files -= this_files;
7465 }
7466
7467 if (i == nr_tables)
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007468 return true;
Jens Axboe65e19f52019-10-26 07:20:21 -06007469
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007470 io_free_file_tables(table, nr_tables * IORING_MAX_FILES_TABLE);
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007471 return false;
Jens Axboe65e19f52019-10-26 07:20:21 -06007472}
7473
Pavel Begunkov47e90392021-04-01 15:43:56 +01007474static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007475{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007476 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007477#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007478 struct sock *sock = ctx->ring_sock->sk;
7479 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7480 struct sk_buff *skb;
7481 int i;
7482
7483 __skb_queue_head_init(&list);
7484
7485 /*
7486 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7487 * remove this entry and rearrange the file array.
7488 */
7489 skb = skb_dequeue(head);
7490 while (skb) {
7491 struct scm_fp_list *fp;
7492
7493 fp = UNIXCB(skb).fp;
7494 for (i = 0; i < fp->count; i++) {
7495 int left;
7496
7497 if (fp->fp[i] != file)
7498 continue;
7499
7500 unix_notinflight(fp->user, fp->fp[i]);
7501 left = fp->count - 1 - i;
7502 if (left) {
7503 memmove(&fp->fp[i], &fp->fp[i + 1],
7504 left * sizeof(struct file *));
7505 }
7506 fp->count--;
7507 if (!fp->count) {
7508 kfree_skb(skb);
7509 skb = NULL;
7510 } else {
7511 __skb_queue_tail(&list, skb);
7512 }
7513 fput(file);
7514 file = NULL;
7515 break;
7516 }
7517
7518 if (!file)
7519 break;
7520
7521 __skb_queue_tail(&list, skb);
7522
7523 skb = skb_dequeue(head);
7524 }
7525
7526 if (skb_peek(&list)) {
7527 spin_lock_irq(&head->lock);
7528 while ((skb = __skb_dequeue(&list)) != NULL)
7529 __skb_queue_tail(head, skb);
7530 spin_unlock_irq(&head->lock);
7531 }
7532#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007533 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007534#endif
7535}
7536
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007537static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007538{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007539 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007540 struct io_ring_ctx *ctx = rsrc_data->ctx;
7541 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007542
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007543 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7544 list_del(&prsrc->list);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007545
7546 if (prsrc->tag) {
7547 bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL;
7548 unsigned long flags;
7549
7550 io_ring_submit_lock(ctx, lock_ring);
7551 spin_lock_irqsave(&ctx->completion_lock, flags);
7552 io_cqring_fill_event(ctx, prsrc->tag, 0, 0);
Pavel Begunkov2840f712021-04-27 16:13:51 +01007553 ctx->cq_extra++;
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007554 io_commit_cqring(ctx);
7555 spin_unlock_irqrestore(&ctx->completion_lock, flags);
7556 io_cqring_ev_posted(ctx);
7557 io_ring_submit_unlock(ctx, lock_ring);
7558 }
7559
Pavel Begunkov40ae0ff2021-04-01 15:43:44 +01007560 rsrc_data->do_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007561 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007562 }
7563
Pavel Begunkov28a9fe22021-04-01 15:43:47 +01007564 io_rsrc_node_destroy(ref_node);
Pavel Begunkov3e942492021-04-11 01:46:34 +01007565 if (atomic_dec_and_test(&rsrc_data->refs))
7566 complete(&rsrc_data->done);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007567}
7568
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007569static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007570{
7571 struct io_ring_ctx *ctx;
7572 struct llist_node *node;
7573
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007574 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7575 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007576
7577 while (node) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007578 struct io_rsrc_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007579 struct llist_node *next = node->next;
7580
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007581 ref_node = llist_entry(node, struct io_rsrc_node, llist);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007582 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007583 node = next;
7584 }
7585}
7586
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007587static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007588{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007589 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
Pavel Begunkov3e942492021-04-11 01:46:34 +01007590 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007591 bool first_add = false;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007592
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007593 io_rsrc_ref_lock(ctx);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007594 node->done = true;
Pavel Begunkove2978222020-11-18 14:56:26 +00007595
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007596 while (!list_empty(&ctx->rsrc_ref_list)) {
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007597 node = list_first_entry(&ctx->rsrc_ref_list,
7598 struct io_rsrc_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007599 /* recycle ref nodes in order */
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007600 if (!node->done)
Pavel Begunkove2978222020-11-18 14:56:26 +00007601 break;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007602 list_del(&node->node);
7603 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007604 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007605 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007606
Pavel Begunkov3e942492021-04-11 01:46:34 +01007607 if (first_add)
7608 mod_delayed_work(system_wq, &ctx->rsrc_put_work, HZ);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007609}
7610
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007611static struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007612{
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007613 struct io_rsrc_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007614
7615 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7616 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007617 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007618
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007619 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007620 0, GFP_KERNEL)) {
7621 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007622 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007623 }
7624 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007625 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007626 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007627 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007628}
7629
Jens Axboe05f3fb32019-12-09 11:22:50 -07007630static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov792e3582021-04-25 14:32:21 +01007631 unsigned nr_args, u64 __user *tags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007632{
7633 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007634 struct file *file;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007635 int fd, ret;
Pavel Begunkov846a4ef2021-04-01 15:44:03 +01007636 unsigned i;
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007637 struct io_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007638
7639 if (ctx->file_data)
7640 return -EBUSY;
7641 if (!nr_args)
7642 return -EINVAL;
7643 if (nr_args > IORING_MAX_FIXED_FILES)
7644 return -EMFILE;
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007645 ret = io_rsrc_node_switch_start(ctx);
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007646 if (ret)
7647 return ret;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007648
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007649 file_data = io_rsrc_data_alloc(ctx, io_rsrc_file_put, nr_args);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007650 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007651 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007652 ctx->file_data = file_data;
Pavel Begunkovf3baed32021-04-01 15:43:42 +01007653 ret = -ENOMEM;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007654 if (!io_alloc_file_tables(&ctx->file_table, nr_args))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007655 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007656
Jens Axboe05f3fb32019-12-09 11:22:50 -07007657 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov792e3582021-04-25 14:32:21 +01007658 u64 tag = 0;
7659
7660 if ((tags && copy_from_user(&tag, &tags[i], sizeof(tag))) ||
7661 copy_from_user(&fd, &fds[i], sizeof(fd))) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007662 ret = -EFAULT;
7663 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007664 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007665 /* allow sparse sets */
Pavel Begunkov792e3582021-04-25 14:32:21 +01007666 if (fd == -1) {
7667 ret = -EINVAL;
7668 if (unlikely(tag))
7669 goto out_fput;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007670 continue;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007671 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007672
Jens Axboe05f3fb32019-12-09 11:22:50 -07007673 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007674 ret = -EBADF;
Pavel Begunkov792e3582021-04-25 14:32:21 +01007675 if (unlikely(!file))
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007676 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007677
7678 /*
7679 * Don't allow io_uring instances to be registered. If UNIX
7680 * isn't enabled, then this causes a reference cycle and this
7681 * instance can never get freed. If UNIX is enabled we'll
7682 * handle it just fine, but there's still no point in allowing
7683 * a ring fd as it doesn't support regular read/write anyway.
7684 */
7685 if (file->f_op == &io_uring_fops) {
7686 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007687 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007688 }
Pavel Begunkov792e3582021-04-25 14:32:21 +01007689 ctx->file_data->tags[i] = tag;
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007690 io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007691 }
7692
Jens Axboe05f3fb32019-12-09 11:22:50 -07007693 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007694 if (ret) {
Pavel Begunkov08480402021-04-13 02:58:38 +01007695 __io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007696 return ret;
7697 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007698
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007699 io_rsrc_node_switch(ctx, NULL);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007700 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007701out_fput:
7702 for (i = 0; i < ctx->nr_user_files; i++) {
7703 file = io_file_from_index(ctx, i);
7704 if (file)
7705 fput(file);
7706 }
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007707 io_free_file_tables(&ctx->file_table, nr_args);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007708 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007709out_free:
Pavel Begunkov44b31f22021-04-25 14:32:16 +01007710 io_rsrc_data_free(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007711 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007712 return ret;
7713}
7714
Jens Axboec3a31e62019-10-03 13:59:56 -06007715static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7716 int index)
7717{
7718#if defined(CONFIG_UNIX)
7719 struct sock *sock = ctx->ring_sock->sk;
7720 struct sk_buff_head *head = &sock->sk_receive_queue;
7721 struct sk_buff *skb;
7722
7723 /*
7724 * See if we can merge this file into an existing skb SCM_RIGHTS
7725 * file set. If there's no room, fall back to allocating a new skb
7726 * and filling it in.
7727 */
7728 spin_lock_irq(&head->lock);
7729 skb = skb_peek(head);
7730 if (skb) {
7731 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7732
7733 if (fpl->count < SCM_MAX_FD) {
7734 __skb_unlink(skb, head);
7735 spin_unlock_irq(&head->lock);
7736 fpl->fp[fpl->count] = get_file(file);
7737 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7738 fpl->count++;
7739 spin_lock_irq(&head->lock);
7740 __skb_queue_head(head, skb);
7741 } else {
7742 skb = NULL;
7743 }
7744 }
7745 spin_unlock_irq(&head->lock);
7746
7747 if (skb) {
7748 fput(file);
7749 return 0;
7750 }
7751
7752 return __io_sqe_files_scm(ctx, 1, index);
7753#else
7754 return 0;
7755#endif
7756}
7757
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007758static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
Pavel Begunkove7c78372021-04-01 15:43:45 +01007759 struct io_rsrc_node *node, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007760{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007761 struct io_rsrc_put *prsrc;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007762
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007763 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7764 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007765 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007766
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007767 prsrc->tag = data->tags[idx];
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007768 prsrc->rsrc = rsrc;
Pavel Begunkove7c78372021-04-01 15:43:45 +01007769 list_add(&prsrc->list, &node->rsrc_list);
Hillf Dantona5318d32020-03-23 17:47:15 +08007770 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007771}
7772
7773static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007774 struct io_uring_rsrc_update2 *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007775 unsigned nr_args)
7776{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007777 u64 __user *tags = u64_to_user_ptr(up->tags);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007778 __s32 __user *fds = u64_to_user_ptr(up->data);
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01007779 struct io_rsrc_data *data = ctx->file_data;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007780 struct io_fixed_file *file_slot;
7781 struct file *file;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007782 int fd, i, err = 0;
7783 unsigned int done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007784 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007785
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01007786 if (!ctx->file_data)
7787 return -ENXIO;
7788 if (up->offset + nr_args > ctx->nr_user_files)
Jens Axboec3a31e62019-10-03 13:59:56 -06007789 return -EINVAL;
7790
Pavel Begunkov67973b92021-01-26 13:51:09 +00007791 for (done = 0; done < nr_args; done++) {
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007792 u64 tag = 0;
7793
7794 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
7795 copy_from_user(&fd, &fds[done], sizeof(fd))) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007796 err = -EFAULT;
7797 break;
7798 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007799 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
7800 err = -EINVAL;
7801 break;
7802 }
noah4e0377a2021-01-26 15:23:28 -05007803 if (fd == IORING_REGISTER_FILES_SKIP)
7804 continue;
7805
Pavel Begunkov67973b92021-01-26 13:51:09 +00007806 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovaeca2412021-04-11 01:46:37 +01007807 file_slot = io_fixed_file_slot(&ctx->file_table, i);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007808
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007809 if (file_slot->file_ptr) {
7810 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
Pavel Begunkovb60c8dc2021-04-25 14:32:18 +01007811 err = io_queue_rsrc_removal(data, up->offset + done,
7812 ctx->rsrc_node, file);
Hillf Dantona5318d32020-03-23 17:47:15 +08007813 if (err)
7814 break;
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007815 file_slot->file_ptr = 0;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007816 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007817 }
7818 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007819 file = fget(fd);
7820 if (!file) {
7821 err = -EBADF;
7822 break;
7823 }
7824 /*
7825 * Don't allow io_uring instances to be registered. If
7826 * UNIX isn't enabled, then this causes a reference
7827 * cycle and this instance can never get freed. If UNIX
7828 * is enabled we'll handle it just fine, but there's
7829 * still no point in allowing a ring fd as it doesn't
7830 * support regular read/write anyway.
7831 */
7832 if (file->f_op == &io_uring_fops) {
7833 fput(file);
7834 err = -EBADF;
7835 break;
7836 }
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01007837 data->tags[up->offset + done] = tag;
Pavel Begunkov9a321c92021-04-01 15:44:01 +01007838 io_fixed_file_set(file_slot, file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007839 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007840 if (err) {
Pavel Begunkova04b0ac2021-04-01 15:44:04 +01007841 file_slot->file_ptr = 0;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007842 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007843 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007844 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007845 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007846 }
7847
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01007848 if (needs_switch)
7849 io_rsrc_node_switch(ctx, data);
Jens Axboec3a31e62019-10-03 13:59:56 -06007850 return done ? done : err;
7851}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007852
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007853static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007854{
7855 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7856
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007857 req = io_put_req_find_next(req);
7858 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07007859}
7860
Jens Axboe685fe7f2021-03-08 09:37:51 -07007861static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
7862 struct task_struct *task)
Pavel Begunkov24369c22020-01-28 03:15:48 +03007863{
Jens Axboee9418942021-02-19 12:33:30 -07007864 struct io_wq_hash *hash;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007865 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007866 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007867
Jens Axboee9418942021-02-19 12:33:30 -07007868 hash = ctx->hash_map;
7869 if (!hash) {
7870 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
7871 if (!hash)
7872 return ERR_PTR(-ENOMEM);
7873 refcount_set(&hash->refs, 1);
7874 init_waitqueue_head(&hash->wait);
7875 ctx->hash_map = hash;
7876 }
7877
7878 data.hash = hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -07007879 data.task = task;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007880 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007881 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007882
Jens Axboed25e3a32021-02-16 11:41:41 -07007883 /* Do QD, or 4 * CPUS, whatever is smallest */
7884 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03007885
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007886 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03007887}
7888
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007889static int io_uring_alloc_task_context(struct task_struct *task,
7890 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06007891{
7892 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007893 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007894
7895 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7896 if (unlikely(!tctx))
7897 return -ENOMEM;
7898
Jens Axboed8a6df12020-10-15 16:24:45 -06007899 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7900 if (unlikely(ret)) {
7901 kfree(tctx);
7902 return ret;
7903 }
7904
Jens Axboe685fe7f2021-03-08 09:37:51 -07007905 tctx->io_wq = io_init_wq_offload(ctx, task);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007906 if (IS_ERR(tctx->io_wq)) {
7907 ret = PTR_ERR(tctx->io_wq);
7908 percpu_counter_destroy(&tctx->inflight);
7909 kfree(tctx);
7910 return ret;
7911 }
7912
Jens Axboe0f212202020-09-13 13:09:39 -06007913 xa_init(&tctx->xa);
7914 init_waitqueue_head(&tctx->wait);
7915 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007916 atomic_set(&tctx->in_idle, 0);
Pavel Begunkovb303fe22021-04-11 01:46:26 +01007917 atomic_set(&tctx->inflight_tracked, 0);
Jens Axboe0f212202020-09-13 13:09:39 -06007918 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00007919 spin_lock_init(&tctx->task_lock);
7920 INIT_WQ_LIST(&tctx->task_list);
7921 tctx->task_state = 0;
7922 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06007923 return 0;
7924}
7925
7926void __io_uring_free(struct task_struct *tsk)
7927{
7928 struct io_uring_task *tctx = tsk->io_uring;
7929
7930 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Pavel Begunkovef8eaa42021-02-27 11:16:45 +00007931 WARN_ON_ONCE(tctx->io_wq);
7932
Jens Axboed8a6df12020-10-15 16:24:45 -06007933 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007934 kfree(tctx);
7935 tsk->io_uring = NULL;
7936}
7937
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007938static int io_sq_offload_create(struct io_ring_ctx *ctx,
7939 struct io_uring_params *p)
Jens Axboe6b063142019-01-10 22:13:58 -07007940{
7941 int ret;
7942
Jens Axboed25e3a32021-02-16 11:41:41 -07007943 /* Retain compatibility with failing for an invalid attach attempt */
7944 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
7945 IORING_SETUP_ATTACH_WQ) {
7946 struct fd f;
7947
7948 f = fdget(p->wq_fd);
7949 if (!f.file)
7950 return -ENXIO;
Jens Axboed25e3a32021-02-16 11:41:41 -07007951 fdput(f);
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01007952 if (f.file->f_op != &io_uring_fops)
7953 return -EINVAL;
Jens Axboed25e3a32021-02-16 11:41:41 -07007954 }
Jens Axboe6b063142019-01-10 22:13:58 -07007955 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe46fe18b2021-03-04 12:39:36 -07007956 struct task_struct *tsk;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007957 struct io_sq_data *sqd;
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007958 bool attached;
Jens Axboe534ca6d2020-09-02 13:52:19 -06007959
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007960 sqd = io_get_sq_data(p, &attached);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007961 if (IS_ERR(sqd)) {
7962 ret = PTR_ERR(sqd);
7963 goto err;
7964 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007965
Stefan Metzmacher7c30f36a2021-03-07 11:54:28 +01007966 ctx->sq_creds = get_current_cred();
Jens Axboe534ca6d2020-09-02 13:52:19 -06007967 ctx->sq_data = sqd;
Jens Axboe6b063142019-01-10 22:13:58 -07007968 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7969 if (!ctx->sq_thread_idle)
7970 ctx->sq_thread_idle = HZ;
7971
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00007972 io_sq_thread_park(sqd);
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00007973 list_add(&ctx->sqd_list, &sqd->ctx_list);
7974 io_sqd_update_thread_idle(sqd);
Pavel Begunkov26984fb2021-03-11 23:29:37 +00007975 /* don't attach to a dying SQPOLL thread, would be racy */
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01007976 ret = (attached && !sqd->thread) ? -ENXIO : 0;
Pavel Begunkov78d7f6b2021-03-10 13:13:53 +00007977 io_sq_thread_unpark(sqd);
7978
Pavel Begunkovde75a3d2021-03-18 11:54:35 +00007979 if (ret < 0)
7980 goto err;
7981 if (attached)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007982 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06007983
Jens Axboe6b063142019-01-10 22:13:58 -07007984 if (p->flags & IORING_SETUP_SQ_AFF) {
7985 int cpu = p->sq_thread_cpu;
7986
7987 ret = -EINVAL;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01007988 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
Jens Axboee8f98f242021-03-09 16:32:13 -07007989 goto err_sqpoll;
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007990 sqd->sq_cpu = cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007991 } else {
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007992 sqd->sq_cpu = -1;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007993 }
Jens Axboe37d1e2e2021-02-17 21:03:43 -07007994
7995 sqd->task_pid = current->pid;
Jens Axboe5c2469e2021-03-11 10:17:56 -07007996 sqd->task_tgid = current->tgid;
Jens Axboe46fe18b2021-03-04 12:39:36 -07007997 tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
7998 if (IS_ERR(tsk)) {
7999 ret = PTR_ERR(tsk);
Jens Axboee8f98f242021-03-09 16:32:13 -07008000 goto err_sqpoll;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008001 }
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008002
Jens Axboe46fe18b2021-03-04 12:39:36 -07008003 sqd->thread = tsk;
Pavel Begunkov97a73a02021-03-08 17:30:54 +00008004 ret = io_uring_alloc_task_context(tsk, ctx);
Jens Axboe46fe18b2021-03-04 12:39:36 -07008005 wake_up_new_task(tsk);
Jens Axboe0f212202020-09-13 13:09:39 -06008006 if (ret)
8007 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008008 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8009 /* Can't have SQ_AFF without SQPOLL */
8010 ret = -EINVAL;
8011 goto err;
8012 }
8013
Jens Axboe2b188cc2019-01-07 10:46:33 -07008014 return 0;
Pavel Begunkovf2a48dd2021-04-20 12:03:33 +01008015err_sqpoll:
8016 complete(&ctx->sq_data->exited);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008017err:
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008018 io_sq_thread_finish(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008019 return ret;
8020}
8021
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008022static inline void __io_unaccount_mem(struct user_struct *user,
8023 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008024{
8025 atomic_long_sub(nr_pages, &user->locked_vm);
8026}
8027
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008028static inline int __io_account_mem(struct user_struct *user,
8029 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008030{
8031 unsigned long page_limit, cur_pages, new_pages;
8032
8033 /* Don't allow more pages than we can safely lock */
8034 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8035
8036 do {
8037 cur_pages = atomic_long_read(&user->locked_vm);
8038 new_pages = cur_pages + nr_pages;
8039 if (new_pages > page_limit)
8040 return -ENOMEM;
8041 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8042 new_pages) != cur_pages);
8043
8044 return 0;
8045}
8046
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008047static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008048{
Jens Axboe62e398b2021-02-21 16:19:37 -07008049 if (ctx->user)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008050 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008051
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008052 if (ctx->mm_account)
8053 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008054}
8055
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008056static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008057{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008058 int ret;
8059
Jens Axboe62e398b2021-02-21 16:19:37 -07008060 if (ctx->user) {
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008061 ret = __io_account_mem(ctx->user, nr_pages);
8062 if (ret)
8063 return ret;
8064 }
8065
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008066 if (ctx->mm_account)
8067 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008068
8069 return 0;
8070}
8071
Jens Axboe2b188cc2019-01-07 10:46:33 -07008072static void io_mem_free(void *ptr)
8073{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008074 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008075
Mark Rutland52e04ef2019-04-30 17:30:21 +01008076 if (!ptr)
8077 return;
8078
8079 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008080 if (put_page_testzero(page))
8081 free_compound_page(page);
8082}
8083
8084static void *io_mem_alloc(size_t size)
8085{
8086 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008087 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008088
8089 return (void *) __get_free_pages(gfp_flags, get_order(size));
8090}
8091
Hristo Venev75b28af2019-08-26 17:23:46 +00008092static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8093 size_t *sq_offset)
8094{
8095 struct io_rings *rings;
8096 size_t off, sq_array_size;
8097
8098 off = struct_size(rings, cqes, cq_entries);
8099 if (off == SIZE_MAX)
8100 return SIZE_MAX;
8101
8102#ifdef CONFIG_SMP
8103 off = ALIGN(off, SMP_CACHE_BYTES);
8104 if (off == 0)
8105 return SIZE_MAX;
8106#endif
8107
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008108 if (sq_offset)
8109 *sq_offset = off;
8110
Hristo Venev75b28af2019-08-26 17:23:46 +00008111 sq_array_size = array_size(sizeof(u32), sq_entries);
8112 if (sq_array_size == SIZE_MAX)
8113 return SIZE_MAX;
8114
8115 if (check_add_overflow(off, sq_array_size, &off))
8116 return SIZE_MAX;
8117
Hristo Venev75b28af2019-08-26 17:23:46 +00008118 return off;
8119}
8120
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008121static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008122{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008123 struct io_mapped_ubuf *imu = *slot;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008124 unsigned int i;
8125
Pavel Begunkov62248432021-04-28 13:11:29 +01008126 if (imu != ctx->dummy_ubuf) {
8127 for (i = 0; i < imu->nr_bvecs; i++)
8128 unpin_user_page(imu->bvec[i].bv_page);
8129 if (imu->acct_pages)
8130 io_unaccount_mem(ctx, imu->acct_pages);
8131 kvfree(imu);
8132 }
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008133 *slot = NULL;
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008134}
8135
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008136static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
8137{
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008138 io_buffer_unmap(ctx, &prsrc->buf);
8139 prsrc->buf = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008140}
8141
8142static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008143{
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008144 unsigned int i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008145
Pavel Begunkov7f61a1e2021-04-11 01:46:35 +01008146 for (i = 0; i < ctx->nr_user_bufs; i++)
8147 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
Jens Axboeedafcce2019-01-09 09:16:05 -07008148 kfree(ctx->user_bufs);
Zqiangbb6659c2021-04-30 16:25:15 +08008149 io_rsrc_data_free(ctx->buf_data);
Jens Axboeedafcce2019-01-09 09:16:05 -07008150 ctx->user_bufs = NULL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008151 ctx->buf_data = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008152 ctx->nr_user_bufs = 0;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008153}
8154
Jens Axboeedafcce2019-01-09 09:16:05 -07008155static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
8156{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008157 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008158
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008159 if (!ctx->buf_data)
Jens Axboeedafcce2019-01-09 09:16:05 -07008160 return -ENXIO;
8161
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008162 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
8163 if (!ret)
8164 __io_sqe_buffers_unregister(ctx);
8165 return ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07008166}
8167
8168static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8169 void __user *arg, unsigned index)
8170{
8171 struct iovec __user *src;
8172
8173#ifdef CONFIG_COMPAT
8174 if (ctx->compat) {
8175 struct compat_iovec __user *ciovs;
8176 struct compat_iovec ciov;
8177
8178 ciovs = (struct compat_iovec __user *) arg;
8179 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8180 return -EFAULT;
8181
Jens Axboed55e5f52019-12-11 16:12:15 -07008182 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008183 dst->iov_len = ciov.iov_len;
8184 return 0;
8185 }
8186#endif
8187 src = (struct iovec __user *) arg;
8188 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8189 return -EFAULT;
8190 return 0;
8191}
8192
Jens Axboede293932020-09-17 16:19:16 -06008193/*
8194 * Not super efficient, but this is just a registration time. And we do cache
8195 * the last compound head, so generally we'll only do a full search if we don't
8196 * match that one.
8197 *
8198 * We check if the given compound head page has already been accounted, to
8199 * avoid double accounting it. This allows us to account the full size of the
8200 * page, not just the constituent pages of a huge page.
8201 */
8202static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8203 int nr_pages, struct page *hpage)
8204{
8205 int i, j;
8206
8207 /* check current page array */
8208 for (i = 0; i < nr_pages; i++) {
8209 if (!PageCompound(pages[i]))
8210 continue;
8211 if (compound_head(pages[i]) == hpage)
8212 return true;
8213 }
8214
8215 /* check previously registered pages */
8216 for (i = 0; i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008217 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
Jens Axboede293932020-09-17 16:19:16 -06008218
8219 for (j = 0; j < imu->nr_bvecs; j++) {
8220 if (!PageCompound(imu->bvec[j].bv_page))
8221 continue;
8222 if (compound_head(imu->bvec[j].bv_page) == hpage)
8223 return true;
8224 }
8225 }
8226
8227 return false;
8228}
8229
8230static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8231 int nr_pages, struct io_mapped_ubuf *imu,
8232 struct page **last_hpage)
8233{
8234 int i, ret;
8235
Pavel Begunkov216e5832021-05-29 12:01:02 +01008236 imu->acct_pages = 0;
Jens Axboede293932020-09-17 16:19:16 -06008237 for (i = 0; i < nr_pages; i++) {
8238 if (!PageCompound(pages[i])) {
8239 imu->acct_pages++;
8240 } else {
8241 struct page *hpage;
8242
8243 hpage = compound_head(pages[i]);
8244 if (hpage == *last_hpage)
8245 continue;
8246 *last_hpage = hpage;
8247 if (headpage_already_acct(ctx, pages, i, hpage))
8248 continue;
8249 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8250 }
8251 }
8252
8253 if (!imu->acct_pages)
8254 return 0;
8255
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008256 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008257 if (ret)
8258 imu->acct_pages = 0;
8259 return ret;
8260}
8261
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008262static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008263 struct io_mapped_ubuf **pimu,
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008264 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008265{
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008266 struct io_mapped_ubuf *imu = NULL;
Jens Axboeedafcce2019-01-09 09:16:05 -07008267 struct vm_area_struct **vmas = NULL;
8268 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008269 unsigned long off, start, end, ubuf;
8270 size_t size;
8271 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008272
Pavel Begunkov62248432021-04-28 13:11:29 +01008273 if (!iov->iov_base) {
8274 *pimu = ctx->dummy_ubuf;
8275 return 0;
8276 }
8277
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008278 ubuf = (unsigned long) iov->iov_base;
8279 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8280 start = ubuf >> PAGE_SHIFT;
8281 nr_pages = end - start;
8282
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008283 *pimu = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008284 ret = -ENOMEM;
8285
8286 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8287 if (!pages)
8288 goto done;
8289
8290 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8291 GFP_KERNEL);
8292 if (!vmas)
8293 goto done;
8294
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008295 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
Pavel Begunkova2b41982021-04-26 00:16:31 +01008296 if (!imu)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008297 goto done;
8298
8299 ret = 0;
8300 mmap_read_lock(current->mm);
8301 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8302 pages, vmas);
8303 if (pret == nr_pages) {
8304 /* don't support file backed memory */
8305 for (i = 0; i < nr_pages; i++) {
8306 struct vm_area_struct *vma = vmas[i];
8307
8308 if (vma->vm_file &&
8309 !is_file_hugepages(vma->vm_file)) {
8310 ret = -EOPNOTSUPP;
8311 break;
8312 }
8313 }
8314 } else {
8315 ret = pret < 0 ? pret : -EFAULT;
8316 }
8317 mmap_read_unlock(current->mm);
8318 if (ret) {
8319 /*
8320 * if we did partial map, or found file backed vmas,
8321 * release any pages we did get
8322 */
8323 if (pret > 0)
8324 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008325 goto done;
8326 }
8327
8328 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8329 if (ret) {
8330 unpin_user_pages(pages, pret);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008331 goto done;
8332 }
8333
8334 off = ubuf & ~PAGE_MASK;
8335 size = iov->iov_len;
8336 for (i = 0; i < nr_pages; i++) {
8337 size_t vec_len;
8338
8339 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8340 imu->bvec[i].bv_page = pages[i];
8341 imu->bvec[i].bv_len = vec_len;
8342 imu->bvec[i].bv_offset = off;
8343 off = 0;
8344 size -= vec_len;
8345 }
8346 /* store original address for later verification */
8347 imu->ubuf = ubuf;
Pavel Begunkov4751f532021-04-01 15:43:55 +01008348 imu->ubuf_end = ubuf + iov->iov_len;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008349 imu->nr_bvecs = nr_pages;
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008350 *pimu = imu;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008351 ret = 0;
8352done:
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008353 if (ret)
8354 kvfree(imu);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008355 kvfree(pages);
8356 kvfree(vmas);
8357 return ret;
8358}
8359
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008360static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008361{
Pavel Begunkov87094462021-04-11 01:46:36 +01008362 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
8363 return ctx->user_bufs ? 0 : -ENOMEM;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008364}
8365
8366static int io_buffer_validate(struct iovec *iov)
8367{
Pavel Begunkov50e96982021-03-24 22:59:01 +00008368 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
8369
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008370 /*
8371 * Don't impose further limits on the size and buffer
8372 * constraints here, we'll -EINVAL later when IO is
8373 * submitted if they are wrong.
8374 */
Pavel Begunkov62248432021-04-28 13:11:29 +01008375 if (!iov->iov_base)
8376 return iov->iov_len ? -EFAULT : 0;
8377 if (!iov->iov_len)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008378 return -EFAULT;
8379
8380 /* arbitrary limit, but we need something */
8381 if (iov->iov_len > SZ_1G)
8382 return -EFAULT;
8383
Pavel Begunkov50e96982021-03-24 22:59:01 +00008384 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
8385 return -EOVERFLOW;
8386
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008387 return 0;
8388}
8389
8390static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008391 unsigned int nr_args, u64 __user *tags)
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008392{
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008393 struct page *last_hpage = NULL;
8394 struct io_rsrc_data *data;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008395 int i, ret;
8396 struct iovec iov;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008397
Pavel Begunkov87094462021-04-11 01:46:36 +01008398 if (ctx->user_bufs)
8399 return -EBUSY;
Pavel Begunkov489809e2021-05-14 12:06:44 +01008400 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
Pavel Begunkov87094462021-04-11 01:46:36 +01008401 return -EINVAL;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008402 ret = io_rsrc_node_switch_start(ctx);
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008403 if (ret)
8404 return ret;
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008405 data = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, nr_args);
8406 if (!data)
8407 return -ENOMEM;
8408 ret = io_buffers_map_alloc(ctx, nr_args);
8409 if (ret) {
Zqiangbb6659c2021-04-30 16:25:15 +08008410 io_rsrc_data_free(data);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008411 return ret;
8412 }
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008413
Pavel Begunkov87094462021-04-11 01:46:36 +01008414 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008415 u64 tag = 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07008416
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008417 if (tags && copy_from_user(&tag, &tags[i], sizeof(tag))) {
8418 ret = -EFAULT;
8419 break;
8420 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008421 ret = io_copy_iov(ctx, &iov, arg, i);
8422 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008423 break;
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008424 ret = io_buffer_validate(&iov);
8425 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008426 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008427 if (!iov.iov_base && tag) {
8428 ret = -EINVAL;
8429 break;
8430 }
Jens Axboeedafcce2019-01-09 09:16:05 -07008431
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01008432 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
8433 &last_hpage);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008434 if (ret)
8435 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008436 data->tags[i] = tag;
Jens Axboeedafcce2019-01-09 09:16:05 -07008437 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008438
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008439 WARN_ON_ONCE(ctx->buf_data);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008440
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008441 ctx->buf_data = data;
8442 if (ret)
8443 __io_sqe_buffers_unregister(ctx);
8444 else
8445 io_rsrc_node_switch(ctx, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07008446 return ret;
8447}
8448
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008449static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
8450 struct io_uring_rsrc_update2 *up,
8451 unsigned int nr_args)
8452{
8453 u64 __user *tags = u64_to_user_ptr(up->tags);
8454 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008455 struct page *last_hpage = NULL;
8456 bool needs_switch = false;
8457 __u32 done;
8458 int i, err;
8459
8460 if (!ctx->buf_data)
8461 return -ENXIO;
8462 if (up->offset + nr_args > ctx->nr_user_bufs)
8463 return -EINVAL;
8464
8465 for (done = 0; done < nr_args; done++) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008466 struct io_mapped_ubuf *imu;
8467 int offset = up->offset + done;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008468 u64 tag = 0;
8469
8470 err = io_copy_iov(ctx, &iov, iovs, done);
8471 if (err)
8472 break;
8473 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
8474 err = -EFAULT;
8475 break;
8476 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008477 err = io_buffer_validate(&iov);
8478 if (err)
8479 break;
Colin Ian Kingcf3770e2021-04-29 11:46:02 +01008480 if (!iov.iov_base && tag) {
8481 err = -EINVAL;
8482 break;
8483 }
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008484 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
8485 if (err)
8486 break;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008487
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008488 i = array_index_nospec(offset, ctx->nr_user_bufs);
Pavel Begunkov62248432021-04-28 13:11:29 +01008489 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008490 err = io_queue_rsrc_removal(ctx->buf_data, offset,
8491 ctx->rsrc_node, ctx->user_bufs[i]);
8492 if (unlikely(err)) {
8493 io_buffer_unmap(ctx, &imu);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008494 break;
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008495 }
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008496 ctx->user_bufs[i] = NULL;
8497 needs_switch = true;
8498 }
8499
Pavel Begunkov0b8c0e72021-04-26 15:17:38 +01008500 ctx->user_bufs[i] = imu;
8501 ctx->buf_data->tags[offset] = tag;
Pavel Begunkov634d00d2021-04-25 14:32:26 +01008502 }
8503
8504 if (needs_switch)
8505 io_rsrc_node_switch(ctx, ctx->buf_data);
8506 return done ? done : err;
8507}
8508
Jens Axboe9b402842019-04-11 11:45:41 -06008509static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8510{
8511 __s32 __user *fds = arg;
8512 int fd;
8513
8514 if (ctx->cq_ev_fd)
8515 return -EBUSY;
8516
8517 if (copy_from_user(&fd, fds, sizeof(*fds)))
8518 return -EFAULT;
8519
8520 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8521 if (IS_ERR(ctx->cq_ev_fd)) {
8522 int ret = PTR_ERR(ctx->cq_ev_fd);
8523 ctx->cq_ev_fd = NULL;
8524 return ret;
8525 }
8526
8527 return 0;
8528}
8529
8530static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8531{
8532 if (ctx->cq_ev_fd) {
8533 eventfd_ctx_put(ctx->cq_ev_fd);
8534 ctx->cq_ev_fd = NULL;
8535 return 0;
8536 }
8537
8538 return -ENXIO;
8539}
8540
Jens Axboe5a2e7452020-02-23 16:23:11 -07008541static void io_destroy_buffers(struct io_ring_ctx *ctx)
8542{
Jens Axboe9e15c3a2021-03-13 12:29:43 -07008543 struct io_buffer *buf;
8544 unsigned long index;
8545
8546 xa_for_each(&ctx->io_buffers, index, buf)
8547 __io_remove_buffers(ctx, buf, index, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008548}
8549
Jens Axboe68e68ee2021-02-13 09:00:02 -07008550static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008551{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008552 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008553
Jens Axboe68e68ee2021-02-13 09:00:02 -07008554 list_for_each_entry_safe(req, nxt, list, compl.list) {
8555 if (tsk && req->task != tsk)
8556 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008557 list_del(&req->compl.list);
8558 kmem_cache_free(req_cachep, req);
8559 }
8560}
8561
Jens Axboe4010fec2021-02-27 15:04:18 -07008562static void io_req_caches_free(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008563{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008564 struct io_submit_state *submit_state = &ctx->submit_state;
Pavel Begunkove5547d22021-02-23 22:17:20 +00008565 struct io_comp_state *cs = &ctx->submit_state.comp;
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008566
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008567 mutex_lock(&ctx->uring_lock);
8568
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008569 if (submit_state->free_reqs) {
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008570 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8571 submit_state->reqs);
Pavel Begunkov8e5c66c2021-02-22 11:45:55 +00008572 submit_state->free_reqs = 0;
8573 }
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008574
Pavel Begunkovdac7a092021-03-19 17:22:39 +00008575 io_flush_cached_locked_reqs(ctx, cs);
Pavel Begunkove5547d22021-02-23 22:17:20 +00008576 io_req_cache_free(&cs->free_list, NULL);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008577 mutex_unlock(&ctx->uring_lock);
8578}
8579
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008580static bool io_wait_rsrc_data(struct io_rsrc_data *data)
8581{
8582 if (!data)
8583 return false;
8584 if (!atomic_dec_and_test(&data->refs))
8585 wait_for_completion(&data->done);
8586 return true;
8587}
8588
Jens Axboe2b188cc2019-01-07 10:46:33 -07008589static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8590{
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008591 io_sq_thread_finish(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008592
Jens Axboe37d1e2e2021-02-17 21:03:43 -07008593 if (ctx->mm_account) {
Jens Axboe2aede0e2020-09-14 10:45:53 -06008594 mmdrop(ctx->mm_account);
8595 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008596 }
Jens Axboedef596e2019-01-09 08:59:42 -07008597
Hao Xu8bad28d2021-02-19 17:19:36 +08008598 mutex_lock(&ctx->uring_lock);
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01008599 if (io_wait_rsrc_data(ctx->buf_data))
8600 __io_sqe_buffers_unregister(ctx);
8601 if (io_wait_rsrc_data(ctx->file_data))
Pavel Begunkov08480402021-04-13 02:58:38 +01008602 __io_sqe_files_unregister(ctx);
Pavel Begunkovc4ea0602021-04-01 15:43:58 +01008603 if (ctx->rings)
8604 __io_cqring_overflow_flush(ctx, true);
Hao Xu8bad28d2021-02-19 17:19:36 +08008605 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008606 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008607 io_destroy_buffers(ctx);
Pavel Begunkov07db2982021-04-20 12:03:32 +01008608 if (ctx->sq_creds)
8609 put_cred(ctx->sq_creds);
Jens Axboedef596e2019-01-09 08:59:42 -07008610
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008611 /* there are no registered resources left, nobody uses it */
8612 if (ctx->rsrc_node)
8613 io_rsrc_node_destroy(ctx->rsrc_node);
Pavel Begunkov8dd03af2021-03-19 17:22:36 +00008614 if (ctx->rsrc_backup_node)
Pavel Begunkovb895c9a2021-04-01 15:43:40 +01008615 io_rsrc_node_destroy(ctx->rsrc_backup_node);
Pavel Begunkova7f0ed52021-04-01 15:43:46 +01008616 flush_delayed_work(&ctx->rsrc_put_work);
8617
8618 WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list));
8619 WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist));
Jens Axboe2b188cc2019-01-07 10:46:33 -07008620
8621#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008622 if (ctx->ring_sock) {
8623 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008624 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008625 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008626#endif
8627
Hristo Venev75b28af2019-08-26 17:23:46 +00008628 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008629 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008630
8631 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008632 free_uid(ctx->user);
Jens Axboe4010fec2021-02-27 15:04:18 -07008633 io_req_caches_free(ctx);
Jens Axboee9418942021-02-19 12:33:30 -07008634 if (ctx->hash_map)
8635 io_wq_put_hash(ctx->hash_map);
Jens Axboe78076bb2019-12-04 19:56:40 -07008636 kfree(ctx->cancel_hash);
Pavel Begunkov62248432021-04-28 13:11:29 +01008637 kfree(ctx->dummy_ubuf);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008638 kfree(ctx);
8639}
8640
8641static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8642{
8643 struct io_ring_ctx *ctx = file->private_data;
8644 __poll_t mask = 0;
8645
8646 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008647 /*
8648 * synchronizes with barrier from wq_has_sleeper call in
8649 * io_commit_cqring
8650 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008651 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008652 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008653 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008654
8655 /*
8656 * Don't flush cqring overflow list here, just do a simple check.
8657 * Otherwise there could possible be ABBA deadlock:
8658 * CPU0 CPU1
8659 * ---- ----
8660 * lock(&ctx->uring_lock);
8661 * lock(&ep->mtx);
8662 * lock(&ctx->uring_lock);
8663 * lock(&ep->mtx);
8664 *
8665 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8666 * pushs them to do the flush.
8667 */
8668 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008669 mask |= EPOLLIN | EPOLLRDNORM;
8670
8671 return mask;
8672}
8673
8674static int io_uring_fasync(int fd, struct file *file, int on)
8675{
8676 struct io_ring_ctx *ctx = file->private_data;
8677
8678 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8679}
8680
Yejune Deng0bead8c2020-12-24 11:02:20 +08008681static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008682{
Jens Axboe4379bf82021-02-15 13:40:22 -07008683 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008684
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008685 creds = xa_erase(&ctx->personalities, id);
Jens Axboe4379bf82021-02-15 13:40:22 -07008686 if (creds) {
8687 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008688 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008689 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008690
8691 return -EINVAL;
8692}
8693
Pavel Begunkov9b465712021-03-15 14:23:07 +00008694static inline bool io_run_ctx_fallback(struct io_ring_ctx *ctx)
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008695{
Pavel Begunkov9b465712021-03-15 14:23:07 +00008696 return io_run_task_work_head(&ctx->exit_task_work);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008697}
8698
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008699struct io_tctx_exit {
8700 struct callback_head task_work;
8701 struct completion completion;
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00008702 struct io_ring_ctx *ctx;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008703};
8704
8705static void io_tctx_exit_cb(struct callback_head *cb)
8706{
8707 struct io_uring_task *tctx = current->io_uring;
8708 struct io_tctx_exit *work;
8709
8710 work = container_of(cb, struct io_tctx_exit, task_work);
8711 /*
8712 * When @in_idle, we're in cancellation and it's racy to remove the
8713 * node. It'll be removed by the end of cancellation, just ignore it.
8714 */
8715 if (!atomic_read(&tctx->in_idle))
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00008716 io_uring_del_task_file((unsigned long)work->ctx);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008717 complete(&work->completion);
8718}
8719
Pavel Begunkov28090c12021-04-25 23:34:45 +01008720static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8721{
8722 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8723
8724 return req->ctx == data;
8725}
8726
Jens Axboe85faa7b2020-04-09 18:14:00 -06008727static void io_ring_exit_work(struct work_struct *work)
8728{
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008729 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work);
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008730 unsigned long timeout = jiffies + HZ * 60 * 5;
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008731 struct io_tctx_exit exit;
8732 struct io_tctx_node *node;
8733 int ret;
Jens Axboe85faa7b2020-04-09 18:14:00 -06008734
Jens Axboe56952e92020-06-17 15:00:04 -06008735 /*
8736 * If we're doing polled IO and end up having requests being
8737 * submitted async (out-of-line), then completions can come in while
8738 * we're waiting for refs to drop. We need to reap these manually,
8739 * as nobody else will be looking for them.
8740 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008741 do {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008742 io_uring_try_cancel_requests(ctx, NULL, true);
Pavel Begunkov28090c12021-04-25 23:34:45 +01008743 if (ctx->sq_data) {
8744 struct io_sq_data *sqd = ctx->sq_data;
8745 struct task_struct *tsk;
8746
8747 io_sq_thread_park(sqd);
8748 tsk = sqd->thread;
8749 if (tsk && tsk->io_uring && tsk->io_uring->io_wq)
8750 io_wq_cancel_cb(tsk->io_uring->io_wq,
8751 io_cancel_ctx_cb, ctx, true);
8752 io_sq_thread_unpark(sqd);
8753 }
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008754
8755 WARN_ON_ONCE(time_after(jiffies, timeout));
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008756 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008757
Pavel Begunkov7f006512021-04-14 13:38:34 +01008758 init_completion(&exit.completion);
8759 init_task_work(&exit.task_work, io_tctx_exit_cb);
8760 exit.ctx = ctx;
Pavel Begunkov89b50662021-04-01 15:43:50 +01008761 /*
8762 * Some may use context even when all refs and requests have been put,
8763 * and they are free to do so while still holding uring_lock or
8764 * completion_lock, see __io_req_task_submit(). Apart from other work,
8765 * this lock/unlock section also waits them to finish.
8766 */
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008767 mutex_lock(&ctx->uring_lock);
8768 while (!list_empty(&ctx->tctx_list)) {
Pavel Begunkovb5bb3a22021-03-06 11:02:16 +00008769 WARN_ON_ONCE(time_after(jiffies, timeout));
8770
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008771 node = list_first_entry(&ctx->tctx_list, struct io_tctx_node,
8772 ctx_node);
Pavel Begunkov7f006512021-04-14 13:38:34 +01008773 /* don't spin on a single task if cancellation failed */
8774 list_rotate_left(&ctx->tctx_list);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008775 ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL);
8776 if (WARN_ON_ONCE(ret))
8777 continue;
8778 wake_up_process(node->task);
8779
8780 mutex_unlock(&ctx->uring_lock);
8781 wait_for_completion(&exit.completion);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008782 mutex_lock(&ctx->uring_lock);
8783 }
8784 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov89b50662021-04-01 15:43:50 +01008785 spin_lock_irq(&ctx->completion_lock);
8786 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkovd56d9382021-03-06 11:02:13 +00008787
Jens Axboe85faa7b2020-04-09 18:14:00 -06008788 io_ring_ctx_free(ctx);
8789}
8790
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008791/* Returns true if we found and killed one or more timeouts */
8792static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008793 bool cancel_all)
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008794{
8795 struct io_kiocb *req, *tmp;
8796 int canceled = 0;
8797
8798 spin_lock_irq(&ctx->completion_lock);
8799 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008800 if (io_match_task(req, tsk, cancel_all)) {
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008801 io_kill_timeout(req, -ECANCELED);
8802 canceled++;
8803 }
8804 }
Pavel Begunkov51520422021-03-29 11:39:29 +01008805 if (canceled != 0)
8806 io_commit_cqring(ctx);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008807 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov80c4cbd2021-03-25 18:32:43 +00008808 if (canceled != 0)
8809 io_cqring_ev_posted(ctx);
8810 return canceled != 0;
8811}
8812
Jens Axboe2b188cc2019-01-07 10:46:33 -07008813static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8814{
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008815 unsigned long index;
8816 struct creds *creds;
8817
Jens Axboe2b188cc2019-01-07 10:46:33 -07008818 mutex_lock(&ctx->uring_lock);
8819 percpu_ref_kill(&ctx->refs);
Pavel Begunkov634578f2020-12-06 22:22:44 +00008820 if (ctx->rings)
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00008821 __io_cqring_overflow_flush(ctx, true);
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00008822 xa_for_each(&ctx->personalities, index, creds)
8823 io_unregister_personality(ctx, index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008824 mutex_unlock(&ctx->uring_lock);
8825
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008826 io_kill_timeouts(ctx, NULL, true);
8827 io_poll_remove_all(ctx, NULL, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008828
Jens Axboe15dff282019-11-13 09:09:23 -07008829 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008830 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008831
Jens Axboe85faa7b2020-04-09 18:14:00 -06008832 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008833 /*
8834 * Use system_unbound_wq to avoid spawning tons of event kworkers
8835 * if we're exiting a ton of rings at the same time. It just adds
8836 * noise and overhead, there's no discernable change in runtime
8837 * over using system_wq.
8838 */
8839 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008840}
8841
8842static int io_uring_release(struct inode *inode, struct file *file)
8843{
8844 struct io_ring_ctx *ctx = file->private_data;
8845
8846 file->private_data = NULL;
8847 io_ring_ctx_wait_and_kill(ctx);
8848 return 0;
8849}
8850
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008851struct io_task_cancel {
8852 struct task_struct *task;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008853 bool all;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008854};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008855
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008856static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008857{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008858 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008859 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008860 bool ret;
8861
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008862 if (!cancel->all && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008863 unsigned long flags;
8864 struct io_ring_ctx *ctx = req->ctx;
8865
8866 /* protect against races with linked timeouts */
8867 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008868 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008869 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8870 } else {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008871 ret = io_match_task(req, cancel->task, cancel->all);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008872 }
8873 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008874}
8875
Pavel Begunkove1915f72021-03-11 23:29:35 +00008876static bool io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008877 struct task_struct *task, bool cancel_all)
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008878{
Pavel Begunkove1915f72021-03-11 23:29:35 +00008879 struct io_defer_entry *de;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008880 LIST_HEAD(list);
8881
8882 spin_lock_irq(&ctx->completion_lock);
8883 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008884 if (io_match_task(de->req, task, cancel_all)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008885 list_cut_position(&list, &ctx->defer_list, &de->list);
8886 break;
8887 }
8888 }
8889 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkove1915f72021-03-11 23:29:35 +00008890 if (list_empty(&list))
8891 return false;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008892
8893 while (!list_empty(&list)) {
8894 de = list_first_entry(&list, struct io_defer_entry, list);
8895 list_del_init(&de->list);
Pavel Begunkovf41db2732021-02-28 22:35:12 +00008896 io_req_complete_failed(de->req, -ECANCELED);
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008897 kfree(de);
8898 }
Pavel Begunkove1915f72021-03-11 23:29:35 +00008899 return true;
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008900}
8901
Pavel Begunkov1b007642021-03-06 11:02:17 +00008902static bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)
8903{
8904 struct io_tctx_node *node;
8905 enum io_wq_cancel cret;
8906 bool ret = false;
8907
8908 mutex_lock(&ctx->uring_lock);
8909 list_for_each_entry(node, &ctx->tctx_list, ctx_node) {
8910 struct io_uring_task *tctx = node->task->io_uring;
8911
8912 /*
8913 * io_wq will stay alive while we hold uring_lock, because it's
8914 * killed after ctx nodes, which requires to take the lock.
8915 */
8916 if (!tctx || !tctx->io_wq)
8917 continue;
8918 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true);
8919 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8920 }
8921 mutex_unlock(&ctx->uring_lock);
8922
8923 return ret;
8924}
8925
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008926static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8927 struct task_struct *task,
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008928 bool cancel_all)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008929{
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008930 struct io_task_cancel cancel = { .task = task, .all = cancel_all, };
Pavel Begunkov1b007642021-03-06 11:02:17 +00008931 struct io_uring_task *tctx = task ? task->io_uring : NULL;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008932
8933 while (1) {
8934 enum io_wq_cancel cret;
8935 bool ret = false;
8936
Pavel Begunkov1b007642021-03-06 11:02:17 +00008937 if (!task) {
8938 ret |= io_uring_try_cancel_iowq(ctx);
8939 } else if (tctx && tctx->io_wq) {
8940 /*
8941 * Cancels requests of all rings, not only @ctx, but
8942 * it's fine as the task is in exit/exec.
8943 */
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008944 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008945 &cancel, true);
8946 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8947 }
8948
8949 /* SQPOLL thread does its own polling */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008950 if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) ||
Jens Axboed052d1d2021-03-11 10:49:20 -07008951 (ctx->sq_data && ctx->sq_data->thread == current)) {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008952 while (!list_empty_careful(&ctx->iopoll_list)) {
8953 io_iopoll_try_reap_events(ctx);
8954 ret = true;
8955 }
8956 }
8957
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01008958 ret |= io_cancel_defer_files(ctx, task, cancel_all);
8959 ret |= io_poll_remove_all(ctx, task, cancel_all);
8960 ret |= io_kill_timeouts(ctx, task, cancel_all);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008961 ret |= io_run_task_work();
Pavel Begunkovba50a032021-02-26 15:47:56 +00008962 ret |= io_run_ctx_fallback(ctx);
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008963 if (!ret)
8964 break;
8965 cond_resched();
8966 }
8967}
8968
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00008969static int __io_uring_add_task_file(struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008970{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008971 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00008972 struct io_tctx_node *node;
Pavel Begunkova528b042020-12-21 18:34:04 +00008973 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008974
8975 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008976 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008977 if (unlikely(ret))
8978 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008979 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008980 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00008981 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
8982 node = kmalloc(sizeof(*node), GFP_KERNEL);
8983 if (!node)
8984 return -ENOMEM;
8985 node->ctx = ctx;
8986 node->task = current;
Jens Axboe0f212202020-09-13 13:09:39 -06008987
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00008988 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
8989 node, GFP_KERNEL));
8990 if (ret) {
8991 kfree(node);
8992 return ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008993 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00008994
8995 mutex_lock(&ctx->uring_lock);
8996 list_add(&node->ctx_node, &ctx->tctx_list);
8997 mutex_unlock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06008998 }
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00008999 tctx->last = ctx;
Jens Axboe0f212202020-09-13 13:09:39 -06009000 return 0;
9001}
9002
9003/*
Pavel Begunkovcf27f3b2021-03-19 17:22:31 +00009004 * Note that this task has used io_uring. We use it for cancelation purposes.
9005 */
9006static inline int io_uring_add_task_file(struct io_ring_ctx *ctx)
9007{
9008 struct io_uring_task *tctx = current->io_uring;
9009
9010 if (likely(tctx && tctx->last == ctx))
9011 return 0;
9012 return __io_uring_add_task_file(ctx);
9013}
9014
9015/*
Jens Axboe0f212202020-09-13 13:09:39 -06009016 * Remove this io_uring_file -> task mapping.
9017 */
Pavel Begunkov29412672021-03-06 11:02:11 +00009018static void io_uring_del_task_file(unsigned long index)
Jens Axboe0f212202020-09-13 13:09:39 -06009019{
9020 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009021 struct io_tctx_node *node;
Pavel Begunkov29412672021-03-06 11:02:11 +00009022
Pavel Begunkoveebd2e32021-03-06 11:02:14 +00009023 if (!tctx)
9024 return;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009025 node = xa_erase(&tctx->xa, index);
9026 if (!node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009027 return;
Jens Axboe0f212202020-09-13 13:09:39 -06009028
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009029 WARN_ON_ONCE(current != node->task);
9030 WARN_ON_ONCE(list_empty(&node->ctx_node));
9031
9032 mutex_lock(&node->ctx->uring_lock);
9033 list_del(&node->ctx_node);
9034 mutex_unlock(&node->ctx->uring_lock);
9035
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009036 if (tctx->last == node->ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06009037 tctx->last = NULL;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009038 kfree(node);
Jens Axboe0f212202020-09-13 13:09:39 -06009039}
9040
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009041static void io_uring_clean_tctx(struct io_uring_task *tctx)
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009042{
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009043 struct io_wq *wq = tctx->io_wq;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009044 struct io_tctx_node *node;
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009045 unsigned long index;
9046
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009047 xa_for_each(&tctx->xa, index, node)
Pavel Begunkov29412672021-03-06 11:02:11 +00009048 io_uring_del_task_file(index);
Marco Elverb16ef422021-05-27 11:25:48 +02009049 if (wq) {
9050 /*
9051 * Must be after io_uring_del_task_file() (removes nodes under
9052 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
9053 */
9054 tctx->io_wq = NULL;
Pavel Begunkovba5ef6d2021-05-20 13:21:20 +01009055 io_wq_put_and_exit(wq);
Marco Elverb16ef422021-05-27 11:25:48 +02009056 }
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009057}
9058
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009059static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked)
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009060{
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009061 if (tracked)
9062 return atomic_read(&tctx->inflight_tracked);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009063 return percpu_counter_sum(&tctx->inflight);
9064}
9065
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009066static void io_uring_try_cancel(bool cancel_all)
Jens Axboe0f212202020-09-13 13:09:39 -06009067{
9068 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov13bf43f2021-03-06 11:02:12 +00009069 struct io_tctx_node *node;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009070 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009071
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009072 xa_for_each(&tctx->xa, index, node) {
9073 struct io_ring_ctx *ctx = node->ctx;
9074
Pavel Begunkov9f59a9d2021-04-25 23:34:46 +01009075 /* sqpoll task will cancel all its requests */
9076 if (!ctx->sq_data)
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009077 io_uring_try_cancel_requests(ctx, current, cancel_all);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009078 }
Jens Axboefdaf0832020-10-30 09:37:30 -06009079}
9080
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009081/* should only be called by SQPOLL task */
Pavel Begunkov734551d2021-04-18 14:52:09 +01009082static void io_uring_cancel_sqpoll(struct io_sq_data *sqd)
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009083{
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009084 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009085 struct io_ring_ctx *ctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009086 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009087 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009088
Palash Oswal6d042ff2021-04-27 18:21:49 +05309089 if (!current->io_uring)
9090 return;
Pavel Begunkov17a91052021-05-23 15:48:39 +01009091 if (tctx->io_wq)
9092 io_wq_exit_start(tctx->io_wq);
9093
Pavel Begunkov734551d2021-04-18 14:52:09 +01009094 WARN_ON_ONCE(!sqd || sqd->thread != current);
Pavel Begunkov521d6a72021-03-11 23:29:38 +00009095
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009096 atomic_inc(&tctx->in_idle);
9097 do {
9098 /* read completions before cancelations */
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009099 inflight = tctx_inflight(tctx, false);
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009100 if (!inflight)
9101 break;
Pavel Begunkov734551d2021-04-18 14:52:09 +01009102 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009103 io_uring_try_cancel_requests(ctx, current, true);
Jens Axboefdaf0832020-10-30 09:37:30 -06009104
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009105 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9106 /*
9107 * If we've seen completions, retry without waiting. This
9108 * avoids a race where a completion comes in before we did
9109 * prepare_to_wait().
9110 */
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009111 if (inflight == tctx_inflight(tctx, false))
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009112 schedule();
9113 finish_wait(&tctx->wait, &wait);
9114 } while (1);
9115 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009116}
9117
Jens Axboe0f212202020-09-13 13:09:39 -06009118/*
9119 * Find any io_uring fd that this task has registered or done IO on, and cancel
9120 * requests.
9121 */
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009122void __io_uring_cancel(struct files_struct *files)
Jens Axboe0f212202020-09-13 13:09:39 -06009123{
9124 struct io_uring_task *tctx = current->io_uring;
9125 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009126 s64 inflight;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009127 bool cancel_all = !files;
Jens Axboe0f212202020-09-13 13:09:39 -06009128
Pavel Begunkov17a91052021-05-23 15:48:39 +01009129 if (tctx->io_wq)
9130 io_wq_exit_start(tctx->io_wq);
9131
Jens Axboe0f212202020-09-13 13:09:39 -06009132 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009133 atomic_inc(&tctx->in_idle);
Jens Axboed8a6df12020-10-15 16:24:45 -06009134 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009135 /* read completions before cancelations */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009136 inflight = tctx_inflight(tctx, !cancel_all);
Jens Axboed8a6df12020-10-15 16:24:45 -06009137 if (!inflight)
9138 break;
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009139 io_uring_try_cancel(cancel_all);
Jens Axboe0f212202020-09-13 13:09:39 -06009140 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9141
9142 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009143 * If we've seen completions, retry without waiting. This
9144 * avoids a race where a completion comes in before we did
9145 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009146 */
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009147 if (inflight == tctx_inflight(tctx, !cancel_all))
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009148 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009149 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009150 } while (1);
Jens Axboefdaf0832020-10-30 09:37:30 -06009151 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009152
Pavel Begunkov8452d4a2021-02-27 11:16:46 +00009153 io_uring_clean_tctx(tctx);
Pavel Begunkov3dd0c972021-05-16 22:58:04 +01009154 if (cancel_all) {
Pavel Begunkov3f48cf12021-04-11 01:46:27 +01009155 /* for exec all current's requests should be gone, kill tctx */
9156 __io_uring_free(current);
9157 }
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009158}
9159
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009160static void *io_uring_validate_mmap_request(struct file *file,
9161 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009162{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009163 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009164 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009165 struct page *page;
9166 void *ptr;
9167
9168 switch (offset) {
9169 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009170 case IORING_OFF_CQ_RING:
9171 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009172 break;
9173 case IORING_OFF_SQES:
9174 ptr = ctx->sq_sqes;
9175 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009176 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009177 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009178 }
9179
9180 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009181 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009182 return ERR_PTR(-EINVAL);
9183
9184 return ptr;
9185}
9186
9187#ifdef CONFIG_MMU
9188
9189static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9190{
9191 size_t sz = vma->vm_end - vma->vm_start;
9192 unsigned long pfn;
9193 void *ptr;
9194
9195 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9196 if (IS_ERR(ptr))
9197 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009198
9199 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9200 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9201}
9202
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009203#else /* !CONFIG_MMU */
9204
9205static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9206{
9207 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9208}
9209
9210static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9211{
9212 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9213}
9214
9215static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9216 unsigned long addr, unsigned long len,
9217 unsigned long pgoff, unsigned long flags)
9218{
9219 void *ptr;
9220
9221 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9222 if (IS_ERR(ptr))
9223 return PTR_ERR(ptr);
9224
9225 return (unsigned long) ptr;
9226}
9227
9228#endif /* !CONFIG_MMU */
9229
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009230static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009231{
9232 DEFINE_WAIT(wait);
9233
9234 do {
9235 if (!io_sqring_full(ctx))
9236 break;
Jens Axboe90554202020-09-03 12:12:41 -06009237 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9238
9239 if (!io_sqring_full(ctx))
9240 break;
Jens Axboe90554202020-09-03 12:12:41 -06009241 schedule();
9242 } while (!signal_pending(current));
9243
9244 finish_wait(&ctx->sqo_sq_wait, &wait);
Yang Li51993282021-03-09 14:30:41 +08009245 return 0;
Jens Axboe90554202020-09-03 12:12:41 -06009246}
9247
Hao Xuc73ebb62020-11-03 10:54:37 +08009248static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9249 struct __kernel_timespec __user **ts,
9250 const sigset_t __user **sig)
9251{
9252 struct io_uring_getevents_arg arg;
9253
9254 /*
9255 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9256 * is just a pointer to the sigset_t.
9257 */
9258 if (!(flags & IORING_ENTER_EXT_ARG)) {
9259 *sig = (const sigset_t __user *) argp;
9260 *ts = NULL;
9261 return 0;
9262 }
9263
9264 /*
9265 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9266 * timespec and sigset_t pointers if good.
9267 */
9268 if (*argsz != sizeof(arg))
9269 return -EINVAL;
9270 if (copy_from_user(&arg, argp, sizeof(arg)))
9271 return -EFAULT;
9272 *sig = u64_to_user_ptr(arg.sigmask);
9273 *argsz = arg.sigmask_sz;
9274 *ts = u64_to_user_ptr(arg.ts);
9275 return 0;
9276}
9277
Jens Axboe2b188cc2019-01-07 10:46:33 -07009278SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009279 u32, min_complete, u32, flags, const void __user *, argp,
9280 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009281{
9282 struct io_ring_ctx *ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009283 int submitted = 0;
9284 struct fd f;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009285 long ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009286
Jens Axboe4c6e2772020-07-01 11:29:10 -06009287 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009288
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009289 if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
9290 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009291 return -EINVAL;
9292
9293 f = fdget(fd);
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009294 if (unlikely(!f.file))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009295 return -EBADF;
9296
9297 ret = -EOPNOTSUPP;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009298 if (unlikely(f.file->f_op != &io_uring_fops))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009299 goto out_fput;
9300
9301 ret = -ENXIO;
9302 ctx = f.file->private_data;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009303 if (unlikely(!percpu_ref_tryget(&ctx->refs)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009304 goto out_fput;
9305
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009306 ret = -EBADFD;
Pavel Begunkov33f993d2021-03-19 17:22:30 +00009307 if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED))
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009308 goto out;
9309
Jens Axboe6c271ce2019-01-10 11:22:30 -07009310 /*
9311 * For SQ polling, the thread will do all submissions and completions.
9312 * Just return the requested submit count, and wake the thread if
9313 * we were asked to.
9314 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009315 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009316 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c2450a2021-02-23 12:40:22 +00009317 io_cqring_overflow_flush(ctx, false);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009318
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009319 ret = -EOWNERDEAD;
Stefan Metzmacher04147482021-03-07 11:54:29 +01009320 if (unlikely(ctx->sq_data->thread == NULL)) {
9321 goto out;
9322 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009323 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009324 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009325 if (flags & IORING_ENTER_SQ_WAIT) {
9326 ret = io_sqpoll_wait_sq(ctx);
9327 if (ret)
9328 goto out;
9329 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009330 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009331 } else if (to_submit) {
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009332 ret = io_uring_add_task_file(ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009333 if (unlikely(ret))
9334 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009335 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009336 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009337 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009338
9339 if (submitted != to_submit)
9340 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009341 }
9342 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009343 const sigset_t __user *sig;
9344 struct __kernel_timespec __user *ts;
9345
9346 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9347 if (unlikely(ret))
9348 goto out;
9349
Jens Axboe2b188cc2019-01-07 10:46:33 -07009350 min_complete = min(min_complete, ctx->cq_entries);
9351
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009352 /*
9353 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9354 * space applications don't need to do io completion events
9355 * polling again, they can rely on io_sq_thread to do polling
9356 * work, which can reduce cpu usage and uring_lock contention.
9357 */
9358 if (ctx->flags & IORING_SETUP_IOPOLL &&
9359 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009360 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009361 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009362 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009363 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009364 }
9365
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009366out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009367 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009368out_fput:
9369 fdput(f);
9370 return submitted ? submitted : ret;
9371}
9372
Tobias Klauserbebdb652020-02-26 18:38:32 +01009373#ifdef CONFIG_PROC_FS
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009374static int io_uring_show_cred(struct seq_file *m, unsigned int id,
9375 const struct cred *cred)
Jens Axboe87ce9552020-01-30 08:25:34 -07009376{
Jens Axboe87ce9552020-01-30 08:25:34 -07009377 struct user_namespace *uns = seq_user_ns(m);
9378 struct group_info *gi;
9379 kernel_cap_t cap;
9380 unsigned __capi;
9381 int g;
9382
9383 seq_printf(m, "%5d\n", id);
9384 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9385 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9386 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9387 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9388 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9389 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9390 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9391 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9392 seq_puts(m, "\n\tGroups:\t");
9393 gi = cred->group_info;
9394 for (g = 0; g < gi->ngroups; g++) {
9395 seq_put_decimal_ull(m, g ? " " : "",
9396 from_kgid_munged(uns, gi->gid[g]));
9397 }
9398 seq_puts(m, "\n\tCapEff:\t");
9399 cap = cred->cap_effective;
9400 CAP_FOR_EACH_U32(__capi)
9401 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9402 seq_putc(m, '\n');
9403 return 0;
9404}
9405
9406static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9407{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009408 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009409 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009410 int i;
9411
Jens Axboefad8e0d2020-09-28 08:57:48 -06009412 /*
9413 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9414 * since fdinfo case grabs it in the opposite direction of normal use
9415 * cases. If we fail to get the lock, we just don't iterate any
9416 * structures that could be going away outside the io_uring mutex.
9417 */
9418 has_lock = mutex_trylock(&ctx->uring_lock);
9419
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009420 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
Joseph Qidbbe9c62020-09-29 09:01:22 -06009421 sq = ctx->sq_data;
Jens Axboe5f3f26f2021-02-25 10:17:46 -07009422 if (!sq->thread)
9423 sq = NULL;
9424 }
Joseph Qidbbe9c62020-09-29 09:01:22 -06009425
9426 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9427 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009428 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009429 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Jens Axboe7b29f922021-03-12 08:30:14 -07009430 struct file *f = io_file_from_index(ctx, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009431
Jens Axboe87ce9552020-01-30 08:25:34 -07009432 if (f)
9433 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9434 else
9435 seq_printf(m, "%5u: <none>\n", i);
9436 }
9437 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009438 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Pavel Begunkov41edf1a2021-04-25 14:32:23 +01009439 struct io_mapped_ubuf *buf = ctx->user_bufs[i];
Pavel Begunkov4751f532021-04-01 15:43:55 +01009440 unsigned int len = buf->ubuf_end - buf->ubuf;
Jens Axboe87ce9552020-01-30 08:25:34 -07009441
Pavel Begunkov4751f532021-04-01 15:43:55 +01009442 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
Jens Axboe87ce9552020-01-30 08:25:34 -07009443 }
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009444 if (has_lock && !xa_empty(&ctx->personalities)) {
9445 unsigned long index;
9446 const struct cred *cred;
9447
Jens Axboe87ce9552020-01-30 08:25:34 -07009448 seq_printf(m, "Personalities:\n");
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009449 xa_for_each(&ctx->personalities, index, cred)
9450 io_uring_show_cred(m, index, cred);
Jens Axboe87ce9552020-01-30 08:25:34 -07009451 }
Jens Axboed7718a92020-02-14 22:23:12 -07009452 seq_printf(m, "PollList:\n");
9453 spin_lock_irq(&ctx->completion_lock);
9454 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9455 struct hlist_head *list = &ctx->cancel_hash[i];
9456 struct io_kiocb *req;
9457
9458 hlist_for_each_entry(req, list, hash_node)
9459 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9460 req->task->task_works != NULL);
9461 }
9462 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009463 if (has_lock)
9464 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009465}
9466
9467static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9468{
9469 struct io_ring_ctx *ctx = f->private_data;
9470
9471 if (percpu_ref_tryget(&ctx->refs)) {
9472 __io_uring_show_fdinfo(ctx, m);
9473 percpu_ref_put(&ctx->refs);
9474 }
9475}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009476#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009477
Jens Axboe2b188cc2019-01-07 10:46:33 -07009478static const struct file_operations io_uring_fops = {
9479 .release = io_uring_release,
9480 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009481#ifndef CONFIG_MMU
9482 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9483 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9484#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485 .poll = io_uring_poll,
9486 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009487#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009488 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009489#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009490};
9491
9492static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9493 struct io_uring_params *p)
9494{
Hristo Venev75b28af2019-08-26 17:23:46 +00009495 struct io_rings *rings;
9496 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009497
Jens Axboebd740482020-08-05 12:58:23 -06009498 /* make sure these are sane, as we already accounted them */
9499 ctx->sq_entries = p->sq_entries;
9500 ctx->cq_entries = p->cq_entries;
9501
Hristo Venev75b28af2019-08-26 17:23:46 +00009502 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9503 if (size == SIZE_MAX)
9504 return -EOVERFLOW;
9505
9506 rings = io_mem_alloc(size);
9507 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009508 return -ENOMEM;
9509
Hristo Venev75b28af2019-08-26 17:23:46 +00009510 ctx->rings = rings;
9511 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9512 rings->sq_ring_mask = p->sq_entries - 1;
9513 rings->cq_ring_mask = p->cq_entries - 1;
9514 rings->sq_ring_entries = p->sq_entries;
9515 rings->cq_ring_entries = p->cq_entries;
9516 ctx->sq_mask = rings->sq_ring_mask;
9517 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009518
9519 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009520 if (size == SIZE_MAX) {
9521 io_mem_free(ctx->rings);
9522 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009523 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009524 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009525
9526 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009527 if (!ctx->sq_sqes) {
9528 io_mem_free(ctx->rings);
9529 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009530 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009531 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009532
Jens Axboe2b188cc2019-01-07 10:46:33 -07009533 return 0;
9534}
9535
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009536static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9537{
9538 int ret, fd;
9539
9540 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9541 if (fd < 0)
9542 return fd;
9543
Pavel Begunkovbaf186c2021-03-06 11:02:15 +00009544 ret = io_uring_add_task_file(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009545 if (ret) {
9546 put_unused_fd(fd);
9547 return ret;
9548 }
9549 fd_install(fd, file);
9550 return fd;
9551}
9552
Jens Axboe2b188cc2019-01-07 10:46:33 -07009553/*
9554 * Allocate an anonymous fd, this is what constitutes the application
9555 * visible backing of an io_uring instance. The application mmaps this
9556 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9557 * we have to tie this fd to a socket for file garbage collection purposes.
9558 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009559static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009560{
9561 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009562#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009563 int ret;
9564
Jens Axboe2b188cc2019-01-07 10:46:33 -07009565 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9566 &ctx->ring_sock);
9567 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009568 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009569#endif
9570
Jens Axboe2b188cc2019-01-07 10:46:33 -07009571 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9572 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009573#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009574 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009575 sock_release(ctx->ring_sock);
9576 ctx->ring_sock = NULL;
9577 } else {
9578 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009579 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009580#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009581 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009582}
9583
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009584static int io_uring_create(unsigned entries, struct io_uring_params *p,
9585 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009586{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009587 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009588 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009589 int ret;
9590
Jens Axboe8110c1a2019-12-28 15:39:54 -07009591 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009592 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009593 if (entries > IORING_MAX_ENTRIES) {
9594 if (!(p->flags & IORING_SETUP_CLAMP))
9595 return -EINVAL;
9596 entries = IORING_MAX_ENTRIES;
9597 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009598
9599 /*
9600 * Use twice as many entries for the CQ ring. It's possible for the
9601 * application to drive a higher depth than the size of the SQ ring,
9602 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009603 * some flexibility in overcommitting a bit. If the application has
9604 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9605 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009606 */
9607 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009608 if (p->flags & IORING_SETUP_CQSIZE) {
9609 /*
9610 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9611 * to a power-of-two, if it isn't already. We do NOT impose
9612 * any cq vs sq ring sizing.
9613 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009614 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009615 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009616 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9617 if (!(p->flags & IORING_SETUP_CLAMP))
9618 return -EINVAL;
9619 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9620 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009621 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9622 if (p->cq_entries < p->sq_entries)
9623 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009624 } else {
9625 p->cq_entries = 2 * p->sq_entries;
9626 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009627
Jens Axboe2b188cc2019-01-07 10:46:33 -07009628 ctx = io_ring_ctx_alloc(p);
Jens Axboe62e398b2021-02-21 16:19:37 -07009629 if (!ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009630 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009631 ctx->compat = in_compat_syscall();
Jens Axboe62e398b2021-02-21 16:19:37 -07009632 if (!capable(CAP_IPC_LOCK))
9633 ctx->user = get_uid(current_user());
Jens Axboe2aede0e2020-09-14 10:45:53 -06009634
9635 /*
9636 * This is just grabbed for accounting purposes. When a process exits,
9637 * the mm is exited and dropped before the files, hence we need to hang
9638 * on to this mm purely for the purposes of being able to unaccount
9639 * memory (locked/pinned vm). It's not used for anything else.
9640 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009641 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009642 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009643
Jens Axboe2b188cc2019-01-07 10:46:33 -07009644 ret = io_allocate_scq_urings(ctx, p);
9645 if (ret)
9646 goto err;
9647
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009648 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649 if (ret)
9650 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009651 /* always set a rsrc node */
Pavel Begunkov47b228c2021-04-29 11:46:48 +01009652 ret = io_rsrc_node_switch_start(ctx);
9653 if (ret)
9654 goto err;
Pavel Begunkoveae071c2021-04-25 14:32:24 +01009655 io_rsrc_node_switch(ctx, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009656
Jens Axboe2b188cc2019-01-07 10:46:33 -07009657 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009658 p->sq_off.head = offsetof(struct io_rings, sq.head);
9659 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9660 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9661 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9662 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9663 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9664 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009665
9666 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009667 p->cq_off.head = offsetof(struct io_rings, cq.head);
9668 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9669 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9670 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9671 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9672 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009673 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009674
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009675 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9676 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009677 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009678 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
Pavel Begunkov96905572021-06-10 16:37:38 +01009679 IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS |
9680 IORING_FEAT_RSRC_TAGS;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009681
9682 if (copy_to_user(params, p, sizeof(*p))) {
9683 ret = -EFAULT;
9684 goto err;
9685 }
Jens Axboed1719f72020-07-30 13:43:53 -06009686
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009687 file = io_uring_get_file(ctx);
9688 if (IS_ERR(file)) {
9689 ret = PTR_ERR(file);
9690 goto err;
9691 }
9692
Jens Axboed1719f72020-07-30 13:43:53 -06009693 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009694 * Install ring fd as the very last thing, so we don't risk someone
9695 * having closed it before we finish setup
9696 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009697 ret = io_uring_install_fd(ctx, file);
9698 if (ret < 0) {
9699 /* fput will clean it up */
9700 fput(file);
9701 return ret;
9702 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009703
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009704 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009705 return ret;
9706err:
9707 io_ring_ctx_wait_and_kill(ctx);
9708 return ret;
9709}
9710
9711/*
9712 * Sets up an aio uring context, and returns the fd. Applications asks for a
9713 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9714 * params structure passed in.
9715 */
9716static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9717{
9718 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009719 int i;
9720
9721 if (copy_from_user(&p, params, sizeof(p)))
9722 return -EFAULT;
9723 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9724 if (p.resv[i])
9725 return -EINVAL;
9726 }
9727
Jens Axboe6c271ce2019-01-10 11:22:30 -07009728 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009729 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009730 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9731 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009732 return -EINVAL;
9733
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009734 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009735}
9736
9737SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9738 struct io_uring_params __user *, params)
9739{
9740 return io_uring_setup(entries, params);
9741}
9742
Jens Axboe66f4af92020-01-16 15:36:52 -07009743static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9744{
9745 struct io_uring_probe *p;
9746 size_t size;
9747 int i, ret;
9748
9749 size = struct_size(p, ops, nr_args);
9750 if (size == SIZE_MAX)
9751 return -EOVERFLOW;
9752 p = kzalloc(size, GFP_KERNEL);
9753 if (!p)
9754 return -ENOMEM;
9755
9756 ret = -EFAULT;
9757 if (copy_from_user(p, arg, size))
9758 goto out;
9759 ret = -EINVAL;
9760 if (memchr_inv(p, 0, size))
9761 goto out;
9762
9763 p->last_op = IORING_OP_LAST - 1;
9764 if (nr_args > IORING_OP_LAST)
9765 nr_args = IORING_OP_LAST;
9766
9767 for (i = 0; i < nr_args; i++) {
9768 p->ops[i].op = i;
9769 if (!io_op_defs[i].not_supported)
9770 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9771 }
9772 p->ops_len = i;
9773
9774 ret = 0;
9775 if (copy_to_user(arg, p, size))
9776 ret = -EFAULT;
9777out:
9778 kfree(p);
9779 return ret;
9780}
9781
Jens Axboe071698e2020-01-28 10:04:42 -07009782static int io_register_personality(struct io_ring_ctx *ctx)
9783{
Jens Axboe4379bf82021-02-15 13:40:22 -07009784 const struct cred *creds;
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009785 u32 id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009786 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009787
Jens Axboe4379bf82021-02-15 13:40:22 -07009788 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009789
Matthew Wilcox (Oracle)61cf9372021-03-08 14:16:16 +00009790 ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
9791 XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
9792 if (!ret)
9793 return id;
9794 put_cred(creds);
Jens Axboe1e6fa522020-10-15 08:46:24 -06009795 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009796}
9797
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009798static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9799 unsigned int nr_args)
9800{
9801 struct io_uring_restriction *res;
9802 size_t size;
9803 int i, ret;
9804
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009805 /* Restrictions allowed only if rings started disabled */
9806 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9807 return -EBADFD;
9808
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009809 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009810 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009811 return -EBUSY;
9812
9813 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9814 return -EINVAL;
9815
9816 size = array_size(nr_args, sizeof(*res));
9817 if (size == SIZE_MAX)
9818 return -EOVERFLOW;
9819
9820 res = memdup_user(arg, size);
9821 if (IS_ERR(res))
9822 return PTR_ERR(res);
9823
9824 ret = 0;
9825
9826 for (i = 0; i < nr_args; i++) {
9827 switch (res[i].opcode) {
9828 case IORING_RESTRICTION_REGISTER_OP:
9829 if (res[i].register_op >= IORING_REGISTER_LAST) {
9830 ret = -EINVAL;
9831 goto out;
9832 }
9833
9834 __set_bit(res[i].register_op,
9835 ctx->restrictions.register_op);
9836 break;
9837 case IORING_RESTRICTION_SQE_OP:
9838 if (res[i].sqe_op >= IORING_OP_LAST) {
9839 ret = -EINVAL;
9840 goto out;
9841 }
9842
9843 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9844 break;
9845 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9846 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9847 break;
9848 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9849 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9850 break;
9851 default:
9852 ret = -EINVAL;
9853 goto out;
9854 }
9855 }
9856
9857out:
9858 /* Reset all restrictions if an error happened */
9859 if (ret != 0)
9860 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9861 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009862 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009863
9864 kfree(res);
9865 return ret;
9866}
9867
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009868static int io_register_enable_rings(struct io_ring_ctx *ctx)
9869{
9870 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9871 return -EBADFD;
9872
9873 if (ctx->restrictions.registered)
9874 ctx->restricted = 1;
9875
Pavel Begunkov0298ef92021-03-08 13:20:57 +00009876 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9877 if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait))
9878 wake_up(&ctx->sq_data->wait);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009879 return 0;
9880}
9881
Pavel Begunkovfdecb662021-04-25 14:32:20 +01009882static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01009883 struct io_uring_rsrc_update2 *up,
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009884 unsigned nr_args)
9885{
9886 __u32 tmp;
9887 int err;
9888
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01009889 if (up->resv)
9890 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009891 if (check_add_overflow(up->offset, nr_args, &tmp))
9892 return -EOVERFLOW;
9893 err = io_rsrc_node_switch_start(ctx);
9894 if (err)
9895 return err;
9896
Pavel Begunkovfdecb662021-04-25 14:32:20 +01009897 switch (type) {
9898 case IORING_RSRC_FILE:
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009899 return __io_sqe_files_update(ctx, up, nr_args);
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009900 case IORING_RSRC_BUFFER:
9901 return __io_sqe_buffers_update(ctx, up, nr_args);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009902 }
9903 return -EINVAL;
9904}
9905
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01009906static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
9907 unsigned nr_args)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009908{
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01009909 struct io_uring_rsrc_update2 up;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009910
9911 if (!nr_args)
9912 return -EINVAL;
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01009913 memset(&up, 0, sizeof(up));
9914 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
9915 return -EFAULT;
9916 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
9917}
9918
9919static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +01009920 unsigned size, unsigned type)
Pavel Begunkovc3bdad02021-04-25 14:32:22 +01009921{
9922 struct io_uring_rsrc_update2 up;
9923
9924 if (size != sizeof(up))
9925 return -EINVAL;
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009926 if (copy_from_user(&up, arg, sizeof(up)))
9927 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +01009928 if (!up.nr || up.resv)
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009929 return -EINVAL;
Pavel Begunkov992da012021-06-10 16:37:37 +01009930 return __io_register_rsrc_update(ctx, type, &up, up.nr);
Pavel Begunkov98f0b3b2021-04-25 14:32:19 +01009931}
9932
Pavel Begunkov792e3582021-04-25 14:32:21 +01009933static int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
Pavel Begunkov992da012021-06-10 16:37:37 +01009934 unsigned int size, unsigned int type)
Pavel Begunkov792e3582021-04-25 14:32:21 +01009935{
9936 struct io_uring_rsrc_register rr;
9937
9938 /* keep it extendible */
9939 if (size != sizeof(rr))
9940 return -EINVAL;
9941
9942 memset(&rr, 0, sizeof(rr));
9943 if (copy_from_user(&rr, arg, size))
9944 return -EFAULT;
Pavel Begunkov992da012021-06-10 16:37:37 +01009945 if (!rr.nr || rr.resv || rr.resv2)
Pavel Begunkov792e3582021-04-25 14:32:21 +01009946 return -EINVAL;
9947
Pavel Begunkov992da012021-06-10 16:37:37 +01009948 switch (type) {
Pavel Begunkov792e3582021-04-25 14:32:21 +01009949 case IORING_RSRC_FILE:
9950 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
9951 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov634d00d2021-04-25 14:32:26 +01009952 case IORING_RSRC_BUFFER:
9953 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
9954 rr.nr, u64_to_user_ptr(rr.tags));
Pavel Begunkov792e3582021-04-25 14:32:21 +01009955 }
9956 return -EINVAL;
9957}
9958
Jens Axboe071698e2020-01-28 10:04:42 -07009959static bool io_register_op_must_quiesce(int op)
9960{
9961 switch (op) {
Bijan Mottahedehbd54b6f2021-04-25 14:32:25 +01009962 case IORING_REGISTER_BUFFERS:
9963 case IORING_UNREGISTER_BUFFERS:
Pavel Begunkovf4f7d212021-04-01 15:44:02 +01009964 case IORING_REGISTER_FILES:
Jens Axboe071698e2020-01-28 10:04:42 -07009965 case IORING_UNREGISTER_FILES:
9966 case IORING_REGISTER_FILES_UPDATE:
9967 case IORING_REGISTER_PROBE:
9968 case IORING_REGISTER_PERSONALITY:
9969 case IORING_UNREGISTER_PERSONALITY:
Pavel Begunkov992da012021-06-10 16:37:37 +01009970 case IORING_REGISTER_FILES2:
9971 case IORING_REGISTER_FILES_UPDATE2:
9972 case IORING_REGISTER_BUFFERS2:
9973 case IORING_REGISTER_BUFFERS_UPDATE:
Jens Axboe071698e2020-01-28 10:04:42 -07009974 return false;
9975 default:
9976 return true;
9977 }
9978}
9979
Jens Axboeedafcce2019-01-09 09:16:05 -07009980static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9981 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009982 __releases(ctx->uring_lock)
9983 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009984{
9985 int ret;
9986
Jens Axboe35fa71a2019-04-22 10:23:23 -06009987 /*
9988 * We're inside the ring mutex, if the ref is already dying, then
9989 * someone else killed the ctx or is already going through
9990 * io_uring_register().
9991 */
9992 if (percpu_ref_is_dying(&ctx->refs))
9993 return -ENXIO;
9994
Pavel Begunkov75c40212021-04-15 13:07:40 +01009995 if (ctx->restricted) {
9996 if (opcode >= IORING_REGISTER_LAST)
9997 return -EINVAL;
9998 opcode = array_index_nospec(opcode, IORING_REGISTER_LAST);
9999 if (!test_bit(opcode, ctx->restrictions.register_op))
10000 return -EACCES;
10001 }
10002
Jens Axboe071698e2020-01-28 10:04:42 -070010003 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010004 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010005
Jens Axboe05f3fb32019-12-09 11:22:50 -070010006 /*
10007 * Drop uring mutex before waiting for references to exit. If
10008 * another thread is currently inside io_uring_enter() it might
10009 * need to grab the uring_lock to make progress. If we hold it
10010 * here across the drain wait, then we can deadlock. It's safe
10011 * to drop the mutex here, since no new references will come in
10012 * after we've killed the percpu ref.
10013 */
10014 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010015 do {
10016 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10017 if (!ret)
10018 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010019 ret = io_run_task_work_sig();
10020 if (ret < 0)
10021 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010022 } while (1);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010023 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010024
Jens Axboec1503682020-01-08 08:26:07 -070010025 if (ret) {
Pavel Begunkovf70865d2021-04-11 01:46:40 +010010026 io_refs_resurrect(&ctx->refs, &ctx->ref_comp);
10027 return ret;
Jens Axboec1503682020-01-08 08:26:07 -070010028 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010029 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010030
10031 switch (opcode) {
10032 case IORING_REGISTER_BUFFERS:
Pavel Begunkov634d00d2021-04-25 14:32:26 +010010033 ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -070010034 break;
10035 case IORING_UNREGISTER_BUFFERS:
10036 ret = -EINVAL;
10037 if (arg || nr_args)
10038 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010039 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010040 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010041 case IORING_REGISTER_FILES:
Pavel Begunkov792e3582021-04-25 14:32:21 +010010042 ret = io_sqe_files_register(ctx, arg, nr_args, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -070010043 break;
10044 case IORING_UNREGISTER_FILES:
10045 ret = -EINVAL;
10046 if (arg || nr_args)
10047 break;
10048 ret = io_sqe_files_unregister(ctx);
10049 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010050 case IORING_REGISTER_FILES_UPDATE:
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010051 ret = io_register_files_update(ctx, arg, nr_args);
Jens Axboec3a31e62019-10-03 13:59:56 -060010052 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010053 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010054 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010055 ret = -EINVAL;
10056 if (nr_args != 1)
10057 break;
10058 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010059 if (ret)
10060 break;
10061 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10062 ctx->eventfd_async = 1;
10063 else
10064 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010065 break;
10066 case IORING_UNREGISTER_EVENTFD:
10067 ret = -EINVAL;
10068 if (arg || nr_args)
10069 break;
10070 ret = io_eventfd_unregister(ctx);
10071 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010072 case IORING_REGISTER_PROBE:
10073 ret = -EINVAL;
10074 if (!arg || nr_args > 256)
10075 break;
10076 ret = io_probe(ctx, arg, nr_args);
10077 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010078 case IORING_REGISTER_PERSONALITY:
10079 ret = -EINVAL;
10080 if (arg || nr_args)
10081 break;
10082 ret = io_register_personality(ctx);
10083 break;
10084 case IORING_UNREGISTER_PERSONALITY:
10085 ret = -EINVAL;
10086 if (arg)
10087 break;
10088 ret = io_unregister_personality(ctx, nr_args);
10089 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010090 case IORING_REGISTER_ENABLE_RINGS:
10091 ret = -EINVAL;
10092 if (arg || nr_args)
10093 break;
10094 ret = io_register_enable_rings(ctx);
10095 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010096 case IORING_REGISTER_RESTRICTIONS:
10097 ret = io_register_restrictions(ctx, arg, nr_args);
10098 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010099 case IORING_REGISTER_FILES2:
10100 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE);
Pavel Begunkov792e3582021-04-25 14:32:21 +010010101 break;
Pavel Begunkov992da012021-06-10 16:37:37 +010010102 case IORING_REGISTER_FILES_UPDATE2:
10103 ret = io_register_rsrc_update(ctx, arg, nr_args,
10104 IORING_RSRC_FILE);
10105 break;
10106 case IORING_REGISTER_BUFFERS2:
10107 ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER);
10108 break;
10109 case IORING_REGISTER_BUFFERS_UPDATE:
10110 ret = io_register_rsrc_update(ctx, arg, nr_args,
10111 IORING_RSRC_BUFFER);
Pavel Begunkovc3bdad02021-04-25 14:32:22 +010010112 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010113 default:
10114 ret = -EINVAL;
10115 break;
10116 }
10117
Jens Axboe071698e2020-01-28 10:04:42 -070010118 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010119 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010120 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010121 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010122 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010123 return ret;
10124}
10125
10126SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10127 void __user *, arg, unsigned int, nr_args)
10128{
10129 struct io_ring_ctx *ctx;
10130 long ret = -EBADF;
10131 struct fd f;
10132
10133 f = fdget(fd);
10134 if (!f.file)
10135 return -EBADF;
10136
10137 ret = -EOPNOTSUPP;
10138 if (f.file->f_op != &io_uring_fops)
10139 goto out_fput;
10140
10141 ctx = f.file->private_data;
10142
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010143 io_run_task_work();
10144
Jens Axboeedafcce2019-01-09 09:16:05 -070010145 mutex_lock(&ctx->uring_lock);
10146 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10147 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010148 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10149 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010150out_fput:
10151 fdput(f);
10152 return ret;
10153}
10154
Jens Axboe2b188cc2019-01-07 10:46:33 -070010155static int __init io_uring_init(void)
10156{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010157#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10158 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10159 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10160} while (0)
10161
10162#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10163 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10164 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10165 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10166 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10167 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10168 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10169 BUILD_BUG_SQE_ELEM(8, __u64, off);
10170 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10171 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010172 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010173 BUILD_BUG_SQE_ELEM(24, __u32, len);
10174 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10175 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10176 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10177 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010178 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10179 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010180 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10181 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10182 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10183 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10184 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10185 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10186 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10187 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010188 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010189 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10190 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10191 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010192 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010193
Pavel Begunkovb0d658ec2021-04-27 16:13:53 +010010194 BUILD_BUG_ON(sizeof(struct io_uring_files_update) !=
10195 sizeof(struct io_uring_rsrc_update));
10196 BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) >
10197 sizeof(struct io_uring_rsrc_update2));
10198 /* should fit into one byte */
10199 BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8));
10200
Jens Axboed3656342019-12-18 09:50:26 -070010201 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010202 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -070010203 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10204 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010205 return 0;
10206};
10207__initcall(io_uring_init);