blob: 1501f20fde844729edb8a62b08a053d03cb62fd4 [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 Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060084#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020086#define CREATE_TRACE_POINTS
87#include <trace/events/io_uring.h>
88
Jens Axboe2b188cc2019-01-07 10:46:33 -070089#include <uapi/linux/io_uring.h>
90
91#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060092#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070093
Daniel Xu5277dea2019-09-14 14:23:45 -070094#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060095#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060096
97/*
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
99 */
100#define IORING_FILE_TABLE_SHIFT 9
101#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200104#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700106
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000107#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
108 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
109 IOSQE_BUFFER_SELECT)
110
Jens Axboe2b188cc2019-01-07 10:46:33 -0700111struct io_uring {
112 u32 head ____cacheline_aligned_in_smp;
113 u32 tail ____cacheline_aligned_in_smp;
114};
115
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 * This data is shared with the application through the mmap at offsets
118 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 *
120 * The offsets to the member fields are published through struct
121 * io_sqring_offsets when calling io_uring_setup.
122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Head and tail offsets into the ring; the offsets need to be
126 * masked to get valid indices.
127 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * The kernel controls head of the sq ring and the tail of the cq ring,
129 * and the application controls tail of the sq ring and the head of the
130 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 * ring_entries - 1)
136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 u32 sq_ring_mask, cq_ring_mask;
138 /* Ring sizes (constant, power of 2) */
139 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 /*
141 * Number of invalid entries dropped by the kernel due to
142 * invalid index stored in array
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application (i.e. get number of "new events" by comparing to
146 * cached value).
147 *
148 * After a new SQ head value was read by the application this
149 * counter includes all submissions that were dropped reaching
150 * the new SQ head (and possibly more).
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200154 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 *
156 * Written by the kernel, shouldn't be modified by the
157 * application.
158 *
159 * The application needs a full memory barrier before checking
160 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
161 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000162 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200163 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200164 * Runtime CQ flags
165 *
166 * Written by the application, shouldn't be modified by the
167 * kernel.
168 */
169 u32 cq_flags;
170 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200171 * Number of completion events lost because the queue was full;
172 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800173 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200174 * the completion queue.
175 *
176 * Written by the kernel, shouldn't be modified by the
177 * application (i.e. get number of "new events" by comparing to
178 * cached value).
179 *
180 * As completion events come in out of order this counter is not
181 * ordered with any other data.
182 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000183 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200184 /*
185 * Ring buffer of completion events.
186 *
187 * The kernel writes completion events fresh every time they are
188 * produced, so the application is allowed to modify pending
189 * entries.
190 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000191 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700192};
193
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000194enum io_uring_cmd_flags {
195 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000196 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000197};
198
Jens Axboeedafcce2019-01-09 09:16:05 -0700199struct io_mapped_ubuf {
200 u64 ubuf;
201 size_t len;
202 struct bio_vec *bvec;
203 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600204 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700205};
206
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000207struct io_ring_ctx;
208
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000209struct io_rsrc_put {
210 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000211 union {
212 void *rsrc;
213 struct file *file;
214 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000215};
216
217struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600218 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700219};
220
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000221struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800222 struct percpu_ref refs;
223 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000224 struct list_head rsrc_list;
225 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000226 void (*rsrc_put)(struct io_ring_ctx *ctx,
227 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600228 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000229 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800230};
231
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000232struct fixed_rsrc_data {
233 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700234 struct io_ring_ctx *ctx;
235
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000236 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700237 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700238 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800239 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700240};
241
Jens Axboe5a2e7452020-02-23 16:23:11 -0700242struct io_buffer {
243 struct list_head list;
244 __u64 addr;
245 __s32 len;
246 __u16 bid;
247};
248
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200249struct io_restriction {
250 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
251 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
252 u8 sqe_flags_allowed;
253 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200254 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200255};
256
Jens Axboe534ca6d2020-09-02 13:52:19 -0600257struct io_sq_data {
258 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600259 struct mutex lock;
260
261 /* ctx's that are using this sqd */
262 struct list_head ctx_list;
263 struct list_head ctx_new_list;
264 struct mutex ctx_lock;
265
Jens Axboe534ca6d2020-09-02 13:52:19 -0600266 struct task_struct *thread;
267 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800268
269 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600270};
271
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000272#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000273#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000274#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000275#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000276
277struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000278 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000279 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700280 unsigned int locked_free_nr;
281 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000282 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700283 /* IRQ completion list, under ->completion_lock */
284 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000285};
286
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000287struct io_submit_link {
288 struct io_kiocb *head;
289 struct io_kiocb *last;
290};
291
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000292struct io_submit_state {
293 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000294 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000295
296 /*
297 * io_kiocb alloc cache
298 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000299 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000300 unsigned int free_reqs;
301
302 bool plug_started;
303
304 /*
305 * Batch completion logic
306 */
307 struct io_comp_state comp;
308
309 /*
310 * File reference cache
311 */
312 struct file *file;
313 unsigned int fd;
314 unsigned int file_refs;
315 unsigned int ios_left;
316};
317
Jens Axboe2b188cc2019-01-07 10:46:33 -0700318struct io_ring_ctx {
319 struct {
320 struct percpu_ref refs;
321 } ____cacheline_aligned_in_smp;
322
323 struct {
324 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800325 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700326 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800327 unsigned int cq_overflow_flushed: 1;
328 unsigned int drain_next: 1;
329 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200330 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000331 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332
Hristo Venev75b28af2019-08-26 17:23:46 +0000333 /*
334 * Ring buffer of indices into array of io_uring_sqe, which is
335 * mmapped by the application using the IORING_OFF_SQES offset.
336 *
337 * This indirection could e.g. be used to assign fixed
338 * io_uring_sqe entries to operations and only submit them to
339 * the queue when needed.
340 *
341 * The kernel modifies neither the indices array nor the entries
342 * array.
343 */
344 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345 unsigned cached_sq_head;
346 unsigned sq_entries;
347 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700348 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600349 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100350 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700351 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600352
353 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600354 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700355 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356
Jens Axboead3eb2c2019-12-18 17:12:20 -0700357 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700358 } ____cacheline_aligned_in_smp;
359
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700360 struct {
361 struct mutex uring_lock;
362 wait_queue_head_t wait;
363 } ____cacheline_aligned_in_smp;
364
365 struct io_submit_state submit_state;
366
Hristo Venev75b28af2019-08-26 17:23:46 +0000367 struct io_rings *rings;
368
Jens Axboe2b188cc2019-01-07 10:46:33 -0700369 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600370 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600371
372 /*
373 * For SQPOLL usage - we hold a reference to the parent task, so we
374 * have access to the ->files
375 */
376 struct task_struct *sqo_task;
377
378 /* Only used for accounting purposes */
379 struct mm_struct *mm_account;
380
Dennis Zhou91d8f512020-09-16 13:41:05 -0700381#ifdef CONFIG_BLK_CGROUP
382 struct cgroup_subsys_state *sqo_blkcg_css;
383#endif
384
Jens Axboe534ca6d2020-09-02 13:52:19 -0600385 struct io_sq_data *sq_data; /* if using sq thread polling */
386
Jens Axboe90554202020-09-03 12:12:41 -0600387 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600388 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700389
Jens Axboe6b063142019-01-10 22:13:58 -0700390 /*
391 * If used, fixed file set. Writers must ensure that ->refs is dead,
392 * readers must ensure that ->refs is alive as long as the file* is
393 * used. Only updated through io_uring_register(2).
394 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000395 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700396 unsigned nr_user_files;
397
Jens Axboeedafcce2019-01-09 09:16:05 -0700398 /* if used, fixed mapped user buffers */
399 unsigned nr_user_bufs;
400 struct io_mapped_ubuf *user_bufs;
401
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402 struct user_struct *user;
403
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700404 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700405
Jens Axboe4ea33a92020-10-15 13:46:44 -0600406#ifdef CONFIG_AUDIT
407 kuid_t loginuid;
408 unsigned int sessionid;
409#endif
410
Jens Axboe0f158b42020-05-14 17:18:39 -0600411 struct completion ref_comp;
412 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700413
414#if defined(CONFIG_UNIX)
415 struct socket *ring_sock;
416#endif
417
Jens Axboe5a2e7452020-02-23 16:23:11 -0700418 struct idr io_buffer_idr;
419
Jens Axboe071698e2020-01-28 10:04:42 -0700420 struct idr personality_idr;
421
Jens Axboe206aefd2019-11-07 18:27:42 -0700422 struct {
423 unsigned cached_cq_tail;
424 unsigned cq_entries;
425 unsigned cq_mask;
426 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500427 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700428 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700429 struct wait_queue_head cq_wait;
430 struct fasync_struct *cq_fasync;
431 struct eventfd_ctx *cq_ev_fd;
432 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433
434 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700435 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700436
Jens Axboedef596e2019-01-09 08:59:42 -0700437 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300438 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700439 * io_uring instances that don't use IORING_SETUP_SQPOLL.
440 * For SQPOLL, only the single threaded io_sq_thread() will
441 * manipulate the list, hence no extra locking is needed there.
442 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300443 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700444 struct hlist_head *cancel_hash;
445 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700446 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600447
448 spinlock_t inflight_lock;
449 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700450 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600451
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000452 struct delayed_work rsrc_put_work;
453 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000454 struct list_head rsrc_ref_list;
455 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600456
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200457 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700458
459 /* Keep this last, we don't need it for the fast path */
460 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Jens Axboe09bb8392019-03-13 12:39:28 -0600463/*
464 * First field must be the file pointer in all the
465 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
466 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700467struct io_poll_iocb {
468 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000469 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700470 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600471 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700472 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700473 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700474};
475
Pavel Begunkov018043b2020-10-27 23:17:18 +0000476struct io_poll_remove {
477 struct file *file;
478 u64 addr;
479};
480
Jens Axboeb5dba592019-12-11 14:02:38 -0700481struct io_close {
482 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700483 int fd;
484};
485
Jens Axboead8a48a2019-11-15 08:49:11 -0700486struct io_timeout_data {
487 struct io_kiocb *req;
488 struct hrtimer timer;
489 struct timespec64 ts;
490 enum hrtimer_mode mode;
491};
492
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700493struct io_accept {
494 struct file *file;
495 struct sockaddr __user *addr;
496 int __user *addr_len;
497 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600498 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700499};
500
501struct io_sync {
502 struct file *file;
503 loff_t len;
504 loff_t off;
505 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700506 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700507};
508
Jens Axboefbf23842019-12-17 18:45:56 -0700509struct io_cancel {
510 struct file *file;
511 u64 addr;
512};
513
Jens Axboeb29472e2019-12-17 18:50:29 -0700514struct io_timeout {
515 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300516 u32 off;
517 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300518 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000519 /* head of the link, used by linked timeouts only */
520 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700521};
522
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100523struct io_timeout_rem {
524 struct file *file;
525 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000526
527 /* timeout update */
528 struct timespec64 ts;
529 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100530};
531
Jens Axboe9adbd452019-12-20 08:45:55 -0700532struct io_rw {
533 /* NOTE: kiocb has the file as the first member, so don't do it here */
534 struct kiocb kiocb;
535 u64 addr;
536 u64 len;
537};
538
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700539struct io_connect {
540 struct file *file;
541 struct sockaddr __user *addr;
542 int addr_len;
543};
544
Jens Axboee47293f2019-12-20 08:58:21 -0700545struct io_sr_msg {
546 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700547 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300548 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700549 void __user *buf;
550 };
Jens Axboee47293f2019-12-20 08:58:21 -0700551 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700552 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700553 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700554 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700555};
556
Jens Axboe15b71ab2019-12-11 11:20:36 -0700557struct io_open {
558 struct file *file;
559 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700560 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700561 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600562 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700563};
564
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000565struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700566 struct file *file;
567 u64 arg;
568 u32 nr_args;
569 u32 offset;
570};
571
Jens Axboe4840e412019-12-25 22:03:45 -0700572struct io_fadvise {
573 struct file *file;
574 u64 offset;
575 u32 len;
576 u32 advice;
577};
578
Jens Axboec1ca7572019-12-25 22:18:28 -0700579struct io_madvise {
580 struct file *file;
581 u64 addr;
582 u32 len;
583 u32 advice;
584};
585
Jens Axboe3e4827b2020-01-08 15:18:09 -0700586struct io_epoll {
587 struct file *file;
588 int epfd;
589 int op;
590 int fd;
591 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700592};
593
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300594struct io_splice {
595 struct file *file_out;
596 struct file *file_in;
597 loff_t off_out;
598 loff_t off_in;
599 u64 len;
600 unsigned int flags;
601};
602
Jens Axboeddf0322d2020-02-23 16:41:33 -0700603struct io_provide_buf {
604 struct file *file;
605 __u64 addr;
606 __s32 len;
607 __u32 bgid;
608 __u16 nbufs;
609 __u16 bid;
610};
611
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700612struct io_statx {
613 struct file *file;
614 int dfd;
615 unsigned int mask;
616 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700617 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700618 struct statx __user *buffer;
619};
620
Jens Axboe36f4fa62020-09-05 11:14:22 -0600621struct io_shutdown {
622 struct file *file;
623 int how;
624};
625
Jens Axboe80a261f2020-09-28 14:23:58 -0600626struct io_rename {
627 struct file *file;
628 int old_dfd;
629 int new_dfd;
630 struct filename *oldpath;
631 struct filename *newpath;
632 int flags;
633};
634
Jens Axboe14a11432020-09-28 14:27:37 -0600635struct io_unlink {
636 struct file *file;
637 int dfd;
638 int flags;
639 struct filename *filename;
640};
641
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300642struct io_completion {
643 struct file *file;
644 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300645 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300646};
647
Jens Axboef499a022019-12-02 16:28:46 -0700648struct io_async_connect {
649 struct sockaddr_storage address;
650};
651
Jens Axboe03b12302019-12-02 18:50:25 -0700652struct io_async_msghdr {
653 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000654 /* points to an allocated iov, if NULL we use fast_iov instead */
655 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700656 struct sockaddr __user *uaddr;
657 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700658 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700659};
660
Jens Axboef67676d2019-12-02 11:03:47 -0700661struct io_async_rw {
662 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600663 const struct iovec *free_iovec;
664 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600665 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600666 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700667};
668
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300669enum {
670 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
671 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
672 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
673 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
674 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700675 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300676
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300677 REQ_F_FAIL_LINK_BIT,
678 REQ_F_INFLIGHT_BIT,
679 REQ_F_CUR_POS_BIT,
680 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300681 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300682 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300683 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700684 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700685 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600686 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800687 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100688 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000689 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700690
691 /* not a real bit, just to check we're not overflowing the space */
692 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300693};
694
695enum {
696 /* ctx owns file */
697 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
698 /* drain existing IO first */
699 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
700 /* linked sqes */
701 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
702 /* doesn't sever on completion < 0 */
703 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
704 /* IOSQE_ASYNC */
705 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700706 /* IOSQE_BUFFER_SELECT */
707 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300708
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300709 /* fail rest of links */
710 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
711 /* on inflight list */
712 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
713 /* read/write uses file position */
714 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
715 /* must not punt to workers */
716 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100717 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300718 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300719 /* regular file */
720 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300721 /* needs cleanup */
722 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700723 /* already went through poll handler */
724 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700725 /* buffer already selected */
726 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600727 /* doesn't need file table for this request */
728 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800729 /* io_wq_work is initialized */
730 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100731 /* linked timeout is active, i.e. prepared by link's head */
732 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000733 /* completion is deferred through io_comp_state */
734 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700735};
736
737struct async_poll {
738 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600739 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300740};
741
Jens Axboe7cbf1722021-02-10 00:03:20 +0000742struct io_task_work {
743 struct io_wq_work_node node;
744 task_work_func_t func;
745};
746
Jens Axboe09bb8392019-03-13 12:39:28 -0600747/*
748 * NOTE! Each of the iocb union members has the file pointer
749 * as the first entry in their struct definition. So you can
750 * access the file pointer through any of the sub-structs,
751 * or directly as just 'ki_filp' in this struct.
752 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700753struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700754 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600755 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700756 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700757 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000758 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700759 struct io_accept accept;
760 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700761 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700762 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100763 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700764 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700765 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700766 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700767 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000768 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700769 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700770 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700771 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300772 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700773 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700774 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600775 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600776 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600777 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300778 /* use only after cleaning per-op data, see io_clean_op() */
779 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700780 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700781
Jens Axboee8c2bc12020-08-15 18:44:09 -0700782 /* opcode allocated if it needs to store data for async defer */
783 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700784 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800785 /* polled IO has completed */
786 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700788 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300789 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700790
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300791 struct io_ring_ctx *ctx;
792 unsigned int flags;
793 refcount_t refs;
794 struct task_struct *task;
795 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700796
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000797 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000798 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700799
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300800 /*
801 * 1. used with ctx->iopoll_list with reads/writes
802 * 2. to track reqs with ->files (see io_op_def::file_table)
803 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300804 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000805 union {
806 struct io_task_work io_task_work;
807 struct callback_head task_work;
808 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300809 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
810 struct hlist_node hash_node;
811 struct async_poll *apoll;
812 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813};
814
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300815struct io_defer_entry {
816 struct list_head list;
817 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300818 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300819};
820
Jens Axboed3656342019-12-18 09:50:26 -0700821struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700822 /* needs req->file assigned */
823 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700824 /* hash wq insertion if file is a regular file */
825 unsigned hash_reg_file : 1;
826 /* unbound wq insertion if file is a non-regular file */
827 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700828 /* opcode is not supported by this kernel */
829 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700830 /* set if opcode supports polled "wait" */
831 unsigned pollin : 1;
832 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700833 /* op supports buffer selection */
834 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 /* must always have async data allocated */
836 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600837 /* should block plug */
838 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700839 /* size of async data needed, if any */
840 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600841 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700842};
843
Jens Axboe09186822020-10-13 15:01:40 -0600844static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_NOP] = {},
846 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700847 .needs_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700849 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700850 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700851 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600852 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700853 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600854 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700855 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300856 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700857 .needs_file = 1,
858 .hash_reg_file = 1,
859 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700860 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700861 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600862 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700863 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600864 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
865 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700868 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600869 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700872 .needs_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700874 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600875 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700876 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600877 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700880 .needs_file = 1,
881 .hash_reg_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700883 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600884 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700885 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600886 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
887 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700888 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300889 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
892 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300893 [IORING_OP_POLL_REMOVE] = {},
894 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700895 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600896 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700897 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300898 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700899 .needs_file = 1,
900 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700901 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700902 .needs_async_data = 1,
903 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000904 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700905 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300906 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700907 .needs_file = 1,
908 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700909 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700910 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700911 .needs_async_data = 1,
912 .async_size = sizeof(struct io_async_msghdr),
Pavel Begunkov10cad2c2020-11-07 13:20:39 +0000913 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700914 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300915 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700916 .needs_async_data = 1,
917 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600918 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700919 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000920 [IORING_OP_TIMEOUT_REMOVE] = {
921 /* used by timeout updates' prep() */
922 .work_flags = IO_WQ_WORK_MM,
923 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300924 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700925 .needs_file = 1,
926 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700927 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600928 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700929 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300930 [IORING_OP_ASYNC_CANCEL] = {},
931 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700932 .needs_async_data = 1,
933 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600934 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700935 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300936 [IORING_OP_CONNECT] = {
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,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700940 .needs_async_data = 1,
941 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600942 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700943 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300944 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700945 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600946 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700947 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300948 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600949 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600950 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700951 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300952 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600953 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700954 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300955 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600956 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700957 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300958 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600959 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
960 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700961 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300962 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700963 .needs_file = 1,
964 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700965 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700966 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600967 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700968 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600969 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700970 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300971 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700972 .needs_file = 1,
973 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700974 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600975 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700976 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600977 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
978 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700979 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300980 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700981 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600982 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700983 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300984 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600985 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700986 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300987 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700988 .needs_file = 1,
989 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700990 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600991 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700992 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300993 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700994 .needs_file = 1,
995 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700996 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700997 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600998 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700999 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001000 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -06001001 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -06001002 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -07001003 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001004 [IORING_OP_EPOLL_CTL] = {
1005 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001006 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001007 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001008 [IORING_OP_SPLICE] = {
1009 .needs_file = 1,
1010 .hash_reg_file = 1,
1011 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001012 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001013 },
1014 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001015 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001016 [IORING_OP_TEE] = {
1017 .needs_file = 1,
1018 .hash_reg_file = 1,
1019 .unbound_nonreg_file = 1,
1020 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001021 [IORING_OP_SHUTDOWN] = {
1022 .needs_file = 1,
1023 },
Jens Axboe80a261f2020-09-28 14:23:58 -06001024 [IORING_OP_RENAMEAT] = {
1025 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1026 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1027 },
Jens Axboe14a11432020-09-28 14:27:37 -06001028 [IORING_OP_UNLINKAT] = {
1029 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1030 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1031 },
Jens Axboed3656342019-12-18 09:50:26 -07001032};
1033
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001034static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1035 struct task_struct *task,
1036 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001037static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001038static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001039 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00001040static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001041
Pavel Begunkov23faba32021-02-11 18:28:22 +00001042static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001043static void io_cqring_fill_event(struct io_kiocb *req, long res);
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 Axboec40f6372020-06-25 15:39:59 -06001046static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001047static void io_dismantle_req(struct io_kiocb *req);
1048static void io_put_task(struct task_struct *task, int nr);
1049static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001050static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001051static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001052static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001053static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001054 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001055 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001056static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001057static struct file *io_file_get(struct io_submit_state *state,
1058 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001059static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001060static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001061
Pavel Begunkov847595d2021-02-04 13:52:06 +00001062static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1063 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001064static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1065 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001066 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001067static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001068static void io_submit_flush_completions(struct io_comp_state *cs,
1069 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001070
Jens Axboe2b188cc2019-01-07 10:46:33 -07001071static struct kmem_cache *req_cachep;
1072
Jens Axboe09186822020-10-13 15:01:40 -06001073static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074
1075struct sock *io_uring_get_socket(struct file *file)
1076{
1077#if defined(CONFIG_UNIX)
1078 if (file->f_op == &io_uring_fops) {
1079 struct io_ring_ctx *ctx = file->private_data;
1080
1081 return ctx->ring_sock->sk;
1082 }
1083#endif
1084 return NULL;
1085}
1086EXPORT_SYMBOL(io_uring_get_socket);
1087
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001088#define io_for_each_link(pos, head) \
1089 for (pos = (head); pos; pos = pos->link)
1090
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001091static inline void io_clean_op(struct io_kiocb *req)
1092{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001093 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001094 __io_clean_op(req);
1095}
1096
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001097static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001098{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001099 struct io_ring_ctx *ctx = req->ctx;
1100
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001101 if (!req->fixed_rsrc_refs) {
1102 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1103 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001104 }
1105}
1106
Pavel Begunkov88f171a2021-02-20 18:03:50 +00001107static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1108{
1109 if (!percpu_ref_tryget(ref)) {
1110 /* already at zero, wait for ->release() */
1111 if (!try_wait_for_completion(compl))
1112 synchronize_rcu();
1113 return false;
1114 }
1115
1116 percpu_ref_resurrect(ref);
1117 reinit_completion(compl);
1118 percpu_ref_put(ref);
1119 return true;
1120}
1121
Pavel Begunkov08d23632020-11-06 13:00:22 +00001122static bool io_match_task(struct io_kiocb *head,
1123 struct task_struct *task,
1124 struct files_struct *files)
1125{
1126 struct io_kiocb *req;
1127
Jens Axboe84965ff2021-01-23 15:51:11 -07001128 if (task && head->task != task) {
1129 /* in terms of cancelation, always match if req task is dead */
1130 if (head->task->flags & PF_EXITING)
1131 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001132 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001133 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001134 if (!files)
1135 return true;
1136
1137 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001138 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1139 continue;
1140 if (req->file && req->file->f_op == &io_uring_fops)
1141 return true;
1142 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001143 req->work.identity->files == files)
1144 return true;
1145 }
1146 return false;
1147}
1148
Jens Axboe28cea78a2020-09-14 10:51:17 -06001149static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001150{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001151 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001152 struct mm_struct *mm = current->mm;
1153
1154 if (mm) {
1155 kthread_unuse_mm(mm);
1156 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001157 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001158 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001159 if (files) {
1160 struct nsproxy *nsproxy = current->nsproxy;
1161
1162 task_lock(current);
1163 current->files = NULL;
1164 current->nsproxy = NULL;
1165 task_unlock(current);
1166 put_files_struct(files);
1167 put_nsproxy(nsproxy);
1168 }
1169}
1170
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001171static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001172{
1173 if (!current->files) {
1174 struct files_struct *files;
1175 struct nsproxy *nsproxy;
1176
1177 task_lock(ctx->sqo_task);
1178 files = ctx->sqo_task->files;
1179 if (!files) {
1180 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001181 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001182 }
1183 atomic_inc(&files->count);
1184 get_nsproxy(ctx->sqo_task->nsproxy);
1185 nsproxy = ctx->sqo_task->nsproxy;
1186 task_unlock(ctx->sqo_task);
1187
1188 task_lock(current);
1189 current->files = files;
1190 current->nsproxy = nsproxy;
1191 task_unlock(current);
1192 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001193 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001194}
1195
1196static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1197{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001198 struct mm_struct *mm;
1199
1200 if (current->mm)
1201 return 0;
1202
Jens Axboe4b70cf92020-11-02 10:39:05 -07001203 task_lock(ctx->sqo_task);
1204 mm = ctx->sqo_task->mm;
1205 if (unlikely(!mm || !mmget_not_zero(mm)))
1206 mm = NULL;
1207 task_unlock(ctx->sqo_task);
1208
1209 if (mm) {
1210 kthread_use_mm(mm);
1211 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001212 }
1213
Jens Axboe4b70cf92020-11-02 10:39:05 -07001214 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001215}
1216
Pavel Begunkov4e326352021-02-12 03:23:52 +00001217static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1218 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001219{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001220 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001221 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001222
1223 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001224 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001225 if (unlikely(ret))
1226 return ret;
1227 }
1228
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001229 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1230 ret = __io_sq_thread_acquire_files(ctx);
1231 if (unlikely(ret))
1232 return ret;
1233 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001234
1235 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001236}
1237
Pavel Begunkov4e326352021-02-12 03:23:52 +00001238static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1239 struct io_kiocb *req)
1240{
Pavel Begunkov4e326352021-02-12 03:23:52 +00001241 if (!(ctx->flags & IORING_SETUP_SQPOLL))
1242 return 0;
1243 return __io_sq_thread_acquire_mm_files(ctx, req);
1244}
1245
Dennis Zhou91d8f512020-09-16 13:41:05 -07001246static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1247 struct cgroup_subsys_state **cur_css)
1248
1249{
1250#ifdef CONFIG_BLK_CGROUP
1251 /* puts the old one when swapping */
1252 if (*cur_css != ctx->sqo_blkcg_css) {
1253 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1254 *cur_css = ctx->sqo_blkcg_css;
1255 }
1256#endif
1257}
1258
1259static void io_sq_thread_unassociate_blkcg(void)
1260{
1261#ifdef CONFIG_BLK_CGROUP
1262 kthread_associate_blkcg(NULL);
1263#endif
1264}
1265
Jens Axboec40f6372020-06-25 15:39:59 -06001266static inline void req_set_fail_links(struct io_kiocb *req)
1267{
1268 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1269 req->flags |= REQ_F_FAIL_LINK;
1270}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001271
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001272/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001273 * None of these are dereferenced, they are simply used to check if any of
1274 * them have changed. If we're under current and check they are still the
1275 * same, we're fine to grab references to them for actual out-of-line use.
1276 */
1277static void io_init_identity(struct io_identity *id)
1278{
1279 id->files = current->files;
1280 id->mm = current->mm;
1281#ifdef CONFIG_BLK_CGROUP
1282 rcu_read_lock();
1283 id->blkcg_css = blkcg_css();
1284 rcu_read_unlock();
1285#endif
1286 id->creds = current_cred();
1287 id->nsproxy = current->nsproxy;
1288 id->fs = current->fs;
1289 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001290#ifdef CONFIG_AUDIT
1291 id->loginuid = current->loginuid;
1292 id->sessionid = current->sessionid;
1293#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001294 refcount_set(&id->count, 1);
1295}
1296
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001297static inline void __io_req_init_async(struct io_kiocb *req)
1298{
1299 memset(&req->work, 0, sizeof(req->work));
1300 req->flags |= REQ_F_WORK_INITIALIZED;
1301}
1302
Jens Axboe1e6fa522020-10-15 08:46:24 -06001303/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001304 * Note: must call io_req_init_async() for the first time you
1305 * touch any members of io_wq_work.
1306 */
1307static inline void io_req_init_async(struct io_kiocb *req)
1308{
Jens Axboe500a3732020-10-15 17:38:03 -06001309 struct io_uring_task *tctx = current->io_uring;
1310
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001311 if (req->flags & REQ_F_WORK_INITIALIZED)
1312 return;
1313
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001314 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001315
1316 /* Grab a ref if this isn't our static identity */
1317 req->work.identity = tctx->identity;
1318 if (tctx->identity != &tctx->__identity)
1319 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001320}
1321
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1323{
1324 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1325
Jens Axboe0f158b42020-05-14 17:18:39 -06001326 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327}
1328
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001329static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1330{
1331 return !req->timeout.off;
1332}
1333
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1335{
1336 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001337 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001338
1339 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1340 if (!ctx)
1341 return NULL;
1342
Jens Axboe78076bb2019-12-04 19:56:40 -07001343 /*
1344 * Use 5 bits less than the max cq entries, that should give us around
1345 * 32 entries per hash list if totally full and uniformly spread.
1346 */
1347 hash_bits = ilog2(p->cq_entries);
1348 hash_bits -= 5;
1349 if (hash_bits <= 0)
1350 hash_bits = 1;
1351 ctx->cancel_hash_bits = hash_bits;
1352 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1353 GFP_KERNEL);
1354 if (!ctx->cancel_hash)
1355 goto err;
1356 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1357
Roman Gushchin21482892019-05-07 10:01:48 -07001358 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001359 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1360 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361
1362 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001363 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001364 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001365 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001366 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001367 init_completion(&ctx->ref_comp);
1368 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001369 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001370 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001371 mutex_init(&ctx->uring_lock);
1372 init_waitqueue_head(&ctx->wait);
1373 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001374 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001375 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001376 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001377 spin_lock_init(&ctx->inflight_lock);
1378 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001379 spin_lock_init(&ctx->rsrc_ref_lock);
1380 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001381 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1382 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001383 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001384 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001385 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001386err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001387 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001388 kfree(ctx);
1389 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390}
1391
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001392static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001393{
Jens Axboe2bc99302020-07-09 09:43:27 -06001394 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1395 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001396
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001397 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001398 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001399 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001400
Bob Liu9d858b22019-11-13 18:06:25 +08001401 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001402}
1403
Jens Axboe5c3462c2020-10-15 09:02:33 -06001404static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001405{
Jens Axboe500a3732020-10-15 17:38:03 -06001406 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001407 return;
1408 if (refcount_dec_and_test(&req->work.identity->count))
1409 kfree(req->work.identity);
1410}
1411
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001412static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001413{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001414 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001415 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001416
Pavel Begunkove86d0042021-02-01 18:59:54 +00001417 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001418 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001419#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001420 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001421 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001422#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001423 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001424 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001425 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001426 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001427
Jens Axboe98447d62020-10-14 10:48:51 -06001428 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001429 if (--fs->users)
1430 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001431 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001432 if (fs)
1433 free_fs_struct(fs);
1434 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001435 if (req->work.flags & IO_WQ_WORK_FILES) {
1436 put_files_struct(req->work.identity->files);
1437 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001438 }
1439 if (req->flags & REQ_F_INFLIGHT) {
1440 struct io_ring_ctx *ctx = req->ctx;
1441 struct io_uring_task *tctx = req->task->io_uring;
1442 unsigned long flags;
1443
1444 spin_lock_irqsave(&ctx->inflight_lock, flags);
1445 list_del(&req->inflight_entry);
1446 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1447 req->flags &= ~REQ_F_INFLIGHT;
1448 if (atomic_read(&tctx->in_idle))
1449 wake_up(&tctx->wait);
1450 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001451
Pavel Begunkove86d0042021-02-01 18:59:54 +00001452 req->flags &= ~REQ_F_WORK_INITIALIZED;
1453 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1454 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001455 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001456}
1457
1458/*
1459 * Create a private copy of io_identity, since some fields don't match
1460 * the current context.
1461 */
1462static bool io_identity_cow(struct io_kiocb *req)
1463{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001464 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001465 const struct cred *creds = NULL;
1466 struct io_identity *id;
1467
1468 if (req->work.flags & IO_WQ_WORK_CREDS)
1469 creds = req->work.identity->creds;
1470
1471 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1472 if (unlikely(!id)) {
1473 req->work.flags |= IO_WQ_WORK_CANCEL;
1474 return false;
1475 }
1476
1477 /*
1478 * We can safely just re-init the creds we copied Either the field
1479 * matches the current one, or we haven't grabbed it yet. The only
1480 * exception is ->creds, through registered personalities, so handle
1481 * that one separately.
1482 */
1483 io_init_identity(id);
1484 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001485 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001486
1487 /* add one for this request */
1488 refcount_inc(&id->count);
1489
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001490 /* drop tctx and req identity references, if needed */
1491 if (tctx->identity != &tctx->__identity &&
1492 refcount_dec_and_test(&tctx->identity->count))
1493 kfree(tctx->identity);
1494 if (req->work.identity != &tctx->__identity &&
1495 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001496 kfree(req->work.identity);
1497
1498 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001499 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001500 return true;
1501}
1502
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001503static void io_req_track_inflight(struct io_kiocb *req)
1504{
1505 struct io_ring_ctx *ctx = req->ctx;
1506
1507 if (!(req->flags & REQ_F_INFLIGHT)) {
1508 io_req_init_async(req);
1509 req->flags |= REQ_F_INFLIGHT;
1510
1511 spin_lock_irq(&ctx->inflight_lock);
1512 list_add(&req->inflight_entry, &ctx->inflight_list);
1513 spin_unlock_irq(&ctx->inflight_lock);
1514 }
1515}
1516
Jens Axboe1e6fa522020-10-15 08:46:24 -06001517static bool io_grab_identity(struct io_kiocb *req)
1518{
1519 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001520 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001521
Jens Axboe69228332020-10-20 14:28:41 -06001522 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1523 if (id->fsize != rlimit(RLIMIT_FSIZE))
1524 return false;
1525 req->work.flags |= IO_WQ_WORK_FSIZE;
1526 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001527#ifdef CONFIG_BLK_CGROUP
1528 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1529 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1530 rcu_read_lock();
1531 if (id->blkcg_css != blkcg_css()) {
1532 rcu_read_unlock();
1533 return false;
1534 }
1535 /*
1536 * This should be rare, either the cgroup is dying or the task
1537 * is moving cgroups. Just punt to root for the handful of ios.
1538 */
1539 if (css_tryget_online(id->blkcg_css))
1540 req->work.flags |= IO_WQ_WORK_BLKCG;
1541 rcu_read_unlock();
1542 }
1543#endif
1544 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1545 if (id->creds != current_cred())
1546 return false;
1547 get_cred(id->creds);
1548 req->work.flags |= IO_WQ_WORK_CREDS;
1549 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001550#ifdef CONFIG_AUDIT
1551 if (!uid_eq(current->loginuid, id->loginuid) ||
1552 current->sessionid != id->sessionid)
1553 return false;
1554#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001555 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1556 (def->work_flags & IO_WQ_WORK_FS)) {
1557 if (current->fs != id->fs)
1558 return false;
1559 spin_lock(&id->fs->lock);
1560 if (!id->fs->in_exec) {
1561 id->fs->users++;
1562 req->work.flags |= IO_WQ_WORK_FS;
1563 } else {
1564 req->work.flags |= IO_WQ_WORK_CANCEL;
1565 }
1566 spin_unlock(&current->fs->lock);
1567 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001568 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1569 (def->work_flags & IO_WQ_WORK_FILES) &&
1570 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1571 if (id->files != current->files ||
1572 id->nsproxy != current->nsproxy)
1573 return false;
1574 atomic_inc(&id->files->count);
1575 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001576 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001577 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001578 }
Jens Axboe77788772020-12-29 10:50:46 -07001579 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1580 (def->work_flags & IO_WQ_WORK_MM)) {
1581 if (id->mm != current->mm)
1582 return false;
1583 mmgrab(id->mm);
1584 req->work.flags |= IO_WQ_WORK_MM;
1585 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001586
1587 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001588}
1589
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001590static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001591{
Jens Axboed3656342019-12-18 09:50:26 -07001592 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001593 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001594
Pavel Begunkov16d59802020-07-12 16:16:47 +03001595 io_req_init_async(req);
1596
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001597 if (req->flags & REQ_F_FORCE_ASYNC)
1598 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1599
Jens Axboed3656342019-12-18 09:50:26 -07001600 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001601 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001602 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001603 } else {
1604 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001605 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001606 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001607
Jens Axboe1e6fa522020-10-15 08:46:24 -06001608 /* if we fail grabbing identity, we must COW, regrab, and retry */
1609 if (io_grab_identity(req))
1610 return;
1611
1612 if (!io_identity_cow(req))
1613 return;
1614
1615 /* can't fail at this point */
1616 if (!io_grab_identity(req))
1617 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001618}
1619
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001620static void io_prep_async_link(struct io_kiocb *req)
1621{
1622 struct io_kiocb *cur;
1623
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001624 io_for_each_link(cur, req)
1625 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001626}
1627
Jens Axboe7271ef32020-08-10 09:55:22 -06001628static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001629{
Jackie Liua197f662019-11-08 08:09:12 -07001630 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001631 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001632
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001633 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1634 &req->work, req->flags);
1635 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001636 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001637}
1638
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001639static void io_queue_async_work(struct io_kiocb *req)
1640{
Jens Axboe7271ef32020-08-10 09:55:22 -06001641 struct io_kiocb *link;
1642
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001643 /* init ->work of the whole link before punting */
1644 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001645 link = __io_queue_async_work(req);
1646
1647 if (link)
1648 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001649}
1650
Jens Axboe5262f562019-09-17 12:26:57 -06001651static void io_kill_timeout(struct io_kiocb *req)
1652{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001653 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001654 int ret;
1655
Jens Axboee8c2bc12020-08-15 18:44:09 -07001656 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001657 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001658 atomic_set(&req->ctx->cq_timeouts,
1659 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001660 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001661 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001662 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001663 }
1664}
1665
Jens Axboe76e1b642020-09-26 15:05:03 -06001666/*
1667 * Returns true if we found and killed one or more timeouts
1668 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001669static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1670 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001671{
1672 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001673 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001674
1675 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001676 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001677 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001678 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001679 canceled++;
1680 }
Jens Axboef3606e32020-09-22 08:18:24 -06001681 }
Jens Axboe5262f562019-09-17 12:26:57 -06001682 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001683 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001684}
1685
Pavel Begunkov04518942020-05-26 20:34:05 +03001686static void __io_queue_deferred(struct io_ring_ctx *ctx)
1687{
1688 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001689 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1690 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001691
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001692 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001693 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001694 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001695 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001696 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001697 } while (!list_empty(&ctx->defer_list));
1698}
1699
Pavel Begunkov360428f2020-05-30 14:54:17 +03001700static void io_flush_timeouts(struct io_ring_ctx *ctx)
1701{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001702 u32 seq;
1703
1704 if (list_empty(&ctx->timeout_list))
1705 return;
1706
1707 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1708
1709 do {
1710 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001711 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001712 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001713
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001714 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001715 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001716
1717 /*
1718 * Since seq can easily wrap around over time, subtract
1719 * the last seq at which timeouts were flushed before comparing.
1720 * Assuming not more than 2^31-1 events have happened since,
1721 * these subtractions won't have wrapped, so we can check if
1722 * target is in [last_seq, current_seq] by comparing the two.
1723 */
1724 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1725 events_got = seq - ctx->cq_last_tm_flush;
1726 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001727 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001728
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001729 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001730 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001731 } while (!list_empty(&ctx->timeout_list));
1732
1733 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001734}
1735
Jens Axboede0617e2019-04-06 21:51:27 -06001736static void io_commit_cqring(struct io_ring_ctx *ctx)
1737{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001738 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001739
1740 /* order cqe stores with ring update */
1741 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001742
Pavel Begunkov04518942020-05-26 20:34:05 +03001743 if (unlikely(!list_empty(&ctx->defer_list)))
1744 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001745}
1746
Jens Axboe90554202020-09-03 12:12:41 -06001747static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1748{
1749 struct io_rings *r = ctx->rings;
1750
1751 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1752}
1753
Pavel Begunkov888aae22021-01-19 13:32:39 +00001754static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1755{
1756 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1757}
1758
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1760{
Hristo Venev75b28af2019-08-26 17:23:46 +00001761 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001762 unsigned tail;
1763
Stefan Bühler115e12e2019-04-24 23:54:18 +02001764 /*
1765 * writes to the cq entry need to come after reading head; the
1766 * control dependency is enough as we're using WRITE_ONCE to
1767 * fill the cq entry
1768 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001769 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001770 return NULL;
1771
Pavel Begunkov888aae22021-01-19 13:32:39 +00001772 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001773 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774}
1775
Jens Axboef2842ab2020-01-08 11:04:00 -07001776static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1777{
Jens Axboef0b493e2020-02-01 21:30:11 -07001778 if (!ctx->cq_ev_fd)
1779 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001780 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1781 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001782 if (!ctx->eventfd_async)
1783 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001784 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001785}
1786
Jens Axboeb41e9852020-02-17 09:52:41 -07001787static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001788{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001789 /* see waitqueue_active() comment */
1790 smp_mb();
1791
Jens Axboe8c838782019-03-12 15:48:16 -06001792 if (waitqueue_active(&ctx->wait))
1793 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001794 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1795 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001796 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001797 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001798 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001799 wake_up_interruptible(&ctx->cq_wait);
1800 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1801 }
Jens Axboe8c838782019-03-12 15:48:16 -06001802}
1803
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001804static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1805{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001806 /* see waitqueue_active() comment */
1807 smp_mb();
1808
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001809 if (ctx->flags & IORING_SETUP_SQPOLL) {
1810 if (waitqueue_active(&ctx->wait))
1811 wake_up(&ctx->wait);
1812 }
1813 if (io_should_trigger_evfd(ctx))
1814 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001815 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001816 wake_up_interruptible(&ctx->cq_wait);
1817 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1818 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001819}
1820
Jens Axboec4a2ed72019-11-21 21:01:26 -07001821/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001822static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1823 struct task_struct *tsk,
1824 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001826 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001827 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001828 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001829 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001830 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001831 LIST_HEAD(list);
1832
Pavel Begunkove23de152020-12-17 00:24:37 +00001833 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1834 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001835
Jens Axboeb18032b2021-01-24 16:58:56 -07001836 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001837 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001838 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001839 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001840 continue;
1841
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001842 cqe = io_get_cqring(ctx);
1843 if (!cqe && !force)
1844 break;
1845
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001846 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001847 if (cqe) {
1848 WRITE_ONCE(cqe->user_data, req->user_data);
1849 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001850 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001851 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001852 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001853 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001854 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001855 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001856 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001857 }
1858
Pavel Begunkov09e88402020-12-17 00:24:38 +00001859 all_flushed = list_empty(&ctx->cq_overflow_list);
1860 if (all_flushed) {
1861 clear_bit(0, &ctx->sq_check_overflow);
1862 clear_bit(0, &ctx->cq_check_overflow);
1863 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1864 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001865
Jens Axboeb18032b2021-01-24 16:58:56 -07001866 if (posted)
1867 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001868 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001869 if (posted)
1870 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001871
1872 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001873 req = list_first_entry(&list, struct io_kiocb, compl.list);
1874 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001875 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001876 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001877
Pavel Begunkov09e88402020-12-17 00:24:38 +00001878 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001879}
1880
Pavel Begunkov6c503152021-01-04 20:36:36 +00001881static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1882 struct task_struct *tsk,
1883 struct files_struct *files)
1884{
1885 if (test_bit(0, &ctx->cq_check_overflow)) {
1886 /* iopoll syncs against uring_lock, not completion_lock */
1887 if (ctx->flags & IORING_SETUP_IOPOLL)
1888 mutex_lock(&ctx->uring_lock);
1889 __io_cqring_overflow_flush(ctx, force, tsk, files);
1890 if (ctx->flags & IORING_SETUP_IOPOLL)
1891 mutex_unlock(&ctx->uring_lock);
1892 }
1893}
1894
Jens Axboebcda7ba2020-02-23 16:42:51 -07001895static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898 struct io_uring_cqe *cqe;
1899
Jens Axboe78e19bb2019-11-06 15:21:34 -07001900 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001901
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902 /*
1903 * If we can't get a cq entry, userspace overflowed the
1904 * submission (by quite a lot). Increment the overflow count in
1905 * the ring.
1906 */
1907 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001908 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001909 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001910 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001911 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001912 } else if (ctx->cq_overflow_flushed ||
1913 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001914 /*
1915 * If we're in ring overflow flush mode, or in task cancel mode,
1916 * then we cannot store the request for later flushing, we need
1917 * to drop it on the floor.
1918 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001919 ctx->cached_cq_overflow++;
1920 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001921 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001922 if (list_empty(&ctx->cq_overflow_list)) {
1923 set_bit(0, &ctx->sq_check_overflow);
1924 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001925 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001926 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001927 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001928 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001929 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001930 refcount_inc(&req->refs);
1931 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001932 }
1933}
1934
Jens Axboebcda7ba2020-02-23 16:42:51 -07001935static void io_cqring_fill_event(struct io_kiocb *req, long res)
1936{
1937 __io_cqring_fill_event(req, res, 0);
1938}
1939
Jens Axboec7dae4b2021-02-09 19:53:37 -07001940static inline void io_req_complete_post(struct io_kiocb *req, long res,
1941 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001943 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001944 unsigned long flags;
1945
1946 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001947 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001948 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001949 /*
1950 * If we're the last reference to this request, add to our locked
1951 * free_list cache.
1952 */
1953 if (refcount_dec_and_test(&req->refs)) {
1954 struct io_comp_state *cs = &ctx->submit_state.comp;
1955
1956 io_dismantle_req(req);
1957 io_put_task(req->task, 1);
1958 list_add(&req->compl.list, &cs->locked_free_list);
1959 cs->locked_free_nr++;
1960 } else
1961 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001962 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1963
Jens Axboe8c838782019-03-12 15:48:16 -06001964 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001965 if (req) {
1966 io_queue_next(req);
1967 percpu_ref_put(&ctx->refs);
1968 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969}
1970
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001971static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001972 unsigned int cflags)
Jens Axboe229a7b62020-06-22 10:13:11 -06001973{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001974 io_clean_op(req);
1975 req->result = res;
1976 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001977 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001978}
1979
Pavel Begunkov889fca72021-02-10 00:03:09 +00001980static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1981 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001982{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001983 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1984 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001985 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001986 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001987}
1988
1989static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001990{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001991 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001992}
1993
Jens Axboec7dae4b2021-02-09 19:53:37 -07001994static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001995{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001996 struct io_submit_state *state = &ctx->submit_state;
1997 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001998 struct io_kiocb *req = NULL;
1999
Jens Axboec7dae4b2021-02-09 19:53:37 -07002000 /*
2001 * If we have more than a batch's worth of requests in our IRQ side
2002 * locked cache, grab the lock and move them over to our submission
2003 * side cache.
2004 */
2005 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
2006 spin_lock_irq(&ctx->completion_lock);
2007 list_splice_init(&cs->locked_free_list, &cs->free_list);
2008 cs->locked_free_nr = 0;
2009 spin_unlock_irq(&ctx->completion_lock);
2010 }
2011
2012 while (!list_empty(&cs->free_list)) {
2013 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002014 compl.list);
2015 list_del(&req->compl.list);
2016 state->reqs[state->free_reqs++] = req;
2017 if (state->free_reqs == ARRAY_SIZE(state->reqs))
2018 break;
2019 }
2020
2021 return req != NULL;
2022}
2023
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002024static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002026 struct io_submit_state *state = &ctx->submit_state;
2027
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002028 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
2029
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002030 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002031 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002032 int ret;
2033
Jens Axboec7dae4b2021-02-09 19:53:37 -07002034 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002035 goto got_req;
2036
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002037 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
2038 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06002039
2040 /*
2041 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2042 * retry single alloc to be on the safe side.
2043 */
2044 if (unlikely(ret <= 0)) {
2045 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2046 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00002047 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06002048 ret = 1;
2049 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002050 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002052got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03002053 state->free_reqs--;
2054 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002055}
2056
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002057static inline void io_put_file(struct io_kiocb *req, struct file *file,
2058 bool fixed)
2059{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002060 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002061 fput(file);
2062}
2063
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002064static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002065{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002066 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002067
Jens Axboee8c2bc12020-08-15 18:44:09 -07002068 if (req->async_data)
2069 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002070 if (req->file)
2071 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002072 if (req->fixed_rsrc_refs)
2073 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002074 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002075}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002076
Pavel Begunkov7c660732021-01-25 11:42:21 +00002077static inline void io_put_task(struct task_struct *task, int nr)
2078{
2079 struct io_uring_task *tctx = task->io_uring;
2080
2081 percpu_counter_sub(&tctx->inflight, nr);
2082 if (unlikely(atomic_read(&tctx->in_idle)))
2083 wake_up(&tctx->wait);
2084 put_task_struct_many(task, nr);
2085}
2086
Pavel Begunkov216578e2020-10-13 09:44:00 +01002087static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002088{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002089 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002090
Pavel Begunkov216578e2020-10-13 09:44:00 +01002091 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002092 io_put_task(req->task, 1);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002093
Pavel Begunkov3893f392021-02-10 00:03:15 +00002094 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002095 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002096}
2097
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002098static inline void io_remove_next_linked(struct io_kiocb *req)
2099{
2100 struct io_kiocb *nxt = req->link;
2101
2102 req->link = nxt->link;
2103 nxt->link = NULL;
2104}
2105
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002106static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002107{
Jackie Liua197f662019-11-08 08:09:12 -07002108 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002109 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002110 bool cancelled = false;
2111 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002112
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002113 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002114 link = req->link;
2115
Pavel Begunkov900fad42020-10-19 16:39:16 +01002116 /*
2117 * Can happen if a linked timeout fired and link had been like
2118 * req -> link t-out -> link t-out [-> ...]
2119 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002120 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2121 struct io_timeout_data *io = link->async_data;
2122 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002123
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002124 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002125 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002126 ret = hrtimer_try_to_cancel(&io->timer);
2127 if (ret != -1) {
2128 io_cqring_fill_event(link, -ECANCELED);
2129 io_commit_cqring(ctx);
2130 cancelled = true;
2131 }
2132 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002133 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002134 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002135
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002136 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002137 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002138 io_put_req(link);
2139 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002140}
2141
Jens Axboe4d7dd462019-11-20 13:03:52 -07002142
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002143static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002144{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002145 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002146 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002147 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002148
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002149 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002150 link = req->link;
2151 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002152
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002153 while (link) {
2154 nxt = link->link;
2155 link->link = NULL;
2156
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002157 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002158 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002159
2160 /*
2161 * It's ok to free under spinlock as they're not linked anymore,
2162 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2163 * work.fs->lock.
2164 */
2165 if (link->flags & REQ_F_WORK_INITIALIZED)
2166 io_put_req_deferred(link, 2);
2167 else
2168 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002169 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002170 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002171 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002172 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002173
Jens Axboe2665abf2019-11-05 12:40:47 -07002174 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002175}
2176
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002177static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002178{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002179 if (req->flags & REQ_F_LINK_TIMEOUT)
2180 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002181
Jens Axboe9e645e112019-05-10 16:07:28 -06002182 /*
2183 * If LINK is set, we have dependent requests in this chain. If we
2184 * didn't fail this request, queue the first one up, moving any other
2185 * dependencies to the next request. In case of failure, fail the rest
2186 * of the chain.
2187 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002188 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2189 struct io_kiocb *nxt = req->link;
2190
2191 req->link = NULL;
2192 return nxt;
2193 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002194 io_fail_links(req);
2195 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002196}
Jens Axboe2665abf2019-11-05 12:40:47 -07002197
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002198static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002199{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002200 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002201 return NULL;
2202 return __io_req_find_next(req);
2203}
2204
Jens Axboe7cbf1722021-02-10 00:03:20 +00002205static bool __tctx_task_work(struct io_uring_task *tctx)
2206{
Jens Axboe65453d12021-02-10 00:03:21 +00002207 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002208 struct io_wq_work_list list;
2209 struct io_wq_work_node *node;
2210
2211 if (wq_list_empty(&tctx->task_list))
2212 return false;
2213
Jens Axboe0b81e802021-02-16 10:33:53 -07002214 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002215 list = tctx->task_list;
2216 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002217 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002218
2219 node = list.first;
2220 while (node) {
2221 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00002222 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002223 struct io_kiocb *req;
2224
2225 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00002226 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002227 req->task_work.func(&req->task_work);
2228 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00002229
2230 if (!ctx) {
2231 ctx = this_ctx;
2232 } else if (ctx != this_ctx) {
2233 mutex_lock(&ctx->uring_lock);
2234 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2235 mutex_unlock(&ctx->uring_lock);
2236 ctx = this_ctx;
2237 }
2238 }
2239
2240 if (ctx && ctx->submit_state.comp.nr) {
2241 mutex_lock(&ctx->uring_lock);
2242 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2243 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002244 }
2245
2246 return list.first != NULL;
2247}
2248
2249static void tctx_task_work(struct callback_head *cb)
2250{
2251 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
2252
2253 while (__tctx_task_work(tctx))
2254 cond_resched();
2255
2256 clear_bit(0, &tctx->task_state);
2257}
2258
2259static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
2260 enum task_work_notify_mode notify)
2261{
2262 struct io_uring_task *tctx = tsk->io_uring;
2263 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07002264 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002265 int ret;
2266
2267 WARN_ON_ONCE(!tctx);
2268
Jens Axboe0b81e802021-02-16 10:33:53 -07002269 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002270 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002271 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002272
2273 /* task_work already pending, we're done */
2274 if (test_bit(0, &tctx->task_state) ||
2275 test_and_set_bit(0, &tctx->task_state))
2276 return 0;
2277
2278 if (!task_work_add(tsk, &tctx->task_work, notify))
2279 return 0;
2280
2281 /*
2282 * Slow path - we failed, find and delete work. if the work is not
2283 * in the list, it got run and we're fine.
2284 */
2285 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07002286 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002287 wq_list_for_each(node, prev, &tctx->task_list) {
2288 if (&req->io_task_work.node == node) {
2289 wq_list_del(&tctx->task_list, node, prev);
2290 ret = 1;
2291 break;
2292 }
2293 }
Jens Axboe0b81e802021-02-16 10:33:53 -07002294 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002295 clear_bit(0, &tctx->task_state);
2296 return ret;
2297}
2298
Jens Axboe355fb9e2020-10-22 20:19:35 -06002299static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002300{
2301 struct task_struct *tsk = req->task;
2302 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002303 enum task_work_notify_mode notify;
2304 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002305
Jens Axboe6200b0a2020-09-13 14:38:30 -06002306 if (tsk->flags & PF_EXITING)
2307 return -ESRCH;
2308
Jens Axboec2c4c832020-07-01 15:37:11 -06002309 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002310 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2311 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2312 * processing task_work. There's no reliable way to tell if TWA_RESUME
2313 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002314 */
Jens Axboe91989c72020-10-16 09:02:26 -06002315 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002316 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002317 notify = TWA_SIGNAL;
2318
Jens Axboe7cbf1722021-02-10 00:03:20 +00002319 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002320 if (!ret)
2321 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002322
Jens Axboec2c4c832020-07-01 15:37:11 -06002323 return ret;
2324}
2325
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002326static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002327 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002328{
2329 struct task_struct *tsk = io_wq_get_task(req->ctx->io_wq);
2330
2331 init_task_work(&req->task_work, cb);
2332 task_work_add(tsk, &req->task_work, TWA_NONE);
2333 wake_up_process(tsk);
2334}
2335
Jens Axboec40f6372020-06-25 15:39:59 -06002336static void __io_req_task_cancel(struct io_kiocb *req, int error)
2337{
2338 struct io_ring_ctx *ctx = req->ctx;
2339
2340 spin_lock_irq(&ctx->completion_lock);
2341 io_cqring_fill_event(req, error);
2342 io_commit_cqring(ctx);
2343 spin_unlock_irq(&ctx->completion_lock);
2344
2345 io_cqring_ev_posted(ctx);
2346 req_set_fail_links(req);
2347 io_double_put_req(req);
2348}
2349
2350static void io_req_task_cancel(struct callback_head *cb)
2351{
2352 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002353 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002354
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002355 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002356 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002357 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002358 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002359}
2360
2361static void __io_req_task_submit(struct io_kiocb *req)
2362{
2363 struct io_ring_ctx *ctx = req->ctx;
2364
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002365 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002366 mutex_lock(&ctx->uring_lock);
Pavel Begunkovdc0eced2021-02-12 18:41:15 +00002367 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) &&
2368 !io_sq_thread_acquire_mm_files(ctx, req))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002369 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002370 else
Jens Axboec40f6372020-06-25 15:39:59 -06002371 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002372 mutex_unlock(&ctx->uring_lock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002373}
2374
Jens Axboec40f6372020-06-25 15:39:59 -06002375static void io_req_task_submit(struct callback_head *cb)
2376{
2377 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2378
2379 __io_req_task_submit(req);
2380}
2381
2382static void io_req_task_queue(struct io_kiocb *req)
2383{
Jens Axboec40f6372020-06-25 15:39:59 -06002384 int ret;
2385
Jens Axboe7cbf1722021-02-10 00:03:20 +00002386 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002387 ret = io_req_task_work_add(req);
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002388 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00002389 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002390 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002391 io_req_task_work_add_fallback(req, io_req_task_cancel);
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002392 }
Jens Axboec40f6372020-06-25 15:39:59 -06002393}
2394
Pavel Begunkova3df76982021-02-18 22:32:52 +00002395static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2396{
2397 percpu_ref_get(&req->ctx->refs);
2398 req->result = ret;
2399 req->task_work.func = io_req_task_cancel;
2400
2401 if (unlikely(io_req_task_work_add(req)))
2402 io_req_task_work_add_fallback(req, io_req_task_cancel);
2403}
2404
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002405static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002406{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002407 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002408
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002409 if (nxt)
2410 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002411}
2412
Jens Axboe9e645e112019-05-10 16:07:28 -06002413static void io_free_req(struct io_kiocb *req)
2414{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002415 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002416 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002417}
2418
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002419struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002420 struct task_struct *task;
2421 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002422 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002423};
2424
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002425static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002426{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002427 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002428 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002429 rb->task = NULL;
2430}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002431
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002432static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2433 struct req_batch *rb)
2434{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002435 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002436 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002437 if (rb->ctx_refs)
2438 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002439}
2440
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002441static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2442 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002443{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002444 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002445
Jens Axboee3bc8e92020-09-24 08:45:57 -06002446 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002447 if (rb->task)
2448 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002449 rb->task = req->task;
2450 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002451 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002452 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002453 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002454
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002455 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002456 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002457 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002458 else
2459 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002460}
2461
Pavel Begunkov905c1722021-02-10 00:03:14 +00002462static void io_submit_flush_completions(struct io_comp_state *cs,
2463 struct io_ring_ctx *ctx)
2464{
2465 int i, nr = cs->nr;
2466 struct io_kiocb *req;
2467 struct req_batch rb;
2468
2469 io_init_req_batch(&rb);
2470 spin_lock_irq(&ctx->completion_lock);
2471 for (i = 0; i < nr; i++) {
2472 req = cs->reqs[i];
2473 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2474 }
2475 io_commit_cqring(ctx);
2476 spin_unlock_irq(&ctx->completion_lock);
2477
2478 io_cqring_ev_posted(ctx);
2479 for (i = 0; i < nr; i++) {
2480 req = cs->reqs[i];
2481
2482 /* submission and completion refs */
2483 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002484 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002485 }
2486
2487 io_req_free_batch_finish(ctx, &rb);
2488 cs->nr = 0;
2489}
2490
Jens Axboeba816ad2019-09-28 11:36:45 -06002491/*
2492 * Drop reference to request, return next in chain (if there is one) if this
2493 * was the last reference to this request.
2494 */
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002495static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002496{
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002497 struct io_kiocb *nxt = NULL;
2498
Jens Axboe2a44f462020-02-25 13:25:41 -07002499 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002500 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002501 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002502 }
Pavel Begunkov9b5f7bd92020-06-29 13:13:00 +03002503 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002504}
2505
Jens Axboe2b188cc2019-01-07 10:46:33 -07002506static void io_put_req(struct io_kiocb *req)
2507{
Jens Axboedef596e2019-01-09 08:59:42 -07002508 if (refcount_dec_and_test(&req->refs))
2509 io_free_req(req);
2510}
2511
Pavel Begunkov216578e2020-10-13 09:44:00 +01002512static void io_put_req_deferred_cb(struct callback_head *cb)
2513{
2514 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2515
2516 io_free_req(req);
2517}
2518
2519static void io_free_req_deferred(struct io_kiocb *req)
2520{
2521 int ret;
2522
Jens Axboe7cbf1722021-02-10 00:03:20 +00002523 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002524 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002525 if (unlikely(ret))
2526 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002527}
2528
2529static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2530{
2531 if (refcount_sub_and_test(refs, &req->refs))
2532 io_free_req_deferred(req);
2533}
2534
Jens Axboe978db572019-11-14 22:39:04 -07002535static void io_double_put_req(struct io_kiocb *req)
2536{
2537 /* drop both submit and complete references */
2538 if (refcount_sub_and_test(2, &req->refs))
2539 io_free_req(req);
2540}
2541
Pavel Begunkov6c503152021-01-04 20:36:36 +00002542static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002543{
2544 /* See comment at the top of this file */
2545 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002546 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002547}
2548
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002549static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2550{
2551 struct io_rings *rings = ctx->rings;
2552
2553 /* make sure SQ entry isn't read before tail */
2554 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2555}
2556
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002557static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002558{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002559 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002560
Jens Axboebcda7ba2020-02-23 16:42:51 -07002561 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2562 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002563 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002564 kfree(kbuf);
2565 return cflags;
2566}
2567
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002568static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2569{
2570 struct io_buffer *kbuf;
2571
2572 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2573 return io_put_kbuf(req, kbuf);
2574}
2575
Jens Axboe4c6e2772020-07-01 11:29:10 -06002576static inline bool io_run_task_work(void)
2577{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002578 /*
2579 * Not safe to run on exiting task, and the task_work handling will
2580 * not add work to such a task.
2581 */
2582 if (unlikely(current->flags & PF_EXITING))
2583 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002584 if (current->task_works) {
2585 __set_current_state(TASK_RUNNING);
2586 task_work_run();
2587 return true;
2588 }
2589
2590 return false;
2591}
2592
Jens Axboedef596e2019-01-09 08:59:42 -07002593/*
2594 * Find and free completed poll iocbs
2595 */
2596static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2597 struct list_head *done)
2598{
Jens Axboe8237e042019-12-28 10:48:22 -07002599 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002600 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002601
2602 /* order with ->result store in io_complete_rw_iopoll() */
2603 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002604
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002605 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002606 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002607 int cflags = 0;
2608
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002609 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002610 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002611
Pavel Begunkovf1613402021-02-11 18:28:21 +00002612 if (READ_ONCE(req->result) == -EAGAIN) {
2613 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002614 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002615 continue;
2616 }
2617
Jens Axboebcda7ba2020-02-23 16:42:51 -07002618 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002619 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002620
2621 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002622 (*nr_events)++;
2623
Pavel Begunkovc3524382020-06-28 12:52:32 +03002624 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002625 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002626 }
Jens Axboedef596e2019-01-09 08:59:42 -07002627
Jens Axboe09bb8392019-03-13 12:39:28 -06002628 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002629 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002630 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002631}
2632
Jens Axboedef596e2019-01-09 08:59:42 -07002633static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2634 long min)
2635{
2636 struct io_kiocb *req, *tmp;
2637 LIST_HEAD(done);
2638 bool spin;
2639 int ret;
2640
2641 /*
2642 * Only spin for completions if we don't have multiple devices hanging
2643 * off our complete list, and we're under the requested amount.
2644 */
2645 spin = !ctx->poll_multi_file && *nr_events < min;
2646
2647 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002648 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002649 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002650
2651 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002652 * Move completed and retryable entries to our local lists.
2653 * If we find a request that requires polling, break out
2654 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002655 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002656 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002657 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002658 continue;
2659 }
2660 if (!list_empty(&done))
2661 break;
2662
2663 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2664 if (ret < 0)
2665 break;
2666
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002667 /* iopoll may have completed current req */
2668 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002669 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002670
Jens Axboedef596e2019-01-09 08:59:42 -07002671 if (ret && spin)
2672 spin = false;
2673 ret = 0;
2674 }
2675
2676 if (!list_empty(&done))
2677 io_iopoll_complete(ctx, nr_events, &done);
2678
2679 return ret;
2680}
2681
2682/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002683 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002684 * non-spinning poll check - we'll still enter the driver poll loop, but only
2685 * as a non-spinning completion check.
2686 */
2687static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2688 long min)
2689{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002690 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002691 int ret;
2692
2693 ret = io_do_iopoll(ctx, nr_events, min);
2694 if (ret < 0)
2695 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002696 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002697 return 0;
2698 }
2699
2700 return 1;
2701}
2702
2703/*
2704 * We can't just wait for polled events to come to us, we have to actively
2705 * find and complete them.
2706 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002707static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002708{
2709 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2710 return;
2711
2712 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002713 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002714 unsigned int nr_events = 0;
2715
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002716 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002717
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002718 /* let it sleep and repeat later if can't complete a request */
2719 if (nr_events == 0)
2720 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002721 /*
2722 * Ensure we allow local-to-the-cpu processing to take place,
2723 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002724 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002725 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002726 if (need_resched()) {
2727 mutex_unlock(&ctx->uring_lock);
2728 cond_resched();
2729 mutex_lock(&ctx->uring_lock);
2730 }
Jens Axboedef596e2019-01-09 08:59:42 -07002731 }
2732 mutex_unlock(&ctx->uring_lock);
2733}
2734
Pavel Begunkov7668b922020-07-07 16:36:21 +03002735static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002736{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002737 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002738 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002739
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002740 /*
2741 * We disallow the app entering submit/complete with polling, but we
2742 * still need to lock the ring to prevent racing with polled issue
2743 * that got punted to a workqueue.
2744 */
2745 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002746 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002747 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002748 * Don't enter poll loop if we already have events pending.
2749 * If we do, we can potentially be spinning for commands that
2750 * already triggered a CQE (eg in error).
2751 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002752 if (test_bit(0, &ctx->cq_check_overflow))
2753 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2754 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002755 break;
2756
2757 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002758 * If a submit got punted to a workqueue, we can have the
2759 * application entering polling for a command before it gets
2760 * issued. That app will hold the uring_lock for the duration
2761 * of the poll right here, so we need to take a breather every
2762 * now and then to ensure that the issue has a chance to add
2763 * the poll to the issued list. Otherwise we can spin here
2764 * forever, while the workqueue is stuck trying to acquire the
2765 * very same mutex.
2766 */
2767 if (!(++iters & 7)) {
2768 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002769 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002770 mutex_lock(&ctx->uring_lock);
2771 }
2772
Pavel Begunkov7668b922020-07-07 16:36:21 +03002773 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002774 if (ret <= 0)
2775 break;
2776 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002777 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002778
Jens Axboe500f9fb2019-08-19 12:15:59 -06002779 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002780 return ret;
2781}
2782
Jens Axboe491381ce2019-10-17 09:20:46 -06002783static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784{
Jens Axboe491381ce2019-10-17 09:20:46 -06002785 /*
2786 * Tell lockdep we inherited freeze protection from submission
2787 * thread.
2788 */
2789 if (req->flags & REQ_F_ISREG) {
2790 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002791
Jens Axboe491381ce2019-10-17 09:20:46 -06002792 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002793 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002794 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002795}
2796
Jens Axboeb63534c2020-06-04 11:28:00 -06002797#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002798static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002799{
2800 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002801 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002802 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002803
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002804 /* already prepared */
2805 if (req->async_data)
2806 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002807
2808 switch (req->opcode) {
2809 case IORING_OP_READV:
2810 case IORING_OP_READ_FIXED:
2811 case IORING_OP_READ:
2812 rw = READ;
2813 break;
2814 case IORING_OP_WRITEV:
2815 case IORING_OP_WRITE_FIXED:
2816 case IORING_OP_WRITE:
2817 rw = WRITE;
2818 break;
2819 default:
2820 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2821 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002822 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002823 }
2824
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002825 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2826 if (ret < 0)
2827 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002828 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002829}
Jens Axboeb63534c2020-06-04 11:28:00 -06002830#endif
2831
Pavel Begunkov23faba32021-02-11 18:28:22 +00002832static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002833{
2834#ifdef CONFIG_BLOCK
Pavel Begunkov23faba32021-02-11 18:28:22 +00002835 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002836 int ret;
2837
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002838 if (!S_ISBLK(mode) && !S_ISREG(mode))
2839 return false;
2840 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002841 return false;
2842
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002843 lockdep_assert_held(&req->ctx->uring_lock);
2844
Jens Axboe28cea78a2020-09-14 10:51:17 -06002845 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002846
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002847 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002848 refcount_inc(&req->refs);
2849 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002850 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002851 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002852 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002853#endif
2854 return false;
2855}
2856
Jens Axboea1d7c392020-06-22 11:09:46 -06002857static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002858 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002859{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002860 int cflags = 0;
2861
Pavel Begunkov23faba32021-02-11 18:28:22 +00002862 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2863 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002864 if (res != req->result)
2865 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002866
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002867 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2868 kiocb_end_write(req);
2869 if (req->flags & REQ_F_BUFFER_SELECTED)
2870 cflags = io_put_rw_kbuf(req);
2871 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002872}
2873
2874static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2875{
Jens Axboe9adbd452019-12-20 08:45:55 -07002876 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002877
Pavel Begunkov889fca72021-02-10 00:03:09 +00002878 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002879}
2880
Jens Axboedef596e2019-01-09 08:59:42 -07002881static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2882{
Jens Axboe9adbd452019-12-20 08:45:55 -07002883 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002884
Jens Axboe491381ce2019-10-17 09:20:46 -06002885 if (kiocb->ki_flags & IOCB_WRITE)
2886 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002887
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002888 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002889 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002890
2891 WRITE_ONCE(req->result, res);
2892 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002893 smp_wmb();
2894 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002895}
2896
2897/*
2898 * After the iocb has been issued, it's safe to be found on the poll list.
2899 * Adding the kiocb to the list AFTER submission ensures that we don't
2900 * find it from a io_iopoll_getevents() thread before the issuer is done
2901 * accessing the kiocb cookie.
2902 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002903static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002904{
2905 struct io_ring_ctx *ctx = req->ctx;
2906
2907 /*
2908 * Track whether we have multiple files in our lists. This will impact
2909 * how we do polling eventually, not spinning if we're on potentially
2910 * different devices.
2911 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002912 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002913 ctx->poll_multi_file = false;
2914 } else if (!ctx->poll_multi_file) {
2915 struct io_kiocb *list_req;
2916
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002917 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002918 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002919 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002920 ctx->poll_multi_file = true;
2921 }
2922
2923 /*
2924 * For fast devices, IO may have already completed. If it has, add
2925 * it to the front so we find it first.
2926 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002927 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002928 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002929 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002930 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002931
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002932 /*
2933 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2934 * task context or in io worker task context. If current task context is
2935 * sq thread, we don't need to check whether should wake up sq thread.
2936 */
2937 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002938 wq_has_sleeper(&ctx->sq_data->wait))
2939 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002940}
2941
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002942static inline void io_state_file_put(struct io_submit_state *state)
2943{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002944 if (state->file_refs) {
2945 fput_many(state->file, state->file_refs);
2946 state->file_refs = 0;
2947 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002948}
2949
2950/*
2951 * Get as many references to a file as we have IOs left in this submission,
2952 * assuming most submissions are for one file, or at least that each file
2953 * has more than one submission.
2954 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002955static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002956{
2957 if (!state)
2958 return fget(fd);
2959
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002960 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002961 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002962 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002963 return state->file;
2964 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002965 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002966 }
2967 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002968 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002969 return NULL;
2970
2971 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002972 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002973 return state->file;
2974}
2975
Jens Axboe4503b762020-06-01 10:00:27 -06002976static bool io_bdev_nowait(struct block_device *bdev)
2977{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002978 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002979}
2980
Jens Axboe2b188cc2019-01-07 10:46:33 -07002981/*
2982 * If we tracked the file through the SCM inflight mechanism, we could support
2983 * any file. For now, just ensure that anything potentially problematic is done
2984 * inline.
2985 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002986static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002987{
2988 umode_t mode = file_inode(file)->i_mode;
2989
Jens Axboe4503b762020-06-01 10:00:27 -06002990 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002991 if (IS_ENABLED(CONFIG_BLOCK) &&
2992 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002993 return true;
2994 return false;
2995 }
2996 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002997 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002998 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002999 if (IS_ENABLED(CONFIG_BLOCK) &&
3000 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06003001 file->f_op != &io_uring_fops)
3002 return true;
3003 return false;
3004 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003005
Jens Axboec5b85622020-06-09 19:23:05 -06003006 /* any ->read/write should understand O_NONBLOCK */
3007 if (file->f_flags & O_NONBLOCK)
3008 return true;
3009
Jens Axboeaf197f52020-04-28 13:15:06 -06003010 if (!(file->f_mode & FMODE_NOWAIT))
3011 return false;
3012
3013 if (rw == READ)
3014 return file->f_op->read_iter != NULL;
3015
3016 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003017}
3018
Pavel Begunkova88fc402020-09-30 22:57:53 +03003019static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003020{
Jens Axboedef596e2019-01-09 08:59:42 -07003021 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07003022 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003023 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06003024 unsigned ioprio;
3025 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003027 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06003028 req->flags |= REQ_F_ISREG;
3029
Jens Axboe2b188cc2019-01-07 10:46:33 -07003030 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003031 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07003032 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003033 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07003034 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003035 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03003036 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
3037 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
3038 if (unlikely(ret))
3039 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003040
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003041 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
3042 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
3043 req->flags |= REQ_F_NOWAIT;
3044
Jens Axboe2b188cc2019-01-07 10:46:33 -07003045 ioprio = READ_ONCE(sqe->ioprio);
3046 if (ioprio) {
3047 ret = ioprio_check_cap(ioprio);
3048 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06003049 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003050
3051 kiocb->ki_ioprio = ioprio;
3052 } else
3053 kiocb->ki_ioprio = get_current_ioprio();
3054
Jens Axboedef596e2019-01-09 08:59:42 -07003055 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003056 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
3057 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06003058 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003059
Jens Axboedef596e2019-01-09 08:59:42 -07003060 kiocb->ki_flags |= IOCB_HIPRI;
3061 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08003062 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07003063 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06003064 if (kiocb->ki_flags & IOCB_HIPRI)
3065 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07003066 kiocb->ki_complete = io_complete_rw;
3067 }
Jens Axboe9adbd452019-12-20 08:45:55 -07003068
Jens Axboe3529d8c2019-12-19 18:24:38 -07003069 req->rw.addr = READ_ONCE(sqe->addr);
3070 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003071 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003072 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003073}
3074
3075static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3076{
3077 switch (ret) {
3078 case -EIOCBQUEUED:
3079 break;
3080 case -ERESTARTSYS:
3081 case -ERESTARTNOINTR:
3082 case -ERESTARTNOHAND:
3083 case -ERESTART_RESTARTBLOCK:
3084 /*
3085 * We can't just restart the syscall, since previously
3086 * submitted sqes may already be in progress. Just fail this
3087 * IO with EINTR.
3088 */
3089 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003090 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003091 default:
3092 kiocb->ki_complete(kiocb, ret, 0);
3093 }
3094}
3095
Jens Axboea1d7c392020-06-22 11:09:46 -06003096static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003097 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003098{
Jens Axboeba042912019-12-25 16:33:42 -07003099 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07003100 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003101
Jens Axboe227c0c92020-08-13 11:51:40 -06003102 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003103 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003104 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003105 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003106 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003107 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003108 }
3109
Jens Axboeba042912019-12-25 16:33:42 -07003110 if (req->flags & REQ_F_CUR_POS)
3111 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03003112 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00003113 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003114 else
3115 io_rw_done(kiocb, ret);
3116}
3117
Pavel Begunkov847595d2021-02-04 13:52:06 +00003118static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003119{
Jens Axboe9adbd452019-12-20 08:45:55 -07003120 struct io_ring_ctx *ctx = req->ctx;
3121 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003122 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003123 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003124 size_t offset;
3125 u64 buf_addr;
3126
Jens Axboeedafcce2019-01-09 09:16:05 -07003127 if (unlikely(buf_index >= ctx->nr_user_bufs))
3128 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003129 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3130 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003131 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003132
3133 /* overflow */
3134 if (buf_addr + len < buf_addr)
3135 return -EFAULT;
3136 /* not inside the mapped region */
3137 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3138 return -EFAULT;
3139
3140 /*
3141 * May not be a start of buffer, set size appropriately
3142 * and advance us to the beginning.
3143 */
3144 offset = buf_addr - imu->ubuf;
3145 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003146
3147 if (offset) {
3148 /*
3149 * Don't use iov_iter_advance() here, as it's really slow for
3150 * using the latter parts of a big fixed buffer - it iterates
3151 * over each segment manually. We can cheat a bit here, because
3152 * we know that:
3153 *
3154 * 1) it's a BVEC iter, we set it up
3155 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3156 * first and last bvec
3157 *
3158 * So just find our index, and adjust the iterator afterwards.
3159 * If the offset is within the first bvec (or the whole first
3160 * bvec, just use iov_iter_advance(). This makes it easier
3161 * since we can just skip the first segment, which may not
3162 * be PAGE_SIZE aligned.
3163 */
3164 const struct bio_vec *bvec = imu->bvec;
3165
3166 if (offset <= bvec->bv_len) {
3167 iov_iter_advance(iter, offset);
3168 } else {
3169 unsigned long seg_skip;
3170
3171 /* skip first vec */
3172 offset -= bvec->bv_len;
3173 seg_skip = 1 + (offset >> PAGE_SHIFT);
3174
3175 iter->bvec = bvec + seg_skip;
3176 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003177 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003178 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003179 }
3180 }
3181
Pavel Begunkov847595d2021-02-04 13:52:06 +00003182 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003183}
3184
Jens Axboebcda7ba2020-02-23 16:42:51 -07003185static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3186{
3187 if (needs_lock)
3188 mutex_unlock(&ctx->uring_lock);
3189}
3190
3191static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3192{
3193 /*
3194 * "Normal" inline submissions always hold the uring_lock, since we
3195 * grab it from the system call. Same is true for the SQPOLL offload.
3196 * The only exception is when we've detached the request and issue it
3197 * from an async worker thread, grab the lock for that case.
3198 */
3199 if (needs_lock)
3200 mutex_lock(&ctx->uring_lock);
3201}
3202
3203static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3204 int bgid, struct io_buffer *kbuf,
3205 bool needs_lock)
3206{
3207 struct io_buffer *head;
3208
3209 if (req->flags & REQ_F_BUFFER_SELECTED)
3210 return kbuf;
3211
3212 io_ring_submit_lock(req->ctx, needs_lock);
3213
3214 lockdep_assert_held(&req->ctx->uring_lock);
3215
3216 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3217 if (head) {
3218 if (!list_empty(&head->list)) {
3219 kbuf = list_last_entry(&head->list, struct io_buffer,
3220 list);
3221 list_del(&kbuf->list);
3222 } else {
3223 kbuf = head;
3224 idr_remove(&req->ctx->io_buffer_idr, bgid);
3225 }
3226 if (*len > kbuf->len)
3227 *len = kbuf->len;
3228 } else {
3229 kbuf = ERR_PTR(-ENOBUFS);
3230 }
3231
3232 io_ring_submit_unlock(req->ctx, needs_lock);
3233
3234 return kbuf;
3235}
3236
Jens Axboe4d954c22020-02-27 07:31:19 -07003237static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3238 bool needs_lock)
3239{
3240 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003241 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003242
3243 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003244 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003245 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3246 if (IS_ERR(kbuf))
3247 return kbuf;
3248 req->rw.addr = (u64) (unsigned long) kbuf;
3249 req->flags |= REQ_F_BUFFER_SELECTED;
3250 return u64_to_user_ptr(kbuf->addr);
3251}
3252
3253#ifdef CONFIG_COMPAT
3254static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3255 bool needs_lock)
3256{
3257 struct compat_iovec __user *uiov;
3258 compat_ssize_t clen;
3259 void __user *buf;
3260 ssize_t len;
3261
3262 uiov = u64_to_user_ptr(req->rw.addr);
3263 if (!access_ok(uiov, sizeof(*uiov)))
3264 return -EFAULT;
3265 if (__get_user(clen, &uiov->iov_len))
3266 return -EFAULT;
3267 if (clen < 0)
3268 return -EINVAL;
3269
3270 len = clen;
3271 buf = io_rw_buffer_select(req, &len, needs_lock);
3272 if (IS_ERR(buf))
3273 return PTR_ERR(buf);
3274 iov[0].iov_base = buf;
3275 iov[0].iov_len = (compat_size_t) len;
3276 return 0;
3277}
3278#endif
3279
3280static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3281 bool needs_lock)
3282{
3283 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3284 void __user *buf;
3285 ssize_t len;
3286
3287 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3288 return -EFAULT;
3289
3290 len = iov[0].iov_len;
3291 if (len < 0)
3292 return -EINVAL;
3293 buf = io_rw_buffer_select(req, &len, needs_lock);
3294 if (IS_ERR(buf))
3295 return PTR_ERR(buf);
3296 iov[0].iov_base = buf;
3297 iov[0].iov_len = len;
3298 return 0;
3299}
3300
3301static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3302 bool needs_lock)
3303{
Jens Axboedddb3e22020-06-04 11:27:01 -06003304 if (req->flags & REQ_F_BUFFER_SELECTED) {
3305 struct io_buffer *kbuf;
3306
3307 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3308 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3309 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003310 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003311 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003312 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003313 return -EINVAL;
3314
3315#ifdef CONFIG_COMPAT
3316 if (req->ctx->compat)
3317 return io_compat_import(req, iov, needs_lock);
3318#endif
3319
3320 return __io_iov_buffer_select(req, iov, needs_lock);
3321}
3322
Pavel Begunkov847595d2021-02-04 13:52:06 +00003323static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3324 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003325{
Jens Axboe9adbd452019-12-20 08:45:55 -07003326 void __user *buf = u64_to_user_ptr(req->rw.addr);
3327 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003328 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003329 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003330
Pavel Begunkov7d009162019-11-25 23:14:40 +03003331 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003332 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003333 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003334 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003335
Jens Axboebcda7ba2020-02-23 16:42:51 -07003336 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003337 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003338 return -EINVAL;
3339
Jens Axboe3a6820f2019-12-22 15:19:35 -07003340 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003341 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003342 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003343 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003344 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003345 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003346 }
3347
Jens Axboe3a6820f2019-12-22 15:19:35 -07003348 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3349 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003350 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003351 }
3352
Jens Axboe4d954c22020-02-27 07:31:19 -07003353 if (req->flags & REQ_F_BUFFER_SELECT) {
3354 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003355 if (!ret)
3356 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003357 *iovec = NULL;
3358 return ret;
3359 }
3360
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003361 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3362 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003363}
3364
Jens Axboe0fef9482020-08-26 10:36:20 -06003365static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3366{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003367 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003368}
3369
Jens Axboe32960612019-09-23 11:05:34 -06003370/*
3371 * For files that don't have ->read_iter() and ->write_iter(), handle them
3372 * by looping over ->read() or ->write() manually.
3373 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003374static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003375{
Jens Axboe4017eb92020-10-22 14:14:12 -06003376 struct kiocb *kiocb = &req->rw.kiocb;
3377 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003378 ssize_t ret = 0;
3379
3380 /*
3381 * Don't support polled IO through this interface, and we can't
3382 * support non-blocking either. For the latter, this just causes
3383 * the kiocb to be handled from an async context.
3384 */
3385 if (kiocb->ki_flags & IOCB_HIPRI)
3386 return -EOPNOTSUPP;
3387 if (kiocb->ki_flags & IOCB_NOWAIT)
3388 return -EAGAIN;
3389
3390 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003391 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003392 ssize_t nr;
3393
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003394 if (!iov_iter_is_bvec(iter)) {
3395 iovec = iov_iter_iovec(iter);
3396 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003397 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3398 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003399 }
3400
Jens Axboe32960612019-09-23 11:05:34 -06003401 if (rw == READ) {
3402 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003403 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003404 } else {
3405 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003406 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003407 }
3408
3409 if (nr < 0) {
3410 if (!ret)
3411 ret = nr;
3412 break;
3413 }
3414 ret += nr;
3415 if (nr != iovec.iov_len)
3416 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003417 req->rw.len -= nr;
3418 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003419 iov_iter_advance(iter, nr);
3420 }
3421
3422 return ret;
3423}
3424
Jens Axboeff6165b2020-08-13 09:47:43 -06003425static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3426 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003427{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003428 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003429
Jens Axboeff6165b2020-08-13 09:47:43 -06003430 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003431 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003432 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003433 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003434 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003435 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003436 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003437 unsigned iov_off = 0;
3438
3439 rw->iter.iov = rw->fast_iov;
3440 if (iter->iov != fast_iov) {
3441 iov_off = iter->iov - fast_iov;
3442 rw->iter.iov += iov_off;
3443 }
3444 if (rw->fast_iov != fast_iov)
3445 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003446 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003447 } else {
3448 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003449 }
3450}
3451
Jens Axboee8c2bc12020-08-15 18:44:09 -07003452static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003453{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003454 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3455 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3456 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003457}
3458
Jens Axboee8c2bc12020-08-15 18:44:09 -07003459static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003460{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003461 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003462 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003463
Jens Axboee8c2bc12020-08-15 18:44:09 -07003464 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003465}
3466
Jens Axboeff6165b2020-08-13 09:47:43 -06003467static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3468 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003469 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003470{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003471 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003472 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003473 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003474 if (__io_alloc_async_data(req)) {
3475 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003476 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003477 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003478
Jens Axboeff6165b2020-08-13 09:47:43 -06003479 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003480 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003481 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003482}
3483
Pavel Begunkov73debe62020-09-30 22:57:54 +03003484static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003485{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003486 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003487 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003488 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003489
Pavel Begunkov2846c482020-11-07 13:16:27 +00003490 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003491 if (unlikely(ret < 0))
3492 return ret;
3493
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003494 iorw->bytes_done = 0;
3495 iorw->free_iovec = iov;
3496 if (iov)
3497 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003498 return 0;
3499}
3500
Pavel Begunkov73debe62020-09-30 22:57:54 +03003501static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003502{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003503 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3504 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003505 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003506}
3507
Jens Axboec1dd91d2020-08-03 16:43:59 -06003508/*
3509 * This is our waitqueue callback handler, registered through lock_page_async()
3510 * when we initially tried to do the IO with the iocb armed our waitqueue.
3511 * This gets called when the page is unlocked, and we generally expect that to
3512 * happen when the page IO is completed and the page is now uptodate. This will
3513 * queue a task_work based retry of the operation, attempting to copy the data
3514 * again. If the latter fails because the page was NOT uptodate, then we will
3515 * do a thread based blocking retry of the operation. That's the unexpected
3516 * slow path.
3517 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003518static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3519 int sync, void *arg)
3520{
3521 struct wait_page_queue *wpq;
3522 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003523 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003524
3525 wpq = container_of(wait, struct wait_page_queue, wait);
3526
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003527 if (!wake_page_match(wpq, key))
3528 return 0;
3529
Hao Xuc8d317a2020-09-29 20:00:45 +08003530 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003531 list_del_init(&wait->entry);
3532
Jens Axboebcf5a062020-05-22 09:24:42 -06003533 /* submit ref gets dropped, acquire a new one */
3534 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003535 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003536 return 1;
3537}
3538
Jens Axboec1dd91d2020-08-03 16:43:59 -06003539/*
3540 * This controls whether a given IO request should be armed for async page
3541 * based retry. If we return false here, the request is handed to the async
3542 * worker threads for retry. If we're doing buffered reads on a regular file,
3543 * we prepare a private wait_page_queue entry and retry the operation. This
3544 * will either succeed because the page is now uptodate and unlocked, or it
3545 * will register a callback when the page is unlocked at IO completion. Through
3546 * that callback, io_uring uses task_work to setup a retry of the operation.
3547 * That retry will attempt the buffered read again. The retry will generally
3548 * succeed, or in rare cases where it fails, we then fall back to using the
3549 * async worker threads for a blocking retry.
3550 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003551static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003552{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003553 struct io_async_rw *rw = req->async_data;
3554 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003555 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003556
3557 /* never retry for NOWAIT, we just complete with -EAGAIN */
3558 if (req->flags & REQ_F_NOWAIT)
3559 return false;
3560
Jens Axboe227c0c92020-08-13 11:51:40 -06003561 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003562 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003563 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003564
Jens Axboebcf5a062020-05-22 09:24:42 -06003565 /*
3566 * just use poll if we can, and don't attempt if the fs doesn't
3567 * support callback based unlocks
3568 */
3569 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3570 return false;
3571
Jens Axboe3b2a4432020-08-16 10:58:43 -07003572 wait->wait.func = io_async_buf_func;
3573 wait->wait.private = req;
3574 wait->wait.flags = 0;
3575 INIT_LIST_HEAD(&wait->wait.entry);
3576 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003577 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003578 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003579 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003580}
3581
3582static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3583{
3584 if (req->file->f_op->read_iter)
3585 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003586 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003587 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003588 else
3589 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003590}
3591
Pavel Begunkov889fca72021-02-10 00:03:09 +00003592static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003593{
3594 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003595 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003596 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003597 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003598 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003599 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003600
Pavel Begunkov2846c482020-11-07 13:16:27 +00003601 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003602 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003603 iovec = NULL;
3604 } else {
3605 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3606 if (ret < 0)
3607 return ret;
3608 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003609 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003610 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003611
Jens Axboefd6c2e42019-12-18 12:19:41 -07003612 /* Ensure we clear previously set non-block flag */
3613 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003614 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003615 else
3616 kiocb->ki_flags |= IOCB_NOWAIT;
3617
Pavel Begunkov24c74672020-06-21 13:09:51 +03003618 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003619 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3620 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003621 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003622 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003623
Pavel Begunkov632546c2020-11-07 13:16:26 +00003624 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003625 if (unlikely(ret)) {
3626 kfree(iovec);
3627 return ret;
3628 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003629
Jens Axboe227c0c92020-08-13 11:51:40 -06003630 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003631
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003632 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003633 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003634 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003635 /* IOPOLL retry should happen for io-wq threads */
3636 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003637 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003638 /* no retry on NONBLOCK nor RWF_NOWAIT */
3639 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003640 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003641 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003642 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003643 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003644 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003645 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003646 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003647 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003648 }
3649
Jens Axboe227c0c92020-08-13 11:51:40 -06003650 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003651 if (ret2)
3652 return ret2;
3653
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003654 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003655 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003656 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003657 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003658
Pavel Begunkovb23df912021-02-04 13:52:04 +00003659 do {
3660 io_size -= ret;
3661 rw->bytes_done += ret;
3662 /* if we can retry, do so with the callbacks armed */
3663 if (!io_rw_should_retry(req)) {
3664 kiocb->ki_flags &= ~IOCB_WAITQ;
3665 return -EAGAIN;
3666 }
3667
3668 /*
3669 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3670 * we get -EIOCBQUEUED, then we'll get a notification when the
3671 * desired page gets unlocked. We can also get a partial read
3672 * here, and if we do, then just retry at the new offset.
3673 */
3674 ret = io_iter_do_read(req, iter);
3675 if (ret == -EIOCBQUEUED)
3676 return 0;
3677 /* we got some bytes, but not all. retry. */
3678 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003679done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003680 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003681out_free:
3682 /* it's faster to check here then delegate to kfree */
3683 if (iovec)
3684 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003685 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003686}
3687
Pavel Begunkov73debe62020-09-30 22:57:54 +03003688static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003689{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003690 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3691 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003692 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003693}
3694
Pavel Begunkov889fca72021-02-10 00:03:09 +00003695static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003696{
3697 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003698 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003699 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003700 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003701 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003702 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003703
Pavel Begunkov2846c482020-11-07 13:16:27 +00003704 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003705 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003706 iovec = NULL;
3707 } else {
3708 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3709 if (ret < 0)
3710 return ret;
3711 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003712 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003713 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003714
Jens Axboefd6c2e42019-12-18 12:19:41 -07003715 /* Ensure we clear previously set non-block flag */
3716 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003717 kiocb->ki_flags &= ~IOCB_NOWAIT;
3718 else
3719 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003720
Pavel Begunkov24c74672020-06-21 13:09:51 +03003721 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003722 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003723 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003724
Jens Axboe10d59342019-12-09 20:16:22 -07003725 /* file path doesn't support NOWAIT for non-direct_IO */
3726 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3727 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003728 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003729
Pavel Begunkov632546c2020-11-07 13:16:26 +00003730 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003731 if (unlikely(ret))
3732 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003733
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003734 /*
3735 * Open-code file_start_write here to grab freeze protection,
3736 * which will be released by another thread in
3737 * io_complete_rw(). Fool lockdep by telling it the lock got
3738 * released so that it doesn't complain about the held lock when
3739 * we return to userspace.
3740 */
3741 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003742 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003743 __sb_writers_release(file_inode(req->file)->i_sb,
3744 SB_FREEZE_WRITE);
3745 }
3746 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003747
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003748 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003749 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003750 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003751 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003752 else
3753 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003754
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003755 /*
3756 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3757 * retry them without IOCB_NOWAIT.
3758 */
3759 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3760 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003761 /* no retry on NONBLOCK nor RWF_NOWAIT */
3762 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003763 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003764 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003765 /* IOPOLL retry should happen for io-wq threads */
3766 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3767 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003768done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003769 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003770 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003771copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003772 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003773 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003774 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003775 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003776 }
Jens Axboe31b51512019-01-18 22:56:34 -07003777out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003778 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003779 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003780 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003781 return ret;
3782}
3783
Jens Axboe80a261f2020-09-28 14:23:58 -06003784static int io_renameat_prep(struct io_kiocb *req,
3785 const struct io_uring_sqe *sqe)
3786{
3787 struct io_rename *ren = &req->rename;
3788 const char __user *oldf, *newf;
3789
3790 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3791 return -EBADF;
3792
3793 ren->old_dfd = READ_ONCE(sqe->fd);
3794 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3795 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3796 ren->new_dfd = READ_ONCE(sqe->len);
3797 ren->flags = READ_ONCE(sqe->rename_flags);
3798
3799 ren->oldpath = getname(oldf);
3800 if (IS_ERR(ren->oldpath))
3801 return PTR_ERR(ren->oldpath);
3802
3803 ren->newpath = getname(newf);
3804 if (IS_ERR(ren->newpath)) {
3805 putname(ren->oldpath);
3806 return PTR_ERR(ren->newpath);
3807 }
3808
3809 req->flags |= REQ_F_NEED_CLEANUP;
3810 return 0;
3811}
3812
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003813static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003814{
3815 struct io_rename *ren = &req->rename;
3816 int ret;
3817
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003818 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003819 return -EAGAIN;
3820
3821 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3822 ren->newpath, ren->flags);
3823
3824 req->flags &= ~REQ_F_NEED_CLEANUP;
3825 if (ret < 0)
3826 req_set_fail_links(req);
3827 io_req_complete(req, ret);
3828 return 0;
3829}
3830
Jens Axboe14a11432020-09-28 14:27:37 -06003831static int io_unlinkat_prep(struct io_kiocb *req,
3832 const struct io_uring_sqe *sqe)
3833{
3834 struct io_unlink *un = &req->unlink;
3835 const char __user *fname;
3836
3837 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3838 return -EBADF;
3839
3840 un->dfd = READ_ONCE(sqe->fd);
3841
3842 un->flags = READ_ONCE(sqe->unlink_flags);
3843 if (un->flags & ~AT_REMOVEDIR)
3844 return -EINVAL;
3845
3846 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3847 un->filename = getname(fname);
3848 if (IS_ERR(un->filename))
3849 return PTR_ERR(un->filename);
3850
3851 req->flags |= REQ_F_NEED_CLEANUP;
3852 return 0;
3853}
3854
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003855static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003856{
3857 struct io_unlink *un = &req->unlink;
3858 int ret;
3859
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003860 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003861 return -EAGAIN;
3862
3863 if (un->flags & AT_REMOVEDIR)
3864 ret = do_rmdir(un->dfd, un->filename);
3865 else
3866 ret = do_unlinkat(un->dfd, un->filename);
3867
3868 req->flags &= ~REQ_F_NEED_CLEANUP;
3869 if (ret < 0)
3870 req_set_fail_links(req);
3871 io_req_complete(req, ret);
3872 return 0;
3873}
3874
Jens Axboe36f4fa62020-09-05 11:14:22 -06003875static int io_shutdown_prep(struct io_kiocb *req,
3876 const struct io_uring_sqe *sqe)
3877{
3878#if defined(CONFIG_NET)
3879 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3880 return -EINVAL;
3881 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3882 sqe->buf_index)
3883 return -EINVAL;
3884
3885 req->shutdown.how = READ_ONCE(sqe->len);
3886 return 0;
3887#else
3888 return -EOPNOTSUPP;
3889#endif
3890}
3891
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003892static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003893{
3894#if defined(CONFIG_NET)
3895 struct socket *sock;
3896 int ret;
3897
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003898 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003899 return -EAGAIN;
3900
Linus Torvalds48aba792020-12-16 12:44:05 -08003901 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003902 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003903 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003904
3905 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003906 if (ret < 0)
3907 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003908 io_req_complete(req, ret);
3909 return 0;
3910#else
3911 return -EOPNOTSUPP;
3912#endif
3913}
3914
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003915static int __io_splice_prep(struct io_kiocb *req,
3916 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003917{
3918 struct io_splice* sp = &req->splice;
3919 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003920
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003921 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3922 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003923
3924 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003925 sp->len = READ_ONCE(sqe->len);
3926 sp->flags = READ_ONCE(sqe->splice_flags);
3927
3928 if (unlikely(sp->flags & ~valid_flags))
3929 return -EINVAL;
3930
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003931 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3932 (sp->flags & SPLICE_F_FD_IN_FIXED));
3933 if (!sp->file_in)
3934 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003935 req->flags |= REQ_F_NEED_CLEANUP;
3936
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003937 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3938 /*
3939 * Splice operation will be punted aync, and here need to
3940 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3941 */
3942 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003943 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003944 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003945
3946 return 0;
3947}
3948
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003949static int io_tee_prep(struct io_kiocb *req,
3950 const struct io_uring_sqe *sqe)
3951{
3952 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3953 return -EINVAL;
3954 return __io_splice_prep(req, sqe);
3955}
3956
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003957static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003958{
3959 struct io_splice *sp = &req->splice;
3960 struct file *in = sp->file_in;
3961 struct file *out = sp->file_out;
3962 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3963 long ret = 0;
3964
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003965 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003966 return -EAGAIN;
3967 if (sp->len)
3968 ret = do_tee(in, out, sp->len, flags);
3969
3970 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3971 req->flags &= ~REQ_F_NEED_CLEANUP;
3972
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003973 if (ret != sp->len)
3974 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003975 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003976 return 0;
3977}
3978
3979static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3980{
3981 struct io_splice* sp = &req->splice;
3982
3983 sp->off_in = READ_ONCE(sqe->splice_off_in);
3984 sp->off_out = READ_ONCE(sqe->off);
3985 return __io_splice_prep(req, sqe);
3986}
3987
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003988static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003989{
3990 struct io_splice *sp = &req->splice;
3991 struct file *in = sp->file_in;
3992 struct file *out = sp->file_out;
3993 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3994 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003995 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003996
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003997 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003998 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003999
4000 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4001 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004002
Jens Axboe948a7742020-05-17 14:21:38 -06004003 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004004 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004005
4006 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
4007 req->flags &= ~REQ_F_NEED_CLEANUP;
4008
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004009 if (ret != sp->len)
4010 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004011 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004012 return 0;
4013}
4014
Jens Axboe2b188cc2019-01-07 10:46:33 -07004015/*
4016 * IORING_OP_NOP just posts a completion event, nothing else.
4017 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004018static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004019{
4020 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004021
Jens Axboedef596e2019-01-09 08:59:42 -07004022 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4023 return -EINVAL;
4024
Pavel Begunkov889fca72021-02-10 00:03:09 +00004025 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026 return 0;
4027}
4028
Pavel Begunkov1155c762021-02-18 18:29:38 +00004029static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004030{
Jens Axboe6b063142019-01-10 22:13:58 -07004031 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004032
Jens Axboe09bb8392019-03-13 12:39:28 -06004033 if (!req->file)
4034 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004035
Jens Axboe6b063142019-01-10 22:13:58 -07004036 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004037 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07004038 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004039 return -EINVAL;
4040
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004041 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4042 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4043 return -EINVAL;
4044
4045 req->sync.off = READ_ONCE(sqe->off);
4046 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004047 return 0;
4048}
4049
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004050static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004051{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004052 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004053 int ret;
4054
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004055 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004056 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004057 return -EAGAIN;
4058
Jens Axboe9adbd452019-12-20 08:45:55 -07004059 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004060 end > 0 ? end : LLONG_MAX,
4061 req->sync.flags & IORING_FSYNC_DATASYNC);
4062 if (ret < 0)
4063 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004064 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004065 return 0;
4066}
4067
Jens Axboed63d1b52019-12-10 10:38:56 -07004068static int io_fallocate_prep(struct io_kiocb *req,
4069 const struct io_uring_sqe *sqe)
4070{
4071 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4072 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004073 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4074 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004075
4076 req->sync.off = READ_ONCE(sqe->off);
4077 req->sync.len = READ_ONCE(sqe->addr);
4078 req->sync.mode = READ_ONCE(sqe->len);
4079 return 0;
4080}
4081
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004082static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004083{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004084 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004085
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004086 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004087 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004088 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004089 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4090 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004091 if (ret < 0)
4092 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004093 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004094 return 0;
4095}
4096
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004097static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004098{
Jens Axboef8748882020-01-08 17:47:02 -07004099 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004100 int ret;
4101
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004102 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004103 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004104 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004105 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004106
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004107 /* open.how should be already initialised */
4108 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004109 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004110
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004111 req->open.dfd = READ_ONCE(sqe->fd);
4112 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004113 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004114 if (IS_ERR(req->open.filename)) {
4115 ret = PTR_ERR(req->open.filename);
4116 req->open.filename = NULL;
4117 return ret;
4118 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004119 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004120 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004121 return 0;
4122}
4123
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004124static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4125{
4126 u64 flags, mode;
4127
Jens Axboe14587a462020-09-05 11:36:08 -06004128 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004129 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004130 mode = READ_ONCE(sqe->len);
4131 flags = READ_ONCE(sqe->open_flags);
4132 req->open.how = build_open_how(flags, mode);
4133 return __io_openat_prep(req, sqe);
4134}
4135
Jens Axboecebdb982020-01-08 17:59:24 -07004136static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4137{
4138 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004139 size_t len;
4140 int ret;
4141
Jens Axboe14587a462020-09-05 11:36:08 -06004142 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004143 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004144 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4145 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004146 if (len < OPEN_HOW_SIZE_VER0)
4147 return -EINVAL;
4148
4149 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4150 len);
4151 if (ret)
4152 return ret;
4153
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004154 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004155}
4156
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004157static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004158{
4159 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004160 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004161 bool nonblock_set;
4162 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004163 int ret;
4164
Jens Axboecebdb982020-01-08 17:59:24 -07004165 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004166 if (ret)
4167 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004168 nonblock_set = op.open_flag & O_NONBLOCK;
4169 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004170 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004171 /*
4172 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4173 * it'll always -EAGAIN
4174 */
4175 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4176 return -EAGAIN;
4177 op.lookup_flags |= LOOKUP_CACHED;
4178 op.open_flag |= O_NONBLOCK;
4179 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004180
Jens Axboe4022e7a2020-03-19 19:23:18 -06004181 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004182 if (ret < 0)
4183 goto err;
4184
4185 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004186 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004187 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4188 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004189 /*
4190 * We could hang on to this 'fd', but seems like marginal
4191 * gain for something that is now known to be a slower path.
4192 * So just put it, and we'll get a new one when we retry.
4193 */
4194 put_unused_fd(ret);
4195 return -EAGAIN;
4196 }
4197
Jens Axboe15b71ab2019-12-11 11:20:36 -07004198 if (IS_ERR(file)) {
4199 put_unused_fd(ret);
4200 ret = PTR_ERR(file);
4201 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004202 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004203 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004204 fsnotify_open(file);
4205 fd_install(ret, file);
4206 }
4207err:
4208 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004209 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004210 if (ret < 0)
4211 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004212 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004213 return 0;
4214}
4215
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004216static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004217{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004218 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004219}
4220
Jens Axboe067524e2020-03-02 16:32:28 -07004221static int io_remove_buffers_prep(struct io_kiocb *req,
4222 const struct io_uring_sqe *sqe)
4223{
4224 struct io_provide_buf *p = &req->pbuf;
4225 u64 tmp;
4226
4227 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4228 return -EINVAL;
4229
4230 tmp = READ_ONCE(sqe->fd);
4231 if (!tmp || tmp > USHRT_MAX)
4232 return -EINVAL;
4233
4234 memset(p, 0, sizeof(*p));
4235 p->nbufs = tmp;
4236 p->bgid = READ_ONCE(sqe->buf_group);
4237 return 0;
4238}
4239
4240static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4241 int bgid, unsigned nbufs)
4242{
4243 unsigned i = 0;
4244
4245 /* shouldn't happen */
4246 if (!nbufs)
4247 return 0;
4248
4249 /* the head kbuf is the list itself */
4250 while (!list_empty(&buf->list)) {
4251 struct io_buffer *nxt;
4252
4253 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4254 list_del(&nxt->list);
4255 kfree(nxt);
4256 if (++i == nbufs)
4257 return i;
4258 }
4259 i++;
4260 kfree(buf);
4261 idr_remove(&ctx->io_buffer_idr, bgid);
4262
4263 return i;
4264}
4265
Pavel Begunkov889fca72021-02-10 00:03:09 +00004266static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004267{
4268 struct io_provide_buf *p = &req->pbuf;
4269 struct io_ring_ctx *ctx = req->ctx;
4270 struct io_buffer *head;
4271 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004272 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004273
4274 io_ring_submit_lock(ctx, !force_nonblock);
4275
4276 lockdep_assert_held(&ctx->uring_lock);
4277
4278 ret = -ENOENT;
4279 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4280 if (head)
4281 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004282 if (ret < 0)
4283 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004284
4285 /* need to hold the lock to complete IOPOLL requests */
4286 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004287 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004288 io_ring_submit_unlock(ctx, !force_nonblock);
4289 } else {
4290 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004291 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004292 }
Jens Axboe067524e2020-03-02 16:32:28 -07004293 return 0;
4294}
4295
Jens Axboeddf0322d2020-02-23 16:41:33 -07004296static int io_provide_buffers_prep(struct io_kiocb *req,
4297 const struct io_uring_sqe *sqe)
4298{
4299 struct io_provide_buf *p = &req->pbuf;
4300 u64 tmp;
4301
4302 if (sqe->ioprio || sqe->rw_flags)
4303 return -EINVAL;
4304
4305 tmp = READ_ONCE(sqe->fd);
4306 if (!tmp || tmp > USHRT_MAX)
4307 return -E2BIG;
4308 p->nbufs = tmp;
4309 p->addr = READ_ONCE(sqe->addr);
4310 p->len = READ_ONCE(sqe->len);
4311
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004312 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004313 return -EFAULT;
4314
4315 p->bgid = READ_ONCE(sqe->buf_group);
4316 tmp = READ_ONCE(sqe->off);
4317 if (tmp > USHRT_MAX)
4318 return -E2BIG;
4319 p->bid = tmp;
4320 return 0;
4321}
4322
4323static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4324{
4325 struct io_buffer *buf;
4326 u64 addr = pbuf->addr;
4327 int i, bid = pbuf->bid;
4328
4329 for (i = 0; i < pbuf->nbufs; i++) {
4330 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4331 if (!buf)
4332 break;
4333
4334 buf->addr = addr;
4335 buf->len = pbuf->len;
4336 buf->bid = bid;
4337 addr += pbuf->len;
4338 bid++;
4339 if (!*head) {
4340 INIT_LIST_HEAD(&buf->list);
4341 *head = buf;
4342 } else {
4343 list_add_tail(&buf->list, &(*head)->list);
4344 }
4345 }
4346
4347 return i ? i : -ENOMEM;
4348}
4349
Pavel Begunkov889fca72021-02-10 00:03:09 +00004350static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004351{
4352 struct io_provide_buf *p = &req->pbuf;
4353 struct io_ring_ctx *ctx = req->ctx;
4354 struct io_buffer *head, *list;
4355 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004356 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004357
4358 io_ring_submit_lock(ctx, !force_nonblock);
4359
4360 lockdep_assert_held(&ctx->uring_lock);
4361
4362 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4363
4364 ret = io_add_buffers(p, &head);
4365 if (ret < 0)
4366 goto out;
4367
4368 if (!list) {
4369 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4370 GFP_KERNEL);
4371 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004372 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004373 goto out;
4374 }
4375 }
4376out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004377 if (ret < 0)
4378 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004379
4380 /* need to hold the lock to complete IOPOLL requests */
4381 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004382 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004383 io_ring_submit_unlock(ctx, !force_nonblock);
4384 } else {
4385 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004386 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004387 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004388 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004389}
4390
Jens Axboe3e4827b2020-01-08 15:18:09 -07004391static int io_epoll_ctl_prep(struct io_kiocb *req,
4392 const struct io_uring_sqe *sqe)
4393{
4394#if defined(CONFIG_EPOLL)
4395 if (sqe->ioprio || sqe->buf_index)
4396 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004397 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004398 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004399
4400 req->epoll.epfd = READ_ONCE(sqe->fd);
4401 req->epoll.op = READ_ONCE(sqe->len);
4402 req->epoll.fd = READ_ONCE(sqe->off);
4403
4404 if (ep_op_has_event(req->epoll.op)) {
4405 struct epoll_event __user *ev;
4406
4407 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4408 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4409 return -EFAULT;
4410 }
4411
4412 return 0;
4413#else
4414 return -EOPNOTSUPP;
4415#endif
4416}
4417
Pavel Begunkov889fca72021-02-10 00:03:09 +00004418static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004419{
4420#if defined(CONFIG_EPOLL)
4421 struct io_epoll *ie = &req->epoll;
4422 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004423 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004424
4425 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4426 if (force_nonblock && ret == -EAGAIN)
4427 return -EAGAIN;
4428
4429 if (ret < 0)
4430 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004431 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004432 return 0;
4433#else
4434 return -EOPNOTSUPP;
4435#endif
4436}
4437
Jens Axboec1ca7572019-12-25 22:18:28 -07004438static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4439{
4440#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4441 if (sqe->ioprio || sqe->buf_index || sqe->off)
4442 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004443 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4444 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004445
4446 req->madvise.addr = READ_ONCE(sqe->addr);
4447 req->madvise.len = READ_ONCE(sqe->len);
4448 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4449 return 0;
4450#else
4451 return -EOPNOTSUPP;
4452#endif
4453}
4454
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004455static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004456{
4457#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4458 struct io_madvise *ma = &req->madvise;
4459 int ret;
4460
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004461 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004462 return -EAGAIN;
4463
Minchan Kim0726b012020-10-17 16:14:50 -07004464 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004465 if (ret < 0)
4466 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004467 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004468 return 0;
4469#else
4470 return -EOPNOTSUPP;
4471#endif
4472}
4473
Jens Axboe4840e412019-12-25 22:03:45 -07004474static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4475{
4476 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4477 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004478 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4479 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004480
4481 req->fadvise.offset = READ_ONCE(sqe->off);
4482 req->fadvise.len = READ_ONCE(sqe->len);
4483 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4484 return 0;
4485}
4486
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004487static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004488{
4489 struct io_fadvise *fa = &req->fadvise;
4490 int ret;
4491
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004492 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004493 switch (fa->advice) {
4494 case POSIX_FADV_NORMAL:
4495 case POSIX_FADV_RANDOM:
4496 case POSIX_FADV_SEQUENTIAL:
4497 break;
4498 default:
4499 return -EAGAIN;
4500 }
4501 }
Jens Axboe4840e412019-12-25 22:03:45 -07004502
4503 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4504 if (ret < 0)
4505 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004506 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004507 return 0;
4508}
4509
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004510static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4511{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004512 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004513 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004514 if (sqe->ioprio || sqe->buf_index)
4515 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004516 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004517 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004518
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004519 req->statx.dfd = READ_ONCE(sqe->fd);
4520 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004521 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004522 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4523 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004524
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004525 return 0;
4526}
4527
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004528static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004529{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004530 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004531 int ret;
4532
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004533 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004534 /* only need file table for an actual valid fd */
4535 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4536 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004537 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004538 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004539
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004540 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4541 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004542
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004543 if (ret < 0)
4544 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004545 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004546 return 0;
4547}
4548
Jens Axboeb5dba592019-12-11 14:02:38 -07004549static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4550{
Jens Axboe14587a462020-09-05 11:36:08 -06004551 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004552 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004553 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4554 sqe->rw_flags || sqe->buf_index)
4555 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004556 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004557 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004558
4559 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004560 return 0;
4561}
4562
Pavel Begunkov889fca72021-02-10 00:03:09 +00004563static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004564{
Jens Axboe9eac1902021-01-19 15:50:37 -07004565 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004566 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004567 struct fdtable *fdt;
4568 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004569 int ret;
4570
Jens Axboe9eac1902021-01-19 15:50:37 -07004571 file = NULL;
4572 ret = -EBADF;
4573 spin_lock(&files->file_lock);
4574 fdt = files_fdtable(files);
4575 if (close->fd >= fdt->max_fds) {
4576 spin_unlock(&files->file_lock);
4577 goto err;
4578 }
4579 file = fdt->fd[close->fd];
4580 if (!file) {
4581 spin_unlock(&files->file_lock);
4582 goto err;
4583 }
4584
4585 if (file->f_op == &io_uring_fops) {
4586 spin_unlock(&files->file_lock);
4587 file = NULL;
4588 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004589 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004590
4591 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004592 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004593 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004594 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004595 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004596
Jens Axboe9eac1902021-01-19 15:50:37 -07004597 ret = __close_fd_get_file(close->fd, &file);
4598 spin_unlock(&files->file_lock);
4599 if (ret < 0) {
4600 if (ret == -ENOENT)
4601 ret = -EBADF;
4602 goto err;
4603 }
4604
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004605 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004606 ret = filp_close(file, current->files);
4607err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004608 if (ret < 0)
4609 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004610 if (file)
4611 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004612 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004613 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004614}
4615
Pavel Begunkov1155c762021-02-18 18:29:38 +00004616static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004617{
4618 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004619
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004620 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4621 return -EINVAL;
4622 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4623 return -EINVAL;
4624
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004625 req->sync.off = READ_ONCE(sqe->off);
4626 req->sync.len = READ_ONCE(sqe->len);
4627 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004628 return 0;
4629}
4630
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004631static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004632{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004633 int ret;
4634
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004635 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004636 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004637 return -EAGAIN;
4638
Jens Axboe9adbd452019-12-20 08:45:55 -07004639 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004640 req->sync.flags);
4641 if (ret < 0)
4642 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004643 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004644 return 0;
4645}
4646
YueHaibing469956e2020-03-04 15:53:52 +08004647#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004648static int io_setup_async_msg(struct io_kiocb *req,
4649 struct io_async_msghdr *kmsg)
4650{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004651 struct io_async_msghdr *async_msg = req->async_data;
4652
4653 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004654 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004655 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004656 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004657 return -ENOMEM;
4658 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004659 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004660 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004661 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004662 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004663 /* if were using fast_iov, set it to the new one */
4664 if (!async_msg->free_iov)
4665 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4666
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004667 return -EAGAIN;
4668}
4669
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004670static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4671 struct io_async_msghdr *iomsg)
4672{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004673 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004674 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004675 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004676 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004677}
4678
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004679static int io_sendmsg_prep_async(struct io_kiocb *req)
4680{
4681 int ret;
4682
4683 if (!io_op_defs[req->opcode].needs_async_data)
4684 return 0;
4685 ret = io_sendmsg_copy_hdr(req, req->async_data);
4686 if (!ret)
4687 req->flags |= REQ_F_NEED_CLEANUP;
4688 return ret;
4689}
4690
Jens Axboe3529d8c2019-12-19 18:24:38 -07004691static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004692{
Jens Axboee47293f2019-12-20 08:58:21 -07004693 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004694
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004695 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4696 return -EINVAL;
4697
Jens Axboee47293f2019-12-20 08:58:21 -07004698 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004699 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004700 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004701
Jens Axboed8768362020-02-27 14:17:49 -07004702#ifdef CONFIG_COMPAT
4703 if (req->ctx->compat)
4704 sr->msg_flags |= MSG_CMSG_COMPAT;
4705#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004706 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004707}
4708
Pavel Begunkov889fca72021-02-10 00:03:09 +00004709static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004710{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004711 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004712 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004713 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004714 int ret;
4715
Florent Revestdba4a922020-12-04 12:36:04 +01004716 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004717 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004718 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004719
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004720 kmsg = req->async_data;
4721 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004722 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004723 if (ret)
4724 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004725 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004726 }
4727
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004728 flags = req->sr_msg.msg_flags;
4729 if (flags & MSG_DONTWAIT)
4730 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004731 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004732 flags |= MSG_DONTWAIT;
4733
4734 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004735 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004736 return io_setup_async_msg(req, kmsg);
4737 if (ret == -ERESTARTSYS)
4738 ret = -EINTR;
4739
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004740 /* fast path, check for non-NULL to avoid function call */
4741 if (kmsg->free_iov)
4742 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004743 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004744 if (ret < 0)
4745 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004746 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004747 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004748}
4749
Pavel Begunkov889fca72021-02-10 00:03:09 +00004750static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004751{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004752 struct io_sr_msg *sr = &req->sr_msg;
4753 struct msghdr msg;
4754 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004755 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004756 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004757 int ret;
4758
Florent Revestdba4a922020-12-04 12:36:04 +01004759 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004760 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004761 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004762
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004763 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4764 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004765 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004766
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004767 msg.msg_name = NULL;
4768 msg.msg_control = NULL;
4769 msg.msg_controllen = 0;
4770 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004771
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004772 flags = req->sr_msg.msg_flags;
4773 if (flags & MSG_DONTWAIT)
4774 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004775 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004776 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004777
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004778 msg.msg_flags = flags;
4779 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004780 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004781 return -EAGAIN;
4782 if (ret == -ERESTARTSYS)
4783 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004784
Jens Axboe03b12302019-12-02 18:50:25 -07004785 if (ret < 0)
4786 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004787 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004788 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004789}
4790
Pavel Begunkov1400e692020-07-12 20:41:05 +03004791static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4792 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004793{
4794 struct io_sr_msg *sr = &req->sr_msg;
4795 struct iovec __user *uiov;
4796 size_t iov_len;
4797 int ret;
4798
Pavel Begunkov1400e692020-07-12 20:41:05 +03004799 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4800 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004801 if (ret)
4802 return ret;
4803
4804 if (req->flags & REQ_F_BUFFER_SELECT) {
4805 if (iov_len > 1)
4806 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004807 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004808 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004809 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004810 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004811 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004812 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004813 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004814 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004815 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004816 if (ret > 0)
4817 ret = 0;
4818 }
4819
4820 return ret;
4821}
4822
4823#ifdef CONFIG_COMPAT
4824static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004825 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004826{
4827 struct compat_msghdr __user *msg_compat;
4828 struct io_sr_msg *sr = &req->sr_msg;
4829 struct compat_iovec __user *uiov;
4830 compat_uptr_t ptr;
4831 compat_size_t len;
4832 int ret;
4833
Pavel Begunkov270a5942020-07-12 20:41:04 +03004834 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004835 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004836 &ptr, &len);
4837 if (ret)
4838 return ret;
4839
4840 uiov = compat_ptr(ptr);
4841 if (req->flags & REQ_F_BUFFER_SELECT) {
4842 compat_ssize_t clen;
4843
4844 if (len > 1)
4845 return -EINVAL;
4846 if (!access_ok(uiov, sizeof(*uiov)))
4847 return -EFAULT;
4848 if (__get_user(clen, &uiov->iov_len))
4849 return -EFAULT;
4850 if (clen < 0)
4851 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004852 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004853 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004854 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004855 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004856 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004857 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004858 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004859 if (ret < 0)
4860 return ret;
4861 }
4862
4863 return 0;
4864}
Jens Axboe03b12302019-12-02 18:50:25 -07004865#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004866
Pavel Begunkov1400e692020-07-12 20:41:05 +03004867static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4868 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004869{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004870 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004871
4872#ifdef CONFIG_COMPAT
4873 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004874 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004875#endif
4876
Pavel Begunkov1400e692020-07-12 20:41:05 +03004877 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004878}
4879
Jens Axboebcda7ba2020-02-23 16:42:51 -07004880static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004881 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004882{
4883 struct io_sr_msg *sr = &req->sr_msg;
4884 struct io_buffer *kbuf;
4885
Jens Axboebcda7ba2020-02-23 16:42:51 -07004886 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4887 if (IS_ERR(kbuf))
4888 return kbuf;
4889
4890 sr->kbuf = kbuf;
4891 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004892 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004893}
4894
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004895static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4896{
4897 return io_put_kbuf(req, req->sr_msg.kbuf);
4898}
4899
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004900static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004901{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004902 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004903
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004904 if (!io_op_defs[req->opcode].needs_async_data)
4905 return 0;
4906 ret = io_recvmsg_copy_hdr(req, req->async_data);
4907 if (!ret)
4908 req->flags |= REQ_F_NEED_CLEANUP;
4909 return ret;
4910}
4911
4912static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4913{
4914 struct io_sr_msg *sr = &req->sr_msg;
4915
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004916 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4917 return -EINVAL;
4918
Jens Axboe3529d8c2019-12-19 18:24:38 -07004919 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004920 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004921 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004922 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004923
Jens Axboed8768362020-02-27 14:17:49 -07004924#ifdef CONFIG_COMPAT
4925 if (req->ctx->compat)
4926 sr->msg_flags |= MSG_CMSG_COMPAT;
4927#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004928 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004929}
4930
Pavel Begunkov889fca72021-02-10 00:03:09 +00004931static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004932{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004933 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004934 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004935 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004936 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004937 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004938 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004939
Florent Revestdba4a922020-12-04 12:36:04 +01004940 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004941 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004942 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004943
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004944 kmsg = req->async_data;
4945 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004946 ret = io_recvmsg_copy_hdr(req, &iomsg);
4947 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004948 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004949 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004950 }
4951
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004952 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004953 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004954 if (IS_ERR(kbuf))
4955 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004956 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004957 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4958 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004959 1, req->sr_msg.len);
4960 }
4961
4962 flags = req->sr_msg.msg_flags;
4963 if (flags & MSG_DONTWAIT)
4964 req->flags |= REQ_F_NOWAIT;
4965 else if (force_nonblock)
4966 flags |= MSG_DONTWAIT;
4967
4968 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4969 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004970 if (force_nonblock && ret == -EAGAIN)
4971 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004972 if (ret == -ERESTARTSYS)
4973 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004974
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004975 if (req->flags & REQ_F_BUFFER_SELECTED)
4976 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004977 /* fast path, check for non-NULL to avoid function call */
4978 if (kmsg->free_iov)
4979 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004980 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004981 if (ret < 0)
4982 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004983 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004984 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004985}
4986
Pavel Begunkov889fca72021-02-10 00:03:09 +00004987static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004988{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004989 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004990 struct io_sr_msg *sr = &req->sr_msg;
4991 struct msghdr msg;
4992 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004993 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004994 struct iovec iov;
4995 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004996 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004997 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004998
Florent Revestdba4a922020-12-04 12:36:04 +01004999 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005000 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005001 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005002
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005003 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005004 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005005 if (IS_ERR(kbuf))
5006 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005007 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005008 }
5009
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005010 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005011 if (unlikely(ret))
5012 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005013
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005014 msg.msg_name = NULL;
5015 msg.msg_control = NULL;
5016 msg.msg_controllen = 0;
5017 msg.msg_namelen = 0;
5018 msg.msg_iocb = NULL;
5019 msg.msg_flags = 0;
5020
5021 flags = req->sr_msg.msg_flags;
5022 if (flags & MSG_DONTWAIT)
5023 req->flags |= REQ_F_NOWAIT;
5024 else if (force_nonblock)
5025 flags |= MSG_DONTWAIT;
5026
5027 ret = sock_recvmsg(sock, &msg, flags);
5028 if (force_nonblock && ret == -EAGAIN)
5029 return -EAGAIN;
5030 if (ret == -ERESTARTSYS)
5031 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005032out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005033 if (req->flags & REQ_F_BUFFER_SELECTED)
5034 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07005035 if (ret < 0)
5036 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005037 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005038 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005039}
5040
Jens Axboe3529d8c2019-12-19 18:24:38 -07005041static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005042{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005043 struct io_accept *accept = &req->accept;
5044
Jens Axboe14587a462020-09-05 11:36:08 -06005045 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005046 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05005047 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005048 return -EINVAL;
5049
Jens Axboed55e5f52019-12-11 16:12:15 -07005050 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5051 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005052 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005053 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005054 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005055}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005056
Pavel Begunkov889fca72021-02-10 00:03:09 +00005057static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005058{
5059 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005060 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005061 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005062 int ret;
5063
Jiufei Xuee697dee2020-06-10 13:41:59 +08005064 if (req->file->f_flags & O_NONBLOCK)
5065 req->flags |= REQ_F_NOWAIT;
5066
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005067 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06005068 accept->addr_len, accept->flags,
5069 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005070 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005071 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005072 if (ret < 0) {
5073 if (ret == -ERESTARTSYS)
5074 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005075 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005076 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005077 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005078 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005079}
5080
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005081static int io_connect_prep_async(struct io_kiocb *req)
5082{
5083 struct io_async_connect *io = req->async_data;
5084 struct io_connect *conn = &req->connect;
5085
5086 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5087}
5088
Jens Axboe3529d8c2019-12-19 18:24:38 -07005089static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005090{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005091 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005092
Jens Axboe14587a462020-09-05 11:36:08 -06005093 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005094 return -EINVAL;
5095 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5096 return -EINVAL;
5097
Jens Axboe3529d8c2019-12-19 18:24:38 -07005098 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5099 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005100 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005101}
5102
Pavel Begunkov889fca72021-02-10 00:03:09 +00005103static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005104{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005105 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005106 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005107 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005108 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005109
Jens Axboee8c2bc12020-08-15 18:44:09 -07005110 if (req->async_data) {
5111 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005112 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005113 ret = move_addr_to_kernel(req->connect.addr,
5114 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005115 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005116 if (ret)
5117 goto out;
5118 io = &__io;
5119 }
5120
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005121 file_flags = force_nonblock ? O_NONBLOCK : 0;
5122
Jens Axboee8c2bc12020-08-15 18:44:09 -07005123 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005124 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005125 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005126 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005127 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005128 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005129 ret = -ENOMEM;
5130 goto out;
5131 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005132 io = req->async_data;
5133 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005134 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005135 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005136 if (ret == -ERESTARTSYS)
5137 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005138out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005139 if (ret < 0)
5140 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005141 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005142 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005143}
YueHaibing469956e2020-03-04 15:53:52 +08005144#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005145#define IO_NETOP_FN(op) \
5146static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5147{ \
5148 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005149}
5150
Jens Axboe99a10082021-02-19 09:35:19 -07005151#define IO_NETOP_PREP(op) \
5152IO_NETOP_FN(op) \
5153static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5154{ \
5155 return -EOPNOTSUPP; \
5156} \
5157
5158#define IO_NETOP_PREP_ASYNC(op) \
5159IO_NETOP_PREP(op) \
5160static int io_##op##_prep_async(struct io_kiocb *req) \
5161{ \
5162 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005163}
5164
Jens Axboe99a10082021-02-19 09:35:19 -07005165IO_NETOP_PREP_ASYNC(sendmsg);
5166IO_NETOP_PREP_ASYNC(recvmsg);
5167IO_NETOP_PREP_ASYNC(connect);
5168IO_NETOP_PREP(accept);
5169IO_NETOP_FN(send);
5170IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005171#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005172
Jens Axboed7718a92020-02-14 22:23:12 -07005173struct io_poll_table {
5174 struct poll_table_struct pt;
5175 struct io_kiocb *req;
5176 int error;
5177};
5178
Jens Axboed7718a92020-02-14 22:23:12 -07005179static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5180 __poll_t mask, task_work_func_t func)
5181{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005182 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005183
5184 /* for instances that support it check for an event match first: */
5185 if (mask && !(mask & poll->events))
5186 return 0;
5187
5188 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5189
5190 list_del_init(&poll->wait.entry);
5191
Jens Axboed7718a92020-02-14 22:23:12 -07005192 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00005193 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005194 percpu_ref_get(&req->ctx->refs);
5195
Jens Axboed7718a92020-02-14 22:23:12 -07005196 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005197 * If this fails, then the task is exiting. When a task exits, the
5198 * work gets canceled, so just cancel this request as well instead
5199 * of executing it. We can't safely execute it anyway, as we may not
5200 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005201 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005202 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005203 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005204 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005205 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005206 }
Jens Axboed7718a92020-02-14 22:23:12 -07005207 return 1;
5208}
5209
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005210static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5211 __acquires(&req->ctx->completion_lock)
5212{
5213 struct io_ring_ctx *ctx = req->ctx;
5214
5215 if (!req->result && !READ_ONCE(poll->canceled)) {
5216 struct poll_table_struct pt = { ._key = poll->events };
5217
5218 req->result = vfs_poll(req->file, &pt) & poll->events;
5219 }
5220
5221 spin_lock_irq(&ctx->completion_lock);
5222 if (!req->result && !READ_ONCE(poll->canceled)) {
5223 add_wait_queue(poll->head, &poll->wait);
5224 return true;
5225 }
5226
5227 return false;
5228}
5229
Jens Axboed4e7cd32020-08-15 11:44:50 -07005230static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005231{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005232 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005233 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005234 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005235 return req->apoll->double_poll;
5236}
5237
5238static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5239{
5240 if (req->opcode == IORING_OP_POLL_ADD)
5241 return &req->poll;
5242 return &req->apoll->poll;
5243}
5244
5245static void io_poll_remove_double(struct io_kiocb *req)
5246{
5247 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005248
5249 lockdep_assert_held(&req->ctx->completion_lock);
5250
5251 if (poll && poll->head) {
5252 struct wait_queue_head *head = poll->head;
5253
5254 spin_lock(&head->lock);
5255 list_del_init(&poll->wait.entry);
5256 if (poll->wait.private)
5257 refcount_dec(&req->refs);
5258 poll->head = NULL;
5259 spin_unlock(&head->lock);
5260 }
5261}
5262
5263static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5264{
5265 struct io_ring_ctx *ctx = req->ctx;
5266
Jens Axboed4e7cd32020-08-15 11:44:50 -07005267 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005268 req->poll.done = true;
5269 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5270 io_commit_cqring(ctx);
5271}
5272
Jens Axboe18bceab2020-05-15 11:56:54 -06005273static void io_poll_task_func(struct callback_head *cb)
5274{
5275 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005276 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005277 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005278
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005279 if (io_poll_rewait(req, &req->poll)) {
5280 spin_unlock_irq(&ctx->completion_lock);
5281 } else {
5282 hash_del(&req->hash_node);
5283 io_poll_complete(req, req->result, 0);
5284 spin_unlock_irq(&ctx->completion_lock);
5285
5286 nxt = io_put_req_find_next(req);
5287 io_cqring_ev_posted(ctx);
5288 if (nxt)
5289 __io_req_task_submit(nxt);
5290 }
5291
Jens Axboe6d816e02020-08-11 08:04:14 -06005292 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005293}
5294
5295static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5296 int sync, void *key)
5297{
5298 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005299 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005300 __poll_t mask = key_to_poll(key);
5301
5302 /* for instances that support it check for an event match first: */
5303 if (mask && !(mask & poll->events))
5304 return 0;
5305
Jens Axboe8706e042020-09-28 08:38:54 -06005306 list_del_init(&wait->entry);
5307
Jens Axboe807abcb2020-07-17 17:09:27 -06005308 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005309 bool done;
5310
Jens Axboe807abcb2020-07-17 17:09:27 -06005311 spin_lock(&poll->head->lock);
5312 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005313 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005314 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005315 /* make sure double remove sees this as being gone */
5316 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005317 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005318 if (!done) {
5319 /* use wait func handler, so it matches the rq type */
5320 poll->wait.func(&poll->wait, mode, sync, key);
5321 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005322 }
5323 refcount_dec(&req->refs);
5324 return 1;
5325}
5326
5327static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5328 wait_queue_func_t wake_func)
5329{
5330 poll->head = NULL;
5331 poll->done = false;
5332 poll->canceled = false;
5333 poll->events = events;
5334 INIT_LIST_HEAD(&poll->wait.entry);
5335 init_waitqueue_func_entry(&poll->wait, wake_func);
5336}
5337
5338static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005339 struct wait_queue_head *head,
5340 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005341{
5342 struct io_kiocb *req = pt->req;
5343
5344 /*
5345 * If poll->head is already set, it's because the file being polled
5346 * uses multiple waitqueues for poll handling (eg one for read, one
5347 * for write). Setup a separate io_poll_iocb if this happens.
5348 */
5349 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005350 struct io_poll_iocb *poll_one = poll;
5351
Jens Axboe18bceab2020-05-15 11:56:54 -06005352 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005353 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005354 pt->error = -EINVAL;
5355 return;
5356 }
5357 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5358 if (!poll) {
5359 pt->error = -ENOMEM;
5360 return;
5361 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005362 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005363 refcount_inc(&req->refs);
5364 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005365 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005366 }
5367
5368 pt->error = 0;
5369 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005370
5371 if (poll->events & EPOLLEXCLUSIVE)
5372 add_wait_queue_exclusive(head, &poll->wait);
5373 else
5374 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005375}
5376
5377static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5378 struct poll_table_struct *p)
5379{
5380 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005381 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005382
Jens Axboe807abcb2020-07-17 17:09:27 -06005383 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005384}
5385
Jens Axboed7718a92020-02-14 22:23:12 -07005386static void io_async_task_func(struct callback_head *cb)
5387{
5388 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5389 struct async_poll *apoll = req->apoll;
5390 struct io_ring_ctx *ctx = req->ctx;
5391
5392 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5393
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005394 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005395 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005396 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005397 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005398 }
5399
Jens Axboe31067252020-05-17 17:43:31 -06005400 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005401 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005402 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005403
Jens Axboed4e7cd32020-08-15 11:44:50 -07005404 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005405 spin_unlock_irq(&ctx->completion_lock);
5406
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005407 if (!READ_ONCE(apoll->poll.canceled))
5408 __io_req_task_submit(req);
5409 else
5410 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005411
Jens Axboe6d816e02020-08-11 08:04:14 -06005412 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005413 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005414 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005415}
5416
5417static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5418 void *key)
5419{
5420 struct io_kiocb *req = wait->private;
5421 struct io_poll_iocb *poll = &req->apoll->poll;
5422
5423 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5424 key_to_poll(key));
5425
5426 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5427}
5428
5429static void io_poll_req_insert(struct io_kiocb *req)
5430{
5431 struct io_ring_ctx *ctx = req->ctx;
5432 struct hlist_head *list;
5433
5434 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5435 hlist_add_head(&req->hash_node, list);
5436}
5437
5438static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5439 struct io_poll_iocb *poll,
5440 struct io_poll_table *ipt, __poll_t mask,
5441 wait_queue_func_t wake_func)
5442 __acquires(&ctx->completion_lock)
5443{
5444 struct io_ring_ctx *ctx = req->ctx;
5445 bool cancel = false;
5446
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005447 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005448 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005449 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005450 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005451
5452 ipt->pt._key = mask;
5453 ipt->req = req;
5454 ipt->error = -EINVAL;
5455
Jens Axboed7718a92020-02-14 22:23:12 -07005456 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5457
5458 spin_lock_irq(&ctx->completion_lock);
5459 if (likely(poll->head)) {
5460 spin_lock(&poll->head->lock);
5461 if (unlikely(list_empty(&poll->wait.entry))) {
5462 if (ipt->error)
5463 cancel = true;
5464 ipt->error = 0;
5465 mask = 0;
5466 }
5467 if (mask || ipt->error)
5468 list_del_init(&poll->wait.entry);
5469 else if (cancel)
5470 WRITE_ONCE(poll->canceled, true);
5471 else if (!poll->done) /* actually waiting for an event */
5472 io_poll_req_insert(req);
5473 spin_unlock(&poll->head->lock);
5474 }
5475
5476 return mask;
5477}
5478
5479static bool io_arm_poll_handler(struct io_kiocb *req)
5480{
5481 const struct io_op_def *def = &io_op_defs[req->opcode];
5482 struct io_ring_ctx *ctx = req->ctx;
5483 struct async_poll *apoll;
5484 struct io_poll_table ipt;
5485 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005486 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005487
5488 if (!req->file || !file_can_poll(req->file))
5489 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005490 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005491 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005492 if (def->pollin)
5493 rw = READ;
5494 else if (def->pollout)
5495 rw = WRITE;
5496 else
5497 return false;
5498 /* if we can't nonblock try, then no point in arming a poll handler */
5499 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005500 return false;
5501
5502 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5503 if (unlikely(!apoll))
5504 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005505 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005506
5507 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005508 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005509
Nathan Chancellor8755d972020-03-02 16:01:19 -07005510 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005511 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005512 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005513 if (def->pollout)
5514 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005515
5516 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5517 if ((req->opcode == IORING_OP_RECVMSG) &&
5518 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5519 mask &= ~POLLIN;
5520
Jens Axboed7718a92020-02-14 22:23:12 -07005521 mask |= POLLERR | POLLPRI;
5522
5523 ipt.pt._qproc = io_async_queue_proc;
5524
5525 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5526 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005527 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005528 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005529 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005530 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005531 kfree(apoll);
5532 return false;
5533 }
5534 spin_unlock_irq(&ctx->completion_lock);
5535 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5536 apoll->poll.events);
5537 return true;
5538}
5539
5540static bool __io_poll_remove_one(struct io_kiocb *req,
5541 struct io_poll_iocb *poll)
5542{
Jens Axboeb41e9852020-02-17 09:52:41 -07005543 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005544
5545 spin_lock(&poll->head->lock);
5546 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005547 if (!list_empty(&poll->wait.entry)) {
5548 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005549 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005550 }
5551 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005552 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005553 return do_complete;
5554}
5555
5556static bool io_poll_remove_one(struct io_kiocb *req)
5557{
5558 bool do_complete;
5559
Jens Axboed4e7cd32020-08-15 11:44:50 -07005560 io_poll_remove_double(req);
5561
Jens Axboed7718a92020-02-14 22:23:12 -07005562 if (req->opcode == IORING_OP_POLL_ADD) {
5563 do_complete = __io_poll_remove_one(req, &req->poll);
5564 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005565 struct async_poll *apoll = req->apoll;
5566
Jens Axboed7718a92020-02-14 22:23:12 -07005567 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005568 do_complete = __io_poll_remove_one(req, &apoll->poll);
5569 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005570 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005571 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005572 kfree(apoll);
5573 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005574 }
5575
Jens Axboeb41e9852020-02-17 09:52:41 -07005576 if (do_complete) {
5577 io_cqring_fill_event(req, -ECANCELED);
5578 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005579 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005580 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005581 }
5582
5583 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005584}
5585
Jens Axboe76e1b642020-09-26 15:05:03 -06005586/*
5587 * Returns true if we found and killed one or more poll requests
5588 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005589static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5590 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005591{
Jens Axboe78076bb2019-12-04 19:56:40 -07005592 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005593 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005594 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005595
5596 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005597 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5598 struct hlist_head *list;
5599
5600 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005601 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005602 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005603 posted += io_poll_remove_one(req);
5604 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005605 }
5606 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005607
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005608 if (posted)
5609 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005610
5611 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005612}
5613
Jens Axboe47f46762019-11-09 17:43:02 -07005614static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5615{
Jens Axboe78076bb2019-12-04 19:56:40 -07005616 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005617 struct io_kiocb *req;
5618
Jens Axboe78076bb2019-12-04 19:56:40 -07005619 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5620 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005621 if (sqe_addr != req->user_data)
5622 continue;
5623 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005624 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005625 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005626 }
5627
5628 return -ENOENT;
5629}
5630
Jens Axboe3529d8c2019-12-19 18:24:38 -07005631static int io_poll_remove_prep(struct io_kiocb *req,
5632 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005633{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005634 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5635 return -EINVAL;
5636 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5637 sqe->poll_events)
5638 return -EINVAL;
5639
Pavel Begunkov018043b2020-10-27 23:17:18 +00005640 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005641 return 0;
5642}
5643
5644/*
5645 * Find a running poll command that matches one specified in sqe->addr,
5646 * and remove it if found.
5647 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005648static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005649{
5650 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005651 int ret;
5652
Jens Axboe221c5eb2019-01-17 09:41:58 -07005653 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005654 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005655 spin_unlock_irq(&ctx->completion_lock);
5656
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005657 if (ret < 0)
5658 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005659 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005660 return 0;
5661}
5662
Jens Axboe221c5eb2019-01-17 09:41:58 -07005663static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5664 void *key)
5665{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005666 struct io_kiocb *req = wait->private;
5667 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005668
Jens Axboed7718a92020-02-14 22:23:12 -07005669 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005670}
5671
Jens Axboe221c5eb2019-01-17 09:41:58 -07005672static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5673 struct poll_table_struct *p)
5674{
5675 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5676
Jens Axboee8c2bc12020-08-15 18:44:09 -07005677 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005678}
5679
Jens Axboe3529d8c2019-12-19 18:24:38 -07005680static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005681{
5682 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005683 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005684
5685 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5686 return -EINVAL;
5687 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5688 return -EINVAL;
5689
Jiufei Xue5769a352020-06-17 17:53:55 +08005690 events = READ_ONCE(sqe->poll32_events);
5691#ifdef __BIG_ENDIAN
5692 events = swahw32(events);
5693#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005694 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5695 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005696 return 0;
5697}
5698
Pavel Begunkov61e98202021-02-10 00:03:08 +00005699static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005700{
5701 struct io_poll_iocb *poll = &req->poll;
5702 struct io_ring_ctx *ctx = req->ctx;
5703 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005704 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005705
Jens Axboed7718a92020-02-14 22:23:12 -07005706 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005707
Jens Axboed7718a92020-02-14 22:23:12 -07005708 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5709 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005710
Jens Axboe8c838782019-03-12 15:48:16 -06005711 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005712 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005713 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005714 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005715 spin_unlock_irq(&ctx->completion_lock);
5716
Jens Axboe8c838782019-03-12 15:48:16 -06005717 if (mask) {
5718 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005719 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005720 }
Jens Axboe8c838782019-03-12 15:48:16 -06005721 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005722}
5723
Jens Axboe5262f562019-09-17 12:26:57 -06005724static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5725{
Jens Axboead8a48a2019-11-15 08:49:11 -07005726 struct io_timeout_data *data = container_of(timer,
5727 struct io_timeout_data, timer);
5728 struct io_kiocb *req = data->req;
5729 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005730 unsigned long flags;
5731
Jens Axboe5262f562019-09-17 12:26:57 -06005732 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005733 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005734 atomic_set(&req->ctx->cq_timeouts,
5735 atomic_read(&req->ctx->cq_timeouts) + 1);
5736
Jens Axboe78e19bb2019-11-06 15:21:34 -07005737 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005738 io_commit_cqring(ctx);
5739 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5740
5741 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005742 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005743 io_put_req(req);
5744 return HRTIMER_NORESTART;
5745}
5746
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005747static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5748 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005749{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005750 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005751 struct io_kiocb *req;
5752 int ret = -ENOENT;
5753
5754 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5755 if (user_data == req->user_data) {
5756 ret = 0;
5757 break;
5758 }
5759 }
5760
5761 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005762 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005763
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005764 io = req->async_data;
5765 ret = hrtimer_try_to_cancel(&io->timer);
5766 if (ret == -1)
5767 return ERR_PTR(-EALREADY);
5768 list_del_init(&req->timeout.list);
5769 return req;
5770}
5771
5772static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5773{
5774 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5775
5776 if (IS_ERR(req))
5777 return PTR_ERR(req);
5778
5779 req_set_fail_links(req);
5780 io_cqring_fill_event(req, -ECANCELED);
5781 io_put_req_deferred(req, 1);
5782 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005783}
5784
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005785static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5786 struct timespec64 *ts, enum hrtimer_mode mode)
5787{
5788 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5789 struct io_timeout_data *data;
5790
5791 if (IS_ERR(req))
5792 return PTR_ERR(req);
5793
5794 req->timeout.off = 0; /* noseq */
5795 data = req->async_data;
5796 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5797 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5798 data->timer.function = io_timeout_fn;
5799 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5800 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005801}
5802
Jens Axboe3529d8c2019-12-19 18:24:38 -07005803static int io_timeout_remove_prep(struct io_kiocb *req,
5804 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005805{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005806 struct io_timeout_rem *tr = &req->timeout_rem;
5807
Jens Axboeb29472e2019-12-17 18:50:29 -07005808 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5809 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005810 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5811 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005812 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005813 return -EINVAL;
5814
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005815 tr->addr = READ_ONCE(sqe->addr);
5816 tr->flags = READ_ONCE(sqe->timeout_flags);
5817 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5818 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5819 return -EINVAL;
5820 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5821 return -EFAULT;
5822 } else if (tr->flags) {
5823 /* timeout removal doesn't support flags */
5824 return -EINVAL;
5825 }
5826
Jens Axboeb29472e2019-12-17 18:50:29 -07005827 return 0;
5828}
5829
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005830static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5831{
5832 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5833 : HRTIMER_MODE_REL;
5834}
5835
Jens Axboe11365042019-10-16 09:08:32 -06005836/*
5837 * Remove or update an existing timeout command
5838 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005839static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005840{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005841 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005842 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005843 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005844
Jens Axboe11365042019-10-16 09:08:32 -06005845 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005846 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005847 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005848 else
5849 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5850 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005851
Jens Axboe47f46762019-11-09 17:43:02 -07005852 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005853 io_commit_cqring(ctx);
5854 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005855 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005856 if (ret < 0)
5857 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005858 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005859 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005860}
5861
Jens Axboe3529d8c2019-12-19 18:24:38 -07005862static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005863 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005864{
Jens Axboead8a48a2019-11-15 08:49:11 -07005865 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005866 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005867 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005868
Jens Axboead8a48a2019-11-15 08:49:11 -07005869 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005870 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005871 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005872 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005873 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005874 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005875 flags = READ_ONCE(sqe->timeout_flags);
5876 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005877 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005878
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005879 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005880
Jens Axboee8c2bc12020-08-15 18:44:09 -07005881 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005882 return -ENOMEM;
5883
Jens Axboee8c2bc12020-08-15 18:44:09 -07005884 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005885 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005886
5887 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005888 return -EFAULT;
5889
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005890 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005891 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5892 return 0;
5893}
5894
Pavel Begunkov61e98202021-02-10 00:03:08 +00005895static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005896{
Jens Axboead8a48a2019-11-15 08:49:11 -07005897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005898 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005899 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005900 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005901
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005902 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005903
Jens Axboe5262f562019-09-17 12:26:57 -06005904 /*
5905 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005906 * timeout event to be satisfied. If it isn't set, then this is
5907 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005908 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005909 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005910 entry = ctx->timeout_list.prev;
5911 goto add;
5912 }
Jens Axboe5262f562019-09-17 12:26:57 -06005913
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005914 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5915 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005916
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005917 /* Update the last seq here in case io_flush_timeouts() hasn't.
5918 * This is safe because ->completion_lock is held, and submissions
5919 * and completions are never mixed in the same ->completion_lock section.
5920 */
5921 ctx->cq_last_tm_flush = tail;
5922
Jens Axboe5262f562019-09-17 12:26:57 -06005923 /*
5924 * Insertion sort, ensuring the first entry in the list is always
5925 * the one we need first.
5926 */
Jens Axboe5262f562019-09-17 12:26:57 -06005927 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005928 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5929 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005930
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005931 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005932 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005933 /* nxt.seq is behind @tail, otherwise would've been completed */
5934 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005935 break;
5936 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005937add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005938 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005939 data->timer.function = io_timeout_fn;
5940 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005941 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005942 return 0;
5943}
5944
Jens Axboe62755e32019-10-28 21:49:21 -06005945static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005946{
Jens Axboe62755e32019-10-28 21:49:21 -06005947 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005948
Jens Axboe62755e32019-10-28 21:49:21 -06005949 return req->user_data == (unsigned long) data;
5950}
5951
Jens Axboee977d6d2019-11-05 12:39:45 -07005952static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005953{
Jens Axboe62755e32019-10-28 21:49:21 -06005954 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005955 int ret = 0;
5956
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005957 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005958 switch (cancel_ret) {
5959 case IO_WQ_CANCEL_OK:
5960 ret = 0;
5961 break;
5962 case IO_WQ_CANCEL_RUNNING:
5963 ret = -EALREADY;
5964 break;
5965 case IO_WQ_CANCEL_NOTFOUND:
5966 ret = -ENOENT;
5967 break;
5968 }
5969
Jens Axboee977d6d2019-11-05 12:39:45 -07005970 return ret;
5971}
5972
Jens Axboe47f46762019-11-09 17:43:02 -07005973static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5974 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005975 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005976{
5977 unsigned long flags;
5978 int ret;
5979
5980 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5981 if (ret != -ENOENT) {
5982 spin_lock_irqsave(&ctx->completion_lock, flags);
5983 goto done;
5984 }
5985
5986 spin_lock_irqsave(&ctx->completion_lock, flags);
5987 ret = io_timeout_cancel(ctx, sqe_addr);
5988 if (ret != -ENOENT)
5989 goto done;
5990 ret = io_poll_cancel(ctx, sqe_addr);
5991done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005992 if (!ret)
5993 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005994 io_cqring_fill_event(req, ret);
5995 io_commit_cqring(ctx);
5996 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5997 io_cqring_ev_posted(ctx);
5998
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005999 if (ret < 0)
6000 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03006001 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07006002}
6003
Jens Axboe3529d8c2019-12-19 18:24:38 -07006004static int io_async_cancel_prep(struct io_kiocb *req,
6005 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006006{
Jens Axboefbf23842019-12-17 18:45:56 -07006007 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006008 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006009 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6010 return -EINVAL;
6011 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07006012 return -EINVAL;
6013
Jens Axboefbf23842019-12-17 18:45:56 -07006014 req->cancel.addr = READ_ONCE(sqe->addr);
6015 return 0;
6016}
6017
Pavel Begunkov61e98202021-02-10 00:03:08 +00006018static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006019{
6020 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07006021
Pavel Begunkov014db002020-03-03 21:33:12 +03006022 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006023 return 0;
6024}
6025
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006026static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006027 const struct io_uring_sqe *sqe)
6028{
Jens Axboe6ca56f82020-09-18 16:51:19 -06006029 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
6030 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006031 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6032 return -EINVAL;
6033 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006034 return -EINVAL;
6035
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006036 req->rsrc_update.offset = READ_ONCE(sqe->off);
6037 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6038 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006039 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006040 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006041 return 0;
6042}
6043
Pavel Begunkov889fca72021-02-10 00:03:09 +00006044static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006045{
6046 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006047 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006048 int ret;
6049
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006050 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006051 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006052
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006053 up.offset = req->rsrc_update.offset;
6054 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006055
6056 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006057 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006058 mutex_unlock(&ctx->uring_lock);
6059
6060 if (ret < 0)
6061 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006062 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006063 return 0;
6064}
6065
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006066static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006067{
Jens Axboed625c6e2019-12-17 19:53:05 -07006068 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006069 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006070 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006071 case IORING_OP_READV:
6072 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006073 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006074 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006075 case IORING_OP_WRITEV:
6076 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006077 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006078 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006079 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006080 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006081 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006082 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006083 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006084 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006085 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006086 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006087 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006088 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006089 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006090 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006091 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006092 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006093 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006094 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006095 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006096 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006097 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006098 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006099 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006100 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006101 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006102 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006103 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006104 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006105 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006106 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006107 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006108 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006109 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006110 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006111 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006112 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006113 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006114 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006115 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006116 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006117 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006118 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006119 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006120 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006121 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006122 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006123 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006124 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006125 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006126 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006127 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006128 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006129 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006130 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006131 case IORING_OP_SHUTDOWN:
6132 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006133 case IORING_OP_RENAMEAT:
6134 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006135 case IORING_OP_UNLINKAT:
6136 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006137 }
6138
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006139 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6140 req->opcode);
6141 return-EINVAL;
6142}
6143
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006144static int io_req_prep_async(struct io_kiocb *req)
6145{
6146 switch (req->opcode) {
6147 case IORING_OP_READV:
6148 case IORING_OP_READ_FIXED:
6149 case IORING_OP_READ:
6150 return io_rw_prep_async(req, READ);
6151 case IORING_OP_WRITEV:
6152 case IORING_OP_WRITE_FIXED:
6153 case IORING_OP_WRITE:
6154 return io_rw_prep_async(req, WRITE);
6155 case IORING_OP_SENDMSG:
6156 case IORING_OP_SEND:
6157 return io_sendmsg_prep_async(req);
6158 case IORING_OP_RECVMSG:
6159 case IORING_OP_RECV:
6160 return io_recvmsg_prep_async(req);
6161 case IORING_OP_CONNECT:
6162 return io_connect_prep_async(req);
6163 }
6164 return 0;
6165}
6166
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006167static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006168{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006169 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006170 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006171 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006172 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006173 return 0;
6174 if (__io_alloc_async_data(req))
6175 return -EAGAIN;
6176 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177}
6178
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006179static u32 io_get_sequence(struct io_kiocb *req)
6180{
6181 struct io_kiocb *pos;
6182 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006183 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006184
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006185 io_for_each_link(pos, req)
6186 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006187
6188 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6189 return total_submitted - nr_reqs;
6190}
6191
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006192static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193{
6194 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006195 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006196 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006197 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006198
6199 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006200 if (likely(list_empty_careful(&ctx->defer_list) &&
6201 !(req->flags & REQ_F_IO_DRAIN)))
6202 return 0;
6203
6204 seq = io_get_sequence(req);
6205 /* Still a chance to pass the sequence check */
6206 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006207 return 0;
6208
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006209 ret = io_req_defer_prep(req);
6210 if (ret)
6211 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006212 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006213 de = kmalloc(sizeof(*de), GFP_KERNEL);
6214 if (!de)
6215 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006216
6217 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006218 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006219 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006220 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006221 io_queue_async_work(req);
6222 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006223 }
6224
6225 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006226 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006227 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006228 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006229 spin_unlock_irq(&ctx->completion_lock);
6230 return -EIOCBQUEUED;
6231}
Jens Axboeedafcce2019-01-09 09:16:05 -07006232
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006233static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006234{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006235 if (req->flags & REQ_F_BUFFER_SELECTED) {
6236 switch (req->opcode) {
6237 case IORING_OP_READV:
6238 case IORING_OP_READ_FIXED:
6239 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006240 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006241 break;
6242 case IORING_OP_RECVMSG:
6243 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006244 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006245 break;
6246 }
6247 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006248 }
6249
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006250 if (req->flags & REQ_F_NEED_CLEANUP) {
6251 switch (req->opcode) {
6252 case IORING_OP_READV:
6253 case IORING_OP_READ_FIXED:
6254 case IORING_OP_READ:
6255 case IORING_OP_WRITEV:
6256 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006257 case IORING_OP_WRITE: {
6258 struct io_async_rw *io = req->async_data;
6259 if (io->free_iovec)
6260 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006261 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006262 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006263 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006264 case IORING_OP_SENDMSG: {
6265 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006266
6267 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006268 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006269 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006270 case IORING_OP_SPLICE:
6271 case IORING_OP_TEE:
6272 io_put_file(req, req->splice.file_in,
6273 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6274 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006275 case IORING_OP_OPENAT:
6276 case IORING_OP_OPENAT2:
6277 if (req->open.filename)
6278 putname(req->open.filename);
6279 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006280 case IORING_OP_RENAMEAT:
6281 putname(req->rename.oldpath);
6282 putname(req->rename.newpath);
6283 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006284 case IORING_OP_UNLINKAT:
6285 putname(req->unlink.filename);
6286 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006287 }
6288 req->flags &= ~REQ_F_NEED_CLEANUP;
6289 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006290}
6291
Pavel Begunkov889fca72021-02-10 00:03:09 +00006292static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006293{
Jens Axboeedafcce2019-01-09 09:16:05 -07006294 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006295 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006296
Jens Axboed625c6e2019-12-17 19:53:05 -07006297 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006298 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006299 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006300 break;
6301 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006302 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006303 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006304 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305 break;
6306 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006308 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006309 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310 break;
6311 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006312 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006313 break;
6314 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006315 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316 break;
6317 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006318 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006319 break;
6320 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006321 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006322 break;
6323 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006324 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006325 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006326 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006327 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006328 break;
6329 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006330 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006331 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006332 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006333 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006334 break;
6335 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006336 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006337 break;
6338 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006339 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006340 break;
6341 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006342 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006343 break;
6344 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006345 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006346 break;
6347 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006348 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006349 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006350 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006351 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006352 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006353 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006354 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006355 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006356 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006357 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006358 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006359 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006360 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006361 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006362 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006363 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006364 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006365 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006366 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006367 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006368 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006369 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006370 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006371 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006372 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006373 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006374 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006375 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006376 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006377 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006378 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006379 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006380 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006381 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006382 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006383 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006384 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006385 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006386 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006387 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006388 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006389 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006390 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006391 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006392 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006393 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006394 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006395 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006396 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006397 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006398 default:
6399 ret = -EINVAL;
6400 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006401 }
6402
6403 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006404 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006405
Jens Axboeb5325762020-05-19 21:20:27 -06006406 /* If the op doesn't have a file, we're not polling for it */
6407 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006408 const bool in_async = io_wq_current_is_worker();
6409
Jens Axboe11ba8202020-01-15 21:51:17 -07006410 /* workqueue context doesn't hold uring_lock, grab it now */
6411 if (in_async)
6412 mutex_lock(&ctx->uring_lock);
6413
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006414 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006415
6416 if (in_async)
6417 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006418 }
6419
6420 return 0;
6421}
6422
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006423static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006424{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006426 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006427 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006428
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006429 timeout = io_prep_linked_timeout(req);
6430 if (timeout)
6431 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006432
Pavel Begunkova3df76982021-02-18 22:32:52 +00006433 if (work->flags & IO_WQ_WORK_CANCEL)
6434 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006435
Jens Axboe561fb042019-10-24 07:25:42 -06006436 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006437 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006438 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006439 /*
6440 * We can get EAGAIN for polled IO even though we're
6441 * forcing a sync submission from here, since we can't
6442 * wait for request slots on the block side.
6443 */
6444 if (ret != -EAGAIN)
6445 break;
6446 cond_resched();
6447 } while (1);
6448 }
Jens Axboe31b51512019-01-18 22:56:34 -07006449
Pavel Begunkova3df76982021-02-18 22:32:52 +00006450 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006451 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006452 /* io-wq is going to take one down */
6453 refcount_inc(&req->refs);
6454 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006455 }
Jens Axboe31b51512019-01-18 22:56:34 -07006456}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006457
Jens Axboe65e19f52019-10-26 07:20:21 -06006458static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6459 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006460{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006461 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006462
Jens Axboe05f3fb32019-12-09 11:22:50 -07006463 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006464 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006465}
6466
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006467static struct file *io_file_get(struct io_submit_state *state,
6468 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006469{
6470 struct io_ring_ctx *ctx = req->ctx;
6471 struct file *file;
6472
6473 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006474 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006475 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006476 fd = array_index_nospec(fd, ctx->nr_user_files);
6477 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006478 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006479 } else {
6480 trace_io_uring_file_get(ctx, fd);
6481 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006482 }
6483
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006484 if (file && unlikely(file->f_op == &io_uring_fops))
6485 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006486 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006487}
6488
Jens Axboe2665abf2019-11-05 12:40:47 -07006489static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6490{
Jens Axboead8a48a2019-11-15 08:49:11 -07006491 struct io_timeout_data *data = container_of(timer,
6492 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006493 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006494 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006495 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006496
6497 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006498 prev = req->timeout.head;
6499 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006500
6501 /*
6502 * We don't expect the list to be empty, that will only happen if we
6503 * race with the completion of the linked work.
6504 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006505 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006506 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006507 else
6508 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006509 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6510
6511 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006512 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006513 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006514 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006515 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006516 io_req_complete_post(req, -ETIME, 0);
6517 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006518 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006519 return HRTIMER_NORESTART;
6520}
6521
Jens Axboe7271ef32020-08-10 09:55:22 -06006522static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006523{
Jens Axboe76a46e02019-11-10 23:34:16 -07006524 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006525 * If the back reference is NULL, then our linked request finished
6526 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006527 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006528 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006529 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006530
Jens Axboead8a48a2019-11-15 08:49:11 -07006531 data->timer.function = io_link_timeout_fn;
6532 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6533 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006534 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006535}
6536
6537static void io_queue_linked_timeout(struct io_kiocb *req)
6538{
6539 struct io_ring_ctx *ctx = req->ctx;
6540
6541 spin_lock_irq(&ctx->completion_lock);
6542 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006543 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006544
Jens Axboe2665abf2019-11-05 12:40:47 -07006545 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006546 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006547}
6548
Jens Axboead8a48a2019-11-15 08:49:11 -07006549static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006550{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006551 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006552
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006553 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6554 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006555 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006556
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006557 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006558 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006559 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006560 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006561}
6562
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006563static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006564{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006565 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe193155c2020-02-22 23:22:19 -07006566 const struct cred *old_creds = NULL;
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006567 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006568
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006569 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6570 (req->work.flags & IO_WQ_WORK_CREDS) &&
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006571 req->work.identity->creds != current_cred())
6572 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006573
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006574 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006575
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006576 if (old_creds)
6577 revert_creds(old_creds);
6578
Jens Axboe491381ce2019-10-17 09:20:46 -06006579 /*
6580 * We async punt it if the file wasn't marked NOWAIT, or if the file
6581 * doesn't support non-blocking read/write attempts
6582 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006583 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006584 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006585 /*
6586 * Queued up for async execution, worker will release
6587 * submit reference when the iocb is actually submitted.
6588 */
6589 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006590 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006591 } else if (likely(!ret)) {
6592 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006593 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006594 struct io_ring_ctx *ctx = req->ctx;
6595 struct io_comp_state *cs = &ctx->submit_state.comp;
6596
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006597 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006598 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006599 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006600 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006601 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006602 }
6603 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006604 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006605 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006606 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006607 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006608 if (linked_timeout)
6609 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006610}
6611
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006612static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006613{
6614 int ret;
6615
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006616 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006617 if (ret) {
6618 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006619fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006620 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006621 io_put_req(req);
6622 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006623 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006624 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006625 ret = io_req_defer_prep(req);
6626 if (unlikely(ret))
6627 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006628 io_queue_async_work(req);
6629 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006630 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006631 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006632}
6633
Pavel Begunkovb16fed662021-02-18 18:29:40 +00006634/*
6635 * Check SQE restrictions (opcode and flags).
6636 *
6637 * Returns 'true' if SQE is allowed, 'false' otherwise.
6638 */
6639static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6640 struct io_kiocb *req,
6641 unsigned int sqe_flags)
6642{
6643 if (!ctx->restricted)
6644 return true;
6645
6646 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6647 return false;
6648
6649 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6650 ctx->restrictions.sqe_flags_required)
6651 return false;
6652
6653 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6654 ctx->restrictions.sqe_flags_required))
6655 return false;
6656
6657 return true;
6658}
6659
6660static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
6661 const struct io_uring_sqe *sqe)
6662{
6663 struct io_submit_state *state;
6664 unsigned int sqe_flags;
6665 int id, ret = 0;
6666
6667 req->opcode = READ_ONCE(sqe->opcode);
6668 /* same numerical values with corresponding REQ_F_*, safe to copy */
6669 req->flags = sqe_flags = READ_ONCE(sqe->flags);
6670 req->user_data = READ_ONCE(sqe->user_data);
6671 req->async_data = NULL;
6672 req->file = NULL;
6673 req->ctx = ctx;
6674 req->link = NULL;
6675 req->fixed_rsrc_refs = NULL;
6676 /* one is dropped after submission, the other at completion */
6677 refcount_set(&req->refs, 2);
6678 req->task = current;
6679 req->result = 0;
6680
6681 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006682 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6683 req->flags = 0;
Pavel Begunkovb16fed662021-02-18 18:29:40 +00006684 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006685 }
Pavel Begunkovb16fed662021-02-18 18:29:40 +00006686
6687 if (unlikely(req->opcode >= IORING_OP_LAST))
6688 return -EINVAL;
6689
6690 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
6691 return -EFAULT;
6692
6693 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6694 return -EACCES;
6695
6696 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6697 !io_op_defs[req->opcode].buffer_select)
6698 return -EOPNOTSUPP;
6699
6700 id = READ_ONCE(sqe->personality);
6701 if (id) {
6702 struct io_identity *iod;
6703
6704 iod = idr_find(&ctx->personality_idr, id);
6705 if (unlikely(!iod))
6706 return -EINVAL;
6707 refcount_inc(&iod->count);
6708
6709 __io_req_init_async(req);
6710 get_cred(iod->creds);
6711 req->work.identity = iod;
6712 req->work.flags |= IO_WQ_WORK_CREDS;
6713 }
6714
6715 state = &ctx->submit_state;
6716
6717 /*
6718 * Plug now if we have more than 1 IO left after this, and the target
6719 * is potentially a read/write to block based storage.
6720 */
6721 if (!state->plug_started && state->ios_left > 1 &&
6722 io_op_defs[req->opcode].plug) {
6723 blk_start_plug(&state->plug);
6724 state->plug_started = true;
6725 }
6726
6727 if (io_op_defs[req->opcode].needs_file) {
6728 bool fixed = req->flags & REQ_F_FIXED_FILE;
6729
6730 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
6731 if (unlikely(!req->file))
6732 ret = -EBADF;
6733 }
6734
6735 state->ios_left--;
6736 return ret;
6737}
6738
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006739static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006740 const struct io_uring_sqe *sqe)
Jens Axboe9e645e112019-05-10 16:07:28 -06006741{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006742 struct io_submit_link *link = &ctx->submit_state.link;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006743 int ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06006744
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006745 ret = io_init_req(ctx, req, sqe);
6746 if (unlikely(ret)) {
6747fail_req:
6748 io_put_req(req);
6749 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006750 if (link->head) {
6751 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006752 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006753 io_put_req(link->head);
6754 io_req_complete(link->head, -ECANCELED);
6755 link->head = NULL;
6756 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006757 return ret;
6758 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006759 ret = io_req_prep(req, sqe);
6760 if (unlikely(ret))
6761 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006762
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006763 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006764 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6765 true, ctx->flags & IORING_SETUP_SQPOLL);
6766
Jens Axboe9e645e112019-05-10 16:07:28 -06006767 /*
6768 * If we already have a head request, queue this one for async
6769 * submittal once the head completes. If we don't have a head but
6770 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6771 * submitted sync once the chain is complete. If none of those
6772 * conditions are true (normal request), then just queue it.
6773 */
Pavel Begunkov863e0562020-10-27 23:25:35 +00006774 if (link->head) {
6775 struct io_kiocb *head = link->head;
Jens Axboe9e645e112019-05-10 16:07:28 -06006776
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006777 /*
6778 * Taking sequential execution of a link, draining both sides
6779 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6780 * requests in the link. So, it drains the head and the
6781 * next after the link request. The last one is done via
6782 * drain_next flag to persist the effect across calls.
6783 */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006784 if (req->flags & REQ_F_IO_DRAIN) {
Pavel Begunkov711be032020-01-17 03:57:59 +03006785 head->flags |= REQ_F_IO_DRAIN;
6786 ctx->drain_next = 1;
6787 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006788 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006789 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006790 goto fail_req;
Pavel Begunkov9d763772019-12-17 02:22:07 +03006791 trace_io_uring_link(ctx, req, head);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006792 link->last->link = req;
Pavel Begunkov863e0562020-10-27 23:25:35 +00006793 link->last = req;
Jens Axboe9e645e112019-05-10 16:07:28 -06006794
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006795 /* last request of a link, enqueue the link */
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006796 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006797 io_queue_sqe(head);
Pavel Begunkov863e0562020-10-27 23:25:35 +00006798 link->head = NULL;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006799 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006800 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03006801 if (unlikely(ctx->drain_next)) {
6802 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006803 ctx->drain_next = 0;
Pavel Begunkov711be032020-01-17 03:57:59 +03006804 }
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006805 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Pavel Begunkov863e0562020-10-27 23:25:35 +00006806 link->head = req;
6807 link->last = req;
Pavel Begunkov711be032020-01-17 03:57:59 +03006808 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006809 io_queue_sqe(req);
Pavel Begunkov711be032020-01-17 03:57:59 +03006810 }
Jens Axboe9e645e112019-05-10 16:07:28 -06006811 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006812
Pavel Begunkov1d4240c2020-04-12 02:05:03 +03006813 return 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06006814}
6815
Jens Axboe9a56a232019-01-09 09:06:50 -07006816/*
6817 * Batched submission is done, ensure local IO is flushed out.
6818 */
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006819static void io_submit_state_end(struct io_submit_state *state,
6820 struct io_ring_ctx *ctx)
Jens Axboe9a56a232019-01-09 09:06:50 -07006821{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006822 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006823 io_queue_sqe(state->link.head);
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006824 if (state->comp.nr)
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006825 io_submit_flush_completions(&state->comp, ctx);
Jens Axboe27926b62020-10-28 09:33:23 -06006826 if (state->plug_started)
6827 blk_finish_plug(&state->plug);
Pavel Begunkov9f13c352020-05-17 14:13:41 +03006828 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07006829}
6830
6831/*
6832 * Start submission side cache.
6833 */
6834static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006835 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07006836{
Jens Axboe27926b62020-10-28 09:33:23 -06006837 state->plug_started = false;
Jens Axboe9a56a232019-01-09 09:06:50 -07006838 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006839 /* set only head, no need to init link_last in advance */
6840 state->link.head = NULL;
Jens Axboe9a56a232019-01-09 09:06:50 -07006841}
6842
Jens Axboe2b188cc2019-01-07 10:46:33 -07006843static void io_commit_sqring(struct io_ring_ctx *ctx)
6844{
Hristo Venev75b28af2019-08-26 17:23:46 +00006845 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006846
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03006847 /*
6848 * Ensure any loads from the SQEs are done at this point,
6849 * since once we write the new head, the application could
6850 * write new data to them.
6851 */
6852 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006853}
6854
6855/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006856 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07006857 * that is mapped by userspace. This means that care needs to be taken to
6858 * ensure that reads are stable, as we cannot rely on userspace always
6859 * being a good citizen. If members of the sqe are validated and then later
6860 * used, it's important that those reads are done through READ_ONCE() to
6861 * prevent a re-load down the line.
6862 */
Pavel Begunkov709b3022020-04-08 08:58:43 +03006863static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006864{
Hristo Venev75b28af2019-08-26 17:23:46 +00006865 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006866 unsigned head;
6867
6868 /*
6869 * The cached sq head (or cq tail) serves two purposes:
6870 *
6871 * 1) allows us to batch the cost of updating the user visible
6872 * head updates.
6873 * 2) allows the kernel side to track the head on its own, even
6874 * though the application is the one updating it.
6875 */
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006876 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006877 if (likely(head < ctx->sq_entries))
6878 return &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07006879
6880 /* drop invalid entries */
Jens Axboe498ccd92019-10-25 10:04:25 -06006881 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006882 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Pavel Begunkov709b3022020-04-08 08:58:43 +03006883 return NULL;
6884}
6885
Jens Axboe0f212202020-09-13 13:09:39 -06006886static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006887{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006888 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006889
Jens Axboec4a2ed72019-11-21 21:01:26 -07006890 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006891 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006892 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006893 return -EBUSY;
6894 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006895
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006896 /* make sure SQ entry isn't read before tail */
6897 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006898
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006899 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6900 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006901
Jens Axboed8a6df12020-10-15 16:24:45 -06006902 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006903 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006904 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006905
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006906 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006907 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006908 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006909
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006910 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006911 if (unlikely(!req)) {
6912 if (!submitted)
6913 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006914 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006915 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006916 sqe = io_get_sqe(ctx);
6917 if (unlikely(!sqe)) {
6918 kmem_cache_free(req_cachep, req);
6919 break;
6920 }
Jens Axboed3656342019-12-18 09:50:26 -07006921 /* will complete beyond this point, count as submitted */
6922 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006923 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006924 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006925 }
6926
Pavel Begunkov9466f432020-01-25 22:34:01 +03006927 if (unlikely(submitted != nr)) {
6928 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006929 struct io_uring_task *tctx = current->io_uring;
6930 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006931
Jens Axboed8a6df12020-10-15 16:24:45 -06006932 percpu_ref_put_many(&ctx->refs, unused);
6933 percpu_counter_sub(&tctx->inflight, unused);
6934 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006935 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006936
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006937 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006938 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6939 io_commit_sqring(ctx);
6940
Jens Axboe6c271ce2019-01-10 11:22:30 -07006941 return submitted;
6942}
6943
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006944static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6945{
6946 /* Tell userspace we may need a wakeup call */
6947 spin_lock_irq(&ctx->completion_lock);
6948 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6949 spin_unlock_irq(&ctx->completion_lock);
6950}
6951
6952static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6953{
6954 spin_lock_irq(&ctx->completion_lock);
6955 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6956 spin_unlock_irq(&ctx->completion_lock);
6957}
6958
Xiaoguang Wang08369242020-11-03 14:15:59 +08006959static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006960{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006961 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006962 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006963
Jens Axboec8d1ba52020-09-14 11:07:26 -06006964 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006965 /* if we're handling multiple rings, cap submit size for fairness */
6966 if (cap_entries && to_submit > 8)
6967 to_submit = 8;
6968
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006969 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6970 unsigned nr_events = 0;
6971
Xiaoguang Wang08369242020-11-03 14:15:59 +08006972 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006973 if (!list_empty(&ctx->iopoll_list))
6974 io_do_iopoll(ctx, &nr_events, 0);
6975
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006976 if (to_submit && !ctx->sqo_dead &&
6977 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006978 ret = io_submit_sqes(ctx, to_submit);
6979 mutex_unlock(&ctx->uring_lock);
6980 }
Jens Axboe90554202020-09-03 12:12:41 -06006981
6982 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6983 wake_up(&ctx->sqo_sq_wait);
6984
Xiaoguang Wang08369242020-11-03 14:15:59 +08006985 return ret;
6986}
6987
6988static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6989{
6990 struct io_ring_ctx *ctx;
6991 unsigned sq_thread_idle = 0;
6992
6993 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6994 if (sq_thread_idle < ctx->sq_thread_idle)
6995 sq_thread_idle = ctx->sq_thread_idle;
6996 }
6997
6998 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006999}
7000
Jens Axboe69fb2132020-09-14 11:16:23 -06007001static void io_sqd_init_new(struct io_sq_data *sqd)
7002{
7003 struct io_ring_ctx *ctx;
7004
7005 while (!list_empty(&sqd->ctx_new_list)) {
7006 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007007 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7008 complete(&ctx->sq_thread_comp);
7009 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007010
7011 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007012}
7013
Jens Axboe6c271ce2019-01-10 11:22:30 -07007014static int io_sq_thread(void *data)
7015{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007016 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007017 struct files_struct *old_files = current->files;
7018 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007019 const struct cred *old_cred = NULL;
7020 struct io_sq_data *sqd = data;
7021 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007022 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007023 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007024
Jens Axboe28cea78a2020-09-14 10:51:17 -06007025 task_lock(current);
7026 current->files = NULL;
7027 current->nsproxy = NULL;
7028 task_unlock(current);
7029
Jens Axboe69fb2132020-09-14 11:16:23 -06007030 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007031 int ret;
7032 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007033
7034 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007035 * Any changes to the sqd lists are synchronized through the
7036 * kthread parking. This synchronizes the thread vs users,
7037 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007038 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007039 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007040 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007041 /*
7042 * When sq thread is unparked, in case the previous park operation
7043 * comes from io_put_sq_data(), which means that sq thread is going
7044 * to be stopped, so here needs to have a check.
7045 */
7046 if (kthread_should_stop())
7047 break;
7048 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007049
Xiaoguang Wang08369242020-11-03 14:15:59 +08007050 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007051 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007052 timeout = jiffies + sqd->sq_thread_idle;
7053 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007054
Xiaoguang Wang08369242020-11-03 14:15:59 +08007055 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007056 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007057 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7058 if (current->cred != ctx->creds) {
7059 if (old_cred)
7060 revert_creds(old_cred);
7061 old_cred = override_creds(ctx->creds);
7062 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007063 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007064#ifdef CONFIG_AUDIT
7065 current->loginuid = ctx->loginuid;
7066 current->sessionid = ctx->sessionid;
7067#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007068
Xiaoguang Wang08369242020-11-03 14:15:59 +08007069 ret = __io_sq_thread(ctx, cap_entries);
7070 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7071 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007072
Jens Axboe28cea78a2020-09-14 10:51:17 -06007073 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007074 }
7075
Xiaoguang Wang08369242020-11-03 14:15:59 +08007076 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007077 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007078 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007079 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007080 if (sqt_spin)
7081 timeout = jiffies + sqd->sq_thread_idle;
7082 continue;
7083 }
7084
Xiaoguang Wang08369242020-11-03 14:15:59 +08007085 needs_sched = true;
7086 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7087 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7088 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7089 !list_empty_careful(&ctx->iopoll_list)) {
7090 needs_sched = false;
7091 break;
7092 }
7093 if (io_sqring_entries(ctx)) {
7094 needs_sched = false;
7095 break;
7096 }
7097 }
7098
Hao Xu8b28fdf2021-01-31 22:39:04 +08007099 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007100 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7101 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007102
Jens Axboe69fb2132020-09-14 11:16:23 -06007103 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007104 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7105 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007106 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007107
7108 finish_wait(&sqd->wait, &wait);
7109 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007110 }
7111
Jens Axboe4c6e2772020-07-01 11:29:10 -06007112 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007113 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007114
Dennis Zhou91d8f512020-09-16 13:41:05 -07007115 if (cur_css)
7116 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007117 if (old_cred)
7118 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007119
Jens Axboe28cea78a2020-09-14 10:51:17 -06007120 task_lock(current);
7121 current->files = old_files;
7122 current->nsproxy = old_nsproxy;
7123 task_unlock(current);
7124
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007125 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007126
Jens Axboe6c271ce2019-01-10 11:22:30 -07007127 return 0;
7128}
7129
Jens Axboebda52162019-09-24 13:47:15 -06007130struct io_wait_queue {
7131 struct wait_queue_entry wq;
7132 struct io_ring_ctx *ctx;
7133 unsigned to_wait;
7134 unsigned nr_timeouts;
7135};
7136
Pavel Begunkov6c503152021-01-04 20:36:36 +00007137static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007138{
7139 struct io_ring_ctx *ctx = iowq->ctx;
7140
7141 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007142 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007143 * started waiting. For timeouts, we always want to return to userspace,
7144 * regardless of event count.
7145 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007146 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007147 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7148}
7149
7150static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7151 int wake_flags, void *key)
7152{
7153 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7154 wq);
7155
Pavel Begunkov6c503152021-01-04 20:36:36 +00007156 /*
7157 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7158 * the task, and the next invocation will do it.
7159 */
7160 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7161 return autoremove_wake_function(curr, mode, wake_flags, key);
7162 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007163}
7164
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007165static int io_run_task_work_sig(void)
7166{
7167 if (io_run_task_work())
7168 return 1;
7169 if (!signal_pending(current))
7170 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007171 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7172 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007173 return -EINTR;
7174}
7175
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007176/* when returns >0, the caller should retry */
7177static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7178 struct io_wait_queue *iowq,
7179 signed long *timeout)
7180{
7181 int ret;
7182
7183 /* make sure we run task_work before checking for signals */
7184 ret = io_run_task_work_sig();
7185 if (ret || io_should_wake(iowq))
7186 return ret;
7187 /* let the caller flush overflows, retry */
7188 if (test_bit(0, &ctx->cq_check_overflow))
7189 return 1;
7190
7191 *timeout = schedule_timeout(*timeout);
7192 return !*timeout ? -ETIME : 1;
7193}
7194
Jens Axboe2b188cc2019-01-07 10:46:33 -07007195/*
7196 * Wait until events become available, if we don't already have some. The
7197 * application must reap them itself, as they reside on the shared cq ring.
7198 */
7199static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007200 const sigset_t __user *sig, size_t sigsz,
7201 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007202{
Jens Axboebda52162019-09-24 13:47:15 -06007203 struct io_wait_queue iowq = {
7204 .wq = {
7205 .private = current,
7206 .func = io_wake_function,
7207 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7208 },
7209 .ctx = ctx,
7210 .to_wait = min_events,
7211 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007212 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007213 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7214 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007215
Jens Axboeb41e9852020-02-17 09:52:41 -07007216 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007217 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7218 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007219 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007220 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007221 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007222 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007223
7224 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007225#ifdef CONFIG_COMPAT
7226 if (in_compat_syscall())
7227 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007228 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007229 else
7230#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007231 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007232
Jens Axboe2b188cc2019-01-07 10:46:33 -07007233 if (ret)
7234 return ret;
7235 }
7236
Hao Xuc73ebb62020-11-03 10:54:37 +08007237 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007238 struct timespec64 ts;
7239
Hao Xuc73ebb62020-11-03 10:54:37 +08007240 if (get_timespec64(&ts, uts))
7241 return -EFAULT;
7242 timeout = timespec64_to_jiffies(&ts);
7243 }
7244
Jens Axboebda52162019-09-24 13:47:15 -06007245 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007246 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007247 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007248 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007249 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7250 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007251 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7252 finish_wait(&ctx->wait, &iowq.wq);
7253 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007254
Jens Axboeb7db41c2020-07-04 08:55:50 -06007255 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007256
Hristo Venev75b28af2019-08-26 17:23:46 +00007257 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007258}
7259
Jens Axboe6b063142019-01-10 22:13:58 -07007260static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7261{
7262#if defined(CONFIG_UNIX)
7263 if (ctx->ring_sock) {
7264 struct sock *sock = ctx->ring_sock->sk;
7265 struct sk_buff *skb;
7266
7267 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7268 kfree_skb(skb);
7269 }
7270#else
7271 int i;
7272
Jens Axboe65e19f52019-10-26 07:20:21 -06007273 for (i = 0; i < ctx->nr_user_files; i++) {
7274 struct file *file;
7275
7276 file = io_file_from_index(ctx, i);
7277 if (file)
7278 fput(file);
7279 }
Jens Axboe6b063142019-01-10 22:13:58 -07007280#endif
7281}
7282
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007283static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007284{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007285 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007286
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007287 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007288 complete(&data->done);
7289}
7290
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007291static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
7292{
7293 spin_lock_bh(&ctx->rsrc_ref_lock);
7294}
7295
7296static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
7297{
7298 spin_unlock_bh(&ctx->rsrc_ref_lock);
7299}
7300
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007301static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7302 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007303 struct fixed_rsrc_ref_node *ref_node)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007304{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007305 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007306 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007307 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007308 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007309 percpu_ref_get(&rsrc_data->refs);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007310}
7311
Hao Xu8bad28d2021-02-19 17:19:36 +08007312static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07007313{
Hao Xu8bad28d2021-02-19 17:19:36 +08007314 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007315
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007316 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007317 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00007318 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007319 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007320 if (ref_node)
7321 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08007322}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007323
Hao Xu8bad28d2021-02-19 17:19:36 +08007324static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7325 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007326 void (*rsrc_put)(struct io_ring_ctx *ctx,
7327 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08007328{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007329 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08007330 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007331
Hao Xu8bad28d2021-02-19 17:19:36 +08007332 if (data->quiesce)
7333 return -ENXIO;
7334
7335 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007336 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007337 ret = -ENOMEM;
7338 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7339 if (!backup_node)
7340 break;
7341 backup_node->rsrc_data = data;
7342 backup_node->rsrc_put = rsrc_put;
7343
Hao Xu8bad28d2021-02-19 17:19:36 +08007344 io_sqe_rsrc_kill_node(ctx, data);
7345 percpu_ref_kill(&data->refs);
7346 flush_delayed_work(&ctx->rsrc_put_work);
7347
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007348 ret = wait_for_completion_interruptible(&data->done);
Pavel Begunkov88f171a2021-02-20 18:03:50 +00007349 if (!ret || !io_refs_resurrect(&data->refs, &data->done))
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007350 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007351
Hao Xu8bad28d2021-02-19 17:19:36 +08007352 io_sqe_rsrc_set_node(ctx, data, backup_node);
7353 backup_node = NULL;
Hao Xu8bad28d2021-02-19 17:19:36 +08007354 mutex_unlock(&ctx->uring_lock);
7355 ret = io_run_task_work_sig();
7356 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007357 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007358 data->quiesce = false;
7359
7360 if (backup_node)
7361 destroy_fixed_rsrc_ref_node(backup_node);
7362 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007363}
7364
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007365static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7366{
7367 struct fixed_rsrc_data *data;
7368
7369 data = kzalloc(sizeof(*data), GFP_KERNEL);
7370 if (!data)
7371 return NULL;
7372
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007373 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007374 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7375 kfree(data);
7376 return NULL;
7377 }
7378 data->ctx = ctx;
7379 init_completion(&data->done);
7380 return data;
7381}
7382
7383static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7384{
7385 percpu_ref_exit(&data->refs);
7386 kfree(data->table);
7387 kfree(data);
7388}
7389
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007390static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7391{
7392 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007393 unsigned nr_tables, i;
7394 int ret;
7395
Hao Xu8bad28d2021-02-19 17:19:36 +08007396 /*
7397 * percpu_ref_is_dying() is to stop parallel files unregister
7398 * Since we possibly drop uring lock later in this function to
7399 * run task work.
7400 */
7401 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007402 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007403 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007404 if (ret)
7405 return ret;
7406
Jens Axboe6b063142019-01-10 22:13:58 -07007407 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007408 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7409 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007410 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007411 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007412 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007413 ctx->nr_user_files = 0;
7414 return 0;
7415}
7416
Jens Axboe534ca6d2020-09-02 13:52:19 -06007417static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007418{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007419 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007420 /*
7421 * The park is a bit of a work-around, without it we get
7422 * warning spews on shutdown with SQPOLL set and affinity
7423 * set to a single CPU.
7424 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007425 if (sqd->thread) {
7426 kthread_park(sqd->thread);
7427 kthread_stop(sqd->thread);
7428 }
7429
7430 kfree(sqd);
7431 }
7432}
7433
Jens Axboeaa061652020-09-02 14:50:27 -06007434static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7435{
7436 struct io_ring_ctx *ctx_attach;
7437 struct io_sq_data *sqd;
7438 struct fd f;
7439
7440 f = fdget(p->wq_fd);
7441 if (!f.file)
7442 return ERR_PTR(-ENXIO);
7443 if (f.file->f_op != &io_uring_fops) {
7444 fdput(f);
7445 return ERR_PTR(-EINVAL);
7446 }
7447
7448 ctx_attach = f.file->private_data;
7449 sqd = ctx_attach->sq_data;
7450 if (!sqd) {
7451 fdput(f);
7452 return ERR_PTR(-EINVAL);
7453 }
7454
7455 refcount_inc(&sqd->refs);
7456 fdput(f);
7457 return sqd;
7458}
7459
Jens Axboe534ca6d2020-09-02 13:52:19 -06007460static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7461{
7462 struct io_sq_data *sqd;
7463
Jens Axboeaa061652020-09-02 14:50:27 -06007464 if (p->flags & IORING_SETUP_ATTACH_WQ)
7465 return io_attach_sq_data(p);
7466
Jens Axboe534ca6d2020-09-02 13:52:19 -06007467 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7468 if (!sqd)
7469 return ERR_PTR(-ENOMEM);
7470
7471 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007472 INIT_LIST_HEAD(&sqd->ctx_list);
7473 INIT_LIST_HEAD(&sqd->ctx_new_list);
7474 mutex_init(&sqd->ctx_lock);
7475 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007476 init_waitqueue_head(&sqd->wait);
7477 return sqd;
7478}
7479
Jens Axboe69fb2132020-09-14 11:16:23 -06007480static void io_sq_thread_unpark(struct io_sq_data *sqd)
7481 __releases(&sqd->lock)
7482{
7483 if (!sqd->thread)
7484 return;
7485 kthread_unpark(sqd->thread);
7486 mutex_unlock(&sqd->lock);
7487}
7488
7489static void io_sq_thread_park(struct io_sq_data *sqd)
7490 __acquires(&sqd->lock)
7491{
7492 if (!sqd->thread)
7493 return;
7494 mutex_lock(&sqd->lock);
7495 kthread_park(sqd->thread);
7496}
7497
Jens Axboe534ca6d2020-09-02 13:52:19 -06007498static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7499{
7500 struct io_sq_data *sqd = ctx->sq_data;
7501
7502 if (sqd) {
7503 if (sqd->thread) {
7504 /*
7505 * We may arrive here from the error branch in
7506 * io_sq_offload_create() where the kthread is created
7507 * without being waked up, thus wake it up now to make
7508 * sure the wait will complete.
7509 */
7510 wake_up_process(sqd->thread);
7511 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007512
7513 io_sq_thread_park(sqd);
7514 }
7515
7516 mutex_lock(&sqd->ctx_lock);
7517 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007518 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007519 mutex_unlock(&sqd->ctx_lock);
7520
Xiaoguang Wang08369242020-11-03 14:15:59 +08007521 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007522 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007523
7524 io_put_sq_data(sqd);
7525 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007526 }
7527}
7528
Jens Axboe6b063142019-01-10 22:13:58 -07007529static void io_finish_async(struct io_ring_ctx *ctx)
7530{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007531 io_sq_thread_stop(ctx);
7532
Jens Axboe561fb042019-10-24 07:25:42 -06007533 if (ctx->io_wq) {
7534 io_wq_destroy(ctx->io_wq);
7535 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007536 }
7537}
7538
7539#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007540/*
7541 * Ensure the UNIX gc is aware of our file set, so we are certain that
7542 * the io_uring can be safely unregistered on process exit, even if we have
7543 * loops in the file referencing.
7544 */
7545static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7546{
7547 struct sock *sk = ctx->ring_sock->sk;
7548 struct scm_fp_list *fpl;
7549 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007550 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007551
Jens Axboe6b063142019-01-10 22:13:58 -07007552 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7553 if (!fpl)
7554 return -ENOMEM;
7555
7556 skb = alloc_skb(0, GFP_KERNEL);
7557 if (!skb) {
7558 kfree(fpl);
7559 return -ENOMEM;
7560 }
7561
7562 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007563
Jens Axboe08a45172019-10-03 08:11:03 -06007564 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007565 fpl->user = get_uid(ctx->user);
7566 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007567 struct file *file = io_file_from_index(ctx, i + offset);
7568
7569 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007570 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007571 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007572 unix_inflight(fpl->user, fpl->fp[nr_files]);
7573 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007574 }
7575
Jens Axboe08a45172019-10-03 08:11:03 -06007576 if (nr_files) {
7577 fpl->max = SCM_MAX_FD;
7578 fpl->count = nr_files;
7579 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007580 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007581 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7582 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007583
Jens Axboe08a45172019-10-03 08:11:03 -06007584 for (i = 0; i < nr_files; i++)
7585 fput(fpl->fp[i]);
7586 } else {
7587 kfree_skb(skb);
7588 kfree(fpl);
7589 }
Jens Axboe6b063142019-01-10 22:13:58 -07007590
7591 return 0;
7592}
7593
7594/*
7595 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7596 * causes regular reference counting to break down. We rely on the UNIX
7597 * garbage collection to take care of this problem for us.
7598 */
7599static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7600{
7601 unsigned left, total;
7602 int ret = 0;
7603
7604 total = 0;
7605 left = ctx->nr_user_files;
7606 while (left) {
7607 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007608
7609 ret = __io_sqe_files_scm(ctx, this_files, total);
7610 if (ret)
7611 break;
7612 left -= this_files;
7613 total += this_files;
7614 }
7615
7616 if (!ret)
7617 return 0;
7618
7619 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007620 struct file *file = io_file_from_index(ctx, total);
7621
7622 if (file)
7623 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007624 total++;
7625 }
7626
7627 return ret;
7628}
7629#else
7630static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7631{
7632 return 0;
7633}
7634#endif
7635
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007636static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007637 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007638{
7639 int i;
7640
7641 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007642 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007643 unsigned this_files;
7644
7645 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7646 table->files = kcalloc(this_files, sizeof(struct file *),
7647 GFP_KERNEL);
7648 if (!table->files)
7649 break;
7650 nr_files -= this_files;
7651 }
7652
7653 if (i == nr_tables)
7654 return 0;
7655
7656 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007657 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007658 kfree(table->files);
7659 }
7660 return 1;
7661}
7662
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007663static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007664{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007665 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007666#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007667 struct sock *sock = ctx->ring_sock->sk;
7668 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7669 struct sk_buff *skb;
7670 int i;
7671
7672 __skb_queue_head_init(&list);
7673
7674 /*
7675 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7676 * remove this entry and rearrange the file array.
7677 */
7678 skb = skb_dequeue(head);
7679 while (skb) {
7680 struct scm_fp_list *fp;
7681
7682 fp = UNIXCB(skb).fp;
7683 for (i = 0; i < fp->count; i++) {
7684 int left;
7685
7686 if (fp->fp[i] != file)
7687 continue;
7688
7689 unix_notinflight(fp->user, fp->fp[i]);
7690 left = fp->count - 1 - i;
7691 if (left) {
7692 memmove(&fp->fp[i], &fp->fp[i + 1],
7693 left * sizeof(struct file *));
7694 }
7695 fp->count--;
7696 if (!fp->count) {
7697 kfree_skb(skb);
7698 skb = NULL;
7699 } else {
7700 __skb_queue_tail(&list, skb);
7701 }
7702 fput(file);
7703 file = NULL;
7704 break;
7705 }
7706
7707 if (!file)
7708 break;
7709
7710 __skb_queue_tail(&list, skb);
7711
7712 skb = skb_dequeue(head);
7713 }
7714
7715 if (skb_peek(&list)) {
7716 spin_lock_irq(&head->lock);
7717 while ((skb = __skb_dequeue(&list)) != NULL)
7718 __skb_queue_tail(head, skb);
7719 spin_unlock_irq(&head->lock);
7720 }
7721#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007722 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007723#endif
7724}
7725
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007726static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007727{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007728 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7729 struct io_ring_ctx *ctx = rsrc_data->ctx;
7730 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007731
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007732 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7733 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007734 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007735 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007736 }
7737
Xiaoguang Wang05589552020-03-31 14:05:18 +08007738 percpu_ref_exit(&ref_node->refs);
7739 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007740 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007741}
7742
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007743static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007744{
7745 struct io_ring_ctx *ctx;
7746 struct llist_node *node;
7747
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007748 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7749 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007750
7751 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007752 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007753 struct llist_node *next = node->next;
7754
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007755 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7756 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007757 node = next;
7758 }
7759}
7760
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007761static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7762 unsigned i)
7763{
7764 struct fixed_rsrc_table *table;
7765
7766 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7767 return &table->files[i & IORING_FILE_TABLE_MASK];
7768}
7769
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007770static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007771{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007772 struct fixed_rsrc_ref_node *ref_node;
7773 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007774 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007775 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007776 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007777
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007778 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7779 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007780 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007781
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007782 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007783 ref_node->done = true;
7784
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007785 while (!list_empty(&ctx->rsrc_ref_list)) {
7786 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007787 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007788 /* recycle ref nodes in order */
7789 if (!ref_node->done)
7790 break;
7791 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007792 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007793 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007794 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007795
7796 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007797 delay = 0;
7798
Jens Axboe4a38aed22020-05-14 17:21:15 -06007799 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007800 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007801 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007802 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007803}
7804
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007805static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007806 struct io_ring_ctx *ctx)
7807{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007808 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007809
7810 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7811 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007812 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007813
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007814 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007815 0, GFP_KERNEL)) {
7816 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007817 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007818 }
7819 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007820 INIT_LIST_HEAD(&ref_node->rsrc_list);
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007821 ref_node->done = false;
7822 return ref_node;
7823}
7824
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007825static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7826 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007827{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007828 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007829 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007830}
7831
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007832static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007833{
7834 percpu_ref_exit(&ref_node->refs);
7835 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007836}
7837
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007838
Jens Axboe05f3fb32019-12-09 11:22:50 -07007839static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7840 unsigned nr_args)
7841{
7842 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007843 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007844 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007845 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007846 struct fixed_rsrc_ref_node *ref_node;
7847 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007848
7849 if (ctx->file_data)
7850 return -EBUSY;
7851 if (!nr_args)
7852 return -EINVAL;
7853 if (nr_args > IORING_MAX_FIXED_FILES)
7854 return -EMFILE;
7855
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007856 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007857 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007858 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007859 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007860
7861 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007862 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007863 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007864 if (!file_data->table)
7865 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007866
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007867 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007868 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007869
7870 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007871 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7872 ret = -EFAULT;
7873 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007874 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007875 /* allow sparse sets */
7876 if (fd == -1)
7877 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007878
Jens Axboe05f3fb32019-12-09 11:22:50 -07007879 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007880 ret = -EBADF;
7881 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007882 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007883
7884 /*
7885 * Don't allow io_uring instances to be registered. If UNIX
7886 * isn't enabled, then this causes a reference cycle and this
7887 * instance can never get freed. If UNIX is enabled we'll
7888 * handle it just fine, but there's still no point in allowing
7889 * a ring fd as it doesn't support regular read/write anyway.
7890 */
7891 if (file->f_op == &io_uring_fops) {
7892 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007893 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007894 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007895 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007896 }
7897
Jens Axboe05f3fb32019-12-09 11:22:50 -07007898 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007899 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007900 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007901 return ret;
7902 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007903
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007904 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007905 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007906 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007907 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007908 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007909 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007910
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007911 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007912 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007913out_fput:
7914 for (i = 0; i < ctx->nr_user_files; i++) {
7915 file = io_file_from_index(ctx, i);
7916 if (file)
7917 fput(file);
7918 }
7919 for (i = 0; i < nr_tables; i++)
7920 kfree(file_data->table[i].files);
7921 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007922out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007923 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007924 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007925 return ret;
7926}
7927
Jens Axboec3a31e62019-10-03 13:59:56 -06007928static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7929 int index)
7930{
7931#if defined(CONFIG_UNIX)
7932 struct sock *sock = ctx->ring_sock->sk;
7933 struct sk_buff_head *head = &sock->sk_receive_queue;
7934 struct sk_buff *skb;
7935
7936 /*
7937 * See if we can merge this file into an existing skb SCM_RIGHTS
7938 * file set. If there's no room, fall back to allocating a new skb
7939 * and filling it in.
7940 */
7941 spin_lock_irq(&head->lock);
7942 skb = skb_peek(head);
7943 if (skb) {
7944 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7945
7946 if (fpl->count < SCM_MAX_FD) {
7947 __skb_unlink(skb, head);
7948 spin_unlock_irq(&head->lock);
7949 fpl->fp[fpl->count] = get_file(file);
7950 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7951 fpl->count++;
7952 spin_lock_irq(&head->lock);
7953 __skb_queue_head(head, skb);
7954 } else {
7955 skb = NULL;
7956 }
7957 }
7958 spin_unlock_irq(&head->lock);
7959
7960 if (skb) {
7961 fput(file);
7962 return 0;
7963 }
7964
7965 return __io_sqe_files_scm(ctx, 1, index);
7966#else
7967 return 0;
7968#endif
7969}
7970
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007971static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007972{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007973 struct io_rsrc_put *prsrc;
7974 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007975
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007976 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7977 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007978 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007979
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007980 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007981 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007982
Hillf Dantona5318d32020-03-23 17:47:15 +08007983 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007984}
7985
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007986static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7987 struct file *file)
7988{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007989 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007990}
7991
Jens Axboe05f3fb32019-12-09 11:22:50 -07007992static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007993 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007994 unsigned nr_args)
7995{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007996 struct fixed_rsrc_data *data = ctx->file_data;
7997 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007998 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007999 __s32 __user *fds;
8000 int fd, i, err;
8001 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008002 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008003
Jens Axboe05f3fb32019-12-09 11:22:50 -07008004 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06008005 return -EOVERFLOW;
8006 if (done > ctx->nr_user_files)
8007 return -EINVAL;
8008
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008009 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00008010 if (!ref_node)
8011 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008012 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008013
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008014 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00008015 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008016 err = 0;
8017 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
8018 err = -EFAULT;
8019 break;
8020 }
noah4e0377a2021-01-26 15:23:28 -05008021 if (fd == IORING_REGISTER_FILES_SKIP)
8022 continue;
8023
Pavel Begunkov67973b92021-01-26 13:51:09 +00008024 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008025 file_slot = io_fixed_file_slot(ctx->file_data, i);
8026
8027 if (*file_slot) {
8028 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008029 if (err)
8030 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008031 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008032 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008033 }
8034 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008035 file = fget(fd);
8036 if (!file) {
8037 err = -EBADF;
8038 break;
8039 }
8040 /*
8041 * Don't allow io_uring instances to be registered. If
8042 * UNIX isn't enabled, then this causes a reference
8043 * cycle and this instance can never get freed. If UNIX
8044 * is enabled we'll handle it just fine, but there's
8045 * still no point in allowing a ring fd as it doesn't
8046 * support regular read/write anyway.
8047 */
8048 if (file->f_op == &io_uring_fops) {
8049 fput(file);
8050 err = -EBADF;
8051 break;
8052 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07008053 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008054 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008055 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07008056 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008057 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008058 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008059 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008060 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008061 }
8062
Xiaoguang Wang05589552020-03-31 14:05:18 +08008063 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008064 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008065 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008066 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008067 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008068
8069 return done ? done : err;
8070}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008071
Jens Axboe05f3fb32019-12-09 11:22:50 -07008072static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8073 unsigned nr_args)
8074{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008075 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008076
8077 if (!ctx->file_data)
8078 return -ENXIO;
8079 if (!nr_args)
8080 return -EINVAL;
8081 if (copy_from_user(&up, arg, sizeof(up)))
8082 return -EFAULT;
8083 if (up.resv)
8084 return -EINVAL;
8085
8086 return __io_sqe_files_update(ctx, &up, nr_args);
8087}
Jens Axboec3a31e62019-10-03 13:59:56 -06008088
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008089static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008090{
8091 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8092
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008093 req = io_put_req_find_next(req);
8094 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008095}
8096
Pavel Begunkov24369c22020-01-28 03:15:48 +03008097static int io_init_wq_offload(struct io_ring_ctx *ctx,
8098 struct io_uring_params *p)
8099{
8100 struct io_wq_data data;
8101 struct fd f;
8102 struct io_ring_ctx *ctx_attach;
8103 unsigned int concurrency;
8104 int ret = 0;
8105
8106 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008107 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008108 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008109
8110 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
8111 /* Do QD, or 4 * CPUS, whatever is smallest */
8112 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
8113
8114 ctx->io_wq = io_wq_create(concurrency, &data);
8115 if (IS_ERR(ctx->io_wq)) {
8116 ret = PTR_ERR(ctx->io_wq);
8117 ctx->io_wq = NULL;
8118 }
8119 return ret;
8120 }
8121
8122 f = fdget(p->wq_fd);
8123 if (!f.file)
8124 return -EBADF;
8125
8126 if (f.file->f_op != &io_uring_fops) {
8127 ret = -EINVAL;
8128 goto out_fput;
8129 }
8130
8131 ctx_attach = f.file->private_data;
8132 /* @io_wq is protected by holding the fd */
8133 if (!io_wq_get(ctx_attach->io_wq, &data)) {
8134 ret = -EINVAL;
8135 goto out_fput;
8136 }
8137
8138 ctx->io_wq = ctx_attach->io_wq;
8139out_fput:
8140 fdput(f);
8141 return ret;
8142}
8143
Jens Axboe0f212202020-09-13 13:09:39 -06008144static int io_uring_alloc_task_context(struct task_struct *task)
8145{
8146 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008147 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008148
8149 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8150 if (unlikely(!tctx))
8151 return -ENOMEM;
8152
Jens Axboed8a6df12020-10-15 16:24:45 -06008153 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8154 if (unlikely(ret)) {
8155 kfree(tctx);
8156 return ret;
8157 }
8158
Jens Axboe0f212202020-09-13 13:09:39 -06008159 xa_init(&tctx->xa);
8160 init_waitqueue_head(&tctx->wait);
8161 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008162 atomic_set(&tctx->in_idle, 0);
8163 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008164 io_init_identity(&tctx->__identity);
8165 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008166 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008167 spin_lock_init(&tctx->task_lock);
8168 INIT_WQ_LIST(&tctx->task_list);
8169 tctx->task_state = 0;
8170 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008171 return 0;
8172}
8173
8174void __io_uring_free(struct task_struct *tsk)
8175{
8176 struct io_uring_task *tctx = tsk->io_uring;
8177
8178 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008179 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8180 if (tctx->identity != &tctx->__identity)
8181 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008182 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008183 kfree(tctx);
8184 tsk->io_uring = NULL;
8185}
8186
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008187static int io_sq_offload_create(struct io_ring_ctx *ctx,
8188 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008189{
8190 int ret;
8191
Jens Axboe6c271ce2019-01-10 11:22:30 -07008192 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008193 struct io_sq_data *sqd;
8194
Jens Axboe3ec482d2019-04-08 10:51:01 -06008195 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008196 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008197 goto err;
8198
Jens Axboe534ca6d2020-09-02 13:52:19 -06008199 sqd = io_get_sq_data(p);
8200 if (IS_ERR(sqd)) {
8201 ret = PTR_ERR(sqd);
8202 goto err;
8203 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008204
Jens Axboe534ca6d2020-09-02 13:52:19 -06008205 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008206 io_sq_thread_park(sqd);
8207 mutex_lock(&sqd->ctx_lock);
8208 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8209 mutex_unlock(&sqd->ctx_lock);
8210 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008211
Jens Axboe917257d2019-04-13 09:28:55 -06008212 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8213 if (!ctx->sq_thread_idle)
8214 ctx->sq_thread_idle = HZ;
8215
Jens Axboeaa061652020-09-02 14:50:27 -06008216 if (sqd->thread)
8217 goto done;
8218
Jens Axboe6c271ce2019-01-10 11:22:30 -07008219 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008220 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008221
Jens Axboe917257d2019-04-13 09:28:55 -06008222 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008223 if (cpu >= nr_cpu_ids)
8224 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008225 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008226 goto err;
8227
Jens Axboe69fb2132020-09-14 11:16:23 -06008228 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008229 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008230 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008231 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008232 "io_uring-sq");
8233 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008234 if (IS_ERR(sqd->thread)) {
8235 ret = PTR_ERR(sqd->thread);
8236 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008237 goto err;
8238 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008239 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008240 if (ret)
8241 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008242 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8243 /* Can't have SQ_AFF without SQPOLL */
8244 ret = -EINVAL;
8245 goto err;
8246 }
8247
Jens Axboeaa061652020-09-02 14:50:27 -06008248done:
Pavel Begunkov24369c22020-01-28 03:15:48 +03008249 ret = io_init_wq_offload(ctx, p);
8250 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008251 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008252
8253 return 0;
8254err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008255 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008256 return ret;
8257}
8258
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008259static void io_sq_offload_start(struct io_ring_ctx *ctx)
8260{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008261 struct io_sq_data *sqd = ctx->sq_data;
8262
8263 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8264 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008265}
8266
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008267static inline void __io_unaccount_mem(struct user_struct *user,
8268 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008269{
8270 atomic_long_sub(nr_pages, &user->locked_vm);
8271}
8272
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008273static inline int __io_account_mem(struct user_struct *user,
8274 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008275{
8276 unsigned long page_limit, cur_pages, new_pages;
8277
8278 /* Don't allow more pages than we can safely lock */
8279 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8280
8281 do {
8282 cur_pages = atomic_long_read(&user->locked_vm);
8283 new_pages = cur_pages + nr_pages;
8284 if (new_pages > page_limit)
8285 return -ENOMEM;
8286 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8287 new_pages) != cur_pages);
8288
8289 return 0;
8290}
8291
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008292static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008293{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008294 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008295 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008296
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008297 if (ctx->mm_account)
8298 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008299}
8300
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008301static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008302{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008303 int ret;
8304
8305 if (ctx->limit_mem) {
8306 ret = __io_account_mem(ctx->user, nr_pages);
8307 if (ret)
8308 return ret;
8309 }
8310
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008311 if (ctx->mm_account)
8312 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008313
8314 return 0;
8315}
8316
Jens Axboe2b188cc2019-01-07 10:46:33 -07008317static void io_mem_free(void *ptr)
8318{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008319 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008320
Mark Rutland52e04ef2019-04-30 17:30:21 +01008321 if (!ptr)
8322 return;
8323
8324 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008325 if (put_page_testzero(page))
8326 free_compound_page(page);
8327}
8328
8329static void *io_mem_alloc(size_t size)
8330{
8331 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008332 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008333
8334 return (void *) __get_free_pages(gfp_flags, get_order(size));
8335}
8336
Hristo Venev75b28af2019-08-26 17:23:46 +00008337static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8338 size_t *sq_offset)
8339{
8340 struct io_rings *rings;
8341 size_t off, sq_array_size;
8342
8343 off = struct_size(rings, cqes, cq_entries);
8344 if (off == SIZE_MAX)
8345 return SIZE_MAX;
8346
8347#ifdef CONFIG_SMP
8348 off = ALIGN(off, SMP_CACHE_BYTES);
8349 if (off == 0)
8350 return SIZE_MAX;
8351#endif
8352
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008353 if (sq_offset)
8354 *sq_offset = off;
8355
Hristo Venev75b28af2019-08-26 17:23:46 +00008356 sq_array_size = array_size(sizeof(u32), sq_entries);
8357 if (sq_array_size == SIZE_MAX)
8358 return SIZE_MAX;
8359
8360 if (check_add_overflow(off, sq_array_size, &off))
8361 return SIZE_MAX;
8362
Hristo Venev75b28af2019-08-26 17:23:46 +00008363 return off;
8364}
8365
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008366static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008367{
8368 int i, j;
8369
8370 if (!ctx->user_bufs)
8371 return -ENXIO;
8372
8373 for (i = 0; i < ctx->nr_user_bufs; i++) {
8374 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8375
8376 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008377 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008378
Jens Axboede293932020-09-17 16:19:16 -06008379 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008380 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008381 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008382 imu->nr_bvecs = 0;
8383 }
8384
8385 kfree(ctx->user_bufs);
8386 ctx->user_bufs = NULL;
8387 ctx->nr_user_bufs = 0;
8388 return 0;
8389}
8390
8391static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8392 void __user *arg, unsigned index)
8393{
8394 struct iovec __user *src;
8395
8396#ifdef CONFIG_COMPAT
8397 if (ctx->compat) {
8398 struct compat_iovec __user *ciovs;
8399 struct compat_iovec ciov;
8400
8401 ciovs = (struct compat_iovec __user *) arg;
8402 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8403 return -EFAULT;
8404
Jens Axboed55e5f52019-12-11 16:12:15 -07008405 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008406 dst->iov_len = ciov.iov_len;
8407 return 0;
8408 }
8409#endif
8410 src = (struct iovec __user *) arg;
8411 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8412 return -EFAULT;
8413 return 0;
8414}
8415
Jens Axboede293932020-09-17 16:19:16 -06008416/*
8417 * Not super efficient, but this is just a registration time. And we do cache
8418 * the last compound head, so generally we'll only do a full search if we don't
8419 * match that one.
8420 *
8421 * We check if the given compound head page has already been accounted, to
8422 * avoid double accounting it. This allows us to account the full size of the
8423 * page, not just the constituent pages of a huge page.
8424 */
8425static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8426 int nr_pages, struct page *hpage)
8427{
8428 int i, j;
8429
8430 /* check current page array */
8431 for (i = 0; i < nr_pages; i++) {
8432 if (!PageCompound(pages[i]))
8433 continue;
8434 if (compound_head(pages[i]) == hpage)
8435 return true;
8436 }
8437
8438 /* check previously registered pages */
8439 for (i = 0; i < ctx->nr_user_bufs; i++) {
8440 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8441
8442 for (j = 0; j < imu->nr_bvecs; j++) {
8443 if (!PageCompound(imu->bvec[j].bv_page))
8444 continue;
8445 if (compound_head(imu->bvec[j].bv_page) == hpage)
8446 return true;
8447 }
8448 }
8449
8450 return false;
8451}
8452
8453static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8454 int nr_pages, struct io_mapped_ubuf *imu,
8455 struct page **last_hpage)
8456{
8457 int i, ret;
8458
8459 for (i = 0; i < nr_pages; i++) {
8460 if (!PageCompound(pages[i])) {
8461 imu->acct_pages++;
8462 } else {
8463 struct page *hpage;
8464
8465 hpage = compound_head(pages[i]);
8466 if (hpage == *last_hpage)
8467 continue;
8468 *last_hpage = hpage;
8469 if (headpage_already_acct(ctx, pages, i, hpage))
8470 continue;
8471 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8472 }
8473 }
8474
8475 if (!imu->acct_pages)
8476 return 0;
8477
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008478 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008479 if (ret)
8480 imu->acct_pages = 0;
8481 return ret;
8482}
8483
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008484static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8485 struct io_mapped_ubuf *imu,
8486 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008487{
8488 struct vm_area_struct **vmas = NULL;
8489 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008490 unsigned long off, start, end, ubuf;
8491 size_t size;
8492 int ret, pret, nr_pages, i;
8493
8494 ubuf = (unsigned long) iov->iov_base;
8495 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8496 start = ubuf >> PAGE_SHIFT;
8497 nr_pages = end - start;
8498
8499 ret = -ENOMEM;
8500
8501 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8502 if (!pages)
8503 goto done;
8504
8505 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8506 GFP_KERNEL);
8507 if (!vmas)
8508 goto done;
8509
8510 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8511 GFP_KERNEL);
8512 if (!imu->bvec)
8513 goto done;
8514
8515 ret = 0;
8516 mmap_read_lock(current->mm);
8517 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8518 pages, vmas);
8519 if (pret == nr_pages) {
8520 /* don't support file backed memory */
8521 for (i = 0; i < nr_pages; i++) {
8522 struct vm_area_struct *vma = vmas[i];
8523
8524 if (vma->vm_file &&
8525 !is_file_hugepages(vma->vm_file)) {
8526 ret = -EOPNOTSUPP;
8527 break;
8528 }
8529 }
8530 } else {
8531 ret = pret < 0 ? pret : -EFAULT;
8532 }
8533 mmap_read_unlock(current->mm);
8534 if (ret) {
8535 /*
8536 * if we did partial map, or found file backed vmas,
8537 * release any pages we did get
8538 */
8539 if (pret > 0)
8540 unpin_user_pages(pages, pret);
8541 kvfree(imu->bvec);
8542 goto done;
8543 }
8544
8545 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8546 if (ret) {
8547 unpin_user_pages(pages, pret);
8548 kvfree(imu->bvec);
8549 goto done;
8550 }
8551
8552 off = ubuf & ~PAGE_MASK;
8553 size = iov->iov_len;
8554 for (i = 0; i < nr_pages; i++) {
8555 size_t vec_len;
8556
8557 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8558 imu->bvec[i].bv_page = pages[i];
8559 imu->bvec[i].bv_len = vec_len;
8560 imu->bvec[i].bv_offset = off;
8561 off = 0;
8562 size -= vec_len;
8563 }
8564 /* store original address for later verification */
8565 imu->ubuf = ubuf;
8566 imu->len = iov->iov_len;
8567 imu->nr_bvecs = nr_pages;
8568 ret = 0;
8569done:
8570 kvfree(pages);
8571 kvfree(vmas);
8572 return ret;
8573}
8574
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008575static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008576{
Jens Axboeedafcce2019-01-09 09:16:05 -07008577 if (ctx->user_bufs)
8578 return -EBUSY;
8579 if (!nr_args || nr_args > UIO_MAXIOV)
8580 return -EINVAL;
8581
8582 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8583 GFP_KERNEL);
8584 if (!ctx->user_bufs)
8585 return -ENOMEM;
8586
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008587 return 0;
8588}
8589
8590static int io_buffer_validate(struct iovec *iov)
8591{
8592 /*
8593 * Don't impose further limits on the size and buffer
8594 * constraints here, we'll -EINVAL later when IO is
8595 * submitted if they are wrong.
8596 */
8597 if (!iov->iov_base || !iov->iov_len)
8598 return -EFAULT;
8599
8600 /* arbitrary limit, but we need something */
8601 if (iov->iov_len > SZ_1G)
8602 return -EFAULT;
8603
8604 return 0;
8605}
8606
8607static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8608 unsigned int nr_args)
8609{
8610 int i, ret;
8611 struct iovec iov;
8612 struct page *last_hpage = NULL;
8613
8614 ret = io_buffers_map_alloc(ctx, nr_args);
8615 if (ret)
8616 return ret;
8617
Jens Axboeedafcce2019-01-09 09:16:05 -07008618 for (i = 0; i < nr_args; i++) {
8619 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008620
8621 ret = io_copy_iov(ctx, &iov, arg, i);
8622 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008623 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008624
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008625 ret = io_buffer_validate(&iov);
8626 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008627 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008628
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008629 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8630 if (ret)
8631 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008632
8633 ctx->nr_user_bufs++;
8634 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008635
8636 if (ret)
8637 io_sqe_buffers_unregister(ctx);
8638
Jens Axboeedafcce2019-01-09 09:16:05 -07008639 return ret;
8640}
8641
Jens Axboe9b402842019-04-11 11:45:41 -06008642static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8643{
8644 __s32 __user *fds = arg;
8645 int fd;
8646
8647 if (ctx->cq_ev_fd)
8648 return -EBUSY;
8649
8650 if (copy_from_user(&fd, fds, sizeof(*fds)))
8651 return -EFAULT;
8652
8653 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8654 if (IS_ERR(ctx->cq_ev_fd)) {
8655 int ret = PTR_ERR(ctx->cq_ev_fd);
8656 ctx->cq_ev_fd = NULL;
8657 return ret;
8658 }
8659
8660 return 0;
8661}
8662
8663static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8664{
8665 if (ctx->cq_ev_fd) {
8666 eventfd_ctx_put(ctx->cq_ev_fd);
8667 ctx->cq_ev_fd = NULL;
8668 return 0;
8669 }
8670
8671 return -ENXIO;
8672}
8673
Jens Axboe5a2e7452020-02-23 16:23:11 -07008674static int __io_destroy_buffers(int id, void *p, void *data)
8675{
8676 struct io_ring_ctx *ctx = data;
8677 struct io_buffer *buf = p;
8678
Jens Axboe067524e2020-03-02 16:32:28 -07008679 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008680 return 0;
8681}
8682
8683static void io_destroy_buffers(struct io_ring_ctx *ctx)
8684{
8685 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8686 idr_destroy(&ctx->io_buffer_idr);
8687}
8688
Jens Axboe68e68ee2021-02-13 09:00:02 -07008689static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008690{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008691 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008692
Jens Axboe68e68ee2021-02-13 09:00:02 -07008693 list_for_each_entry_safe(req, nxt, list, compl.list) {
8694 if (tsk && req->task != tsk)
8695 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008696 list_del(&req->compl.list);
8697 kmem_cache_free(req_cachep, req);
8698 }
8699}
8700
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008701static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008702{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008703 struct io_submit_state *submit_state = &ctx->submit_state;
8704
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008705 mutex_lock(&ctx->uring_lock);
8706
8707 if (submit_state->free_reqs)
8708 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8709 submit_state->reqs);
8710
8711 io_req_cache_free(&submit_state->comp.free_list, NULL);
8712
8713 spin_lock_irq(&ctx->completion_lock);
8714 io_req_cache_free(&submit_state->comp.locked_free_list, NULL);
8715 spin_unlock_irq(&ctx->completion_lock);
8716
8717 mutex_unlock(&ctx->uring_lock);
8718}
8719
8720static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8721{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008722 /*
8723 * Some may use context even when all refs and requests have been put,
8724 * and they are free to do so while still holding uring_lock, see
8725 * __io_req_task_submit(). Wait for them to finish.
8726 */
8727 mutex_lock(&ctx->uring_lock);
8728 mutex_unlock(&ctx->uring_lock);
8729
Jens Axboe6b063142019-01-10 22:13:58 -07008730 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008731 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008732
8733 if (ctx->sqo_task) {
8734 put_task_struct(ctx->sqo_task);
8735 ctx->sqo_task = NULL;
8736 mmdrop(ctx->mm_account);
8737 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008738 }
Jens Axboedef596e2019-01-09 08:59:42 -07008739
Dennis Zhou91d8f512020-09-16 13:41:05 -07008740#ifdef CONFIG_BLK_CGROUP
8741 if (ctx->sqo_blkcg_css)
8742 css_put(ctx->sqo_blkcg_css);
8743#endif
8744
Hao Xu8bad28d2021-02-19 17:19:36 +08008745 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008746 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008747 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008748 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008749 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008750 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008751
Jens Axboe2b188cc2019-01-07 10:46:33 -07008752#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008753 if (ctx->ring_sock) {
8754 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008755 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008756 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008757#endif
8758
Hristo Venev75b28af2019-08-26 17:23:46 +00008759 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008760 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008761
8762 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008763 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008764 put_cred(ctx->creds);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008765 io_req_caches_free(ctx, NULL);
Jens Axboe78076bb2019-12-04 19:56:40 -07008766 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008767 kfree(ctx);
8768}
8769
8770static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8771{
8772 struct io_ring_ctx *ctx = file->private_data;
8773 __poll_t mask = 0;
8774
8775 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008776 /*
8777 * synchronizes with barrier from wq_has_sleeper call in
8778 * io_commit_cqring
8779 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008780 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008781 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008783
8784 /*
8785 * Don't flush cqring overflow list here, just do a simple check.
8786 * Otherwise there could possible be ABBA deadlock:
8787 * CPU0 CPU1
8788 * ---- ----
8789 * lock(&ctx->uring_lock);
8790 * lock(&ep->mtx);
8791 * lock(&ctx->uring_lock);
8792 * lock(&ep->mtx);
8793 *
8794 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8795 * pushs them to do the flush.
8796 */
8797 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008798 mask |= EPOLLIN | EPOLLRDNORM;
8799
8800 return mask;
8801}
8802
8803static int io_uring_fasync(int fd, struct file *file, int on)
8804{
8805 struct io_ring_ctx *ctx = file->private_data;
8806
8807 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8808}
8809
Yejune Deng0bead8c2020-12-24 11:02:20 +08008810static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008811{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008812 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008813
Jens Axboe1e6fa522020-10-15 08:46:24 -06008814 iod = idr_remove(&ctx->personality_idr, id);
8815 if (iod) {
8816 put_cred(iod->creds);
8817 if (refcount_dec_and_test(&iod->count))
8818 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008819 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008820 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008821
8822 return -EINVAL;
8823}
8824
8825static int io_remove_personalities(int id, void *p, void *data)
8826{
8827 struct io_ring_ctx *ctx = data;
8828
8829 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008830 return 0;
8831}
8832
Jens Axboe85faa7b2020-04-09 18:14:00 -06008833static void io_ring_exit_work(struct work_struct *work)
8834{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008835 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8836 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008837
Jens Axboe56952e92020-06-17 15:00:04 -06008838 /*
8839 * If we're doing polled IO and end up having requests being
8840 * submitted async (out-of-line), then completions can come in while
8841 * we're waiting for refs to drop. We need to reap these manually,
8842 * as nobody else will be looking for them.
8843 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008844 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008845 io_uring_try_cancel_requests(ctx, NULL, NULL);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008846 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008847 io_ring_ctx_free(ctx);
8848}
8849
Jens Axboe00c18642020-12-20 10:45:02 -07008850static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8851{
8852 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8853
8854 return req->ctx == data;
8855}
8856
Jens Axboe2b188cc2019-01-07 10:46:33 -07008857static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8858{
8859 mutex_lock(&ctx->uring_lock);
8860 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008861
8862 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8863 ctx->sqo_dead = 1;
8864
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008865 /* if force is set, the ring is going away. always drop after that */
8866 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008867 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008868 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008869 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008870 mutex_unlock(&ctx->uring_lock);
8871
Pavel Begunkov6b819282020-11-06 13:00:25 +00008872 io_kill_timeouts(ctx, NULL, NULL);
8873 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008874
8875 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008876 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008877
Jens Axboe15dff282019-11-13 09:09:23 -07008878 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008879 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008880
Jens Axboe85faa7b2020-04-09 18:14:00 -06008881 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008882 /*
8883 * Use system_unbound_wq to avoid spawning tons of event kworkers
8884 * if we're exiting a ton of rings at the same time. It just adds
8885 * noise and overhead, there's no discernable change in runtime
8886 * over using system_wq.
8887 */
8888 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008889}
8890
8891static int io_uring_release(struct inode *inode, struct file *file)
8892{
8893 struct io_ring_ctx *ctx = file->private_data;
8894
8895 file->private_data = NULL;
8896 io_ring_ctx_wait_and_kill(ctx);
8897 return 0;
8898}
8899
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008900struct io_task_cancel {
8901 struct task_struct *task;
8902 struct files_struct *files;
8903};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008904
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008905static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008906{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008907 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008908 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008909 bool ret;
8910
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008911 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008912 unsigned long flags;
8913 struct io_ring_ctx *ctx = req->ctx;
8914
8915 /* protect against races with linked timeouts */
8916 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008917 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008918 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8919 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008920 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008921 }
8922 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008923}
8924
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008925static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008926 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008927 struct files_struct *files)
8928{
8929 struct io_defer_entry *de = NULL;
8930 LIST_HEAD(list);
8931
8932 spin_lock_irq(&ctx->completion_lock);
8933 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008934 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008935 list_cut_position(&list, &ctx->defer_list, &de->list);
8936 break;
8937 }
8938 }
8939 spin_unlock_irq(&ctx->completion_lock);
8940
8941 while (!list_empty(&list)) {
8942 de = list_first_entry(&list, struct io_defer_entry, list);
8943 list_del_init(&de->list);
8944 req_set_fail_links(de->req);
8945 io_put_req(de->req);
8946 io_req_complete(de->req, -ECANCELED);
8947 kfree(de);
8948 }
8949}
8950
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008951static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8952 struct task_struct *task,
8953 struct files_struct *files)
8954{
8955 struct io_task_cancel cancel = { .task = task, .files = files, };
8956
8957 while (1) {
8958 enum io_wq_cancel cret;
8959 bool ret = false;
8960
8961 if (ctx->io_wq) {
8962 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8963 &cancel, true);
8964 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8965 }
8966
8967 /* SQPOLL thread does its own polling */
8968 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8969 while (!list_empty_careful(&ctx->iopoll_list)) {
8970 io_iopoll_try_reap_events(ctx);
8971 ret = true;
8972 }
8973 }
8974
8975 ret |= io_poll_remove_all(ctx, task, files);
8976 ret |= io_kill_timeouts(ctx, task, files);
8977 ret |= io_run_task_work();
8978 io_cqring_overflow_flush(ctx, true, task, files);
8979 if (!ret)
8980 break;
8981 cond_resched();
8982 }
8983}
8984
Pavel Begunkovca70f002021-01-26 15:28:27 +00008985static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8986 struct task_struct *task,
8987 struct files_struct *files)
8988{
8989 struct io_kiocb *req;
8990 int cnt = 0;
8991
8992 spin_lock_irq(&ctx->inflight_lock);
8993 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8994 cnt += io_match_task(req, task, files);
8995 spin_unlock_irq(&ctx->inflight_lock);
8996 return cnt;
8997}
8998
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008999static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009000 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06009001 struct files_struct *files)
9002{
Jens Axboefcb323c2019-10-24 12:39:47 -06009003 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08009004 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009005 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06009006
Pavel Begunkovca70f002021-01-26 15:28:27 +00009007 inflight = io_uring_count_inflight(ctx, task, files);
9008 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06009009 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009010
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009011 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkov34343782021-02-10 11:45:42 +00009012
9013 if (ctx->sq_data)
9014 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009015 prepare_to_wait(&task->io_uring->wait, &wait,
9016 TASK_UNINTERRUPTIBLE);
9017 if (inflight == io_uring_count_inflight(ctx, task, files))
9018 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00009019 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00009020 if (ctx->sq_data)
9021 io_sq_thread_park(ctx->sq_data);
Jens Axboefcb323c2019-10-24 12:39:47 -06009022 }
9023}
9024
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009025static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
9026{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009027 mutex_lock(&ctx->uring_lock);
9028 ctx->sqo_dead = 1;
9029 mutex_unlock(&ctx->uring_lock);
9030
9031 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00009032 if (ctx->rings)
9033 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009034}
9035
Jens Axboe0f212202020-09-13 13:09:39 -06009036/*
9037 * We need to iteratively cancel requests, in case a request has dependent
9038 * hard links. These persist even for failure of cancelations, hence keep
9039 * looping until none are found.
9040 */
9041static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9042 struct files_struct *files)
9043{
9044 struct task_struct *task = current;
9045
Jens Axboefdaf0832020-10-30 09:37:30 -06009046 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009047 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06009048 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06009049 atomic_inc(&task->io_uring->in_idle);
9050 io_sq_thread_park(ctx->sq_data);
9051 }
Jens Axboe0f212202020-09-13 13:09:39 -06009052
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009053 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06009054
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009055 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009056 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009057 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009058
9059 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9060 atomic_dec(&task->io_uring->in_idle);
9061 /*
9062 * If the files that are going away are the ones in the thread
9063 * identity, clear them out.
9064 */
9065 if (task->io_uring->identity->files == files)
9066 task->io_uring->identity->files = NULL;
9067 io_sq_thread_unpark(ctx->sq_data);
9068 }
Jens Axboe0f212202020-09-13 13:09:39 -06009069}
9070
9071/*
9072 * Note that this task has used io_uring. We use it for cancelation purposes.
9073 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009074static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009075{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009076 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009077 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009078
9079 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009080 ret = io_uring_alloc_task_context(current);
9081 if (unlikely(ret))
9082 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009083 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009084 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009085 if (tctx->last != file) {
9086 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009087
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009088 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009089 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009090 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9091 file, GFP_KERNEL));
9092 if (ret) {
9093 fput(file);
9094 return ret;
9095 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009096
9097 /* one and only SQPOLL file note, held by sqo_task */
9098 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9099 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009100 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009101 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009102 }
9103
Jens Axboefdaf0832020-10-30 09:37:30 -06009104 /*
9105 * This is race safe in that the task itself is doing this, hence it
9106 * cannot be going through the exit/cancel paths at the same time.
9107 * This cannot be modified while exit/cancel is running.
9108 */
9109 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9110 tctx->sqpoll = true;
9111
Jens Axboe0f212202020-09-13 13:09:39 -06009112 return 0;
9113}
9114
9115/*
9116 * Remove this io_uring_file -> task mapping.
9117 */
9118static void io_uring_del_task_file(struct file *file)
9119{
9120 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009121
9122 if (tctx->last == file)
9123 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009124 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009125 if (file)
9126 fput(file);
9127}
9128
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009129static void io_uring_remove_task_files(struct io_uring_task *tctx)
9130{
9131 struct file *file;
9132 unsigned long index;
9133
9134 xa_for_each(&tctx->xa, index, file)
9135 io_uring_del_task_file(file);
9136}
9137
Jens Axboe0f212202020-09-13 13:09:39 -06009138void __io_uring_files_cancel(struct files_struct *files)
9139{
9140 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009141 struct file *file;
9142 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009143
9144 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009145 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009146 xa_for_each(&tctx->xa, index, file)
9147 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009148 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009149
9150 if (files)
9151 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009152}
9153
9154static s64 tctx_inflight(struct io_uring_task *tctx)
9155{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009156 return percpu_counter_sum(&tctx->inflight);
9157}
9158
9159static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9160{
9161 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009162 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009163 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009164
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009165 if (!ctx->sq_data)
9166 return;
9167 tctx = ctx->sq_data->thread->io_uring;
9168 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009169
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009170 atomic_inc(&tctx->in_idle);
9171 do {
9172 /* read completions before cancelations */
9173 inflight = tctx_inflight(tctx);
9174 if (!inflight)
9175 break;
9176 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009177
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009178 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9179 /*
9180 * If we've seen completions, retry without waiting. This
9181 * avoids a race where a completion comes in before we did
9182 * prepare_to_wait().
9183 */
9184 if (inflight == tctx_inflight(tctx))
9185 schedule();
9186 finish_wait(&tctx->wait, &wait);
9187 } while (1);
9188 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009189}
9190
Jens Axboe0f212202020-09-13 13:09:39 -06009191/*
9192 * Find any io_uring fd that this task has registered or done IO on, and cancel
9193 * requests.
9194 */
9195void __io_uring_task_cancel(void)
9196{
9197 struct io_uring_task *tctx = current->io_uring;
9198 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009199 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009200
9201 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009202 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009203
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009204 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009205 if (tctx->sqpoll) {
9206 struct file *file;
9207 unsigned long index;
9208
9209 xa_for_each(&tctx->xa, index, file)
9210 io_uring_cancel_sqpoll(file->private_data);
9211 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009212
Jens Axboed8a6df12020-10-15 16:24:45 -06009213 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009214 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009215 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009216 if (!inflight)
9217 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009218 __io_uring_files_cancel(NULL);
9219
9220 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9221
9222 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009223 * If we've seen completions, retry without waiting. This
9224 * avoids a race where a completion comes in before we did
9225 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009226 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009227 if (inflight == tctx_inflight(tctx))
9228 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009229 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009230 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009231
Jens Axboefdaf0832020-10-30 09:37:30 -06009232 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009233
9234 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009235}
9236
Jens Axboefcb323c2019-10-24 12:39:47 -06009237static int io_uring_flush(struct file *file, void *data)
9238{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009239 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009240 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009241
Jens Axboe41be53e2021-02-13 09:11:04 -07009242 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07009243 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe41be53e2021-02-13 09:11:04 -07009244 io_req_caches_free(ctx, current);
9245 }
Jens Axboe84965ff2021-01-23 15:51:11 -07009246
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009247 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009248 return 0;
9249
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009250 /* we should have cancelled and erased it before PF_EXITING */
9251 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9252 xa_load(&tctx->xa, (unsigned long)file));
9253
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009254 /*
9255 * fput() is pending, will be 2 if the only other ref is our potential
9256 * task file note. If the task is exiting, drop regardless of count.
9257 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009258 if (atomic_long_read(&file->f_count) != 2)
9259 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009260
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009261 if (ctx->flags & IORING_SETUP_SQPOLL) {
9262 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009263 WARN_ON_ONCE(ctx->sqo_task != current &&
9264 xa_load(&tctx->xa, (unsigned long)file));
9265 /* sqo_dead check is for when this happens after cancellation */
9266 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009267 !xa_load(&tctx->xa, (unsigned long)file));
9268
9269 io_disable_sqo_submit(ctx);
9270 }
9271
9272 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9273 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009274 return 0;
9275}
9276
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009277static void *io_uring_validate_mmap_request(struct file *file,
9278 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009279{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009280 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009281 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009282 struct page *page;
9283 void *ptr;
9284
9285 switch (offset) {
9286 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009287 case IORING_OFF_CQ_RING:
9288 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009289 break;
9290 case IORING_OFF_SQES:
9291 ptr = ctx->sq_sqes;
9292 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009293 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009294 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009295 }
9296
9297 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009298 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009299 return ERR_PTR(-EINVAL);
9300
9301 return ptr;
9302}
9303
9304#ifdef CONFIG_MMU
9305
9306static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9307{
9308 size_t sz = vma->vm_end - vma->vm_start;
9309 unsigned long pfn;
9310 void *ptr;
9311
9312 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9313 if (IS_ERR(ptr))
9314 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009315
9316 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9317 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9318}
9319
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009320#else /* !CONFIG_MMU */
9321
9322static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9323{
9324 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9325}
9326
9327static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9328{
9329 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9330}
9331
9332static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9333 unsigned long addr, unsigned long len,
9334 unsigned long pgoff, unsigned long flags)
9335{
9336 void *ptr;
9337
9338 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9339 if (IS_ERR(ptr))
9340 return PTR_ERR(ptr);
9341
9342 return (unsigned long) ptr;
9343}
9344
9345#endif /* !CONFIG_MMU */
9346
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009347static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009348{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009349 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009350 DEFINE_WAIT(wait);
9351
9352 do {
9353 if (!io_sqring_full(ctx))
9354 break;
9355
9356 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9357
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009358 if (unlikely(ctx->sqo_dead)) {
9359 ret = -EOWNERDEAD;
9360 goto out;
9361 }
9362
Jens Axboe90554202020-09-03 12:12:41 -06009363 if (!io_sqring_full(ctx))
9364 break;
9365
9366 schedule();
9367 } while (!signal_pending(current));
9368
9369 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009370out:
9371 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009372}
9373
Hao Xuc73ebb62020-11-03 10:54:37 +08009374static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9375 struct __kernel_timespec __user **ts,
9376 const sigset_t __user **sig)
9377{
9378 struct io_uring_getevents_arg arg;
9379
9380 /*
9381 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9382 * is just a pointer to the sigset_t.
9383 */
9384 if (!(flags & IORING_ENTER_EXT_ARG)) {
9385 *sig = (const sigset_t __user *) argp;
9386 *ts = NULL;
9387 return 0;
9388 }
9389
9390 /*
9391 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9392 * timespec and sigset_t pointers if good.
9393 */
9394 if (*argsz != sizeof(arg))
9395 return -EINVAL;
9396 if (copy_from_user(&arg, argp, sizeof(arg)))
9397 return -EFAULT;
9398 *sig = u64_to_user_ptr(arg.sigmask);
9399 *argsz = arg.sigmask_sz;
9400 *ts = u64_to_user_ptr(arg.ts);
9401 return 0;
9402}
9403
Jens Axboe2b188cc2019-01-07 10:46:33 -07009404SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009405 u32, min_complete, u32, flags, const void __user *, argp,
9406 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009407{
9408 struct io_ring_ctx *ctx;
9409 long ret = -EBADF;
9410 int submitted = 0;
9411 struct fd f;
9412
Jens Axboe4c6e2772020-07-01 11:29:10 -06009413 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009414
Jens Axboe90554202020-09-03 12:12:41 -06009415 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009416 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417 return -EINVAL;
9418
9419 f = fdget(fd);
9420 if (!f.file)
9421 return -EBADF;
9422
9423 ret = -EOPNOTSUPP;
9424 if (f.file->f_op != &io_uring_fops)
9425 goto out_fput;
9426
9427 ret = -ENXIO;
9428 ctx = f.file->private_data;
9429 if (!percpu_ref_tryget(&ctx->refs))
9430 goto out_fput;
9431
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009432 ret = -EBADFD;
9433 if (ctx->flags & IORING_SETUP_R_DISABLED)
9434 goto out;
9435
Jens Axboe6c271ce2019-01-10 11:22:30 -07009436 /*
9437 * For SQ polling, the thread will do all submissions and completions.
9438 * Just return the requested submit count, and wake the thread if
9439 * we were asked to.
9440 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009441 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009442 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009443 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009444
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009445 ret = -EOWNERDEAD;
9446 if (unlikely(ctx->sqo_dead))
9447 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009448 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009449 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009450 if (flags & IORING_ENTER_SQ_WAIT) {
9451 ret = io_sqpoll_wait_sq(ctx);
9452 if (ret)
9453 goto out;
9454 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009455 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009456 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009457 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009458 if (unlikely(ret))
9459 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009460 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009461 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009462 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009463
9464 if (submitted != to_submit)
9465 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009466 }
9467 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009468 const sigset_t __user *sig;
9469 struct __kernel_timespec __user *ts;
9470
9471 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9472 if (unlikely(ret))
9473 goto out;
9474
Jens Axboe2b188cc2019-01-07 10:46:33 -07009475 min_complete = min(min_complete, ctx->cq_entries);
9476
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009477 /*
9478 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9479 * space applications don't need to do io completion events
9480 * polling again, they can rely on io_sq_thread to do polling
9481 * work, which can reduce cpu usage and uring_lock contention.
9482 */
9483 if (ctx->flags & IORING_SETUP_IOPOLL &&
9484 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009485 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009486 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009487 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009488 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009489 }
9490
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009491out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009492 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009493out_fput:
9494 fdput(f);
9495 return submitted ? submitted : ret;
9496}
9497
Tobias Klauserbebdb652020-02-26 18:38:32 +01009498#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009499static int io_uring_show_cred(int id, void *p, void *data)
9500{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009501 struct io_identity *iod = p;
9502 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009503 struct seq_file *m = data;
9504 struct user_namespace *uns = seq_user_ns(m);
9505 struct group_info *gi;
9506 kernel_cap_t cap;
9507 unsigned __capi;
9508 int g;
9509
9510 seq_printf(m, "%5d\n", id);
9511 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9512 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9513 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9514 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9515 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9516 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9517 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9518 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9519 seq_puts(m, "\n\tGroups:\t");
9520 gi = cred->group_info;
9521 for (g = 0; g < gi->ngroups; g++) {
9522 seq_put_decimal_ull(m, g ? " " : "",
9523 from_kgid_munged(uns, gi->gid[g]));
9524 }
9525 seq_puts(m, "\n\tCapEff:\t");
9526 cap = cred->cap_effective;
9527 CAP_FOR_EACH_U32(__capi)
9528 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9529 seq_putc(m, '\n');
9530 return 0;
9531}
9532
9533static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9534{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009535 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009536 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009537 int i;
9538
Jens Axboefad8e0d2020-09-28 08:57:48 -06009539 /*
9540 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9541 * since fdinfo case grabs it in the opposite direction of normal use
9542 * cases. If we fail to get the lock, we just don't iterate any
9543 * structures that could be going away outside the io_uring mutex.
9544 */
9545 has_lock = mutex_trylock(&ctx->uring_lock);
9546
Joseph Qidbbe9c62020-09-29 09:01:22 -06009547 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9548 sq = ctx->sq_data;
9549
9550 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9551 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009552 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009553 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009554 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009555
Jens Axboe87ce9552020-01-30 08:25:34 -07009556 if (f)
9557 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9558 else
9559 seq_printf(m, "%5u: <none>\n", i);
9560 }
9561 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009562 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009563 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9564
9565 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9566 (unsigned int) buf->len);
9567 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009568 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009569 seq_printf(m, "Personalities:\n");
9570 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9571 }
Jens Axboed7718a92020-02-14 22:23:12 -07009572 seq_printf(m, "PollList:\n");
9573 spin_lock_irq(&ctx->completion_lock);
9574 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9575 struct hlist_head *list = &ctx->cancel_hash[i];
9576 struct io_kiocb *req;
9577
9578 hlist_for_each_entry(req, list, hash_node)
9579 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9580 req->task->task_works != NULL);
9581 }
9582 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009583 if (has_lock)
9584 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009585}
9586
9587static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9588{
9589 struct io_ring_ctx *ctx = f->private_data;
9590
9591 if (percpu_ref_tryget(&ctx->refs)) {
9592 __io_uring_show_fdinfo(ctx, m);
9593 percpu_ref_put(&ctx->refs);
9594 }
9595}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009596#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009597
Jens Axboe2b188cc2019-01-07 10:46:33 -07009598static const struct file_operations io_uring_fops = {
9599 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009600 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009601 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009602#ifndef CONFIG_MMU
9603 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9604 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9605#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009606 .poll = io_uring_poll,
9607 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009608#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009609 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009610#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009611};
9612
9613static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9614 struct io_uring_params *p)
9615{
Hristo Venev75b28af2019-08-26 17:23:46 +00009616 struct io_rings *rings;
9617 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009618
Jens Axboebd740482020-08-05 12:58:23 -06009619 /* make sure these are sane, as we already accounted them */
9620 ctx->sq_entries = p->sq_entries;
9621 ctx->cq_entries = p->cq_entries;
9622
Hristo Venev75b28af2019-08-26 17:23:46 +00009623 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9624 if (size == SIZE_MAX)
9625 return -EOVERFLOW;
9626
9627 rings = io_mem_alloc(size);
9628 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009629 return -ENOMEM;
9630
Hristo Venev75b28af2019-08-26 17:23:46 +00009631 ctx->rings = rings;
9632 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9633 rings->sq_ring_mask = p->sq_entries - 1;
9634 rings->cq_ring_mask = p->cq_entries - 1;
9635 rings->sq_ring_entries = p->sq_entries;
9636 rings->cq_ring_entries = p->cq_entries;
9637 ctx->sq_mask = rings->sq_ring_mask;
9638 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009639
9640 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009641 if (size == SIZE_MAX) {
9642 io_mem_free(ctx->rings);
9643 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009644 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009645 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009646
9647 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009648 if (!ctx->sq_sqes) {
9649 io_mem_free(ctx->rings);
9650 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009651 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009652 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009653
Jens Axboe2b188cc2019-01-07 10:46:33 -07009654 return 0;
9655}
9656
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009657static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9658{
9659 int ret, fd;
9660
9661 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9662 if (fd < 0)
9663 return fd;
9664
9665 ret = io_uring_add_task_file(ctx, file);
9666 if (ret) {
9667 put_unused_fd(fd);
9668 return ret;
9669 }
9670 fd_install(fd, file);
9671 return fd;
9672}
9673
Jens Axboe2b188cc2019-01-07 10:46:33 -07009674/*
9675 * Allocate an anonymous fd, this is what constitutes the application
9676 * visible backing of an io_uring instance. The application mmaps this
9677 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9678 * we have to tie this fd to a socket for file garbage collection purposes.
9679 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009680static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009681{
9682 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009683#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009684 int ret;
9685
Jens Axboe2b188cc2019-01-07 10:46:33 -07009686 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9687 &ctx->ring_sock);
9688 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009689 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009690#endif
9691
Jens Axboe2b188cc2019-01-07 10:46:33 -07009692 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9693 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009694#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009695 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009696 sock_release(ctx->ring_sock);
9697 ctx->ring_sock = NULL;
9698 } else {
9699 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009700 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009701#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009702 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009703}
9704
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009705static int io_uring_create(unsigned entries, struct io_uring_params *p,
9706 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009707{
9708 struct user_struct *user = NULL;
9709 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009710 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009711 int ret;
9712
Jens Axboe8110c1a2019-12-28 15:39:54 -07009713 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009714 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009715 if (entries > IORING_MAX_ENTRIES) {
9716 if (!(p->flags & IORING_SETUP_CLAMP))
9717 return -EINVAL;
9718 entries = IORING_MAX_ENTRIES;
9719 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009720
9721 /*
9722 * Use twice as many entries for the CQ ring. It's possible for the
9723 * application to drive a higher depth than the size of the SQ ring,
9724 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009725 * some flexibility in overcommitting a bit. If the application has
9726 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9727 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009728 */
9729 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009730 if (p->flags & IORING_SETUP_CQSIZE) {
9731 /*
9732 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9733 * to a power-of-two, if it isn't already. We do NOT impose
9734 * any cq vs sq ring sizing.
9735 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009736 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009737 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009738 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9739 if (!(p->flags & IORING_SETUP_CLAMP))
9740 return -EINVAL;
9741 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9742 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009743 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9744 if (p->cq_entries < p->sq_entries)
9745 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009746 } else {
9747 p->cq_entries = 2 * p->sq_entries;
9748 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009749
9750 user = get_uid(current_user());
Jens Axboe2b188cc2019-01-07 10:46:33 -07009751
9752 ctx = io_ring_ctx_alloc(p);
9753 if (!ctx) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07009754 free_uid(user);
9755 return -ENOMEM;
9756 }
9757 ctx->compat = in_compat_syscall();
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009758 ctx->limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009759 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009760 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009761#ifdef CONFIG_AUDIT
9762 ctx->loginuid = current->loginuid;
9763 ctx->sessionid = current->sessionid;
9764#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009765 ctx->sqo_task = get_task_struct(current);
9766
9767 /*
9768 * This is just grabbed for accounting purposes. When a process exits,
9769 * the mm is exited and dropped before the files, hence we need to hang
9770 * on to this mm purely for the purposes of being able to unaccount
9771 * memory (locked/pinned vm). It's not used for anything else.
9772 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009773 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009774 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009775
Dennis Zhou91d8f512020-09-16 13:41:05 -07009776#ifdef CONFIG_BLK_CGROUP
9777 /*
9778 * The sq thread will belong to the original cgroup it was inited in.
9779 * If the cgroup goes offline (e.g. disabling the io controller), then
9780 * issued bios will be associated with the closest cgroup later in the
9781 * block layer.
9782 */
9783 rcu_read_lock();
9784 ctx->sqo_blkcg_css = blkcg_css();
9785 ret = css_tryget_online(ctx->sqo_blkcg_css);
9786 rcu_read_unlock();
9787 if (!ret) {
9788 /* don't init against a dying cgroup, have the user try again */
9789 ctx->sqo_blkcg_css = NULL;
9790 ret = -ENODEV;
9791 goto err;
9792 }
9793#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009794 ret = io_allocate_scq_urings(ctx, p);
9795 if (ret)
9796 goto err;
9797
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009798 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009799 if (ret)
9800 goto err;
9801
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009802 if (!(p->flags & IORING_SETUP_R_DISABLED))
9803 io_sq_offload_start(ctx);
9804
Jens Axboe2b188cc2019-01-07 10:46:33 -07009805 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009806 p->sq_off.head = offsetof(struct io_rings, sq.head);
9807 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9808 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9809 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9810 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9811 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9812 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009813
9814 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009815 p->cq_off.head = offsetof(struct io_rings, cq.head);
9816 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9817 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9818 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9819 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9820 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009821 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009822
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009823 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9824 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009825 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009826 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9827 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009828
9829 if (copy_to_user(params, p, sizeof(*p))) {
9830 ret = -EFAULT;
9831 goto err;
9832 }
Jens Axboed1719f72020-07-30 13:43:53 -06009833
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009834 file = io_uring_get_file(ctx);
9835 if (IS_ERR(file)) {
9836 ret = PTR_ERR(file);
9837 goto err;
9838 }
9839
Jens Axboed1719f72020-07-30 13:43:53 -06009840 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009841 * Install ring fd as the very last thing, so we don't risk someone
9842 * having closed it before we finish setup
9843 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009844 ret = io_uring_install_fd(ctx, file);
9845 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009846 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009847 /* fput will clean it up */
9848 fput(file);
9849 return ret;
9850 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009851
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009852 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009853 return ret;
9854err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009855 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009856 io_ring_ctx_wait_and_kill(ctx);
9857 return ret;
9858}
9859
9860/*
9861 * Sets up an aio uring context, and returns the fd. Applications asks for a
9862 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9863 * params structure passed in.
9864 */
9865static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9866{
9867 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009868 int i;
9869
9870 if (copy_from_user(&p, params, sizeof(p)))
9871 return -EFAULT;
9872 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9873 if (p.resv[i])
9874 return -EINVAL;
9875 }
9876
Jens Axboe6c271ce2019-01-10 11:22:30 -07009877 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009878 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009879 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9880 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009881 return -EINVAL;
9882
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009883 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009884}
9885
9886SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9887 struct io_uring_params __user *, params)
9888{
9889 return io_uring_setup(entries, params);
9890}
9891
Jens Axboe66f4af92020-01-16 15:36:52 -07009892static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9893{
9894 struct io_uring_probe *p;
9895 size_t size;
9896 int i, ret;
9897
9898 size = struct_size(p, ops, nr_args);
9899 if (size == SIZE_MAX)
9900 return -EOVERFLOW;
9901 p = kzalloc(size, GFP_KERNEL);
9902 if (!p)
9903 return -ENOMEM;
9904
9905 ret = -EFAULT;
9906 if (copy_from_user(p, arg, size))
9907 goto out;
9908 ret = -EINVAL;
9909 if (memchr_inv(p, 0, size))
9910 goto out;
9911
9912 p->last_op = IORING_OP_LAST - 1;
9913 if (nr_args > IORING_OP_LAST)
9914 nr_args = IORING_OP_LAST;
9915
9916 for (i = 0; i < nr_args; i++) {
9917 p->ops[i].op = i;
9918 if (!io_op_defs[i].not_supported)
9919 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9920 }
9921 p->ops_len = i;
9922
9923 ret = 0;
9924 if (copy_to_user(arg, p, size))
9925 ret = -EFAULT;
9926out:
9927 kfree(p);
9928 return ret;
9929}
9930
Jens Axboe071698e2020-01-28 10:04:42 -07009931static int io_register_personality(struct io_ring_ctx *ctx)
9932{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009933 struct io_identity *id;
9934 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009935
Jens Axboe1e6fa522020-10-15 08:46:24 -06009936 id = kmalloc(sizeof(*id), GFP_KERNEL);
9937 if (unlikely(!id))
9938 return -ENOMEM;
9939
9940 io_init_identity(id);
9941 id->creds = get_current_cred();
9942
9943 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9944 if (ret < 0) {
9945 put_cred(id->creds);
9946 kfree(id);
9947 }
9948 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009949}
9950
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009951static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9952 unsigned int nr_args)
9953{
9954 struct io_uring_restriction *res;
9955 size_t size;
9956 int i, ret;
9957
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009958 /* Restrictions allowed only if rings started disabled */
9959 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9960 return -EBADFD;
9961
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009962 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009963 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009964 return -EBUSY;
9965
9966 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9967 return -EINVAL;
9968
9969 size = array_size(nr_args, sizeof(*res));
9970 if (size == SIZE_MAX)
9971 return -EOVERFLOW;
9972
9973 res = memdup_user(arg, size);
9974 if (IS_ERR(res))
9975 return PTR_ERR(res);
9976
9977 ret = 0;
9978
9979 for (i = 0; i < nr_args; i++) {
9980 switch (res[i].opcode) {
9981 case IORING_RESTRICTION_REGISTER_OP:
9982 if (res[i].register_op >= IORING_REGISTER_LAST) {
9983 ret = -EINVAL;
9984 goto out;
9985 }
9986
9987 __set_bit(res[i].register_op,
9988 ctx->restrictions.register_op);
9989 break;
9990 case IORING_RESTRICTION_SQE_OP:
9991 if (res[i].sqe_op >= IORING_OP_LAST) {
9992 ret = -EINVAL;
9993 goto out;
9994 }
9995
9996 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9997 break;
9998 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9999 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10000 break;
10001 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10002 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10003 break;
10004 default:
10005 ret = -EINVAL;
10006 goto out;
10007 }
10008 }
10009
10010out:
10011 /* Reset all restrictions if an error happened */
10012 if (ret != 0)
10013 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10014 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010015 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010016
10017 kfree(res);
10018 return ret;
10019}
10020
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010021static int io_register_enable_rings(struct io_ring_ctx *ctx)
10022{
10023 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10024 return -EBADFD;
10025
10026 if (ctx->restrictions.registered)
10027 ctx->restricted = 1;
10028
10029 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10030
10031 io_sq_offload_start(ctx);
10032
10033 return 0;
10034}
10035
Jens Axboe071698e2020-01-28 10:04:42 -070010036static bool io_register_op_must_quiesce(int op)
10037{
10038 switch (op) {
10039 case IORING_UNREGISTER_FILES:
10040 case IORING_REGISTER_FILES_UPDATE:
10041 case IORING_REGISTER_PROBE:
10042 case IORING_REGISTER_PERSONALITY:
10043 case IORING_UNREGISTER_PERSONALITY:
10044 return false;
10045 default:
10046 return true;
10047 }
10048}
10049
Jens Axboeedafcce2019-01-09 09:16:05 -070010050static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10051 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010052 __releases(ctx->uring_lock)
10053 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010054{
10055 int ret;
10056
Jens Axboe35fa71a2019-04-22 10:23:23 -060010057 /*
10058 * We're inside the ring mutex, if the ref is already dying, then
10059 * someone else killed the ctx or is already going through
10060 * io_uring_register().
10061 */
10062 if (percpu_ref_is_dying(&ctx->refs))
10063 return -ENXIO;
10064
Jens Axboe071698e2020-01-28 10:04:42 -070010065 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010066 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010067
Jens Axboe05f3fb32019-12-09 11:22:50 -070010068 /*
10069 * Drop uring mutex before waiting for references to exit. If
10070 * another thread is currently inside io_uring_enter() it might
10071 * need to grab the uring_lock to make progress. If we hold it
10072 * here across the drain wait, then we can deadlock. It's safe
10073 * to drop the mutex here, since no new references will come in
10074 * after we've killed the percpu ref.
10075 */
10076 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010077 do {
10078 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10079 if (!ret)
10080 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010081 ret = io_run_task_work_sig();
10082 if (ret < 0)
10083 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010084 } while (1);
10085
Jens Axboe05f3fb32019-12-09 11:22:50 -070010086 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010087
Pavel Begunkov88f171a2021-02-20 18:03:50 +000010088 if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp))
10089 return ret;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010090 }
10091
10092 if (ctx->restricted) {
10093 if (opcode >= IORING_REGISTER_LAST) {
10094 ret = -EINVAL;
10095 goto out;
10096 }
10097
10098 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10099 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010100 goto out;
10101 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010102 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010103
10104 switch (opcode) {
10105 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010106 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010107 break;
10108 case IORING_UNREGISTER_BUFFERS:
10109 ret = -EINVAL;
10110 if (arg || nr_args)
10111 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010112 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010113 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010114 case IORING_REGISTER_FILES:
10115 ret = io_sqe_files_register(ctx, arg, nr_args);
10116 break;
10117 case IORING_UNREGISTER_FILES:
10118 ret = -EINVAL;
10119 if (arg || nr_args)
10120 break;
10121 ret = io_sqe_files_unregister(ctx);
10122 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010123 case IORING_REGISTER_FILES_UPDATE:
10124 ret = io_sqe_files_update(ctx, arg, nr_args);
10125 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010126 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010127 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010128 ret = -EINVAL;
10129 if (nr_args != 1)
10130 break;
10131 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010132 if (ret)
10133 break;
10134 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10135 ctx->eventfd_async = 1;
10136 else
10137 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010138 break;
10139 case IORING_UNREGISTER_EVENTFD:
10140 ret = -EINVAL;
10141 if (arg || nr_args)
10142 break;
10143 ret = io_eventfd_unregister(ctx);
10144 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010145 case IORING_REGISTER_PROBE:
10146 ret = -EINVAL;
10147 if (!arg || nr_args > 256)
10148 break;
10149 ret = io_probe(ctx, arg, nr_args);
10150 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010151 case IORING_REGISTER_PERSONALITY:
10152 ret = -EINVAL;
10153 if (arg || nr_args)
10154 break;
10155 ret = io_register_personality(ctx);
10156 break;
10157 case IORING_UNREGISTER_PERSONALITY:
10158 ret = -EINVAL;
10159 if (arg)
10160 break;
10161 ret = io_unregister_personality(ctx, nr_args);
10162 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010163 case IORING_REGISTER_ENABLE_RINGS:
10164 ret = -EINVAL;
10165 if (arg || nr_args)
10166 break;
10167 ret = io_register_enable_rings(ctx);
10168 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010169 case IORING_REGISTER_RESTRICTIONS:
10170 ret = io_register_restrictions(ctx, arg, nr_args);
10171 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010172 default:
10173 ret = -EINVAL;
10174 break;
10175 }
10176
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010177out:
Jens Axboe071698e2020-01-28 10:04:42 -070010178 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010179 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010180 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010181 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010182 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010183 return ret;
10184}
10185
10186SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10187 void __user *, arg, unsigned int, nr_args)
10188{
10189 struct io_ring_ctx *ctx;
10190 long ret = -EBADF;
10191 struct fd f;
10192
10193 f = fdget(fd);
10194 if (!f.file)
10195 return -EBADF;
10196
10197 ret = -EOPNOTSUPP;
10198 if (f.file->f_op != &io_uring_fops)
10199 goto out_fput;
10200
10201 ctx = f.file->private_data;
10202
10203 mutex_lock(&ctx->uring_lock);
10204 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10205 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010206 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10207 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010208out_fput:
10209 fdput(f);
10210 return ret;
10211}
10212
Jens Axboe2b188cc2019-01-07 10:46:33 -070010213static int __init io_uring_init(void)
10214{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010215#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10216 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10217 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10218} while (0)
10219
10220#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10221 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10222 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10223 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10224 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10225 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10226 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10227 BUILD_BUG_SQE_ELEM(8, __u64, off);
10228 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10229 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010230 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010231 BUILD_BUG_SQE_ELEM(24, __u32, len);
10232 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10233 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10234 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10235 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010236 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10237 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010238 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10239 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10240 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10241 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10242 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10243 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10244 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10245 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010246 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010247 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10248 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10249 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010250 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010251
Jens Axboed3656342019-12-18 09:50:26 -070010252 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010253 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -070010254 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10255 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010256 return 0;
10257};
10258__initcall(io_uring_init);