blob: 6d851033e48d7065d4a0280d4c5ad11e183ddf53 [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 Axboe2aede0e2020-09-14 10:45:53 -0600369 /*
370 * For SQPOLL usage - we hold a reference to the parent task, so we
371 * have access to the ->files
372 */
373 struct task_struct *sqo_task;
374
375 /* Only used for accounting purposes */
376 struct mm_struct *mm_account;
377
Dennis Zhou91d8f512020-09-16 13:41:05 -0700378#ifdef CONFIG_BLK_CGROUP
379 struct cgroup_subsys_state *sqo_blkcg_css;
380#endif
381
Jens Axboe534ca6d2020-09-02 13:52:19 -0600382 struct io_sq_data *sq_data; /* if using sq thread polling */
383
Jens Axboe90554202020-09-03 12:12:41 -0600384 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600385 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700386
Jens Axboe6b063142019-01-10 22:13:58 -0700387 /*
388 * If used, fixed file set. Writers must ensure that ->refs is dead,
389 * readers must ensure that ->refs is alive as long as the file* is
390 * used. Only updated through io_uring_register(2).
391 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000392 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700393 unsigned nr_user_files;
394
Jens Axboeedafcce2019-01-09 09:16:05 -0700395 /* if used, fixed mapped user buffers */
396 unsigned nr_user_bufs;
397 struct io_mapped_ubuf *user_bufs;
398
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399 struct user_struct *user;
400
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700401 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700402
Jens Axboe4ea33a92020-10-15 13:46:44 -0600403#ifdef CONFIG_AUDIT
404 kuid_t loginuid;
405 unsigned int sessionid;
406#endif
407
Jens Axboe0f158b42020-05-14 17:18:39 -0600408 struct completion ref_comp;
409 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700410
411#if defined(CONFIG_UNIX)
412 struct socket *ring_sock;
413#endif
414
Jens Axboe5a2e7452020-02-23 16:23:11 -0700415 struct idr io_buffer_idr;
416
Jens Axboe071698e2020-01-28 10:04:42 -0700417 struct idr personality_idr;
418
Jens Axboe206aefd2019-11-07 18:27:42 -0700419 struct {
420 unsigned cached_cq_tail;
421 unsigned cq_entries;
422 unsigned cq_mask;
423 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500424 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700425 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700426 struct wait_queue_head cq_wait;
427 struct fasync_struct *cq_fasync;
428 struct eventfd_ctx *cq_ev_fd;
429 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430
431 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700433
Jens Axboedef596e2019-01-09 08:59:42 -0700434 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300435 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700436 * io_uring instances that don't use IORING_SETUP_SQPOLL.
437 * For SQPOLL, only the single threaded io_sq_thread() will
438 * manipulate the list, hence no extra locking is needed there.
439 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300440 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700441 struct hlist_head *cancel_hash;
442 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700443 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600444
445 spinlock_t inflight_lock;
446 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700447 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600448
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000449 struct delayed_work rsrc_put_work;
450 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000451 struct list_head rsrc_ref_list;
452 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600453
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200454 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700455
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700456 /* exit task_work */
457 struct callback_head *exit_task_work;
458
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700459 /* 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 Axboed3656342019-12-18 09:50:26 -0700841};
842
Jens Axboe09186822020-10-13 15:01:40 -0600843static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300844 [IORING_OP_NOP] = {},
845 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700846 .needs_file = 1,
847 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700848 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700849 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700850 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600851 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700852 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700853 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300854 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700855 .needs_file = 1,
856 .hash_reg_file = 1,
857 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700858 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700859 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600860 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700861 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700862 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300863 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700864 .needs_file = 1,
865 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300866 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700867 .needs_file = 1,
868 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700869 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600870 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700871 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700872 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300873 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700874 .needs_file = 1,
875 .hash_reg_file = 1,
876 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700877 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600878 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700879 .async_size = sizeof(struct io_async_rw),
Jens Axboed3656342019-12-18 09:50:26 -0700880 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300881 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700882 .needs_file = 1,
883 .unbound_nonreg_file = 1,
884 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300885 [IORING_OP_POLL_REMOVE] = {},
886 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700887 .needs_file = 1,
888 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300889 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700892 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700893 .needs_async_data = 1,
894 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700897 .needs_file = 1,
898 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700899 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700900 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700901 .needs_async_data = 1,
902 .async_size = sizeof(struct io_async_msghdr),
Jens Axboed3656342019-12-18 09:50:26 -0700903 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300904 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700905 .needs_async_data = 1,
906 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700907 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000908 [IORING_OP_TIMEOUT_REMOVE] = {
909 /* used by timeout updates' prep() */
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000910 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300911 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700912 .needs_file = 1,
913 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700914 .pollin = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700915 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300916 [IORING_OP_ASYNC_CANCEL] = {},
917 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700918 .needs_async_data = 1,
919 .async_size = sizeof(struct io_timeout_data),
Jens Axboed3656342019-12-18 09:50:26 -0700920 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300921 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700922 .needs_file = 1,
923 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700924 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700925 .needs_async_data = 1,
926 .async_size = sizeof(struct io_async_connect),
Jens Axboed3656342019-12-18 09:50:26 -0700927 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300928 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700929 .needs_file = 1,
930 },
Jens Axboe44526be2021-02-15 13:32:18 -0700931 [IORING_OP_OPENAT] = {},
932 [IORING_OP_CLOSE] = {},
933 [IORING_OP_FILES_UPDATE] = {},
934 [IORING_OP_STATX] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700936 .needs_file = 1,
937 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700938 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700939 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600940 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700941 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700942 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300943 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700944 .needs_file = 1,
945 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700946 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600947 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700948 .async_size = sizeof(struct io_async_rw),
Jens Axboe3a6820f2019-12-22 15:19:35 -0700949 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300950 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700951 .needs_file = 1,
952 },
Jens Axboe44526be2021-02-15 13:32:18 -0700953 [IORING_OP_MADVISE] = {},
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700955 .needs_file = 1,
956 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700957 .pollout = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700958 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300959 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700960 .needs_file = 1,
961 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700962 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700963 .buffer_select = 1,
Jens Axboefddafac2020-01-04 20:19:44 -0700964 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300965 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700966 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700967 [IORING_OP_EPOLL_CTL] = {
968 .unbound_nonreg_file = 1,
Jens Axboe3e4827b2020-01-08 15:18:09 -0700969 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300970 [IORING_OP_SPLICE] = {
971 .needs_file = 1,
972 .hash_reg_file = 1,
973 .unbound_nonreg_file = 1,
Jens Axboeddf0322d2020-02-23 16:41:33 -0700974 },
975 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -0700976 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +0300977 [IORING_OP_TEE] = {
978 .needs_file = 1,
979 .hash_reg_file = 1,
980 .unbound_nonreg_file = 1,
981 },
Jens Axboe36f4fa62020-09-05 11:14:22 -0600982 [IORING_OP_SHUTDOWN] = {
983 .needs_file = 1,
984 },
Jens Axboe44526be2021-02-15 13:32:18 -0700985 [IORING_OP_RENAMEAT] = {},
986 [IORING_OP_UNLINKAT] = {},
Jens Axboed3656342019-12-18 09:50:26 -0700987};
988
Pavel Begunkov9936c7c2021-02-04 13:51:56 +0000989static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
990 struct task_struct *task,
991 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000992static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +0000993static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000994 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +0000995static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +0000996
Pavel Begunkov23faba32021-02-11 18:28:22 +0000997static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700998static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800999static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001000static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001001static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001002static void io_dismantle_req(struct io_kiocb *req);
1003static void io_put_task(struct task_struct *task, int nr);
1004static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001005static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001006static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001007static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001008static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001009 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001010 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001011static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001012static struct file *io_file_get(struct io_submit_state *state,
1013 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001014static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001015static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001016
Pavel Begunkov847595d2021-02-04 13:52:06 +00001017static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1018 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001019static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1020 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001021 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001022static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001023static void io_submit_flush_completions(struct io_comp_state *cs,
1024 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001025
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026static struct kmem_cache *req_cachep;
1027
Jens Axboe09186822020-10-13 15:01:40 -06001028static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001029
1030struct sock *io_uring_get_socket(struct file *file)
1031{
1032#if defined(CONFIG_UNIX)
1033 if (file->f_op == &io_uring_fops) {
1034 struct io_ring_ctx *ctx = file->private_data;
1035
1036 return ctx->ring_sock->sk;
1037 }
1038#endif
1039 return NULL;
1040}
1041EXPORT_SYMBOL(io_uring_get_socket);
1042
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001043#define io_for_each_link(pos, head) \
1044 for (pos = (head); pos; pos = pos->link)
1045
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001046static inline void io_clean_op(struct io_kiocb *req)
1047{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001048 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001049 __io_clean_op(req);
1050}
1051
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001052static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001053{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001054 struct io_ring_ctx *ctx = req->ctx;
1055
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001056 if (!req->fixed_rsrc_refs) {
1057 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1058 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001059 }
1060}
1061
Pavel Begunkov88f171a2021-02-20 18:03:50 +00001062static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1063{
1064 if (!percpu_ref_tryget(ref)) {
1065 /* already at zero, wait for ->release() */
1066 if (!try_wait_for_completion(compl))
1067 synchronize_rcu();
1068 return false;
1069 }
1070
1071 percpu_ref_resurrect(ref);
1072 reinit_completion(compl);
1073 percpu_ref_put(ref);
1074 return true;
1075}
1076
Pavel Begunkov08d23632020-11-06 13:00:22 +00001077static bool io_match_task(struct io_kiocb *head,
1078 struct task_struct *task,
1079 struct files_struct *files)
1080{
1081 struct io_kiocb *req;
1082
Jens Axboe84965ff2021-01-23 15:51:11 -07001083 if (task && head->task != task) {
1084 /* in terms of cancelation, always match if req task is dead */
1085 if (head->task->flags & PF_EXITING)
1086 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001087 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001088 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001089 if (!files)
1090 return true;
1091
1092 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001093 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1094 continue;
1095 if (req->file && req->file->f_op == &io_uring_fops)
1096 return true;
Jens Axboe4379bf82021-02-15 13:40:22 -07001097 if (req->task->files == files)
Pavel Begunkov08d23632020-11-06 13:00:22 +00001098 return true;
1099 }
1100 return false;
1101}
1102
Jens Axboe28cea78a2020-09-14 10:51:17 -06001103static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001104{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001105 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001106 struct mm_struct *mm = current->mm;
1107
1108 if (mm) {
1109 kthread_unuse_mm(mm);
1110 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001111 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001112 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001113 if (files) {
1114 struct nsproxy *nsproxy = current->nsproxy;
1115
1116 task_lock(current);
1117 current->files = NULL;
1118 current->nsproxy = NULL;
1119 task_unlock(current);
1120 put_files_struct(files);
1121 put_nsproxy(nsproxy);
1122 }
1123}
1124
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001125static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001126{
1127 if (!current->files) {
1128 struct files_struct *files;
1129 struct nsproxy *nsproxy;
1130
1131 task_lock(ctx->sqo_task);
1132 files = ctx->sqo_task->files;
1133 if (!files) {
1134 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001135 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001136 }
1137 atomic_inc(&files->count);
1138 get_nsproxy(ctx->sqo_task->nsproxy);
1139 nsproxy = ctx->sqo_task->nsproxy;
1140 task_unlock(ctx->sqo_task);
1141
1142 task_lock(current);
1143 current->files = files;
1144 current->nsproxy = nsproxy;
1145 task_unlock(current);
1146 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001147 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001148}
1149
1150static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1151{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001152 struct mm_struct *mm;
1153
1154 if (current->mm)
1155 return 0;
1156
Jens Axboe4b70cf92020-11-02 10:39:05 -07001157 task_lock(ctx->sqo_task);
1158 mm = ctx->sqo_task->mm;
1159 if (unlikely(!mm || !mmget_not_zero(mm)))
1160 mm = NULL;
1161 task_unlock(ctx->sqo_task);
1162
1163 if (mm) {
1164 kthread_use_mm(mm);
1165 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001166 }
1167
Jens Axboe4b70cf92020-11-02 10:39:05 -07001168 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001169}
1170
Pavel Begunkov4e326352021-02-12 03:23:52 +00001171static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1172 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001173{
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001174 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001175
Jens Axboe44526be2021-02-15 13:32:18 -07001176 ret = __io_sq_thread_acquire_mm(ctx);
1177 if (unlikely(ret))
1178 return ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001179
Jens Axboe44526be2021-02-15 13:32:18 -07001180 ret = __io_sq_thread_acquire_files(ctx);
1181 if (unlikely(ret))
1182 return ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001183
1184 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001185}
1186
Pavel Begunkov4e326352021-02-12 03:23:52 +00001187static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1188 struct io_kiocb *req)
1189{
Pavel Begunkov4e326352021-02-12 03:23:52 +00001190 if (!(ctx->flags & IORING_SETUP_SQPOLL))
1191 return 0;
1192 return __io_sq_thread_acquire_mm_files(ctx, req);
1193}
1194
Dennis Zhou91d8f512020-09-16 13:41:05 -07001195static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1196 struct cgroup_subsys_state **cur_css)
1197
1198{
1199#ifdef CONFIG_BLK_CGROUP
1200 /* puts the old one when swapping */
1201 if (*cur_css != ctx->sqo_blkcg_css) {
1202 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1203 *cur_css = ctx->sqo_blkcg_css;
1204 }
1205#endif
1206}
1207
1208static void io_sq_thread_unassociate_blkcg(void)
1209{
1210#ifdef CONFIG_BLK_CGROUP
1211 kthread_associate_blkcg(NULL);
1212#endif
1213}
1214
Jens Axboec40f6372020-06-25 15:39:59 -06001215static inline void req_set_fail_links(struct io_kiocb *req)
1216{
1217 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1218 req->flags |= REQ_F_FAIL_LINK;
1219}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001220
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001221static inline void __io_req_init_async(struct io_kiocb *req)
1222{
1223 memset(&req->work, 0, sizeof(req->work));
1224 req->flags |= REQ_F_WORK_INITIALIZED;
1225}
1226
Jens Axboe1e6fa522020-10-15 08:46:24 -06001227/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001228 * Note: must call io_req_init_async() for the first time you
1229 * touch any members of io_wq_work.
1230 */
1231static inline void io_req_init_async(struct io_kiocb *req)
1232{
1233 if (req->flags & REQ_F_WORK_INITIALIZED)
1234 return;
1235
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001236 __io_req_init_async(req);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001237}
1238
Jens Axboe2b188cc2019-01-07 10:46:33 -07001239static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1240{
1241 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1242
Jens Axboe0f158b42020-05-14 17:18:39 -06001243 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001244}
1245
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001246static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1247{
1248 return !req->timeout.off;
1249}
1250
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1252{
1253 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001254 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255
1256 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1257 if (!ctx)
1258 return NULL;
1259
Jens Axboe78076bb2019-12-04 19:56:40 -07001260 /*
1261 * Use 5 bits less than the max cq entries, that should give us around
1262 * 32 entries per hash list if totally full and uniformly spread.
1263 */
1264 hash_bits = ilog2(p->cq_entries);
1265 hash_bits -= 5;
1266 if (hash_bits <= 0)
1267 hash_bits = 1;
1268 ctx->cancel_hash_bits = hash_bits;
1269 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1270 GFP_KERNEL);
1271 if (!ctx->cancel_hash)
1272 goto err;
1273 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1274
Roman Gushchin21482892019-05-07 10:01:48 -07001275 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001276 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1277 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278
1279 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001280 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001281 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001283 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001284 init_completion(&ctx->ref_comp);
1285 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001286 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001287 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288 mutex_init(&ctx->uring_lock);
1289 init_waitqueue_head(&ctx->wait);
1290 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001291 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001292 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001293 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001294 spin_lock_init(&ctx->inflight_lock);
1295 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001296 spin_lock_init(&ctx->rsrc_ref_lock);
1297 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001298 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1299 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001300 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001301 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001302 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001303err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001304 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001305 kfree(ctx);
1306 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001307}
1308
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001309static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001310{
Jens Axboe2bc99302020-07-09 09:43:27 -06001311 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1312 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001313
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001314 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001315 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001316 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001317
Bob Liu9d858b22019-11-13 18:06:25 +08001318 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001319}
1320
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001321static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001322{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001323 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001324 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001325
Jens Axboe4379bf82021-02-15 13:40:22 -07001326 if (req->work.creds) {
1327 put_cred(req->work.creds);
1328 req->work.creds = NULL;
1329 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001330 if (req->flags & REQ_F_INFLIGHT) {
1331 struct io_ring_ctx *ctx = req->ctx;
1332 struct io_uring_task *tctx = req->task->io_uring;
1333 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001334
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001335 spin_lock_irqsave(&ctx->inflight_lock, flags);
1336 list_del(&req->inflight_entry);
1337 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1338 req->flags &= ~REQ_F_INFLIGHT;
1339 if (atomic_read(&tctx->in_idle))
1340 wake_up(&tctx->wait);
1341 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001342
Pavel Begunkove86d0042021-02-01 18:59:54 +00001343 req->flags &= ~REQ_F_WORK_INITIALIZED;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001344}
1345
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001346static void io_req_track_inflight(struct io_kiocb *req)
1347{
1348 struct io_ring_ctx *ctx = req->ctx;
1349
1350 if (!(req->flags & REQ_F_INFLIGHT)) {
1351 io_req_init_async(req);
1352 req->flags |= REQ_F_INFLIGHT;
1353
1354 spin_lock_irq(&ctx->inflight_lock);
1355 list_add(&req->inflight_entry, &ctx->inflight_list);
1356 spin_unlock_irq(&ctx->inflight_lock);
1357 }
1358}
1359
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001360static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001361{
Jens Axboed3656342019-12-18 09:50:26 -07001362 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001363 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001364
Pavel Begunkov16d59802020-07-12 16:16:47 +03001365 io_req_init_async(req);
1366
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001367 if (req->flags & REQ_F_FORCE_ASYNC)
1368 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1369
Jens Axboed3656342019-12-18 09:50:26 -07001370 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001371 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001372 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001373 } else {
1374 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001375 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001376 }
Jens Axboe4379bf82021-02-15 13:40:22 -07001377 if (!req->work.creds)
1378 req->work.creds = get_current_cred();
Jens Axboe561fb042019-10-24 07:25:42 -06001379}
1380
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001381static void io_prep_async_link(struct io_kiocb *req)
1382{
1383 struct io_kiocb *cur;
1384
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001385 io_for_each_link(cur, req)
1386 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001387}
1388
Jens Axboe7271ef32020-08-10 09:55:22 -06001389static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001390{
Jackie Liua197f662019-11-08 08:09:12 -07001391 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001392 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001393 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001394
Jens Axboe3bfe6102021-02-16 14:15:30 -07001395 BUG_ON(!tctx);
1396 BUG_ON(!tctx->io_wq);
1397
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001398 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1399 &req->work, req->flags);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001400 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001401 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001402}
1403
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001404static void io_queue_async_work(struct io_kiocb *req)
1405{
Jens Axboe7271ef32020-08-10 09:55:22 -06001406 struct io_kiocb *link;
1407
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001408 /* init ->work of the whole link before punting */
1409 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001410 link = __io_queue_async_work(req);
1411
1412 if (link)
1413 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001414}
1415
Jens Axboe5262f562019-09-17 12:26:57 -06001416static void io_kill_timeout(struct io_kiocb *req)
1417{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001418 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001419 int ret;
1420
Jens Axboee8c2bc12020-08-15 18:44:09 -07001421 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001422 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001423 atomic_set(&req->ctx->cq_timeouts,
1424 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001425 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001426 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001427 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001428 }
1429}
1430
Jens Axboe76e1b642020-09-26 15:05:03 -06001431/*
1432 * Returns true if we found and killed one or more timeouts
1433 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001434static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1435 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001436{
1437 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001438 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001439
1440 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001441 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001442 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001443 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001444 canceled++;
1445 }
Jens Axboef3606e32020-09-22 08:18:24 -06001446 }
Jens Axboe5262f562019-09-17 12:26:57 -06001447 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001448 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001449}
1450
Pavel Begunkov04518942020-05-26 20:34:05 +03001451static void __io_queue_deferred(struct io_ring_ctx *ctx)
1452{
1453 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001454 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1455 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001456
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001457 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001458 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001459 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001460 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001461 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001462 } while (!list_empty(&ctx->defer_list));
1463}
1464
Pavel Begunkov360428f2020-05-30 14:54:17 +03001465static void io_flush_timeouts(struct io_ring_ctx *ctx)
1466{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001467 u32 seq;
1468
1469 if (list_empty(&ctx->timeout_list))
1470 return;
1471
1472 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1473
1474 do {
1475 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001476 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001477 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001478
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001479 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001480 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001481
1482 /*
1483 * Since seq can easily wrap around over time, subtract
1484 * the last seq at which timeouts were flushed before comparing.
1485 * Assuming not more than 2^31-1 events have happened since,
1486 * these subtractions won't have wrapped, so we can check if
1487 * target is in [last_seq, current_seq] by comparing the two.
1488 */
1489 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1490 events_got = seq - ctx->cq_last_tm_flush;
1491 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001492 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001493
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001494 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001495 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001496 } while (!list_empty(&ctx->timeout_list));
1497
1498 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001499}
1500
Jens Axboede0617e2019-04-06 21:51:27 -06001501static void io_commit_cqring(struct io_ring_ctx *ctx)
1502{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001503 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001504
1505 /* order cqe stores with ring update */
1506 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001507
Pavel Begunkov04518942020-05-26 20:34:05 +03001508 if (unlikely(!list_empty(&ctx->defer_list)))
1509 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001510}
1511
Jens Axboe90554202020-09-03 12:12:41 -06001512static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1513{
1514 struct io_rings *r = ctx->rings;
1515
1516 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1517}
1518
Pavel Begunkov888aae22021-01-19 13:32:39 +00001519static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1520{
1521 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1522}
1523
Jens Axboe2b188cc2019-01-07 10:46:33 -07001524static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1525{
Hristo Venev75b28af2019-08-26 17:23:46 +00001526 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527 unsigned tail;
1528
Stefan Bühler115e12e2019-04-24 23:54:18 +02001529 /*
1530 * writes to the cq entry need to come after reading head; the
1531 * control dependency is enough as we're using WRITE_ONCE to
1532 * fill the cq entry
1533 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001534 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001535 return NULL;
1536
Pavel Begunkov888aae22021-01-19 13:32:39 +00001537 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001538 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001539}
1540
Jens Axboef2842ab2020-01-08 11:04:00 -07001541static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1542{
Jens Axboef0b493e2020-02-01 21:30:11 -07001543 if (!ctx->cq_ev_fd)
1544 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001545 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1546 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001547 if (!ctx->eventfd_async)
1548 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001549 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001550}
1551
Jens Axboeb41e9852020-02-17 09:52:41 -07001552static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001553{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001554 /* see waitqueue_active() comment */
1555 smp_mb();
1556
Jens Axboe8c838782019-03-12 15:48:16 -06001557 if (waitqueue_active(&ctx->wait))
1558 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001559 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1560 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001561 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001562 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001563 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001564 wake_up_interruptible(&ctx->cq_wait);
1565 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1566 }
Jens Axboe8c838782019-03-12 15:48:16 -06001567}
1568
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001569static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1570{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001571 /* see waitqueue_active() comment */
1572 smp_mb();
1573
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001574 if (ctx->flags & IORING_SETUP_SQPOLL) {
1575 if (waitqueue_active(&ctx->wait))
1576 wake_up(&ctx->wait);
1577 }
1578 if (io_should_trigger_evfd(ctx))
1579 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001580 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001581 wake_up_interruptible(&ctx->cq_wait);
1582 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1583 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001584}
1585
Jens Axboec4a2ed72019-11-21 21:01:26 -07001586/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001587static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1588 struct task_struct *tsk,
1589 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001591 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001592 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001593 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001594 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001595 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001596 LIST_HEAD(list);
1597
Pavel Begunkove23de152020-12-17 00:24:37 +00001598 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1599 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001600
Jens Axboeb18032b2021-01-24 16:58:56 -07001601 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001602 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001603 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001604 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001605 continue;
1606
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001607 cqe = io_get_cqring(ctx);
1608 if (!cqe && !force)
1609 break;
1610
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001611 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001612 if (cqe) {
1613 WRITE_ONCE(cqe->user_data, req->user_data);
1614 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001615 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001616 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001617 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001618 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001619 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001620 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001621 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001622 }
1623
Pavel Begunkov09e88402020-12-17 00:24:38 +00001624 all_flushed = list_empty(&ctx->cq_overflow_list);
1625 if (all_flushed) {
1626 clear_bit(0, &ctx->sq_check_overflow);
1627 clear_bit(0, &ctx->cq_check_overflow);
1628 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1629 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001630
Jens Axboeb18032b2021-01-24 16:58:56 -07001631 if (posted)
1632 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001633 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001634 if (posted)
1635 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001636
1637 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001638 req = list_first_entry(&list, struct io_kiocb, compl.list);
1639 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001640 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001641 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001642
Pavel Begunkov09e88402020-12-17 00:24:38 +00001643 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001644}
1645
Pavel Begunkov6c503152021-01-04 20:36:36 +00001646static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1647 struct task_struct *tsk,
1648 struct files_struct *files)
1649{
1650 if (test_bit(0, &ctx->cq_check_overflow)) {
1651 /* iopoll syncs against uring_lock, not completion_lock */
1652 if (ctx->flags & IORING_SETUP_IOPOLL)
1653 mutex_lock(&ctx->uring_lock);
1654 __io_cqring_overflow_flush(ctx, force, tsk, files);
1655 if (ctx->flags & IORING_SETUP_IOPOLL)
1656 mutex_unlock(&ctx->uring_lock);
1657 }
1658}
1659
Jens Axboebcda7ba2020-02-23 16:42:51 -07001660static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001662 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663 struct io_uring_cqe *cqe;
1664
Jens Axboe78e19bb2019-11-06 15:21:34 -07001665 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001666
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667 /*
1668 * If we can't get a cq entry, userspace overflowed the
1669 * submission (by quite a lot). Increment the overflow count in
1670 * the ring.
1671 */
1672 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001673 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001674 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001676 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001677 } else if (ctx->cq_overflow_flushed ||
1678 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001679 /*
1680 * If we're in ring overflow flush mode, or in task cancel mode,
1681 * then we cannot store the request for later flushing, we need
1682 * to drop it on the floor.
1683 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001684 ctx->cached_cq_overflow++;
1685 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001686 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001687 if (list_empty(&ctx->cq_overflow_list)) {
1688 set_bit(0, &ctx->sq_check_overflow);
1689 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001690 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001691 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001692 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001693 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001694 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001695 refcount_inc(&req->refs);
1696 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697 }
1698}
1699
Jens Axboebcda7ba2020-02-23 16:42:51 -07001700static void io_cqring_fill_event(struct io_kiocb *req, long res)
1701{
1702 __io_cqring_fill_event(req, res, 0);
1703}
1704
Jens Axboec7dae4b2021-02-09 19:53:37 -07001705static inline void io_req_complete_post(struct io_kiocb *req, long res,
1706 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001707{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001708 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001709 unsigned long flags;
1710
1711 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001712 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001714 /*
1715 * If we're the last reference to this request, add to our locked
1716 * free_list cache.
1717 */
1718 if (refcount_dec_and_test(&req->refs)) {
1719 struct io_comp_state *cs = &ctx->submit_state.comp;
1720
1721 io_dismantle_req(req);
1722 io_put_task(req->task, 1);
1723 list_add(&req->compl.list, &cs->locked_free_list);
1724 cs->locked_free_nr++;
1725 } else
1726 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1728
Jens Axboe8c838782019-03-12 15:48:16 -06001729 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001730 if (req) {
1731 io_queue_next(req);
1732 percpu_ref_put(&ctx->refs);
1733 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734}
1735
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001736static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001737 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001738{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001739 io_clean_op(req);
1740 req->result = res;
1741 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001742 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001743}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744
Pavel Begunkov889fca72021-02-10 00:03:09 +00001745static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1746 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001747{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001748 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1749 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001750 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001751 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001752}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001753
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001754static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001755{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001756 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001757}
1758
Jens Axboec7dae4b2021-02-09 19:53:37 -07001759static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001760{
Jens Axboec7dae4b2021-02-09 19:53:37 -07001761 struct io_submit_state *state = &ctx->submit_state;
1762 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001763 struct io_kiocb *req = NULL;
1764
Jens Axboec7dae4b2021-02-09 19:53:37 -07001765 /*
1766 * If we have more than a batch's worth of requests in our IRQ side
1767 * locked cache, grab the lock and move them over to our submission
1768 * side cache.
1769 */
1770 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
1771 spin_lock_irq(&ctx->completion_lock);
1772 list_splice_init(&cs->locked_free_list, &cs->free_list);
1773 cs->locked_free_nr = 0;
1774 spin_unlock_irq(&ctx->completion_lock);
1775 }
1776
1777 while (!list_empty(&cs->free_list)) {
1778 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001779 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001780 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001781 state->reqs[state->free_reqs++] = req;
1782 if (state->free_reqs == ARRAY_SIZE(state->reqs))
1783 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001785
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001786 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001787}
1788
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001789static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00001791 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001793 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03001795 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03001796 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07001797 int ret;
1798
Jens Axboec7dae4b2021-02-09 19:53:37 -07001799 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001800 goto got_req;
1801
Pavel Begunkovbf019da2021-02-10 00:03:17 +00001802 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
1803 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06001804
1805 /*
1806 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1807 * retry single alloc to be on the safe side.
1808 */
1809 if (unlikely(ret <= 0)) {
1810 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1811 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00001812 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06001813 ret = 1;
1814 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03001815 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001816 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00001817got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03001818 state->free_reqs--;
1819 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001820}
1821
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001822static inline void io_put_file(struct io_kiocb *req, struct file *file,
1823 bool fixed)
1824{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001825 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001826 fput(file);
1827}
1828
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001829static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001831 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001832
Jens Axboee8c2bc12020-08-15 18:44:09 -07001833 if (req->async_data)
1834 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001835 if (req->file)
1836 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001837 if (req->fixed_rsrc_refs)
1838 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001839 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001840}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001841
Pavel Begunkov7c660732021-01-25 11:42:21 +00001842static inline void io_put_task(struct task_struct *task, int nr)
1843{
1844 struct io_uring_task *tctx = task->io_uring;
1845
1846 percpu_counter_sub(&tctx->inflight, nr);
1847 if (unlikely(atomic_read(&tctx->in_idle)))
1848 wake_up(&tctx->wait);
1849 put_task_struct_many(task, nr);
1850}
1851
Pavel Begunkov216578e2020-10-13 09:44:00 +01001852static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03001853{
Jens Axboe51a4cc12020-08-10 10:55:56 -06001854 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001855
Pavel Begunkov216578e2020-10-13 09:44:00 +01001856 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00001857 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03001858
Pavel Begunkov3893f392021-02-10 00:03:15 +00001859 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03001860 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06001861}
1862
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001863static inline void io_remove_next_linked(struct io_kiocb *req)
1864{
1865 struct io_kiocb *nxt = req->link;
1866
1867 req->link = nxt->link;
1868 nxt->link = NULL;
1869}
1870
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001871static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001872{
Jackie Liua197f662019-11-08 08:09:12 -07001873 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001874 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001875 bool cancelled = false;
1876 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001877
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001878 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001879 link = req->link;
1880
Pavel Begunkov900fad42020-10-19 16:39:16 +01001881 /*
1882 * Can happen if a linked timeout fired and link had been like
1883 * req -> link t-out -> link t-out [-> ...]
1884 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001885 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
1886 struct io_timeout_data *io = link->async_data;
1887 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001888
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001889 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00001890 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001891 ret = hrtimer_try_to_cancel(&io->timer);
1892 if (ret != -1) {
1893 io_cqring_fill_event(link, -ECANCELED);
1894 io_commit_cqring(ctx);
1895 cancelled = true;
1896 }
1897 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001898 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01001899 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06001900
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001901 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001902 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01001903 io_put_req(link);
1904 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001905}
1906
Jens Axboe4d7dd462019-11-20 13:03:52 -07001907
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001908static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001909{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001910 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07001911 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001912 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06001913
Pavel Begunkovd148ca42020-10-18 10:17:39 +01001914 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001915 link = req->link;
1916 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06001917
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001918 while (link) {
1919 nxt = link->link;
1920 link->link = NULL;
1921
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001922 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001923 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001924
1925 /*
1926 * It's ok to free under spinlock as they're not linked anymore,
1927 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
1928 * work.fs->lock.
1929 */
1930 if (link->flags & REQ_F_WORK_INITIALIZED)
1931 io_put_req_deferred(link, 2);
1932 else
1933 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001934 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06001935 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001936 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001937 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001938
Jens Axboe2665abf2019-11-05 12:40:47 -07001939 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001940}
1941
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001942static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001943{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03001944 if (req->flags & REQ_F_LINK_TIMEOUT)
1945 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001946
Jens Axboe9e645e112019-05-10 16:07:28 -06001947 /*
1948 * If LINK is set, we have dependent requests in this chain. If we
1949 * didn't fail this request, queue the first one up, moving any other
1950 * dependencies to the next request. In case of failure, fail the rest
1951 * of the chain.
1952 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001953 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
1954 struct io_kiocb *nxt = req->link;
1955
1956 req->link = NULL;
1957 return nxt;
1958 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03001959 io_fail_links(req);
1960 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07001961}
Jens Axboe2665abf2019-11-05 12:40:47 -07001962
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001963static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001964{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00001965 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03001966 return NULL;
1967 return __io_req_find_next(req);
1968}
1969
Jens Axboe7cbf1722021-02-10 00:03:20 +00001970static bool __tctx_task_work(struct io_uring_task *tctx)
1971{
Jens Axboe65453d12021-02-10 00:03:21 +00001972 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001973 struct io_wq_work_list list;
1974 struct io_wq_work_node *node;
1975
1976 if (wq_list_empty(&tctx->task_list))
1977 return false;
1978
Jens Axboe0b81e802021-02-16 10:33:53 -07001979 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001980 list = tctx->task_list;
1981 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07001982 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00001983
1984 node = list.first;
1985 while (node) {
1986 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00001987 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001988 struct io_kiocb *req;
1989
1990 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00001991 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00001992 req->task_work.func(&req->task_work);
1993 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00001994
1995 if (!ctx) {
1996 ctx = this_ctx;
1997 } else if (ctx != this_ctx) {
1998 mutex_lock(&ctx->uring_lock);
1999 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2000 mutex_unlock(&ctx->uring_lock);
2001 ctx = this_ctx;
2002 }
2003 }
2004
2005 if (ctx && ctx->submit_state.comp.nr) {
2006 mutex_lock(&ctx->uring_lock);
2007 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2008 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002009 }
2010
2011 return list.first != NULL;
2012}
2013
2014static void tctx_task_work(struct callback_head *cb)
2015{
2016 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
2017
2018 while (__tctx_task_work(tctx))
2019 cond_resched();
2020
2021 clear_bit(0, &tctx->task_state);
2022}
2023
2024static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
2025 enum task_work_notify_mode notify)
2026{
2027 struct io_uring_task *tctx = tsk->io_uring;
2028 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07002029 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002030 int ret;
2031
2032 WARN_ON_ONCE(!tctx);
2033
Jens Axboe0b81e802021-02-16 10:33:53 -07002034 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002035 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002036 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002037
2038 /* task_work already pending, we're done */
2039 if (test_bit(0, &tctx->task_state) ||
2040 test_and_set_bit(0, &tctx->task_state))
2041 return 0;
2042
2043 if (!task_work_add(tsk, &tctx->task_work, notify))
2044 return 0;
2045
2046 /*
2047 * Slow path - we failed, find and delete work. if the work is not
2048 * in the list, it got run and we're fine.
2049 */
2050 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07002051 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002052 wq_list_for_each(node, prev, &tctx->task_list) {
2053 if (&req->io_task_work.node == node) {
2054 wq_list_del(&tctx->task_list, node, prev);
2055 ret = 1;
2056 break;
2057 }
2058 }
Jens Axboe0b81e802021-02-16 10:33:53 -07002059 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002060 clear_bit(0, &tctx->task_state);
2061 return ret;
2062}
2063
Jens Axboe355fb9e2020-10-22 20:19:35 -06002064static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002065{
2066 struct task_struct *tsk = req->task;
2067 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002068 enum task_work_notify_mode notify;
2069 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002070
Jens Axboe6200b0a2020-09-13 14:38:30 -06002071 if (tsk->flags & PF_EXITING)
2072 return -ESRCH;
2073
Jens Axboec2c4c832020-07-01 15:37:11 -06002074 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002075 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2076 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2077 * processing task_work. There's no reliable way to tell if TWA_RESUME
2078 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002079 */
Jens Axboe91989c72020-10-16 09:02:26 -06002080 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002081 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002082 notify = TWA_SIGNAL;
2083
Jens Axboe7cbf1722021-02-10 00:03:20 +00002084 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002085 if (!ret)
2086 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002087
Jens Axboec2c4c832020-07-01 15:37:11 -06002088 return ret;
2089}
2090
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002091static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002092 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002093{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07002094 struct io_ring_ctx *ctx = req->ctx;
2095 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002096
2097 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07002098 do {
2099 head = READ_ONCE(ctx->exit_task_work);
2100 req->task_work.next = head;
2101 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002102}
2103
Jens Axboec40f6372020-06-25 15:39:59 -06002104static void __io_req_task_cancel(struct io_kiocb *req, int error)
2105{
2106 struct io_ring_ctx *ctx = req->ctx;
2107
2108 spin_lock_irq(&ctx->completion_lock);
2109 io_cqring_fill_event(req, error);
2110 io_commit_cqring(ctx);
2111 spin_unlock_irq(&ctx->completion_lock);
2112
2113 io_cqring_ev_posted(ctx);
2114 req_set_fail_links(req);
2115 io_double_put_req(req);
2116}
2117
2118static void io_req_task_cancel(struct callback_head *cb)
2119{
2120 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002121 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002122
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002123 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002124 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002125 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002126 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002127}
2128
2129static void __io_req_task_submit(struct io_kiocb *req)
2130{
2131 struct io_ring_ctx *ctx = req->ctx;
2132
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002133 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002134 mutex_lock(&ctx->uring_lock);
Pavel Begunkovdc0eced2021-02-12 18:41:15 +00002135 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) &&
2136 !io_sq_thread_acquire_mm_files(ctx, req))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002137 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002138 else
Jens Axboec40f6372020-06-25 15:39:59 -06002139 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002140 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovaec18a52021-02-04 19:22:46 +00002141
2142 if (ctx->flags & IORING_SETUP_SQPOLL)
2143 io_sq_thread_drop_mm_files();
Jens Axboe9e645e112019-05-10 16:07:28 -06002144}
2145
Jens Axboec40f6372020-06-25 15:39:59 -06002146static void io_req_task_submit(struct callback_head *cb)
2147{
2148 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2149
2150 __io_req_task_submit(req);
2151}
2152
2153static void io_req_task_queue(struct io_kiocb *req)
2154{
Jens Axboec40f6372020-06-25 15:39:59 -06002155 int ret;
2156
Jens Axboe7cbf1722021-02-10 00:03:20 +00002157 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002158 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002159 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00002160 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002161 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002162 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002163 }
Jens Axboec40f6372020-06-25 15:39:59 -06002164}
2165
Pavel Begunkova3df76982021-02-18 22:32:52 +00002166static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2167{
2168 percpu_ref_get(&req->ctx->refs);
2169 req->result = ret;
2170 req->task_work.func = io_req_task_cancel;
2171
2172 if (unlikely(io_req_task_work_add(req)))
2173 io_req_task_work_add_fallback(req, io_req_task_cancel);
2174}
2175
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002176static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002177{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002178 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002179
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002180 if (nxt)
2181 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002182}
2183
Jens Axboe9e645e112019-05-10 16:07:28 -06002184static void io_free_req(struct io_kiocb *req)
2185{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002186 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002187 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002188}
2189
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002190struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002191 struct task_struct *task;
2192 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002193 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002194};
2195
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002196static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002197{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002198 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002199 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002200 rb->task = NULL;
2201}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002202
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002203static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2204 struct req_batch *rb)
2205{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002206 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002207 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002208 if (rb->ctx_refs)
2209 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002210}
2211
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002212static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2213 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002214{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002215 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002216
Jens Axboee3bc8e92020-09-24 08:45:57 -06002217 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002218 if (rb->task)
2219 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002220 rb->task = req->task;
2221 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002222 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002223 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002224 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002225
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002226 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002227 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002228 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002229 else
2230 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002231}
2232
Pavel Begunkov905c1722021-02-10 00:03:14 +00002233static void io_submit_flush_completions(struct io_comp_state *cs,
2234 struct io_ring_ctx *ctx)
2235{
2236 int i, nr = cs->nr;
2237 struct io_kiocb *req;
2238 struct req_batch rb;
2239
2240 io_init_req_batch(&rb);
2241 spin_lock_irq(&ctx->completion_lock);
2242 for (i = 0; i < nr; i++) {
2243 req = cs->reqs[i];
2244 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2245 }
2246 io_commit_cqring(ctx);
2247 spin_unlock_irq(&ctx->completion_lock);
2248
2249 io_cqring_ev_posted(ctx);
2250 for (i = 0; i < nr; i++) {
2251 req = cs->reqs[i];
2252
2253 /* submission and completion refs */
2254 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002255 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002256 }
2257
2258 io_req_free_batch_finish(ctx, &rb);
2259 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002260}
2261
Jens Axboeba816ad2019-09-28 11:36:45 -06002262/*
2263 * Drop reference to request, return next in chain (if there is one) if this
2264 * was the last reference to this request.
2265 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002266static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002267{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002268 struct io_kiocb *nxt = NULL;
2269
Jens Axboe2a44f462020-02-25 13:25:41 -07002270 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002271 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002272 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002273 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002274 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275}
2276
Jens Axboe2b188cc2019-01-07 10:46:33 -07002277static void io_put_req(struct io_kiocb *req)
2278{
Jens Axboedef596e2019-01-09 08:59:42 -07002279 if (refcount_dec_and_test(&req->refs))
2280 io_free_req(req);
2281}
2282
Pavel Begunkov216578e2020-10-13 09:44:00 +01002283static void io_put_req_deferred_cb(struct callback_head *cb)
2284{
2285 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2286
2287 io_free_req(req);
2288}
2289
2290static void io_free_req_deferred(struct io_kiocb *req)
2291{
2292 int ret;
2293
Jens Axboe7cbf1722021-02-10 00:03:20 +00002294 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002295 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002296 if (unlikely(ret))
2297 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002298}
2299
2300static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2301{
2302 if (refcount_sub_and_test(refs, &req->refs))
2303 io_free_req_deferred(req);
2304}
2305
Jens Axboe978db572019-11-14 22:39:04 -07002306static void io_double_put_req(struct io_kiocb *req)
2307{
2308 /* drop both submit and complete references */
2309 if (refcount_sub_and_test(2, &req->refs))
2310 io_free_req(req);
2311}
2312
Pavel Begunkov6c503152021-01-04 20:36:36 +00002313static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002314{
2315 /* See comment at the top of this file */
2316 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002317 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002318}
2319
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002320static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2321{
2322 struct io_rings *rings = ctx->rings;
2323
2324 /* make sure SQ entry isn't read before tail */
2325 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2326}
2327
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002328static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002329{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002330 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002331
Jens Axboebcda7ba2020-02-23 16:42:51 -07002332 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2333 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002334 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002335 kfree(kbuf);
2336 return cflags;
2337}
2338
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002339static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2340{
2341 struct io_buffer *kbuf;
2342
2343 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2344 return io_put_kbuf(req, kbuf);
2345}
2346
Jens Axboe4c6e2772020-07-01 11:29:10 -06002347static inline bool io_run_task_work(void)
2348{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002349 /*
2350 * Not safe to run on exiting task, and the task_work handling will
2351 * not add work to such a task.
2352 */
2353 if (unlikely(current->flags & PF_EXITING))
2354 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002355 if (current->task_works) {
2356 __set_current_state(TASK_RUNNING);
2357 task_work_run();
2358 return true;
2359 }
2360
2361 return false;
2362}
2363
Jens Axboedef596e2019-01-09 08:59:42 -07002364/*
2365 * Find and free completed poll iocbs
2366 */
2367static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2368 struct list_head *done)
2369{
Jens Axboe8237e042019-12-28 10:48:22 -07002370 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002371 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002372
2373 /* order with ->result store in io_complete_rw_iopoll() */
2374 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002375
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002376 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002377 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002378 int cflags = 0;
2379
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002380 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002381 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002382
Pavel Begunkovf1613402021-02-11 18:28:21 +00002383 if (READ_ONCE(req->result) == -EAGAIN) {
2384 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002385 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002386 continue;
2387 }
2388
Jens Axboebcda7ba2020-02-23 16:42:51 -07002389 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002390 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002391
2392 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002393 (*nr_events)++;
2394
Pavel Begunkovc3524382020-06-28 12:52:32 +03002395 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002396 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002397 }
Jens Axboedef596e2019-01-09 08:59:42 -07002398
Jens Axboe09bb8392019-03-13 12:39:28 -06002399 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002400 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002401 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002402}
2403
Jens Axboedef596e2019-01-09 08:59:42 -07002404static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2405 long min)
2406{
2407 struct io_kiocb *req, *tmp;
2408 LIST_HEAD(done);
2409 bool spin;
2410 int ret;
2411
2412 /*
2413 * Only spin for completions if we don't have multiple devices hanging
2414 * off our complete list, and we're under the requested amount.
2415 */
2416 spin = !ctx->poll_multi_file && *nr_events < min;
2417
2418 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002419 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002420 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002421
2422 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002423 * Move completed and retryable entries to our local lists.
2424 * If we find a request that requires polling, break out
2425 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002426 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002427 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002428 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002429 continue;
2430 }
2431 if (!list_empty(&done))
2432 break;
2433
2434 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2435 if (ret < 0)
2436 break;
2437
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002438 /* iopoll may have completed current req */
2439 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002440 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002441
Jens Axboedef596e2019-01-09 08:59:42 -07002442 if (ret && spin)
2443 spin = false;
2444 ret = 0;
2445 }
2446
2447 if (!list_empty(&done))
2448 io_iopoll_complete(ctx, nr_events, &done);
2449
2450 return ret;
2451}
2452
2453/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002454 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002455 * non-spinning poll check - we'll still enter the driver poll loop, but only
2456 * as a non-spinning completion check.
2457 */
2458static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2459 long min)
2460{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002461 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002462 int ret;
2463
2464 ret = io_do_iopoll(ctx, nr_events, min);
2465 if (ret < 0)
2466 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002467 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002468 return 0;
2469 }
2470
2471 return 1;
2472}
2473
2474/*
2475 * We can't just wait for polled events to come to us, we have to actively
2476 * find and complete them.
2477 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002478static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002479{
2480 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2481 return;
2482
2483 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002484 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002485 unsigned int nr_events = 0;
2486
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002487 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002488
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002489 /* let it sleep and repeat later if can't complete a request */
2490 if (nr_events == 0)
2491 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002492 /*
2493 * Ensure we allow local-to-the-cpu processing to take place,
2494 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002495 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002496 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002497 if (need_resched()) {
2498 mutex_unlock(&ctx->uring_lock);
2499 cond_resched();
2500 mutex_lock(&ctx->uring_lock);
2501 }
Jens Axboedef596e2019-01-09 08:59:42 -07002502 }
2503 mutex_unlock(&ctx->uring_lock);
2504}
2505
Pavel Begunkov7668b922020-07-07 16:36:21 +03002506static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002507{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002508 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002509 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002510
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002511 /*
2512 * We disallow the app entering submit/complete with polling, but we
2513 * still need to lock the ring to prevent racing with polled issue
2514 * that got punted to a workqueue.
2515 */
2516 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002517 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002518 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002519 * Don't enter poll loop if we already have events pending.
2520 * If we do, we can potentially be spinning for commands that
2521 * already triggered a CQE (eg in error).
2522 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002523 if (test_bit(0, &ctx->cq_check_overflow))
2524 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2525 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002526 break;
2527
2528 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002529 * If a submit got punted to a workqueue, we can have the
2530 * application entering polling for a command before it gets
2531 * issued. That app will hold the uring_lock for the duration
2532 * of the poll right here, so we need to take a breather every
2533 * now and then to ensure that the issue has a chance to add
2534 * the poll to the issued list. Otherwise we can spin here
2535 * forever, while the workqueue is stuck trying to acquire the
2536 * very same mutex.
2537 */
2538 if (!(++iters & 7)) {
2539 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002540 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002541 mutex_lock(&ctx->uring_lock);
2542 }
2543
Pavel Begunkov7668b922020-07-07 16:36:21 +03002544 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002545 if (ret <= 0)
2546 break;
2547 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002548 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002549
Jens Axboe500f9fb2019-08-19 12:15:59 -06002550 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002551 return ret;
2552}
2553
Jens Axboe491381ce2019-10-17 09:20:46 -06002554static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555{
Jens Axboe491381ce2019-10-17 09:20:46 -06002556 /*
2557 * Tell lockdep we inherited freeze protection from submission
2558 * thread.
2559 */
2560 if (req->flags & REQ_F_ISREG) {
2561 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002562
Jens Axboe491381ce2019-10-17 09:20:46 -06002563 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002564 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002565 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566}
2567
Jens Axboeb63534c2020-06-04 11:28:00 -06002568#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002569static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002570{
2571 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002572 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002573 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002574
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002575 /* already prepared */
2576 if (req->async_data)
2577 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002578
2579 switch (req->opcode) {
2580 case IORING_OP_READV:
2581 case IORING_OP_READ_FIXED:
2582 case IORING_OP_READ:
2583 rw = READ;
2584 break;
2585 case IORING_OP_WRITEV:
2586 case IORING_OP_WRITE_FIXED:
2587 case IORING_OP_WRITE:
2588 rw = WRITE;
2589 break;
2590 default:
2591 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2592 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002593 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002594 }
2595
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002596 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2597 if (ret < 0)
2598 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002599 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002600}
Jens Axboeb63534c2020-06-04 11:28:00 -06002601#endif
2602
Pavel Begunkov23faba32021-02-11 18:28:22 +00002603static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002604{
2605#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002606 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002607 int ret;
2608
Jens Axboe355afae2020-09-02 09:30:31 -06002609 if (!S_ISBLK(mode) && !S_ISREG(mode))
2610 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002611 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002612 return false;
2613
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002614 lockdep_assert_held(&req->ctx->uring_lock);
2615
Jens Axboe28cea78a2020-09-14 10:51:17 -06002616 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002617
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002618 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002619 refcount_inc(&req->refs);
2620 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002621 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002622 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002623 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002624#endif
2625 return false;
2626}
2627
Jens Axboea1d7c392020-06-22 11:09:46 -06002628static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002629 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002630{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002631 int cflags = 0;
2632
Pavel Begunkov23faba32021-02-11 18:28:22 +00002633 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2634 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002635 if (res != req->result)
2636 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002637
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002638 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2639 kiocb_end_write(req);
2640 if (req->flags & REQ_F_BUFFER_SELECTED)
2641 cflags = io_put_rw_kbuf(req);
2642 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002643}
2644
2645static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2646{
Jens Axboe9adbd452019-12-20 08:45:55 -07002647 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002648
Pavel Begunkov889fca72021-02-10 00:03:09 +00002649 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650}
2651
Jens Axboedef596e2019-01-09 08:59:42 -07002652static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2653{
Jens Axboe9adbd452019-12-20 08:45:55 -07002654 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002655
Jens Axboe491381ce2019-10-17 09:20:46 -06002656 if (kiocb->ki_flags & IOCB_WRITE)
2657 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002658
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002659 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002660 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002661
2662 WRITE_ONCE(req->result, res);
2663 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002664 smp_wmb();
2665 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002666}
2667
2668/*
2669 * After the iocb has been issued, it's safe to be found on the poll list.
2670 * Adding the kiocb to the list AFTER submission ensures that we don't
2671 * find it from a io_iopoll_getevents() thread before the issuer is done
2672 * accessing the kiocb cookie.
2673 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002674static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002675{
2676 struct io_ring_ctx *ctx = req->ctx;
2677
2678 /*
2679 * Track whether we have multiple files in our lists. This will impact
2680 * how we do polling eventually, not spinning if we're on potentially
2681 * different devices.
2682 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002683 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002684 ctx->poll_multi_file = false;
2685 } else if (!ctx->poll_multi_file) {
2686 struct io_kiocb *list_req;
2687
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002688 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002689 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002690 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002691 ctx->poll_multi_file = true;
2692 }
2693
2694 /*
2695 * For fast devices, IO may have already completed. If it has, add
2696 * it to the front so we find it first.
2697 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002698 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002699 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002700 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002701 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002702
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002703 /*
2704 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2705 * task context or in io worker task context. If current task context is
2706 * sq thread, we don't need to check whether should wake up sq thread.
2707 */
2708 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002709 wq_has_sleeper(&ctx->sq_data->wait))
2710 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002711}
2712
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002713static inline void io_state_file_put(struct io_submit_state *state)
2714{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002715 if (state->file_refs) {
2716 fput_many(state->file, state->file_refs);
2717 state->file_refs = 0;
2718 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002719}
2720
2721/*
2722 * Get as many references to a file as we have IOs left in this submission,
2723 * assuming most submissions are for one file, or at least that each file
2724 * has more than one submission.
2725 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002726static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002727{
2728 if (!state)
2729 return fget(fd);
2730
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002731 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002732 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002733 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002734 return state->file;
2735 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002736 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002737 }
2738 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002739 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002740 return NULL;
2741
2742 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002743 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002744 return state->file;
2745}
2746
Jens Axboe4503b762020-06-01 10:00:27 -06002747static bool io_bdev_nowait(struct block_device *bdev)
2748{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002749 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002750}
2751
Jens Axboe2b188cc2019-01-07 10:46:33 -07002752/*
2753 * If we tracked the file through the SCM inflight mechanism, we could support
2754 * any file. For now, just ensure that anything potentially problematic is done
2755 * inline.
2756 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002757static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002758{
2759 umode_t mode = file_inode(file)->i_mode;
2760
Jens Axboe4503b762020-06-01 10:00:27 -06002761 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002762 if (IS_ENABLED(CONFIG_BLOCK) &&
2763 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06002764 return true;
2765 return false;
2766 }
2767 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06002769 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01002770 if (IS_ENABLED(CONFIG_BLOCK) &&
2771 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06002772 file->f_op != &io_uring_fops)
2773 return true;
2774 return false;
2775 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002776
Jens Axboec5b85622020-06-09 19:23:05 -06002777 /* any ->read/write should understand O_NONBLOCK */
2778 if (file->f_flags & O_NONBLOCK)
2779 return true;
2780
Jens Axboeaf197f52020-04-28 13:15:06 -06002781 if (!(file->f_mode & FMODE_NOWAIT))
2782 return false;
2783
2784 if (rw == READ)
2785 return file->f_op->read_iter != NULL;
2786
2787 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788}
2789
Pavel Begunkova88fc402020-09-30 22:57:53 +03002790static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002791{
Jens Axboedef596e2019-01-09 08:59:42 -07002792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07002793 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002794 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06002795 unsigned ioprio;
2796 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002797
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002798 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06002799 req->flags |= REQ_F_ISREG;
2800
Jens Axboe2b188cc2019-01-07 10:46:33 -07002801 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002802 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07002803 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002804 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07002805 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03002807 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
2808 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
2809 if (unlikely(ret))
2810 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002811
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002812 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
2813 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
2814 req->flags |= REQ_F_NOWAIT;
2815
Jens Axboe2b188cc2019-01-07 10:46:33 -07002816 ioprio = READ_ONCE(sqe->ioprio);
2817 if (ioprio) {
2818 ret = ioprio_check_cap(ioprio);
2819 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06002820 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002821
2822 kiocb->ki_ioprio = ioprio;
2823 } else
2824 kiocb->ki_ioprio = get_current_ioprio();
2825
Jens Axboedef596e2019-01-09 08:59:42 -07002826 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07002827 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
2828 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06002829 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002830
Jens Axboedef596e2019-01-09 08:59:42 -07002831 kiocb->ki_flags |= IOCB_HIPRI;
2832 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002833 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002834 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06002835 if (kiocb->ki_flags & IOCB_HIPRI)
2836 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07002837 kiocb->ki_complete = io_complete_rw;
2838 }
Jens Axboe9adbd452019-12-20 08:45:55 -07002839
Jens Axboe3529d8c2019-12-19 18:24:38 -07002840 req->rw.addr = READ_ONCE(sqe->addr);
2841 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07002842 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002843 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002844}
2845
2846static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
2847{
2848 switch (ret) {
2849 case -EIOCBQUEUED:
2850 break;
2851 case -ERESTARTSYS:
2852 case -ERESTARTNOINTR:
2853 case -ERESTARTNOHAND:
2854 case -ERESTART_RESTARTBLOCK:
2855 /*
2856 * We can't just restart the syscall, since previously
2857 * submitted sqes may already be in progress. Just fail this
2858 * IO with EINTR.
2859 */
2860 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002861 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862 default:
2863 kiocb->ki_complete(kiocb, ret, 0);
2864 }
2865}
2866
Jens Axboea1d7c392020-06-22 11:09:46 -06002867static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002868 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06002869{
Jens Axboeba042912019-12-25 16:33:42 -07002870 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07002871 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07002872
Jens Axboe227c0c92020-08-13 11:51:40 -06002873 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07002874 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06002875 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07002876 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002877 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07002878 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06002879 }
2880
Jens Axboeba042912019-12-25 16:33:42 -07002881 if (req->flags & REQ_F_CUR_POS)
2882 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002883 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00002884 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002885 else
2886 io_rw_done(kiocb, ret);
2887}
2888
Pavel Begunkov847595d2021-02-04 13:52:06 +00002889static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002890{
Jens Axboe9adbd452019-12-20 08:45:55 -07002891 struct io_ring_ctx *ctx = req->ctx;
2892 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002893 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03002894 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07002895 size_t offset;
2896 u64 buf_addr;
2897
Jens Axboeedafcce2019-01-09 09:16:05 -07002898 if (unlikely(buf_index >= ctx->nr_user_bufs))
2899 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07002900 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2901 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002902 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002903
2904 /* overflow */
2905 if (buf_addr + len < buf_addr)
2906 return -EFAULT;
2907 /* not inside the mapped region */
2908 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2909 return -EFAULT;
2910
2911 /*
2912 * May not be a start of buffer, set size appropriately
2913 * and advance us to the beginning.
2914 */
2915 offset = buf_addr - imu->ubuf;
2916 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002917
2918 if (offset) {
2919 /*
2920 * Don't use iov_iter_advance() here, as it's really slow for
2921 * using the latter parts of a big fixed buffer - it iterates
2922 * over each segment manually. We can cheat a bit here, because
2923 * we know that:
2924 *
2925 * 1) it's a BVEC iter, we set it up
2926 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2927 * first and last bvec
2928 *
2929 * So just find our index, and adjust the iterator afterwards.
2930 * If the offset is within the first bvec (or the whole first
2931 * bvec, just use iov_iter_advance(). This makes it easier
2932 * since we can just skip the first segment, which may not
2933 * be PAGE_SIZE aligned.
2934 */
2935 const struct bio_vec *bvec = imu->bvec;
2936
2937 if (offset <= bvec->bv_len) {
2938 iov_iter_advance(iter, offset);
2939 } else {
2940 unsigned long seg_skip;
2941
2942 /* skip first vec */
2943 offset -= bvec->bv_len;
2944 seg_skip = 1 + (offset >> PAGE_SHIFT);
2945
2946 iter->bvec = bvec + seg_skip;
2947 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002948 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002949 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002950 }
2951 }
2952
Pavel Begunkov847595d2021-02-04 13:52:06 +00002953 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07002954}
2955
Jens Axboebcda7ba2020-02-23 16:42:51 -07002956static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
2957{
2958 if (needs_lock)
2959 mutex_unlock(&ctx->uring_lock);
2960}
2961
2962static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
2963{
2964 /*
2965 * "Normal" inline submissions always hold the uring_lock, since we
2966 * grab it from the system call. Same is true for the SQPOLL offload.
2967 * The only exception is when we've detached the request and issue it
2968 * from an async worker thread, grab the lock for that case.
2969 */
2970 if (needs_lock)
2971 mutex_lock(&ctx->uring_lock);
2972}
2973
2974static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
2975 int bgid, struct io_buffer *kbuf,
2976 bool needs_lock)
2977{
2978 struct io_buffer *head;
2979
2980 if (req->flags & REQ_F_BUFFER_SELECTED)
2981 return kbuf;
2982
2983 io_ring_submit_lock(req->ctx, needs_lock);
2984
2985 lockdep_assert_held(&req->ctx->uring_lock);
2986
2987 head = idr_find(&req->ctx->io_buffer_idr, bgid);
2988 if (head) {
2989 if (!list_empty(&head->list)) {
2990 kbuf = list_last_entry(&head->list, struct io_buffer,
2991 list);
2992 list_del(&kbuf->list);
2993 } else {
2994 kbuf = head;
2995 idr_remove(&req->ctx->io_buffer_idr, bgid);
2996 }
2997 if (*len > kbuf->len)
2998 *len = kbuf->len;
2999 } else {
3000 kbuf = ERR_PTR(-ENOBUFS);
3001 }
3002
3003 io_ring_submit_unlock(req->ctx, needs_lock);
3004
3005 return kbuf;
3006}
3007
Jens Axboe4d954c22020-02-27 07:31:19 -07003008static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3009 bool needs_lock)
3010{
3011 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003012 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003013
3014 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003015 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003016 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3017 if (IS_ERR(kbuf))
3018 return kbuf;
3019 req->rw.addr = (u64) (unsigned long) kbuf;
3020 req->flags |= REQ_F_BUFFER_SELECTED;
3021 return u64_to_user_ptr(kbuf->addr);
3022}
3023
3024#ifdef CONFIG_COMPAT
3025static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3026 bool needs_lock)
3027{
3028 struct compat_iovec __user *uiov;
3029 compat_ssize_t clen;
3030 void __user *buf;
3031 ssize_t len;
3032
3033 uiov = u64_to_user_ptr(req->rw.addr);
3034 if (!access_ok(uiov, sizeof(*uiov)))
3035 return -EFAULT;
3036 if (__get_user(clen, &uiov->iov_len))
3037 return -EFAULT;
3038 if (clen < 0)
3039 return -EINVAL;
3040
3041 len = clen;
3042 buf = io_rw_buffer_select(req, &len, needs_lock);
3043 if (IS_ERR(buf))
3044 return PTR_ERR(buf);
3045 iov[0].iov_base = buf;
3046 iov[0].iov_len = (compat_size_t) len;
3047 return 0;
3048}
3049#endif
3050
3051static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3052 bool needs_lock)
3053{
3054 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3055 void __user *buf;
3056 ssize_t len;
3057
3058 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3059 return -EFAULT;
3060
3061 len = iov[0].iov_len;
3062 if (len < 0)
3063 return -EINVAL;
3064 buf = io_rw_buffer_select(req, &len, needs_lock);
3065 if (IS_ERR(buf))
3066 return PTR_ERR(buf);
3067 iov[0].iov_base = buf;
3068 iov[0].iov_len = len;
3069 return 0;
3070}
3071
3072static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3073 bool needs_lock)
3074{
Jens Axboedddb3e22020-06-04 11:27:01 -06003075 if (req->flags & REQ_F_BUFFER_SELECTED) {
3076 struct io_buffer *kbuf;
3077
3078 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3079 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3080 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003081 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003082 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003083 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003084 return -EINVAL;
3085
3086#ifdef CONFIG_COMPAT
3087 if (req->ctx->compat)
3088 return io_compat_import(req, iov, needs_lock);
3089#endif
3090
3091 return __io_iov_buffer_select(req, iov, needs_lock);
3092}
3093
Pavel Begunkov847595d2021-02-04 13:52:06 +00003094static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3095 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003096{
Jens Axboe9adbd452019-12-20 08:45:55 -07003097 void __user *buf = u64_to_user_ptr(req->rw.addr);
3098 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003099 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003100 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003101
Pavel Begunkov7d009162019-11-25 23:14:40 +03003102 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003103 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003104 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003105 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003106
Jens Axboebcda7ba2020-02-23 16:42:51 -07003107 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003108 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003109 return -EINVAL;
3110
Jens Axboe3a6820f2019-12-22 15:19:35 -07003111 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003112 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003113 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003114 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003115 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003116 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003117 }
3118
Jens Axboe3a6820f2019-12-22 15:19:35 -07003119 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3120 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003121 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003122 }
3123
Jens Axboe4d954c22020-02-27 07:31:19 -07003124 if (req->flags & REQ_F_BUFFER_SELECT) {
3125 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003126 if (!ret)
3127 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003128 *iovec = NULL;
3129 return ret;
3130 }
3131
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003132 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3133 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003134}
3135
Jens Axboe0fef9482020-08-26 10:36:20 -06003136static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3137{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003138 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003139}
3140
Jens Axboe32960612019-09-23 11:05:34 -06003141/*
3142 * For files that don't have ->read_iter() and ->write_iter(), handle them
3143 * by looping over ->read() or ->write() manually.
3144 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003145static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003146{
Jens Axboe4017eb92020-10-22 14:14:12 -06003147 struct kiocb *kiocb = &req->rw.kiocb;
3148 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003149 ssize_t ret = 0;
3150
3151 /*
3152 * Don't support polled IO through this interface, and we can't
3153 * support non-blocking either. For the latter, this just causes
3154 * the kiocb to be handled from an async context.
3155 */
3156 if (kiocb->ki_flags & IOCB_HIPRI)
3157 return -EOPNOTSUPP;
3158 if (kiocb->ki_flags & IOCB_NOWAIT)
3159 return -EAGAIN;
3160
3161 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003162 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003163 ssize_t nr;
3164
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003165 if (!iov_iter_is_bvec(iter)) {
3166 iovec = iov_iter_iovec(iter);
3167 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003168 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3169 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003170 }
3171
Jens Axboe32960612019-09-23 11:05:34 -06003172 if (rw == READ) {
3173 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003174 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003175 } else {
3176 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003177 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003178 }
3179
3180 if (nr < 0) {
3181 if (!ret)
3182 ret = nr;
3183 break;
3184 }
3185 ret += nr;
3186 if (nr != iovec.iov_len)
3187 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003188 req->rw.len -= nr;
3189 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003190 iov_iter_advance(iter, nr);
3191 }
3192
3193 return ret;
3194}
3195
Jens Axboeff6165b2020-08-13 09:47:43 -06003196static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3197 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003198{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003199 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003200
Jens Axboeff6165b2020-08-13 09:47:43 -06003201 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003202 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003203 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003204 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003205 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003206 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003207 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003208 unsigned iov_off = 0;
3209
3210 rw->iter.iov = rw->fast_iov;
3211 if (iter->iov != fast_iov) {
3212 iov_off = iter->iov - fast_iov;
3213 rw->iter.iov += iov_off;
3214 }
3215 if (rw->fast_iov != fast_iov)
3216 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003217 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003218 } else {
3219 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003220 }
3221}
3222
Jens Axboee8c2bc12020-08-15 18:44:09 -07003223static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003224{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003225 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3226 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3227 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003228}
3229
Jens Axboee8c2bc12020-08-15 18:44:09 -07003230static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003231{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003232 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003233 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003234
Jens Axboee8c2bc12020-08-15 18:44:09 -07003235 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003236}
3237
Jens Axboeff6165b2020-08-13 09:47:43 -06003238static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3239 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003240 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003241{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003242 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003243 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003244 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003245 if (__io_alloc_async_data(req)) {
3246 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003247 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003248 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003249
Jens Axboeff6165b2020-08-13 09:47:43 -06003250 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003251 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003252 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003253}
3254
Pavel Begunkov73debe62020-09-30 22:57:54 +03003255static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003256{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003257 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003258 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003259 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003260
Pavel Begunkov2846c482020-11-07 13:16:27 +00003261 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003262 if (unlikely(ret < 0))
3263 return ret;
3264
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003265 iorw->bytes_done = 0;
3266 iorw->free_iovec = iov;
3267 if (iov)
3268 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003269 return 0;
3270}
3271
Pavel Begunkov73debe62020-09-30 22:57:54 +03003272static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003273{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003274 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3275 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003276 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003277}
3278
Jens Axboec1dd91d2020-08-03 16:43:59 -06003279/*
3280 * This is our waitqueue callback handler, registered through lock_page_async()
3281 * when we initially tried to do the IO with the iocb armed our waitqueue.
3282 * This gets called when the page is unlocked, and we generally expect that to
3283 * happen when the page IO is completed and the page is now uptodate. This will
3284 * queue a task_work based retry of the operation, attempting to copy the data
3285 * again. If the latter fails because the page was NOT uptodate, then we will
3286 * do a thread based blocking retry of the operation. That's the unexpected
3287 * slow path.
3288 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003289static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3290 int sync, void *arg)
3291{
3292 struct wait_page_queue *wpq;
3293 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003294 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003295
3296 wpq = container_of(wait, struct wait_page_queue, wait);
3297
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003298 if (!wake_page_match(wpq, key))
3299 return 0;
3300
Hao Xuc8d317a2020-09-29 20:00:45 +08003301 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003302 list_del_init(&wait->entry);
3303
Jens Axboebcf5a062020-05-22 09:24:42 -06003304 /* submit ref gets dropped, acquire a new one */
3305 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003306 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003307 return 1;
3308}
3309
Jens Axboec1dd91d2020-08-03 16:43:59 -06003310/*
3311 * This controls whether a given IO request should be armed for async page
3312 * based retry. If we return false here, the request is handed to the async
3313 * worker threads for retry. If we're doing buffered reads on a regular file,
3314 * we prepare a private wait_page_queue entry and retry the operation. This
3315 * will either succeed because the page is now uptodate and unlocked, or it
3316 * will register a callback when the page is unlocked at IO completion. Through
3317 * that callback, io_uring uses task_work to setup a retry of the operation.
3318 * That retry will attempt the buffered read again. The retry will generally
3319 * succeed, or in rare cases where it fails, we then fall back to using the
3320 * async worker threads for a blocking retry.
3321 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003322static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003323{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003324 struct io_async_rw *rw = req->async_data;
3325 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003326 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003327
3328 /* never retry for NOWAIT, we just complete with -EAGAIN */
3329 if (req->flags & REQ_F_NOWAIT)
3330 return false;
3331
Jens Axboe227c0c92020-08-13 11:51:40 -06003332 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003333 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003334 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003335
Jens Axboebcf5a062020-05-22 09:24:42 -06003336 /*
3337 * just use poll if we can, and don't attempt if the fs doesn't
3338 * support callback based unlocks
3339 */
3340 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3341 return false;
3342
Jens Axboe3b2a4432020-08-16 10:58:43 -07003343 wait->wait.func = io_async_buf_func;
3344 wait->wait.private = req;
3345 wait->wait.flags = 0;
3346 INIT_LIST_HEAD(&wait->wait.entry);
3347 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003348 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003349 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003350 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003351}
3352
3353static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3354{
3355 if (req->file->f_op->read_iter)
3356 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003357 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003358 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003359 else
3360 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003361}
3362
Pavel Begunkov889fca72021-02-10 00:03:09 +00003363static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003364{
3365 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003366 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003367 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003368 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003369 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003370 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003371
Pavel Begunkov2846c482020-11-07 13:16:27 +00003372 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003373 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003374 iovec = NULL;
3375 } else {
3376 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3377 if (ret < 0)
3378 return ret;
3379 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003380 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003381 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003382
Jens Axboefd6c2e42019-12-18 12:19:41 -07003383 /* Ensure we clear previously set non-block flag */
3384 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003385 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003386 else
3387 kiocb->ki_flags |= IOCB_NOWAIT;
3388
Pavel Begunkov24c74672020-06-21 13:09:51 +03003389 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003390 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3391 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003392 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003393 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003394
Pavel Begunkov632546c2020-11-07 13:16:26 +00003395 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003396 if (unlikely(ret)) {
3397 kfree(iovec);
3398 return ret;
3399 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003400
Jens Axboe227c0c92020-08-13 11:51:40 -06003401 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003402
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003403 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003404 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003405 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003406 /* IOPOLL retry should happen for io-wq threads */
3407 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003408 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003409 /* no retry on NONBLOCK nor RWF_NOWAIT */
3410 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003411 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003412 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003413 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003414 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003415 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003416 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003417 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003418 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003419 }
3420
Jens Axboe227c0c92020-08-13 11:51:40 -06003421 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003422 if (ret2)
3423 return ret2;
3424
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003425 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003426 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003427 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003428 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003429
Pavel Begunkovb23df912021-02-04 13:52:04 +00003430 do {
3431 io_size -= ret;
3432 rw->bytes_done += ret;
3433 /* if we can retry, do so with the callbacks armed */
3434 if (!io_rw_should_retry(req)) {
3435 kiocb->ki_flags &= ~IOCB_WAITQ;
3436 return -EAGAIN;
3437 }
3438
3439 /*
3440 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3441 * we get -EIOCBQUEUED, then we'll get a notification when the
3442 * desired page gets unlocked. We can also get a partial read
3443 * here, and if we do, then just retry at the new offset.
3444 */
3445 ret = io_iter_do_read(req, iter);
3446 if (ret == -EIOCBQUEUED)
3447 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003448 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003449 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003450done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003451 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003452out_free:
3453 /* it's faster to check here then delegate to kfree */
3454 if (iovec)
3455 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003456 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003457}
3458
Pavel Begunkov73debe62020-09-30 22:57:54 +03003459static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003460{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003461 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3462 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003463 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003464}
3465
Pavel Begunkov889fca72021-02-10 00:03:09 +00003466static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003467{
3468 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003469 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003470 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003471 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003472 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003473 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003474
Pavel Begunkov2846c482020-11-07 13:16:27 +00003475 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003476 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003477 iovec = NULL;
3478 } else {
3479 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3480 if (ret < 0)
3481 return ret;
3482 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003483 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003484 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003485
Jens Axboefd6c2e42019-12-18 12:19:41 -07003486 /* Ensure we clear previously set non-block flag */
3487 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003488 kiocb->ki_flags &= ~IOCB_NOWAIT;
3489 else
3490 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003491
Pavel Begunkov24c74672020-06-21 13:09:51 +03003492 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003493 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003494 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003495
Jens Axboe10d59342019-12-09 20:16:22 -07003496 /* file path doesn't support NOWAIT for non-direct_IO */
3497 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3498 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003499 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003500
Pavel Begunkov632546c2020-11-07 13:16:26 +00003501 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003502 if (unlikely(ret))
3503 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003504
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003505 /*
3506 * Open-code file_start_write here to grab freeze protection,
3507 * which will be released by another thread in
3508 * io_complete_rw(). Fool lockdep by telling it the lock got
3509 * released so that it doesn't complain about the held lock when
3510 * we return to userspace.
3511 */
3512 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003513 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003514 __sb_writers_release(file_inode(req->file)->i_sb,
3515 SB_FREEZE_WRITE);
3516 }
3517 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003518
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003519 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003520 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003521 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003522 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003523 else
3524 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003525
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003526 /*
3527 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3528 * retry them without IOCB_NOWAIT.
3529 */
3530 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3531 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003532 /* no retry on NONBLOCK nor RWF_NOWAIT */
3533 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003534 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003535 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003536 /* IOPOLL retry should happen for io-wq threads */
3537 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3538 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003539done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003540 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003541 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003542copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003543 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003544 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003545 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003546 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003547 }
Jens Axboe31b51512019-01-18 22:56:34 -07003548out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003549 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003550 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003551 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003552 return ret;
3553}
3554
Jens Axboe80a261f2020-09-28 14:23:58 -06003555static int io_renameat_prep(struct io_kiocb *req,
3556 const struct io_uring_sqe *sqe)
3557{
3558 struct io_rename *ren = &req->rename;
3559 const char __user *oldf, *newf;
3560
3561 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3562 return -EBADF;
3563
3564 ren->old_dfd = READ_ONCE(sqe->fd);
3565 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3566 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3567 ren->new_dfd = READ_ONCE(sqe->len);
3568 ren->flags = READ_ONCE(sqe->rename_flags);
3569
3570 ren->oldpath = getname(oldf);
3571 if (IS_ERR(ren->oldpath))
3572 return PTR_ERR(ren->oldpath);
3573
3574 ren->newpath = getname(newf);
3575 if (IS_ERR(ren->newpath)) {
3576 putname(ren->oldpath);
3577 return PTR_ERR(ren->newpath);
3578 }
3579
3580 req->flags |= REQ_F_NEED_CLEANUP;
3581 return 0;
3582}
3583
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003584static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003585{
3586 struct io_rename *ren = &req->rename;
3587 int ret;
3588
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003589 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003590 return -EAGAIN;
3591
3592 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3593 ren->newpath, ren->flags);
3594
3595 req->flags &= ~REQ_F_NEED_CLEANUP;
3596 if (ret < 0)
3597 req_set_fail_links(req);
3598 io_req_complete(req, ret);
3599 return 0;
3600}
3601
Jens Axboe14a11432020-09-28 14:27:37 -06003602static int io_unlinkat_prep(struct io_kiocb *req,
3603 const struct io_uring_sqe *sqe)
3604{
3605 struct io_unlink *un = &req->unlink;
3606 const char __user *fname;
3607
3608 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3609 return -EBADF;
3610
3611 un->dfd = READ_ONCE(sqe->fd);
3612
3613 un->flags = READ_ONCE(sqe->unlink_flags);
3614 if (un->flags & ~AT_REMOVEDIR)
3615 return -EINVAL;
3616
3617 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3618 un->filename = getname(fname);
3619 if (IS_ERR(un->filename))
3620 return PTR_ERR(un->filename);
3621
3622 req->flags |= REQ_F_NEED_CLEANUP;
3623 return 0;
3624}
3625
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003626static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003627{
3628 struct io_unlink *un = &req->unlink;
3629 int ret;
3630
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003631 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003632 return -EAGAIN;
3633
3634 if (un->flags & AT_REMOVEDIR)
3635 ret = do_rmdir(un->dfd, un->filename);
3636 else
3637 ret = do_unlinkat(un->dfd, un->filename);
3638
3639 req->flags &= ~REQ_F_NEED_CLEANUP;
3640 if (ret < 0)
3641 req_set_fail_links(req);
3642 io_req_complete(req, ret);
3643 return 0;
3644}
3645
Jens Axboe36f4fa62020-09-05 11:14:22 -06003646static int io_shutdown_prep(struct io_kiocb *req,
3647 const struct io_uring_sqe *sqe)
3648{
3649#if defined(CONFIG_NET)
3650 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3651 return -EINVAL;
3652 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3653 sqe->buf_index)
3654 return -EINVAL;
3655
3656 req->shutdown.how = READ_ONCE(sqe->len);
3657 return 0;
3658#else
3659 return -EOPNOTSUPP;
3660#endif
3661}
3662
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003663static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003664{
3665#if defined(CONFIG_NET)
3666 struct socket *sock;
3667 int ret;
3668
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003669 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003670 return -EAGAIN;
3671
Linus Torvalds48aba792020-12-16 12:44:05 -08003672 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003673 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003674 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003675
3676 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003677 if (ret < 0)
3678 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003679 io_req_complete(req, ret);
3680 return 0;
3681#else
3682 return -EOPNOTSUPP;
3683#endif
3684}
3685
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003686static int __io_splice_prep(struct io_kiocb *req,
3687 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003688{
3689 struct io_splice* sp = &req->splice;
3690 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003691
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003692 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3693 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003694
3695 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003696 sp->len = READ_ONCE(sqe->len);
3697 sp->flags = READ_ONCE(sqe->splice_flags);
3698
3699 if (unlikely(sp->flags & ~valid_flags))
3700 return -EINVAL;
3701
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003702 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3703 (sp->flags & SPLICE_F_FD_IN_FIXED));
3704 if (!sp->file_in)
3705 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003706 req->flags |= REQ_F_NEED_CLEANUP;
3707
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003708 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3709 /*
3710 * Splice operation will be punted aync, and here need to
3711 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3712 */
3713 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003714 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003715 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003716
3717 return 0;
3718}
3719
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003720static int io_tee_prep(struct io_kiocb *req,
3721 const struct io_uring_sqe *sqe)
3722{
3723 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3724 return -EINVAL;
3725 return __io_splice_prep(req, sqe);
3726}
3727
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003728static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003729{
3730 struct io_splice *sp = &req->splice;
3731 struct file *in = sp->file_in;
3732 struct file *out = sp->file_out;
3733 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3734 long ret = 0;
3735
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003736 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003737 return -EAGAIN;
3738 if (sp->len)
3739 ret = do_tee(in, out, sp->len, flags);
3740
3741 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3742 req->flags &= ~REQ_F_NEED_CLEANUP;
3743
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003744 if (ret != sp->len)
3745 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003746 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003747 return 0;
3748}
3749
3750static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3751{
3752 struct io_splice* sp = &req->splice;
3753
3754 sp->off_in = READ_ONCE(sqe->splice_off_in);
3755 sp->off_out = READ_ONCE(sqe->off);
3756 return __io_splice_prep(req, sqe);
3757}
3758
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003759static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003760{
3761 struct io_splice *sp = &req->splice;
3762 struct file *in = sp->file_in;
3763 struct file *out = sp->file_out;
3764 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3765 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003766 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003767
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003768 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03003769 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003770
3771 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
3772 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03003773
Jens Axboe948a7742020-05-17 14:21:38 -06003774 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03003775 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003776
3777 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3778 req->flags &= ~REQ_F_NEED_CLEANUP;
3779
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003780 if (ret != sp->len)
3781 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003782 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003783 return 0;
3784}
3785
Jens Axboe2b188cc2019-01-07 10:46:33 -07003786/*
3787 * IORING_OP_NOP just posts a completion event, nothing else.
3788 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00003789static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003790{
3791 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003792
Jens Axboedef596e2019-01-09 08:59:42 -07003793 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3794 return -EINVAL;
3795
Pavel Begunkov889fca72021-02-10 00:03:09 +00003796 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003797 return 0;
3798}
3799
Pavel Begunkov1155c762021-02-18 18:29:38 +00003800static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003801{
Jens Axboe6b063142019-01-10 22:13:58 -07003802 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003803
Jens Axboe09bb8392019-03-13 12:39:28 -06003804 if (!req->file)
3805 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003806
Jens Axboe6b063142019-01-10 22:13:58 -07003807 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07003808 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07003809 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003810 return -EINVAL;
3811
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003812 req->sync.flags = READ_ONCE(sqe->fsync_flags);
3813 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
3814 return -EINVAL;
3815
3816 req->sync.off = READ_ONCE(sqe->off);
3817 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003818 return 0;
3819}
3820
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003821static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07003822{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003823 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003824 int ret;
3825
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003826 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003827 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003828 return -EAGAIN;
3829
Jens Axboe9adbd452019-12-20 08:45:55 -07003830 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003831 end > 0 ? end : LLONG_MAX,
3832 req->sync.flags & IORING_FSYNC_DATASYNC);
3833 if (ret < 0)
3834 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003835 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003836 return 0;
3837}
3838
Jens Axboed63d1b52019-12-10 10:38:56 -07003839static int io_fallocate_prep(struct io_kiocb *req,
3840 const struct io_uring_sqe *sqe)
3841{
3842 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
3843 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003844 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3845 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07003846
3847 req->sync.off = READ_ONCE(sqe->off);
3848 req->sync.len = READ_ONCE(sqe->addr);
3849 req->sync.mode = READ_ONCE(sqe->len);
3850 return 0;
3851}
3852
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003853static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07003854{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003855 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07003856
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003857 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003858 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003859 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003860 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
3861 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03003862 if (ret < 0)
3863 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003864 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07003865 return 0;
3866}
3867
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003868static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003869{
Jens Axboef8748882020-01-08 17:47:02 -07003870 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003871 int ret;
3872
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003873 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07003874 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003875 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07003876 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003877
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003878 /* open.how should be already initialised */
3879 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06003880 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003881
Pavel Begunkov25e72d12020-06-03 18:03:23 +03003882 req->open.dfd = READ_ONCE(sqe->fd);
3883 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07003884 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003885 if (IS_ERR(req->open.filename)) {
3886 ret = PTR_ERR(req->open.filename);
3887 req->open.filename = NULL;
3888 return ret;
3889 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06003890 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003891 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003892 return 0;
3893}
3894
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003895static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3896{
3897 u64 flags, mode;
3898
Jens Axboe14587a462020-09-05 11:36:08 -06003899 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003900 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003901 mode = READ_ONCE(sqe->len);
3902 flags = READ_ONCE(sqe->open_flags);
3903 req->open.how = build_open_how(flags, mode);
3904 return __io_openat_prep(req, sqe);
3905}
3906
Jens Axboecebdb982020-01-08 17:59:24 -07003907static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3908{
3909 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07003910 size_t len;
3911 int ret;
3912
Jens Axboe14587a462020-09-05 11:36:08 -06003913 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06003914 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07003915 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3916 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07003917 if (len < OPEN_HOW_SIZE_VER0)
3918 return -EINVAL;
3919
3920 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
3921 len);
3922 if (ret)
3923 return ret;
3924
Pavel Begunkovec65fea2020-06-03 18:03:24 +03003925 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07003926}
3927
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003928static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07003929{
3930 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003931 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003932 bool nonblock_set;
3933 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003934 int ret;
3935
Jens Axboecebdb982020-01-08 17:59:24 -07003936 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003937 if (ret)
3938 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07003939 nonblock_set = op.open_flag & O_NONBLOCK;
3940 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003941 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003942 /*
3943 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
3944 * it'll always -EAGAIN
3945 */
3946 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
3947 return -EAGAIN;
3948 op.lookup_flags |= LOOKUP_CACHED;
3949 op.open_flag |= O_NONBLOCK;
3950 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07003951
Jens Axboe4022e7a2020-03-19 19:23:18 -06003952 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003953 if (ret < 0)
3954 goto err;
3955
3956 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07003957 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003958 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
3959 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07003960 /*
3961 * We could hang on to this 'fd', but seems like marginal
3962 * gain for something that is now known to be a slower path.
3963 * So just put it, and we'll get a new one when we retry.
3964 */
3965 put_unused_fd(ret);
3966 return -EAGAIN;
3967 }
3968
Jens Axboe15b71ab2019-12-11 11:20:36 -07003969 if (IS_ERR(file)) {
3970 put_unused_fd(ret);
3971 ret = PTR_ERR(file);
3972 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003973 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07003974 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003975 fsnotify_open(file);
3976 fd_install(ret, file);
3977 }
3978err:
3979 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03003980 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003981 if (ret < 0)
3982 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003983 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07003984 return 0;
3985}
3986
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003987static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07003988{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003989 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07003990}
3991
Jens Axboe067524e2020-03-02 16:32:28 -07003992static int io_remove_buffers_prep(struct io_kiocb *req,
3993 const struct io_uring_sqe *sqe)
3994{
3995 struct io_provide_buf *p = &req->pbuf;
3996 u64 tmp;
3997
3998 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
3999 return -EINVAL;
4000
4001 tmp = READ_ONCE(sqe->fd);
4002 if (!tmp || tmp > USHRT_MAX)
4003 return -EINVAL;
4004
4005 memset(p, 0, sizeof(*p));
4006 p->nbufs = tmp;
4007 p->bgid = READ_ONCE(sqe->buf_group);
4008 return 0;
4009}
4010
4011static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4012 int bgid, unsigned nbufs)
4013{
4014 unsigned i = 0;
4015
4016 /* shouldn't happen */
4017 if (!nbufs)
4018 return 0;
4019
4020 /* the head kbuf is the list itself */
4021 while (!list_empty(&buf->list)) {
4022 struct io_buffer *nxt;
4023
4024 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4025 list_del(&nxt->list);
4026 kfree(nxt);
4027 if (++i == nbufs)
4028 return i;
4029 }
4030 i++;
4031 kfree(buf);
4032 idr_remove(&ctx->io_buffer_idr, bgid);
4033
4034 return i;
4035}
4036
Pavel Begunkov889fca72021-02-10 00:03:09 +00004037static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004038{
4039 struct io_provide_buf *p = &req->pbuf;
4040 struct io_ring_ctx *ctx = req->ctx;
4041 struct io_buffer *head;
4042 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004043 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004044
4045 io_ring_submit_lock(ctx, !force_nonblock);
4046
4047 lockdep_assert_held(&ctx->uring_lock);
4048
4049 ret = -ENOENT;
4050 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4051 if (head)
4052 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004053 if (ret < 0)
4054 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004055
4056 /* need to hold the lock to complete IOPOLL requests */
4057 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004058 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004059 io_ring_submit_unlock(ctx, !force_nonblock);
4060 } else {
4061 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004062 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004063 }
Jens Axboe067524e2020-03-02 16:32:28 -07004064 return 0;
4065}
4066
Jens Axboeddf0322d2020-02-23 16:41:33 -07004067static int io_provide_buffers_prep(struct io_kiocb *req,
4068 const struct io_uring_sqe *sqe)
4069{
4070 struct io_provide_buf *p = &req->pbuf;
4071 u64 tmp;
4072
4073 if (sqe->ioprio || sqe->rw_flags)
4074 return -EINVAL;
4075
4076 tmp = READ_ONCE(sqe->fd);
4077 if (!tmp || tmp > USHRT_MAX)
4078 return -E2BIG;
4079 p->nbufs = tmp;
4080 p->addr = READ_ONCE(sqe->addr);
4081 p->len = READ_ONCE(sqe->len);
4082
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004083 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004084 return -EFAULT;
4085
4086 p->bgid = READ_ONCE(sqe->buf_group);
4087 tmp = READ_ONCE(sqe->off);
4088 if (tmp > USHRT_MAX)
4089 return -E2BIG;
4090 p->bid = tmp;
4091 return 0;
4092}
4093
4094static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4095{
4096 struct io_buffer *buf;
4097 u64 addr = pbuf->addr;
4098 int i, bid = pbuf->bid;
4099
4100 for (i = 0; i < pbuf->nbufs; i++) {
4101 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4102 if (!buf)
4103 break;
4104
4105 buf->addr = addr;
4106 buf->len = pbuf->len;
4107 buf->bid = bid;
4108 addr += pbuf->len;
4109 bid++;
4110 if (!*head) {
4111 INIT_LIST_HEAD(&buf->list);
4112 *head = buf;
4113 } else {
4114 list_add_tail(&buf->list, &(*head)->list);
4115 }
4116 }
4117
4118 return i ? i : -ENOMEM;
4119}
4120
Pavel Begunkov889fca72021-02-10 00:03:09 +00004121static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004122{
4123 struct io_provide_buf *p = &req->pbuf;
4124 struct io_ring_ctx *ctx = req->ctx;
4125 struct io_buffer *head, *list;
4126 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004127 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004128
4129 io_ring_submit_lock(ctx, !force_nonblock);
4130
4131 lockdep_assert_held(&ctx->uring_lock);
4132
4133 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4134
4135 ret = io_add_buffers(p, &head);
4136 if (ret < 0)
4137 goto out;
4138
4139 if (!list) {
4140 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4141 GFP_KERNEL);
4142 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004143 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004144 goto out;
4145 }
4146 }
4147out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004148 if (ret < 0)
4149 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004150
4151 /* need to hold the lock to complete IOPOLL requests */
4152 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004153 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004154 io_ring_submit_unlock(ctx, !force_nonblock);
4155 } else {
4156 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004157 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004158 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004159 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004160}
4161
Jens Axboe3e4827b2020-01-08 15:18:09 -07004162static int io_epoll_ctl_prep(struct io_kiocb *req,
4163 const struct io_uring_sqe *sqe)
4164{
4165#if defined(CONFIG_EPOLL)
4166 if (sqe->ioprio || sqe->buf_index)
4167 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004168 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004169 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004170
4171 req->epoll.epfd = READ_ONCE(sqe->fd);
4172 req->epoll.op = READ_ONCE(sqe->len);
4173 req->epoll.fd = READ_ONCE(sqe->off);
4174
4175 if (ep_op_has_event(req->epoll.op)) {
4176 struct epoll_event __user *ev;
4177
4178 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4179 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4180 return -EFAULT;
4181 }
4182
4183 return 0;
4184#else
4185 return -EOPNOTSUPP;
4186#endif
4187}
4188
Pavel Begunkov889fca72021-02-10 00:03:09 +00004189static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004190{
4191#if defined(CONFIG_EPOLL)
4192 struct io_epoll *ie = &req->epoll;
4193 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004194 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004195
4196 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4197 if (force_nonblock && ret == -EAGAIN)
4198 return -EAGAIN;
4199
4200 if (ret < 0)
4201 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004202 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004203 return 0;
4204#else
4205 return -EOPNOTSUPP;
4206#endif
4207}
4208
Jens Axboec1ca7572019-12-25 22:18:28 -07004209static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4210{
4211#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4212 if (sqe->ioprio || sqe->buf_index || sqe->off)
4213 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004214 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4215 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004216
4217 req->madvise.addr = READ_ONCE(sqe->addr);
4218 req->madvise.len = READ_ONCE(sqe->len);
4219 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4220 return 0;
4221#else
4222 return -EOPNOTSUPP;
4223#endif
4224}
4225
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004226static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004227{
4228#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4229 struct io_madvise *ma = &req->madvise;
4230 int ret;
4231
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004232 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004233 return -EAGAIN;
4234
Minchan Kim0726b012020-10-17 16:14:50 -07004235 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004236 if (ret < 0)
4237 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004238 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004239 return 0;
4240#else
4241 return -EOPNOTSUPP;
4242#endif
4243}
4244
Jens Axboe4840e412019-12-25 22:03:45 -07004245static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4246{
4247 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4248 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004249 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4250 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004251
4252 req->fadvise.offset = READ_ONCE(sqe->off);
4253 req->fadvise.len = READ_ONCE(sqe->len);
4254 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4255 return 0;
4256}
4257
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004258static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004259{
4260 struct io_fadvise *fa = &req->fadvise;
4261 int ret;
4262
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004263 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004264 switch (fa->advice) {
4265 case POSIX_FADV_NORMAL:
4266 case POSIX_FADV_RANDOM:
4267 case POSIX_FADV_SEQUENTIAL:
4268 break;
4269 default:
4270 return -EAGAIN;
4271 }
4272 }
Jens Axboe4840e412019-12-25 22:03:45 -07004273
4274 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4275 if (ret < 0)
4276 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004277 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004278 return 0;
4279}
4280
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004281static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4282{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004283 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004284 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004285 if (sqe->ioprio || sqe->buf_index)
4286 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004287 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004288 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004289
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004290 req->statx.dfd = READ_ONCE(sqe->fd);
4291 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004292 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004293 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4294 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004295
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004296 return 0;
4297}
4298
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004299static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004300{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004301 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004302 int ret;
4303
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004304 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004305 /* only need file table for an actual valid fd */
4306 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4307 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004308 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004309 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004310
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004311 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4312 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004313
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004314 if (ret < 0)
4315 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004316 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004317 return 0;
4318}
4319
Jens Axboeb5dba592019-12-11 14:02:38 -07004320static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4321{
Jens Axboe14587a462020-09-05 11:36:08 -06004322 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004323 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004324 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4325 sqe->rw_flags || sqe->buf_index)
4326 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004327 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004328 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004329
4330 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004331 return 0;
4332}
4333
Pavel Begunkov889fca72021-02-10 00:03:09 +00004334static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004335{
Jens Axboe9eac1902021-01-19 15:50:37 -07004336 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004337 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004338 struct fdtable *fdt;
4339 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004340 int ret;
4341
Jens Axboe9eac1902021-01-19 15:50:37 -07004342 file = NULL;
4343 ret = -EBADF;
4344 spin_lock(&files->file_lock);
4345 fdt = files_fdtable(files);
4346 if (close->fd >= fdt->max_fds) {
4347 spin_unlock(&files->file_lock);
4348 goto err;
4349 }
4350 file = fdt->fd[close->fd];
4351 if (!file) {
4352 spin_unlock(&files->file_lock);
4353 goto err;
4354 }
4355
4356 if (file->f_op == &io_uring_fops) {
4357 spin_unlock(&files->file_lock);
4358 file = NULL;
4359 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004360 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004361
4362 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004363 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004364 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004365 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004366 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004367
Jens Axboe9eac1902021-01-19 15:50:37 -07004368 ret = __close_fd_get_file(close->fd, &file);
4369 spin_unlock(&files->file_lock);
4370 if (ret < 0) {
4371 if (ret == -ENOENT)
4372 ret = -EBADF;
4373 goto err;
4374 }
4375
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004376 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004377 ret = filp_close(file, current->files);
4378err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004379 if (ret < 0)
4380 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004381 if (file)
4382 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004383 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004384 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004385}
4386
Pavel Begunkov1155c762021-02-18 18:29:38 +00004387static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004388{
4389 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004390
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004391 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4392 return -EINVAL;
4393 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4394 return -EINVAL;
4395
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004396 req->sync.off = READ_ONCE(sqe->off);
4397 req->sync.len = READ_ONCE(sqe->len);
4398 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004399 return 0;
4400}
4401
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004402static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004403{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004404 int ret;
4405
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004406 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004407 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004408 return -EAGAIN;
4409
Jens Axboe9adbd452019-12-20 08:45:55 -07004410 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004411 req->sync.flags);
4412 if (ret < 0)
4413 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004414 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004415 return 0;
4416}
4417
YueHaibing469956e2020-03-04 15:53:52 +08004418#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004419static int io_setup_async_msg(struct io_kiocb *req,
4420 struct io_async_msghdr *kmsg)
4421{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004422 struct io_async_msghdr *async_msg = req->async_data;
4423
4424 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004425 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004426 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004427 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004428 return -ENOMEM;
4429 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004430 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004431 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004432 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004433 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004434 /* if were using fast_iov, set it to the new one */
4435 if (!async_msg->free_iov)
4436 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4437
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004438 return -EAGAIN;
4439}
4440
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004441static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4442 struct io_async_msghdr *iomsg)
4443{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004444 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004445 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004446 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004447 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004448}
4449
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004450static int io_sendmsg_prep_async(struct io_kiocb *req)
4451{
4452 int ret;
4453
4454 if (!io_op_defs[req->opcode].needs_async_data)
4455 return 0;
4456 ret = io_sendmsg_copy_hdr(req, req->async_data);
4457 if (!ret)
4458 req->flags |= REQ_F_NEED_CLEANUP;
4459 return ret;
4460}
4461
Jens Axboe3529d8c2019-12-19 18:24:38 -07004462static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004463{
Jens Axboee47293f2019-12-20 08:58:21 -07004464 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004465
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004466 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4467 return -EINVAL;
4468
Jens Axboee47293f2019-12-20 08:58:21 -07004469 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004470 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004471 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004472
Jens Axboed8768362020-02-27 14:17:49 -07004473#ifdef CONFIG_COMPAT
4474 if (req->ctx->compat)
4475 sr->msg_flags |= MSG_CMSG_COMPAT;
4476#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004477 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004478}
4479
Pavel Begunkov889fca72021-02-10 00:03:09 +00004480static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004481{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004482 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004483 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004484 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004485 int ret;
4486
Florent Revestdba4a922020-12-04 12:36:04 +01004487 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004488 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004489 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004490
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004491 kmsg = req->async_data;
4492 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004493 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004494 if (ret)
4495 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004496 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004497 }
4498
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004499 flags = req->sr_msg.msg_flags;
4500 if (flags & MSG_DONTWAIT)
4501 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004502 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004503 flags |= MSG_DONTWAIT;
4504
4505 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004506 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004507 return io_setup_async_msg(req, kmsg);
4508 if (ret == -ERESTARTSYS)
4509 ret = -EINTR;
4510
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004511 /* fast path, check for non-NULL to avoid function call */
4512 if (kmsg->free_iov)
4513 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004514 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004515 if (ret < 0)
4516 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004517 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004518 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004519}
4520
Pavel Begunkov889fca72021-02-10 00:03:09 +00004521static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004522{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004523 struct io_sr_msg *sr = &req->sr_msg;
4524 struct msghdr msg;
4525 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004526 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004527 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004528 int ret;
4529
Florent Revestdba4a922020-12-04 12:36:04 +01004530 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004531 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004532 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004533
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004534 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4535 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004536 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004537
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004538 msg.msg_name = NULL;
4539 msg.msg_control = NULL;
4540 msg.msg_controllen = 0;
4541 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004542
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004543 flags = req->sr_msg.msg_flags;
4544 if (flags & MSG_DONTWAIT)
4545 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004546 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004547 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004548
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004549 msg.msg_flags = flags;
4550 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004551 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004552 return -EAGAIN;
4553 if (ret == -ERESTARTSYS)
4554 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004555
Jens Axboe03b12302019-12-02 18:50:25 -07004556 if (ret < 0)
4557 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004558 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004559 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004560}
4561
Pavel Begunkov1400e692020-07-12 20:41:05 +03004562static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4563 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004564{
4565 struct io_sr_msg *sr = &req->sr_msg;
4566 struct iovec __user *uiov;
4567 size_t iov_len;
4568 int ret;
4569
Pavel Begunkov1400e692020-07-12 20:41:05 +03004570 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4571 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004572 if (ret)
4573 return ret;
4574
4575 if (req->flags & REQ_F_BUFFER_SELECT) {
4576 if (iov_len > 1)
4577 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004578 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004579 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004580 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004581 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004582 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004583 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004584 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004585 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004586 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004587 if (ret > 0)
4588 ret = 0;
4589 }
4590
4591 return ret;
4592}
4593
4594#ifdef CONFIG_COMPAT
4595static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004596 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004597{
4598 struct compat_msghdr __user *msg_compat;
4599 struct io_sr_msg *sr = &req->sr_msg;
4600 struct compat_iovec __user *uiov;
4601 compat_uptr_t ptr;
4602 compat_size_t len;
4603 int ret;
4604
Pavel Begunkov270a5942020-07-12 20:41:04 +03004605 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004606 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004607 &ptr, &len);
4608 if (ret)
4609 return ret;
4610
4611 uiov = compat_ptr(ptr);
4612 if (req->flags & REQ_F_BUFFER_SELECT) {
4613 compat_ssize_t clen;
4614
4615 if (len > 1)
4616 return -EINVAL;
4617 if (!access_ok(uiov, sizeof(*uiov)))
4618 return -EFAULT;
4619 if (__get_user(clen, &uiov->iov_len))
4620 return -EFAULT;
4621 if (clen < 0)
4622 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004623 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004624 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004625 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004626 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004627 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004628 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004629 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004630 if (ret < 0)
4631 return ret;
4632 }
4633
4634 return 0;
4635}
Jens Axboe03b12302019-12-02 18:50:25 -07004636#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004637
Pavel Begunkov1400e692020-07-12 20:41:05 +03004638static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4639 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004640{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004641 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004642
4643#ifdef CONFIG_COMPAT
4644 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004645 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004646#endif
4647
Pavel Begunkov1400e692020-07-12 20:41:05 +03004648 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004649}
4650
Jens Axboebcda7ba2020-02-23 16:42:51 -07004651static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004652 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004653{
4654 struct io_sr_msg *sr = &req->sr_msg;
4655 struct io_buffer *kbuf;
4656
Jens Axboebcda7ba2020-02-23 16:42:51 -07004657 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4658 if (IS_ERR(kbuf))
4659 return kbuf;
4660
4661 sr->kbuf = kbuf;
4662 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004663 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004664}
4665
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004666static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4667{
4668 return io_put_kbuf(req, req->sr_msg.kbuf);
4669}
4670
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004671static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004672{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004673 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004674
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004675 if (!io_op_defs[req->opcode].needs_async_data)
4676 return 0;
4677 ret = io_recvmsg_copy_hdr(req, req->async_data);
4678 if (!ret)
4679 req->flags |= REQ_F_NEED_CLEANUP;
4680 return ret;
4681}
4682
4683static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4684{
4685 struct io_sr_msg *sr = &req->sr_msg;
4686
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004687 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4688 return -EINVAL;
4689
Jens Axboe3529d8c2019-12-19 18:24:38 -07004690 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004691 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004692 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004693 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004694
Jens Axboed8768362020-02-27 14:17:49 -07004695#ifdef CONFIG_COMPAT
4696 if (req->ctx->compat)
4697 sr->msg_flags |= MSG_CMSG_COMPAT;
4698#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004699 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004700}
4701
Pavel Begunkov889fca72021-02-10 00:03:09 +00004702static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004703{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004704 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004705 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004706 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004707 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004708 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004709 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004710
Florent Revestdba4a922020-12-04 12:36:04 +01004711 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004712 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004713 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004714
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004715 kmsg = req->async_data;
4716 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004717 ret = io_recvmsg_copy_hdr(req, &iomsg);
4718 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004719 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004720 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004721 }
4722
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004723 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004724 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004725 if (IS_ERR(kbuf))
4726 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004727 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004728 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4729 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004730 1, req->sr_msg.len);
4731 }
4732
4733 flags = req->sr_msg.msg_flags;
4734 if (flags & MSG_DONTWAIT)
4735 req->flags |= REQ_F_NOWAIT;
4736 else if (force_nonblock)
4737 flags |= MSG_DONTWAIT;
4738
4739 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4740 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004741 if (force_nonblock && ret == -EAGAIN)
4742 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004743 if (ret == -ERESTARTSYS)
4744 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004745
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004746 if (req->flags & REQ_F_BUFFER_SELECTED)
4747 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004748 /* fast path, check for non-NULL to avoid function call */
4749 if (kmsg->free_iov)
4750 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004751 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004752 if (ret < 0)
4753 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004754 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004755 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004756}
4757
Pavel Begunkov889fca72021-02-10 00:03:09 +00004758static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004759{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004760 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004761 struct io_sr_msg *sr = &req->sr_msg;
4762 struct msghdr msg;
4763 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07004764 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004765 struct iovec iov;
4766 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004767 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004768 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004769
Florent Revestdba4a922020-12-04 12:36:04 +01004770 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004771 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004772 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07004773
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004774 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004775 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004776 if (IS_ERR(kbuf))
4777 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004778 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07004779 }
4780
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004781 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004782 if (unlikely(ret))
4783 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07004784
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004785 msg.msg_name = NULL;
4786 msg.msg_control = NULL;
4787 msg.msg_controllen = 0;
4788 msg.msg_namelen = 0;
4789 msg.msg_iocb = NULL;
4790 msg.msg_flags = 0;
4791
4792 flags = req->sr_msg.msg_flags;
4793 if (flags & MSG_DONTWAIT)
4794 req->flags |= REQ_F_NOWAIT;
4795 else if (force_nonblock)
4796 flags |= MSG_DONTWAIT;
4797
4798 ret = sock_recvmsg(sock, &msg, flags);
4799 if (force_nonblock && ret == -EAGAIN)
4800 return -EAGAIN;
4801 if (ret == -ERESTARTSYS)
4802 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03004803out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004804 if (req->flags & REQ_F_BUFFER_SELECTED)
4805 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07004806 if (ret < 0)
4807 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004808 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07004809 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004810}
4811
Jens Axboe3529d8c2019-12-19 18:24:38 -07004812static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004813{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004814 struct io_accept *accept = &req->accept;
4815
Jens Axboe14587a462020-09-05 11:36:08 -06004816 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06004817 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05004818 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004819 return -EINVAL;
4820
Jens Axboed55e5f52019-12-11 16:12:15 -07004821 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4822 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004823 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06004824 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004825 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004826}
Jens Axboe17f2fe32019-10-17 14:42:58 -06004827
Pavel Begunkov889fca72021-02-10 00:03:09 +00004828static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004829{
4830 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004831 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004832 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004833 int ret;
4834
Jiufei Xuee697dee2020-06-10 13:41:59 +08004835 if (req->file->f_flags & O_NONBLOCK)
4836 req->flags |= REQ_F_NOWAIT;
4837
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004838 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06004839 accept->addr_len, accept->flags,
4840 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004841 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06004842 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004843 if (ret < 0) {
4844 if (ret == -ERESTARTSYS)
4845 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004846 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004847 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00004848 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004849 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004850}
4851
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004852static int io_connect_prep_async(struct io_kiocb *req)
4853{
4854 struct io_async_connect *io = req->async_data;
4855 struct io_connect *conn = &req->connect;
4856
4857 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
4858}
4859
Jens Axboe3529d8c2019-12-19 18:24:38 -07004860static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07004861{
Jens Axboe3529d8c2019-12-19 18:24:38 -07004862 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07004863
Jens Axboe14587a462020-09-05 11:36:08 -06004864 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004865 return -EINVAL;
4866 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
4867 return -EINVAL;
4868
Jens Axboe3529d8c2019-12-19 18:24:38 -07004869 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
4870 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004871 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07004872}
4873
Pavel Begunkov889fca72021-02-10 00:03:09 +00004874static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07004875{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004876 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004877 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004878 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004879 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004880
Jens Axboee8c2bc12020-08-15 18:44:09 -07004881 if (req->async_data) {
4882 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07004883 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004884 ret = move_addr_to_kernel(req->connect.addr,
4885 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07004886 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07004887 if (ret)
4888 goto out;
4889 io = &__io;
4890 }
4891
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004892 file_flags = force_nonblock ? O_NONBLOCK : 0;
4893
Jens Axboee8c2bc12020-08-15 18:44:09 -07004894 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07004895 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07004896 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07004897 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004898 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004899 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07004900 ret = -ENOMEM;
4901 goto out;
4902 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004903 io = req->async_data;
4904 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07004905 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07004906 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07004907 if (ret == -ERESTARTSYS)
4908 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07004909out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004910 if (ret < 0)
4911 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004912 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004913 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004914}
YueHaibing469956e2020-03-04 15:53:52 +08004915#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07004916#define IO_NETOP_FN(op) \
4917static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
4918{ \
4919 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07004920}
4921
Jens Axboe99a10082021-02-19 09:35:19 -07004922#define IO_NETOP_PREP(op) \
4923IO_NETOP_FN(op) \
4924static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
4925{ \
4926 return -EOPNOTSUPP; \
4927} \
4928
4929#define IO_NETOP_PREP_ASYNC(op) \
4930IO_NETOP_PREP(op) \
4931static int io_##op##_prep_async(struct io_kiocb *req) \
4932{ \
4933 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08004934}
4935
Jens Axboe99a10082021-02-19 09:35:19 -07004936IO_NETOP_PREP_ASYNC(sendmsg);
4937IO_NETOP_PREP_ASYNC(recvmsg);
4938IO_NETOP_PREP_ASYNC(connect);
4939IO_NETOP_PREP(accept);
4940IO_NETOP_FN(send);
4941IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08004942#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06004943
Jens Axboed7718a92020-02-14 22:23:12 -07004944struct io_poll_table {
4945 struct poll_table_struct pt;
4946 struct io_kiocb *req;
4947 int error;
4948};
4949
Jens Axboed7718a92020-02-14 22:23:12 -07004950static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
4951 __poll_t mask, task_work_func_t func)
4952{
Jens Axboeaa96bf82020-04-03 11:26:26 -06004953 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07004954
4955 /* for instances that support it check for an event match first: */
4956 if (mask && !(mask & poll->events))
4957 return 0;
4958
4959 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
4960
4961 list_del_init(&poll->wait.entry);
4962
Jens Axboed7718a92020-02-14 22:23:12 -07004963 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00004964 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06004965 percpu_ref_get(&req->ctx->refs);
4966
Jens Axboed7718a92020-02-14 22:23:12 -07004967 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06004968 * If this fails, then the task is exiting. When a task exits, the
4969 * work gets canceled, so just cancel this request as well instead
4970 * of executing it. We can't safely execute it anyway, as we may not
4971 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07004972 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06004973 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004974 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06004975 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00004976 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06004977 }
Jens Axboed7718a92020-02-14 22:23:12 -07004978 return 1;
4979}
4980
Jens Axboe74ce6ce2020-04-13 11:09:12 -06004981static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
4982 __acquires(&req->ctx->completion_lock)
4983{
4984 struct io_ring_ctx *ctx = req->ctx;
4985
4986 if (!req->result && !READ_ONCE(poll->canceled)) {
4987 struct poll_table_struct pt = { ._key = poll->events };
4988
4989 req->result = vfs_poll(req->file, &pt) & poll->events;
4990 }
4991
4992 spin_lock_irq(&ctx->completion_lock);
4993 if (!req->result && !READ_ONCE(poll->canceled)) {
4994 add_wait_queue(poll->head, &poll->wait);
4995 return true;
4996 }
4997
4998 return false;
4999}
5000
Jens Axboed4e7cd32020-08-15 11:44:50 -07005001static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005002{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005003 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005004 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005005 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005006 return req->apoll->double_poll;
5007}
5008
5009static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5010{
5011 if (req->opcode == IORING_OP_POLL_ADD)
5012 return &req->poll;
5013 return &req->apoll->poll;
5014}
5015
5016static void io_poll_remove_double(struct io_kiocb *req)
5017{
5018 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005019
5020 lockdep_assert_held(&req->ctx->completion_lock);
5021
5022 if (poll && poll->head) {
5023 struct wait_queue_head *head = poll->head;
5024
5025 spin_lock(&head->lock);
5026 list_del_init(&poll->wait.entry);
5027 if (poll->wait.private)
5028 refcount_dec(&req->refs);
5029 poll->head = NULL;
5030 spin_unlock(&head->lock);
5031 }
5032}
5033
5034static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5035{
5036 struct io_ring_ctx *ctx = req->ctx;
5037
Jens Axboed4e7cd32020-08-15 11:44:50 -07005038 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005039 req->poll.done = true;
5040 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5041 io_commit_cqring(ctx);
5042}
5043
Jens Axboe18bceab2020-05-15 11:56:54 -06005044static void io_poll_task_func(struct callback_head *cb)
5045{
5046 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005047 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005048 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005049
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005050 if (io_poll_rewait(req, &req->poll)) {
5051 spin_unlock_irq(&ctx->completion_lock);
5052 } else {
5053 hash_del(&req->hash_node);
5054 io_poll_complete(req, req->result, 0);
5055 spin_unlock_irq(&ctx->completion_lock);
5056
5057 nxt = io_put_req_find_next(req);
5058 io_cqring_ev_posted(ctx);
5059 if (nxt)
5060 __io_req_task_submit(nxt);
5061 }
5062
Jens Axboe6d816e02020-08-11 08:04:14 -06005063 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005064}
5065
5066static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5067 int sync, void *key)
5068{
5069 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005070 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005071 __poll_t mask = key_to_poll(key);
5072
5073 /* for instances that support it check for an event match first: */
5074 if (mask && !(mask & poll->events))
5075 return 0;
5076
Jens Axboe8706e042020-09-28 08:38:54 -06005077 list_del_init(&wait->entry);
5078
Jens Axboe807abcb2020-07-17 17:09:27 -06005079 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005080 bool done;
5081
Jens Axboe807abcb2020-07-17 17:09:27 -06005082 spin_lock(&poll->head->lock);
5083 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005084 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005085 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005086 /* make sure double remove sees this as being gone */
5087 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005088 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005089 if (!done) {
5090 /* use wait func handler, so it matches the rq type */
5091 poll->wait.func(&poll->wait, mode, sync, key);
5092 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005093 }
5094 refcount_dec(&req->refs);
5095 return 1;
5096}
5097
5098static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5099 wait_queue_func_t wake_func)
5100{
5101 poll->head = NULL;
5102 poll->done = false;
5103 poll->canceled = false;
5104 poll->events = events;
5105 INIT_LIST_HEAD(&poll->wait.entry);
5106 init_waitqueue_func_entry(&poll->wait, wake_func);
5107}
5108
5109static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005110 struct wait_queue_head *head,
5111 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005112{
5113 struct io_kiocb *req = pt->req;
5114
5115 /*
5116 * If poll->head is already set, it's because the file being polled
5117 * uses multiple waitqueues for poll handling (eg one for read, one
5118 * for write). Setup a separate io_poll_iocb if this happens.
5119 */
5120 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005121 struct io_poll_iocb *poll_one = poll;
5122
Jens Axboe18bceab2020-05-15 11:56:54 -06005123 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005124 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005125 pt->error = -EINVAL;
5126 return;
5127 }
5128 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5129 if (!poll) {
5130 pt->error = -ENOMEM;
5131 return;
5132 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005133 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005134 refcount_inc(&req->refs);
5135 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005136 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005137 }
5138
5139 pt->error = 0;
5140 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005141
5142 if (poll->events & EPOLLEXCLUSIVE)
5143 add_wait_queue_exclusive(head, &poll->wait);
5144 else
5145 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005146}
5147
5148static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5149 struct poll_table_struct *p)
5150{
5151 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005152 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005153
Jens Axboe807abcb2020-07-17 17:09:27 -06005154 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005155}
5156
Jens Axboed7718a92020-02-14 22:23:12 -07005157static void io_async_task_func(struct callback_head *cb)
5158{
5159 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5160 struct async_poll *apoll = req->apoll;
5161 struct io_ring_ctx *ctx = req->ctx;
5162
5163 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5164
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005165 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005166 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005167 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005168 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005169 }
5170
Jens Axboe31067252020-05-17 17:43:31 -06005171 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005172 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005173 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005174
Jens Axboed4e7cd32020-08-15 11:44:50 -07005175 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005176 spin_unlock_irq(&ctx->completion_lock);
5177
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005178 if (!READ_ONCE(apoll->poll.canceled))
5179 __io_req_task_submit(req);
5180 else
5181 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005182
Jens Axboe6d816e02020-08-11 08:04:14 -06005183 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005184 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005185 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005186}
5187
5188static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5189 void *key)
5190{
5191 struct io_kiocb *req = wait->private;
5192 struct io_poll_iocb *poll = &req->apoll->poll;
5193
5194 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5195 key_to_poll(key));
5196
5197 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5198}
5199
5200static void io_poll_req_insert(struct io_kiocb *req)
5201{
5202 struct io_ring_ctx *ctx = req->ctx;
5203 struct hlist_head *list;
5204
5205 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5206 hlist_add_head(&req->hash_node, list);
5207}
5208
5209static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5210 struct io_poll_iocb *poll,
5211 struct io_poll_table *ipt, __poll_t mask,
5212 wait_queue_func_t wake_func)
5213 __acquires(&ctx->completion_lock)
5214{
5215 struct io_ring_ctx *ctx = req->ctx;
5216 bool cancel = false;
5217
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005218 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005219 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005220 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005221 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005222
5223 ipt->pt._key = mask;
5224 ipt->req = req;
5225 ipt->error = -EINVAL;
5226
Jens Axboed7718a92020-02-14 22:23:12 -07005227 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5228
5229 spin_lock_irq(&ctx->completion_lock);
5230 if (likely(poll->head)) {
5231 spin_lock(&poll->head->lock);
5232 if (unlikely(list_empty(&poll->wait.entry))) {
5233 if (ipt->error)
5234 cancel = true;
5235 ipt->error = 0;
5236 mask = 0;
5237 }
5238 if (mask || ipt->error)
5239 list_del_init(&poll->wait.entry);
5240 else if (cancel)
5241 WRITE_ONCE(poll->canceled, true);
5242 else if (!poll->done) /* actually waiting for an event */
5243 io_poll_req_insert(req);
5244 spin_unlock(&poll->head->lock);
5245 }
5246
5247 return mask;
5248}
5249
5250static bool io_arm_poll_handler(struct io_kiocb *req)
5251{
5252 const struct io_op_def *def = &io_op_defs[req->opcode];
5253 struct io_ring_ctx *ctx = req->ctx;
5254 struct async_poll *apoll;
5255 struct io_poll_table ipt;
5256 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005257 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005258
5259 if (!req->file || !file_can_poll(req->file))
5260 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005261 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005262 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005263 if (def->pollin)
5264 rw = READ;
5265 else if (def->pollout)
5266 rw = WRITE;
5267 else
5268 return false;
5269 /* if we can't nonblock try, then no point in arming a poll handler */
5270 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005271 return false;
5272
5273 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5274 if (unlikely(!apoll))
5275 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005276 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005277
5278 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005279 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005280
Nathan Chancellor8755d972020-03-02 16:01:19 -07005281 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005282 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005283 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005284 if (def->pollout)
5285 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005286
5287 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5288 if ((req->opcode == IORING_OP_RECVMSG) &&
5289 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5290 mask &= ~POLLIN;
5291
Jens Axboed7718a92020-02-14 22:23:12 -07005292 mask |= POLLERR | POLLPRI;
5293
5294 ipt.pt._qproc = io_async_queue_proc;
5295
5296 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5297 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005298 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005299 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005300 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005301 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005302 kfree(apoll);
5303 return false;
5304 }
5305 spin_unlock_irq(&ctx->completion_lock);
5306 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5307 apoll->poll.events);
5308 return true;
5309}
5310
5311static bool __io_poll_remove_one(struct io_kiocb *req,
5312 struct io_poll_iocb *poll)
5313{
Jens Axboeb41e9852020-02-17 09:52:41 -07005314 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005315
5316 spin_lock(&poll->head->lock);
5317 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005318 if (!list_empty(&poll->wait.entry)) {
5319 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005320 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005321 }
5322 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005323 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005324 return do_complete;
5325}
5326
5327static bool io_poll_remove_one(struct io_kiocb *req)
5328{
5329 bool do_complete;
5330
Jens Axboed4e7cd32020-08-15 11:44:50 -07005331 io_poll_remove_double(req);
5332
Jens Axboed7718a92020-02-14 22:23:12 -07005333 if (req->opcode == IORING_OP_POLL_ADD) {
5334 do_complete = __io_poll_remove_one(req, &req->poll);
5335 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005336 struct async_poll *apoll = req->apoll;
5337
Jens Axboed7718a92020-02-14 22:23:12 -07005338 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005339 do_complete = __io_poll_remove_one(req, &apoll->poll);
5340 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005341 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005342 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005343 kfree(apoll);
5344 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005345 }
5346
Jens Axboeb41e9852020-02-17 09:52:41 -07005347 if (do_complete) {
5348 io_cqring_fill_event(req, -ECANCELED);
5349 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005350 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005351 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005352 }
5353
5354 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005355}
5356
Jens Axboe76e1b642020-09-26 15:05:03 -06005357/*
5358 * Returns true if we found and killed one or more poll requests
5359 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005360static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5361 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005362{
Jens Axboe78076bb2019-12-04 19:56:40 -07005363 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005364 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005365 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005366
5367 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005368 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5369 struct hlist_head *list;
5370
5371 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005372 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005373 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005374 posted += io_poll_remove_one(req);
5375 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005376 }
5377 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005378
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005379 if (posted)
5380 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005381
5382 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005383}
5384
Jens Axboe47f46762019-11-09 17:43:02 -07005385static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5386{
Jens Axboe78076bb2019-12-04 19:56:40 -07005387 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005388 struct io_kiocb *req;
5389
Jens Axboe78076bb2019-12-04 19:56:40 -07005390 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5391 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005392 if (sqe_addr != req->user_data)
5393 continue;
5394 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005395 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005396 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005397 }
5398
5399 return -ENOENT;
5400}
5401
Jens Axboe3529d8c2019-12-19 18:24:38 -07005402static int io_poll_remove_prep(struct io_kiocb *req,
5403 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005404{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005405 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5406 return -EINVAL;
5407 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5408 sqe->poll_events)
5409 return -EINVAL;
5410
Pavel Begunkov018043b2020-10-27 23:17:18 +00005411 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005412 return 0;
5413}
5414
5415/*
5416 * Find a running poll command that matches one specified in sqe->addr,
5417 * and remove it if found.
5418 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005419static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005420{
5421 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005422 int ret;
5423
Jens Axboe221c5eb2019-01-17 09:41:58 -07005424 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005425 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005426 spin_unlock_irq(&ctx->completion_lock);
5427
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005428 if (ret < 0)
5429 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005430 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005431 return 0;
5432}
5433
Jens Axboe221c5eb2019-01-17 09:41:58 -07005434static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5435 void *key)
5436{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005437 struct io_kiocb *req = wait->private;
5438 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005439
Jens Axboed7718a92020-02-14 22:23:12 -07005440 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005441}
5442
Jens Axboe221c5eb2019-01-17 09:41:58 -07005443static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5444 struct poll_table_struct *p)
5445{
5446 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5447
Jens Axboee8c2bc12020-08-15 18:44:09 -07005448 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005449}
5450
Jens Axboe3529d8c2019-12-19 18:24:38 -07005451static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005452{
5453 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005454 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005455
5456 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5457 return -EINVAL;
5458 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5459 return -EINVAL;
5460
Jiufei Xue5769a352020-06-17 17:53:55 +08005461 events = READ_ONCE(sqe->poll32_events);
5462#ifdef __BIG_ENDIAN
5463 events = swahw32(events);
5464#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005465 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5466 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005467 return 0;
5468}
5469
Pavel Begunkov61e98202021-02-10 00:03:08 +00005470static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005471{
5472 struct io_poll_iocb *poll = &req->poll;
5473 struct io_ring_ctx *ctx = req->ctx;
5474 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005475 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005476
Jens Axboed7718a92020-02-14 22:23:12 -07005477 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005478
Jens Axboed7718a92020-02-14 22:23:12 -07005479 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5480 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005481
Jens Axboe8c838782019-03-12 15:48:16 -06005482 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005483 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005484 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005485 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005486 spin_unlock_irq(&ctx->completion_lock);
5487
Jens Axboe8c838782019-03-12 15:48:16 -06005488 if (mask) {
5489 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005490 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005491 }
Jens Axboe8c838782019-03-12 15:48:16 -06005492 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005493}
5494
Jens Axboe5262f562019-09-17 12:26:57 -06005495static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5496{
Jens Axboead8a48a2019-11-15 08:49:11 -07005497 struct io_timeout_data *data = container_of(timer,
5498 struct io_timeout_data, timer);
5499 struct io_kiocb *req = data->req;
5500 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005501 unsigned long flags;
5502
Jens Axboe5262f562019-09-17 12:26:57 -06005503 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005504 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005505 atomic_set(&req->ctx->cq_timeouts,
5506 atomic_read(&req->ctx->cq_timeouts) + 1);
5507
Jens Axboe78e19bb2019-11-06 15:21:34 -07005508 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005509 io_commit_cqring(ctx);
5510 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5511
5512 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005513 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005514 io_put_req(req);
5515 return HRTIMER_NORESTART;
5516}
5517
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005518static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5519 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005520{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005521 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005522 struct io_kiocb *req;
5523 int ret = -ENOENT;
5524
5525 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5526 if (user_data == req->user_data) {
5527 ret = 0;
5528 break;
5529 }
5530 }
5531
5532 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005533 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005534
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005535 io = req->async_data;
5536 ret = hrtimer_try_to_cancel(&io->timer);
5537 if (ret == -1)
5538 return ERR_PTR(-EALREADY);
5539 list_del_init(&req->timeout.list);
5540 return req;
5541}
5542
5543static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5544{
5545 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5546
5547 if (IS_ERR(req))
5548 return PTR_ERR(req);
5549
5550 req_set_fail_links(req);
5551 io_cqring_fill_event(req, -ECANCELED);
5552 io_put_req_deferred(req, 1);
5553 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005554}
5555
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005556static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5557 struct timespec64 *ts, enum hrtimer_mode mode)
5558{
5559 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5560 struct io_timeout_data *data;
5561
5562 if (IS_ERR(req))
5563 return PTR_ERR(req);
5564
5565 req->timeout.off = 0; /* noseq */
5566 data = req->async_data;
5567 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5568 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5569 data->timer.function = io_timeout_fn;
5570 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5571 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005572}
5573
Jens Axboe3529d8c2019-12-19 18:24:38 -07005574static int io_timeout_remove_prep(struct io_kiocb *req,
5575 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005576{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005577 struct io_timeout_rem *tr = &req->timeout_rem;
5578
Jens Axboeb29472e2019-12-17 18:50:29 -07005579 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5580 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005581 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5582 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005583 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005584 return -EINVAL;
5585
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005586 tr->addr = READ_ONCE(sqe->addr);
5587 tr->flags = READ_ONCE(sqe->timeout_flags);
5588 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5589 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5590 return -EINVAL;
5591 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5592 return -EFAULT;
5593 } else if (tr->flags) {
5594 /* timeout removal doesn't support flags */
5595 return -EINVAL;
5596 }
5597
Jens Axboeb29472e2019-12-17 18:50:29 -07005598 return 0;
5599}
5600
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005601static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5602{
5603 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5604 : HRTIMER_MODE_REL;
5605}
5606
Jens Axboe11365042019-10-16 09:08:32 -06005607/*
5608 * Remove or update an existing timeout command
5609 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005610static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005611{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005612 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005613 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005614 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005615
Jens Axboe11365042019-10-16 09:08:32 -06005616 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005617 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005618 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005619 else
5620 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5621 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005622
Jens Axboe47f46762019-11-09 17:43:02 -07005623 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005624 io_commit_cqring(ctx);
5625 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005626 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005627 if (ret < 0)
5628 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005629 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005630 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005631}
5632
Jens Axboe3529d8c2019-12-19 18:24:38 -07005633static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005634 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005635{
Jens Axboead8a48a2019-11-15 08:49:11 -07005636 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005637 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005638 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005639
Jens Axboead8a48a2019-11-15 08:49:11 -07005640 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005641 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005642 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005643 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005644 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005645 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005646 flags = READ_ONCE(sqe->timeout_flags);
5647 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005648 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005649
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005650 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005651
Jens Axboee8c2bc12020-08-15 18:44:09 -07005652 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005653 return -ENOMEM;
5654
Jens Axboee8c2bc12020-08-15 18:44:09 -07005655 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005656 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005657
5658 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005659 return -EFAULT;
5660
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005661 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005662 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5663 return 0;
5664}
5665
Pavel Begunkov61e98202021-02-10 00:03:08 +00005666static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005667{
Jens Axboead8a48a2019-11-15 08:49:11 -07005668 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005669 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005670 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005671 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005672
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005673 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005674
Jens Axboe5262f562019-09-17 12:26:57 -06005675 /*
5676 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005677 * timeout event to be satisfied. If it isn't set, then this is
5678 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005679 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005680 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005681 entry = ctx->timeout_list.prev;
5682 goto add;
5683 }
Jens Axboe5262f562019-09-17 12:26:57 -06005684
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005685 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5686 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005687
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005688 /* Update the last seq here in case io_flush_timeouts() hasn't.
5689 * This is safe because ->completion_lock is held, and submissions
5690 * and completions are never mixed in the same ->completion_lock section.
5691 */
5692 ctx->cq_last_tm_flush = tail;
5693
Jens Axboe5262f562019-09-17 12:26:57 -06005694 /*
5695 * Insertion sort, ensuring the first entry in the list is always
5696 * the one we need first.
5697 */
Jens Axboe5262f562019-09-17 12:26:57 -06005698 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005699 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5700 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005701
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005702 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005703 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005704 /* nxt.seq is behind @tail, otherwise would've been completed */
5705 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005706 break;
5707 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005708add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005709 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005710 data->timer.function = io_timeout_fn;
5711 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005712 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005713 return 0;
5714}
5715
Jens Axboe62755e32019-10-28 21:49:21 -06005716static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005717{
Jens Axboe62755e32019-10-28 21:49:21 -06005718 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005719
Jens Axboe62755e32019-10-28 21:49:21 -06005720 return req->user_data == (unsigned long) data;
5721}
5722
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005723static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005724{
Jens Axboe62755e32019-10-28 21:49:21 -06005725 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005726 int ret = 0;
5727
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005728 if (!tctx->io_wq)
5729 return -ENOENT;
5730
5731 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005732 switch (cancel_ret) {
5733 case IO_WQ_CANCEL_OK:
5734 ret = 0;
5735 break;
5736 case IO_WQ_CANCEL_RUNNING:
5737 ret = -EALREADY;
5738 break;
5739 case IO_WQ_CANCEL_NOTFOUND:
5740 ret = -ENOENT;
5741 break;
5742 }
5743
Jens Axboee977d6d2019-11-05 12:39:45 -07005744 return ret;
5745}
5746
Jens Axboe47f46762019-11-09 17:43:02 -07005747static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5748 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005749 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005750{
5751 unsigned long flags;
5752 int ret;
5753
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005754 ret = io_async_cancel_one(req->task->io_uring,
5755 (void *) (unsigned long) sqe_addr);
Jens Axboe47f46762019-11-09 17:43:02 -07005756 if (ret != -ENOENT) {
5757 spin_lock_irqsave(&ctx->completion_lock, flags);
5758 goto done;
5759 }
5760
5761 spin_lock_irqsave(&ctx->completion_lock, flags);
5762 ret = io_timeout_cancel(ctx, sqe_addr);
5763 if (ret != -ENOENT)
5764 goto done;
5765 ret = io_poll_cancel(ctx, sqe_addr);
5766done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005767 if (!ret)
5768 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07005769 io_cqring_fill_event(req, ret);
5770 io_commit_cqring(ctx);
5771 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5772 io_cqring_ev_posted(ctx);
5773
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005774 if (ret < 0)
5775 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03005776 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07005777}
5778
Jens Axboe3529d8c2019-12-19 18:24:38 -07005779static int io_async_cancel_prep(struct io_kiocb *req,
5780 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07005781{
Jens Axboefbf23842019-12-17 18:45:56 -07005782 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07005783 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005784 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5785 return -EINVAL;
5786 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07005787 return -EINVAL;
5788
Jens Axboefbf23842019-12-17 18:45:56 -07005789 req->cancel.addr = READ_ONCE(sqe->addr);
5790 return 0;
5791}
5792
Pavel Begunkov61e98202021-02-10 00:03:08 +00005793static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07005794{
5795 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07005796
Pavel Begunkov014db002020-03-03 21:33:12 +03005797 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06005798 return 0;
5799}
5800
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005801static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07005802 const struct io_uring_sqe *sqe)
5803{
Jens Axboe6ca56f82020-09-18 16:51:19 -06005804 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
5805 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005806 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5807 return -EINVAL;
5808 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005809 return -EINVAL;
5810
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005811 req->rsrc_update.offset = READ_ONCE(sqe->off);
5812 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
5813 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005814 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005815 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005816 return 0;
5817}
5818
Pavel Begunkov889fca72021-02-10 00:03:09 +00005819static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005820{
5821 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005822 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005823 int ret;
5824
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005825 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005826 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005827
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005828 up.offset = req->rsrc_update.offset;
5829 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005830
5831 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005832 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005833 mutex_unlock(&ctx->uring_lock);
5834
5835 if (ret < 0)
5836 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005837 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005838 return 0;
5839}
5840
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005841static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07005842{
Jens Axboed625c6e2019-12-17 19:53:05 -07005843 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07005844 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005845 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07005846 case IORING_OP_READV:
5847 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005848 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005849 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005850 case IORING_OP_WRITEV:
5851 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07005852 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005853 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005854 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005855 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07005856 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005857 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005858 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005859 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005860 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00005861 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005862 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005863 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005864 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07005865 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07005866 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005867 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07005868 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005869 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005870 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005871 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07005872 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005873 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07005874 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005875 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005876 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005877 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005878 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005879 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07005880 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005881 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07005882 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005883 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07005884 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005885 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005886 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00005887 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07005888 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005889 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07005890 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005891 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07005892 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005893 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07005894 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005895 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07005896 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005897 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03005898 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005899 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07005900 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005901 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07005902 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005903 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03005904 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005905 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06005906 case IORING_OP_SHUTDOWN:
5907 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06005908 case IORING_OP_RENAMEAT:
5909 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06005910 case IORING_OP_UNLINKAT:
5911 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07005912 }
5913
Pavel Begunkovbfe76552020-09-30 22:57:55 +03005914 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
5915 req->opcode);
5916 return-EINVAL;
5917}
5918
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005919static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005920{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005921 switch (req->opcode) {
5922 case IORING_OP_READV:
5923 case IORING_OP_READ_FIXED:
5924 case IORING_OP_READ:
5925 return io_rw_prep_async(req, READ);
5926 case IORING_OP_WRITEV:
5927 case IORING_OP_WRITE_FIXED:
5928 case IORING_OP_WRITE:
5929 return io_rw_prep_async(req, WRITE);
5930 case IORING_OP_SENDMSG:
5931 case IORING_OP_SEND:
5932 return io_sendmsg_prep_async(req);
5933 case IORING_OP_RECVMSG:
5934 case IORING_OP_RECV:
5935 return io_recvmsg_prep_async(req);
5936 case IORING_OP_CONNECT:
5937 return io_connect_prep_async(req);
5938 }
5939 return 0;
5940}
5941
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005942static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07005943{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005944 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005945 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005946 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005947 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005948 return 0;
5949 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07005950 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005951 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005952}
5953
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005954static u32 io_get_sequence(struct io_kiocb *req)
5955{
5956 struct io_kiocb *pos;
5957 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005958 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005959
Pavel Begunkovf2f87372020-10-27 23:25:37 +00005960 io_for_each_link(pos, req)
5961 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005962
5963 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
5964 return total_submitted - nr_reqs;
5965}
5966
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005967static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005968{
5969 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005970 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005971 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005972 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005973
5974 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005975 if (likely(list_empty_careful(&ctx->defer_list) &&
5976 !(req->flags & REQ_F_IO_DRAIN)))
5977 return 0;
5978
5979 seq = io_get_sequence(req);
5980 /* Still a chance to pass the sequence check */
5981 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005982 return 0;
5983
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00005984 ret = io_req_defer_prep(req);
5985 if (ret)
5986 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03005987 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005988 de = kmalloc(sizeof(*de), GFP_KERNEL);
5989 if (!de)
5990 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07005991
5992 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03005993 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07005994 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03005995 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03005996 io_queue_async_work(req);
5997 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07005998 }
5999
6000 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006001 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006002 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006003 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006004 spin_unlock_irq(&ctx->completion_lock);
6005 return -EIOCBQUEUED;
6006}
Jens Axboeedafcce2019-01-09 09:16:05 -07006007
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006008static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006009{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006010 if (req->flags & REQ_F_BUFFER_SELECTED) {
6011 switch (req->opcode) {
6012 case IORING_OP_READV:
6013 case IORING_OP_READ_FIXED:
6014 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006015 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006016 break;
6017 case IORING_OP_RECVMSG:
6018 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006019 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006020 break;
6021 }
6022 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006023 }
6024
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006025 if (req->flags & REQ_F_NEED_CLEANUP) {
6026 switch (req->opcode) {
6027 case IORING_OP_READV:
6028 case IORING_OP_READ_FIXED:
6029 case IORING_OP_READ:
6030 case IORING_OP_WRITEV:
6031 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006032 case IORING_OP_WRITE: {
6033 struct io_async_rw *io = req->async_data;
6034 if (io->free_iovec)
6035 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006036 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006037 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006038 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006039 case IORING_OP_SENDMSG: {
6040 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006041
6042 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006043 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006044 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006045 case IORING_OP_SPLICE:
6046 case IORING_OP_TEE:
6047 io_put_file(req, req->splice.file_in,
6048 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6049 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006050 case IORING_OP_OPENAT:
6051 case IORING_OP_OPENAT2:
6052 if (req->open.filename)
6053 putname(req->open.filename);
6054 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006055 case IORING_OP_RENAMEAT:
6056 putname(req->rename.oldpath);
6057 putname(req->rename.newpath);
6058 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006059 case IORING_OP_UNLINKAT:
6060 putname(req->unlink.filename);
6061 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006062 }
6063 req->flags &= ~REQ_F_NEED_CLEANUP;
6064 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006065}
6066
Pavel Begunkov889fca72021-02-10 00:03:09 +00006067static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006068{
Jens Axboeedafcce2019-01-09 09:16:05 -07006069 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006070 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006071
Jens Axboed625c6e2019-12-17 19:53:05 -07006072 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006073 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006074 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006075 break;
6076 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006077 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006078 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006079 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006080 break;
6081 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006082 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006083 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006084 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085 break;
6086 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006087 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006088 break;
6089 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006090 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091 break;
6092 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006093 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006094 break;
6095 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006096 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006097 break;
6098 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006099 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006100 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006101 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006102 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006103 break;
6104 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006105 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006106 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006107 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006108 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006109 break;
6110 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006111 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006112 break;
6113 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006114 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006115 break;
6116 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006117 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006118 break;
6119 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006120 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006121 break;
6122 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006123 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006124 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006125 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006126 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006127 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006128 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006129 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006130 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006131 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006132 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006133 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006134 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006135 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006136 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006137 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006138 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006139 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006140 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006141 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006142 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006143 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006144 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006145 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006146 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006147 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006148 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006149 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006150 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006151 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006152 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006153 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006154 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006155 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006156 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006157 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006158 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006159 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006160 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006161 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006162 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006163 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006164 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006165 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006166 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006167 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006168 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006169 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006170 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006171 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006172 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006173 default:
6174 ret = -EINVAL;
6175 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006176 }
6177
6178 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006179 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180
Jens Axboeb5325762020-05-19 21:20:27 -06006181 /* If the op doesn't have a file, we're not polling for it */
6182 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006183 const bool in_async = io_wq_current_is_worker();
6184
Jens Axboe11ba8202020-01-15 21:51:17 -07006185 /* workqueue context doesn't hold uring_lock, grab it now */
6186 if (in_async)
6187 mutex_lock(&ctx->uring_lock);
6188
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006189 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006190
6191 if (in_async)
6192 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193 }
6194
6195 return 0;
6196}
6197
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006198static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006199{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006200 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006201 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006202 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006203
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006204 timeout = io_prep_linked_timeout(req);
6205 if (timeout)
6206 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006207
Jens Axboe4014d942021-01-19 15:53:54 -07006208 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006209 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006210
Jens Axboe561fb042019-10-24 07:25:42 -06006211 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006212 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006213 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006214 /*
6215 * We can get EAGAIN for polled IO even though we're
6216 * forcing a sync submission from here, since we can't
6217 * wait for request slots on the block side.
6218 */
6219 if (ret != -EAGAIN)
6220 break;
6221 cond_resched();
6222 } while (1);
6223 }
Jens Axboe31b51512019-01-18 22:56:34 -07006224
Pavel Begunkova3df76982021-02-18 22:32:52 +00006225 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006226 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006227 /* io-wq is going to take one down */
6228 refcount_inc(&req->refs);
6229 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006230 }
Jens Axboe31b51512019-01-18 22:56:34 -07006231}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006232
Jens Axboe65e19f52019-10-26 07:20:21 -06006233static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6234 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006235{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006236 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006237
Jens Axboe05f3fb32019-12-09 11:22:50 -07006238 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006239 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006240}
6241
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006242static struct file *io_file_get(struct io_submit_state *state,
6243 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006244{
6245 struct io_ring_ctx *ctx = req->ctx;
6246 struct file *file;
6247
6248 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006249 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006250 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006251 fd = array_index_nospec(fd, ctx->nr_user_files);
6252 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006253 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006254 } else {
6255 trace_io_uring_file_get(ctx, fd);
6256 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006257 }
6258
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006259 if (file && unlikely(file->f_op == &io_uring_fops))
6260 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006261 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006262}
6263
Jens Axboe2665abf2019-11-05 12:40:47 -07006264static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6265{
Jens Axboead8a48a2019-11-15 08:49:11 -07006266 struct io_timeout_data *data = container_of(timer,
6267 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006268 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006269 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006270 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006271
6272 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006273 prev = req->timeout.head;
6274 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006275
6276 /*
6277 * We don't expect the list to be empty, that will only happen if we
6278 * race with the completion of the linked work.
6279 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006280 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006281 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006282 else
6283 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006284 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6285
6286 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006287 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006288 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006289 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006290 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006291 io_req_complete_post(req, -ETIME, 0);
6292 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006293 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006294 return HRTIMER_NORESTART;
6295}
6296
Jens Axboe7271ef32020-08-10 09:55:22 -06006297static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006298{
Jens Axboe76a46e02019-11-10 23:34:16 -07006299 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006300 * If the back reference is NULL, then our linked request finished
6301 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006302 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006303 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006304 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006305
Jens Axboead8a48a2019-11-15 08:49:11 -07006306 data->timer.function = io_link_timeout_fn;
6307 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6308 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006309 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006310}
6311
6312static void io_queue_linked_timeout(struct io_kiocb *req)
6313{
6314 struct io_ring_ctx *ctx = req->ctx;
6315
6316 spin_lock_irq(&ctx->completion_lock);
6317 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006318 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006319
Jens Axboe2665abf2019-11-05 12:40:47 -07006320 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006321 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006322}
6323
Jens Axboead8a48a2019-11-15 08:49:11 -07006324static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006325{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006326 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006327
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006328 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6329 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006330 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006331
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006332 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006333 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006334 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006335 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006336}
6337
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006338static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006340 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe193155c2020-02-22 23:22:19 -07006341 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006342 int ret;
6343
Jens Axboe4379bf82021-02-15 13:40:22 -07006344 if ((req->flags & REQ_F_WORK_INITIALIZED) && req->work.creds &&
6345 req->work.creds != current_cred())
6346 old_creds = override_creds(req->work.creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006347
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006348 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006349
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006350 if (old_creds)
6351 revert_creds(old_creds);
Jens Axboe491381ce2019-10-17 09:20:46 -06006352
6353 /*
6354 * We async punt it if the file wasn't marked NOWAIT, or if the file
6355 * doesn't support non-blocking read/write attempts
6356 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006357 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006358 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006359 /*
6360 * Queued up for async execution, worker will release
6361 * submit reference when the iocb is actually submitted.
6362 */
6363 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006364 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006365 } else if (likely(!ret)) {
6366 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006367 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006368 struct io_ring_ctx *ctx = req->ctx;
6369 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006370
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006371 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006372 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006373 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006374 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006375 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006376 }
6377 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006378 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006379 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006380 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006381 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006382 if (linked_timeout)
6383 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006384}
6385
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006386static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006387{
6388 int ret;
6389
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006390 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006391 if (ret) {
6392 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006393fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006394 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006395 io_put_req(req);
6396 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006397 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006398 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006399 ret = io_req_defer_prep(req);
6400 if (unlikely(ret))
6401 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006402 io_queue_async_work(req);
6403 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006404 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006405 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006406}
6407
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006408/*
6409 * Check SQE restrictions (opcode and flags).
6410 *
6411 * Returns 'true' if SQE is allowed, 'false' otherwise.
6412 */
6413static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6414 struct io_kiocb *req,
6415 unsigned int sqe_flags)
6416{
6417 if (!ctx->restricted)
6418 return true;
6419
6420 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6421 return false;
6422
6423 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6424 ctx->restrictions.sqe_flags_required)
6425 return false;
6426
6427 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6428 ctx->restrictions.sqe_flags_required))
6429 return false;
6430
6431 return true;
6432}
6433
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006434static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006435 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006436{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006437 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006438 unsigned int sqe_flags;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006439 int id, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006440
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006441 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006442 /* same numerical values with corresponding REQ_F_*, safe to copy */
6443 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006444 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006445 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006446 req->file = NULL;
6447 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006448 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006449 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006450 /* one is dropped after submission, the other at completion */
6451 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006452 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006453 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006454
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006455 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006456 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6457 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006458 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006459 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006460
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006461 if (unlikely(req->opcode >= IORING_OP_LAST))
6462 return -EINVAL;
6463
Jens Axboe28cea78a2020-09-14 10:51:17 -06006464 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006465 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006466
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006467 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6468 return -EACCES;
6469
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006470 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6471 !io_op_defs[req->opcode].buffer_select)
6472 return -EOPNOTSUPP;
6473
6474 id = READ_ONCE(sqe->personality);
6475 if (id) {
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006476 __io_req_init_async(req);
Jens Axboe4379bf82021-02-15 13:40:22 -07006477 req->work.creds = idr_find(&ctx->personality_idr, id);
6478 if (unlikely(!req->work.creds))
6479 return -EINVAL;
6480 get_cred(req->work.creds);
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006481 }
6482
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006483 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006484
Jens Axboe27926b62020-10-28 09:33:23 -06006485 /*
6486 * Plug now if we have more than 1 IO left after this, and the target
6487 * is potentially a read/write to block based storage.
6488 */
6489 if (!state->plug_started && state->ios_left > 1 &&
6490 io_op_defs[req->opcode].plug) {
6491 blk_start_plug(&state->plug);
6492 state->plug_started = true;
6493 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006494
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006495 if (io_op_defs[req->opcode].needs_file) {
6496 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006497
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006498 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006499 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006500 ret = -EBADF;
6501 }
6502
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006503 state->ios_left--;
6504 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006505}
6506
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006507static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006508 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006509{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006510 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006511 int ret;
6512
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006513 ret = io_init_req(ctx, req, sqe);
6514 if (unlikely(ret)) {
6515fail_req:
6516 io_put_req(req);
6517 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006518 if (link->head) {
6519 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006520 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006521 io_put_req(link->head);
6522 io_req_complete(link->head, -ECANCELED);
6523 link->head = NULL;
6524 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006525 return ret;
6526 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006527 ret = io_req_prep(req, sqe);
6528 if (unlikely(ret))
6529 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006530
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006531 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006532 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6533 true, ctx->flags & IORING_SETUP_SQPOLL);
6534
Jens Axboe6c271ce2019-01-10 11:22:30 -07006535 /*
6536 * If we already have a head request, queue this one for async
6537 * submittal once the head completes. If we don't have a head but
6538 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6539 * submitted sync once the chain is complete. If none of those
6540 * conditions are true (normal request), then just queue it.
6541 */
6542 if (link->head) {
6543 struct io_kiocb *head = link->head;
6544
6545 /*
6546 * Taking sequential execution of a link, draining both sides
6547 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6548 * requests in the link. So, it drains the head and the
6549 * next after the link request. The last one is done via
6550 * drain_next flag to persist the effect across calls.
6551 */
6552 if (req->flags & REQ_F_IO_DRAIN) {
6553 head->flags |= REQ_F_IO_DRAIN;
6554 ctx->drain_next = 1;
6555 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006556 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006557 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006558 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006559 trace_io_uring_link(ctx, req, head);
6560 link->last->link = req;
6561 link->last = req;
6562
6563 /* last request of a link, enqueue the link */
6564 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006565 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006566 link->head = NULL;
6567 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006568 } else {
6569 if (unlikely(ctx->drain_next)) {
6570 req->flags |= REQ_F_IO_DRAIN;
6571 ctx->drain_next = 0;
6572 }
6573 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006574 link->head = req;
6575 link->last = req;
6576 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006577 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006578 }
6579 }
6580
6581 return 0;
6582}
6583
6584/*
6585 * Batched submission is done, ensure local IO is flushed out.
6586 */
6587static void io_submit_state_end(struct io_submit_state *state,
6588 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006589{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006590 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006591 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006592 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006593 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006594 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006595 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006596 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006597}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006598
Jens Axboe9e645e112019-05-10 16:07:28 -06006599/*
6600 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006601 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006602static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006603 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006604{
6605 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006606 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006607 /* set only head, no need to init link_last in advance */
6608 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006609}
6610
Jens Axboe193155c2020-02-22 23:22:19 -07006611static void io_commit_sqring(struct io_ring_ctx *ctx)
6612{
Jens Axboe75c6a032020-01-28 10:15:23 -07006613 struct io_rings *rings = ctx->rings;
6614
6615 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006616 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006617 * since once we write the new head, the application could
6618 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006619 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006620 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006621}
6622
Jens Axboe9e645e112019-05-10 16:07:28 -06006623/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006624 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006625 * that is mapped by userspace. This means that care needs to be taken to
6626 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006627 * being a good citizen. If members of the sqe are validated and then later
6628 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006629 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006630 */
6631static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006632{
6633 u32 *sq_array = ctx->sq_array;
6634 unsigned head;
6635
6636 /*
6637 * The cached sq head (or cq tail) serves two purposes:
6638 *
6639 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006640 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006641 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006642 * though the application is the one updating it.
6643 */
6644 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6645 if (likely(head < ctx->sq_entries))
6646 return &ctx->sq_sqes[head];
6647
6648 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006649 ctx->cached_sq_dropped++;
6650 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6651 return NULL;
6652}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006653
Jens Axboe0f212202020-09-13 13:09:39 -06006654static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006655{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006656 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006657
Jens Axboec4a2ed72019-11-21 21:01:26 -07006658 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006659 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006660 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006661 return -EBUSY;
6662 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006663
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006664 /* make sure SQ entry isn't read before tail */
6665 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006666
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006667 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6668 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006669
Jens Axboed8a6df12020-10-15 16:24:45 -06006670 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006671 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006672 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006673
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006674 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006675 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006676 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006677
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006678 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006679 if (unlikely(!req)) {
6680 if (!submitted)
6681 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006682 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006683 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006684 sqe = io_get_sqe(ctx);
6685 if (unlikely(!sqe)) {
6686 kmem_cache_free(req_cachep, req);
6687 break;
6688 }
Jens Axboed3656342019-12-18 09:50:26 -07006689 /* will complete beyond this point, count as submitted */
6690 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006691 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006692 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006693 }
6694
Pavel Begunkov9466f432020-01-25 22:34:01 +03006695 if (unlikely(submitted != nr)) {
6696 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006697 struct io_uring_task *tctx = current->io_uring;
6698 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006699
Jens Axboed8a6df12020-10-15 16:24:45 -06006700 percpu_ref_put_many(&ctx->refs, unused);
6701 percpu_counter_sub(&tctx->inflight, unused);
6702 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006703 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006704
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006705 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006706 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6707 io_commit_sqring(ctx);
6708
Jens Axboe6c271ce2019-01-10 11:22:30 -07006709 return submitted;
6710}
6711
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006712static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6713{
6714 /* Tell userspace we may need a wakeup call */
6715 spin_lock_irq(&ctx->completion_lock);
6716 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6717 spin_unlock_irq(&ctx->completion_lock);
6718}
6719
6720static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6721{
6722 spin_lock_irq(&ctx->completion_lock);
6723 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6724 spin_unlock_irq(&ctx->completion_lock);
6725}
6726
Xiaoguang Wang08369242020-11-03 14:15:59 +08006727static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006728{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006729 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006730 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006731
Jens Axboec8d1ba52020-09-14 11:07:26 -06006732 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006733 /* if we're handling multiple rings, cap submit size for fairness */
6734 if (cap_entries && to_submit > 8)
6735 to_submit = 8;
6736
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006737 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6738 unsigned nr_events = 0;
6739
Xiaoguang Wang08369242020-11-03 14:15:59 +08006740 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006741 if (!list_empty(&ctx->iopoll_list))
6742 io_do_iopoll(ctx, &nr_events, 0);
6743
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006744 if (to_submit && !ctx->sqo_dead &&
6745 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006746 ret = io_submit_sqes(ctx, to_submit);
6747 mutex_unlock(&ctx->uring_lock);
6748 }
Jens Axboe90554202020-09-03 12:12:41 -06006749
6750 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6751 wake_up(&ctx->sqo_sq_wait);
6752
Xiaoguang Wang08369242020-11-03 14:15:59 +08006753 return ret;
6754}
6755
6756static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
6757{
6758 struct io_ring_ctx *ctx;
6759 unsigned sq_thread_idle = 0;
6760
6761 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6762 if (sq_thread_idle < ctx->sq_thread_idle)
6763 sq_thread_idle = ctx->sq_thread_idle;
6764 }
6765
6766 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06006767}
6768
Jens Axboe69fb2132020-09-14 11:16:23 -06006769static void io_sqd_init_new(struct io_sq_data *sqd)
6770{
6771 struct io_ring_ctx *ctx;
6772
6773 while (!list_empty(&sqd->ctx_new_list)) {
6774 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006775 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
6776 complete(&ctx->sq_thread_comp);
6777 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006778
6779 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06006780}
6781
Jens Axboe6c271ce2019-01-10 11:22:30 -07006782static int io_sq_thread(void *data)
6783{
Dennis Zhou91d8f512020-09-16 13:41:05 -07006784 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06006785 struct files_struct *old_files = current->files;
6786 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06006787 const struct cred *old_cred = NULL;
6788 struct io_sq_data *sqd = data;
6789 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08006790 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08006791 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006792
Jens Axboe28cea78a2020-09-14 10:51:17 -06006793 task_lock(current);
6794 current->files = NULL;
6795 current->nsproxy = NULL;
6796 task_unlock(current);
6797
Jens Axboe69fb2132020-09-14 11:16:23 -06006798 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08006799 int ret;
6800 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07006801
6802 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06006803 * Any changes to the sqd lists are synchronized through the
6804 * kthread parking. This synchronizes the thread vs users,
6805 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07006806 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006807 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006808 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08006809 /*
6810 * When sq thread is unparked, in case the previous park operation
6811 * comes from io_put_sq_data(), which means that sq thread is going
6812 * to be stopped, so here needs to have a check.
6813 */
6814 if (kthread_should_stop())
6815 break;
6816 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006817
Xiaoguang Wang08369242020-11-03 14:15:59 +08006818 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006819 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006820 timeout = jiffies + sqd->sq_thread_idle;
6821 }
Jens Axboe69fb2132020-09-14 11:16:23 -06006822
Xiaoguang Wang08369242020-11-03 14:15:59 +08006823 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06006824 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06006825 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6826 if (current->cred != ctx->creds) {
6827 if (old_cred)
6828 revert_creds(old_cred);
6829 old_cred = override_creds(ctx->creds);
6830 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07006831 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06006832#ifdef CONFIG_AUDIT
6833 current->loginuid = ctx->loginuid;
6834 current->sessionid = ctx->sessionid;
6835#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06006836
Xiaoguang Wang08369242020-11-03 14:15:59 +08006837 ret = __io_sq_thread(ctx, cap_entries);
6838 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
6839 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06006840
Jens Axboe28cea78a2020-09-14 10:51:17 -06006841 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07006842 }
6843
Xiaoguang Wang08369242020-11-03 14:15:59 +08006844 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06006845 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00006846 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06006847 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08006848 if (sqt_spin)
6849 timeout = jiffies + sqd->sq_thread_idle;
6850 continue;
6851 }
6852
Xiaoguang Wang08369242020-11-03 14:15:59 +08006853 needs_sched = true;
6854 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
6855 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
6856 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
6857 !list_empty_careful(&ctx->iopoll_list)) {
6858 needs_sched = false;
6859 break;
6860 }
6861 if (io_sqring_entries(ctx)) {
6862 needs_sched = false;
6863 break;
6864 }
6865 }
6866
Hao Xu8b28fdf2021-01-31 22:39:04 +08006867 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06006868 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6869 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08006870
Jens Axboe69fb2132020-09-14 11:16:23 -06006871 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06006872 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
6873 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006874 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08006875
6876 finish_wait(&sqd->wait, &wait);
6877 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006878 }
6879
Jens Axboe4c6e2772020-07-01 11:29:10 -06006880 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00006881 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07006882
Dennis Zhou91d8f512020-09-16 13:41:05 -07006883 if (cur_css)
6884 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06006885 if (old_cred)
6886 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06006887
Jens Axboe28cea78a2020-09-14 10:51:17 -06006888 task_lock(current);
6889 current->files = old_files;
6890 current->nsproxy = old_nsproxy;
6891 task_unlock(current);
6892
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02006893 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06006894
Jens Axboe6c271ce2019-01-10 11:22:30 -07006895 return 0;
6896}
6897
Jens Axboebda52162019-09-24 13:47:15 -06006898struct io_wait_queue {
6899 struct wait_queue_entry wq;
6900 struct io_ring_ctx *ctx;
6901 unsigned to_wait;
6902 unsigned nr_timeouts;
6903};
6904
Pavel Begunkov6c503152021-01-04 20:36:36 +00006905static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06006906{
6907 struct io_ring_ctx *ctx = iowq->ctx;
6908
6909 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08006910 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06006911 * started waiting. For timeouts, we always want to return to userspace,
6912 * regardless of event count.
6913 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00006914 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06006915 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
6916}
6917
6918static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
6919 int wake_flags, void *key)
6920{
6921 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
6922 wq);
6923
Pavel Begunkov6c503152021-01-04 20:36:36 +00006924 /*
6925 * Cannot safely flush overflowed CQEs from here, ensure we wake up
6926 * the task, and the next invocation will do it.
6927 */
6928 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
6929 return autoremove_wake_function(curr, mode, wake_flags, key);
6930 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06006931}
6932
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006933static int io_run_task_work_sig(void)
6934{
6935 if (io_run_task_work())
6936 return 1;
6937 if (!signal_pending(current))
6938 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06006939 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
6940 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06006941 return -EINTR;
6942}
6943
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00006944/* when returns >0, the caller should retry */
6945static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
6946 struct io_wait_queue *iowq,
6947 signed long *timeout)
6948{
6949 int ret;
6950
6951 /* make sure we run task_work before checking for signals */
6952 ret = io_run_task_work_sig();
6953 if (ret || io_should_wake(iowq))
6954 return ret;
6955 /* let the caller flush overflows, retry */
6956 if (test_bit(0, &ctx->cq_check_overflow))
6957 return 1;
6958
6959 *timeout = schedule_timeout(*timeout);
6960 return !*timeout ? -ETIME : 1;
6961}
6962
Jens Axboe2b188cc2019-01-07 10:46:33 -07006963/*
6964 * Wait until events become available, if we don't already have some. The
6965 * application must reap them itself, as they reside on the shared cq ring.
6966 */
6967static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08006968 const sigset_t __user *sig, size_t sigsz,
6969 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006970{
Jens Axboebda52162019-09-24 13:47:15 -06006971 struct io_wait_queue iowq = {
6972 .wq = {
6973 .private = current,
6974 .func = io_wake_function,
6975 .entry = LIST_HEAD_INIT(iowq.wq.entry),
6976 },
6977 .ctx = ctx,
6978 .to_wait = min_events,
6979 };
Hristo Venev75b28af2019-08-26 17:23:46 +00006980 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00006981 signed long timeout = MAX_SCHEDULE_TIMEOUT;
6982 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006983
Jens Axboeb41e9852020-02-17 09:52:41 -07006984 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006985 io_cqring_overflow_flush(ctx, false, NULL, NULL);
6986 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07006987 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06006988 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07006989 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07006990 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006991
6992 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006993#ifdef CONFIG_COMPAT
6994 if (in_compat_syscall())
6995 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07006996 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01006997 else
6998#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07006999 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007000
Jens Axboe2b188cc2019-01-07 10:46:33 -07007001 if (ret)
7002 return ret;
7003 }
7004
Hao Xuc73ebb62020-11-03 10:54:37 +08007005 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007006 struct timespec64 ts;
7007
Hao Xuc73ebb62020-11-03 10:54:37 +08007008 if (get_timespec64(&ts, uts))
7009 return -EFAULT;
7010 timeout = timespec64_to_jiffies(&ts);
7011 }
7012
Jens Axboebda52162019-09-24 13:47:15 -06007013 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007014 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007015 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007016 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007017 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7018 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007019 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7020 finish_wait(&ctx->wait, &iowq.wq);
7021 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007022
Jens Axboeb7db41c2020-07-04 08:55:50 -06007023 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007024
Hristo Venev75b28af2019-08-26 17:23:46 +00007025 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007026}
7027
Jens Axboe6b063142019-01-10 22:13:58 -07007028static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7029{
7030#if defined(CONFIG_UNIX)
7031 if (ctx->ring_sock) {
7032 struct sock *sock = ctx->ring_sock->sk;
7033 struct sk_buff *skb;
7034
7035 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7036 kfree_skb(skb);
7037 }
7038#else
7039 int i;
7040
Jens Axboe65e19f52019-10-26 07:20:21 -06007041 for (i = 0; i < ctx->nr_user_files; i++) {
7042 struct file *file;
7043
7044 file = io_file_from_index(ctx, i);
7045 if (file)
7046 fput(file);
7047 }
Jens Axboe6b063142019-01-10 22:13:58 -07007048#endif
7049}
7050
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007051static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007052{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007053 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007054
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007055 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007056 complete(&data->done);
7057}
7058
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007059static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007060{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007061 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007062}
7063
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007064static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07007065{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007066 spin_unlock_bh(&ctx->rsrc_ref_lock);
7067}
7068
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007069static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7070 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007071 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007072{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007073 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007074 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007075 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007076 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007077 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07007078}
7079
Hao Xu8bad28d2021-02-19 17:19:36 +08007080static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07007081{
Hao Xu8bad28d2021-02-19 17:19:36 +08007082 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007083
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007084 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007085 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00007086 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007087 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007088 if (ref_node)
7089 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08007090}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007091
Hao Xu8bad28d2021-02-19 17:19:36 +08007092static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7093 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007094 void (*rsrc_put)(struct io_ring_ctx *ctx,
7095 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08007096{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007097 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08007098 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007099
Hao Xu8bad28d2021-02-19 17:19:36 +08007100 if (data->quiesce)
7101 return -ENXIO;
7102
7103 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007104 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007105 ret = -ENOMEM;
7106 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7107 if (!backup_node)
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007108 break;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007109 backup_node->rsrc_data = data;
7110 backup_node->rsrc_put = rsrc_put;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007111
Hao Xu8bad28d2021-02-19 17:19:36 +08007112 io_sqe_rsrc_kill_node(ctx, data);
7113 percpu_ref_kill(&data->refs);
7114 flush_delayed_work(&ctx->rsrc_put_work);
7115
Jens Axboe6b063142019-01-10 22:13:58 -07007116 ret = wait_for_completion_interruptible(&data->done);
Pavel Begunkov88f171a2021-02-20 18:03:50 +00007117 if (!ret || !io_refs_resurrect(&data->refs, &data->done))
Jens Axboe6b063142019-01-10 22:13:58 -07007118 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007119
Hao Xu8bad28d2021-02-19 17:19:36 +08007120 io_sqe_rsrc_set_node(ctx, data, backup_node);
7121 backup_node = NULL;
Hao Xu8bad28d2021-02-19 17:19:36 +08007122 mutex_unlock(&ctx->uring_lock);
7123 ret = io_run_task_work_sig();
7124 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007125 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007126 data->quiesce = false;
7127
7128 if (backup_node)
7129 destroy_fixed_rsrc_ref_node(backup_node);
7130 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007131}
7132
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007133static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7134{
7135 struct fixed_rsrc_data *data;
7136
7137 data = kzalloc(sizeof(*data), GFP_KERNEL);
7138 if (!data)
7139 return NULL;
7140
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007141 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007142 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7143 kfree(data);
7144 return NULL;
7145 }
7146 data->ctx = ctx;
7147 init_completion(&data->done);
7148 return data;
7149}
7150
7151static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7152{
7153 percpu_ref_exit(&data->refs);
7154 kfree(data->table);
7155 kfree(data);
7156}
7157
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007158static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7159{
7160 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007161 unsigned nr_tables, i;
7162 int ret;
7163
Hao Xu8bad28d2021-02-19 17:19:36 +08007164 /*
7165 * percpu_ref_is_dying() is to stop parallel files unregister
7166 * Since we possibly drop uring lock later in this function to
7167 * run task work.
7168 */
7169 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007170 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007171 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007172 if (ret)
7173 return ret;
7174
Jens Axboe6b063142019-01-10 22:13:58 -07007175 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007176 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7177 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007178 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007179 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007180 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007181 ctx->nr_user_files = 0;
7182 return 0;
7183}
7184
Jens Axboe534ca6d2020-09-02 13:52:19 -06007185static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007186{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007187 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007188 /*
7189 * The park is a bit of a work-around, without it we get
7190 * warning spews on shutdown with SQPOLL set and affinity
7191 * set to a single CPU.
7192 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007193 if (sqd->thread) {
7194 kthread_park(sqd->thread);
7195 kthread_stop(sqd->thread);
7196 }
7197
7198 kfree(sqd);
7199 }
7200}
7201
Jens Axboeaa061652020-09-02 14:50:27 -06007202static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7203{
7204 struct io_ring_ctx *ctx_attach;
7205 struct io_sq_data *sqd;
7206 struct fd f;
7207
7208 f = fdget(p->wq_fd);
7209 if (!f.file)
7210 return ERR_PTR(-ENXIO);
7211 if (f.file->f_op != &io_uring_fops) {
7212 fdput(f);
7213 return ERR_PTR(-EINVAL);
7214 }
7215
7216 ctx_attach = f.file->private_data;
7217 sqd = ctx_attach->sq_data;
7218 if (!sqd) {
7219 fdput(f);
7220 return ERR_PTR(-EINVAL);
7221 }
7222
7223 refcount_inc(&sqd->refs);
7224 fdput(f);
7225 return sqd;
7226}
7227
Jens Axboe534ca6d2020-09-02 13:52:19 -06007228static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7229{
7230 struct io_sq_data *sqd;
7231
Jens Axboeaa061652020-09-02 14:50:27 -06007232 if (p->flags & IORING_SETUP_ATTACH_WQ)
7233 return io_attach_sq_data(p);
7234
Jens Axboe534ca6d2020-09-02 13:52:19 -06007235 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7236 if (!sqd)
7237 return ERR_PTR(-ENOMEM);
7238
7239 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007240 INIT_LIST_HEAD(&sqd->ctx_list);
7241 INIT_LIST_HEAD(&sqd->ctx_new_list);
7242 mutex_init(&sqd->ctx_lock);
7243 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007244 init_waitqueue_head(&sqd->wait);
7245 return sqd;
7246}
7247
Jens Axboe69fb2132020-09-14 11:16:23 -06007248static void io_sq_thread_unpark(struct io_sq_data *sqd)
7249 __releases(&sqd->lock)
7250{
7251 if (!sqd->thread)
7252 return;
7253 kthread_unpark(sqd->thread);
7254 mutex_unlock(&sqd->lock);
7255}
7256
7257static void io_sq_thread_park(struct io_sq_data *sqd)
7258 __acquires(&sqd->lock)
7259{
7260 if (!sqd->thread)
7261 return;
7262 mutex_lock(&sqd->lock);
7263 kthread_park(sqd->thread);
7264}
7265
Jens Axboe534ca6d2020-09-02 13:52:19 -06007266static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7267{
7268 struct io_sq_data *sqd = ctx->sq_data;
7269
7270 if (sqd) {
7271 if (sqd->thread) {
7272 /*
7273 * We may arrive here from the error branch in
7274 * io_sq_offload_create() where the kthread is created
7275 * without being waked up, thus wake it up now to make
7276 * sure the wait will complete.
7277 */
7278 wake_up_process(sqd->thread);
7279 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007280
7281 io_sq_thread_park(sqd);
7282 }
7283
7284 mutex_lock(&sqd->ctx_lock);
7285 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007286 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007287 mutex_unlock(&sqd->ctx_lock);
7288
Xiaoguang Wang08369242020-11-03 14:15:59 +08007289 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007290 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007291
7292 io_put_sq_data(sqd);
7293 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007294 }
7295}
7296
Jens Axboe6b063142019-01-10 22:13:58 -07007297#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007298/*
7299 * Ensure the UNIX gc is aware of our file set, so we are certain that
7300 * the io_uring can be safely unregistered on process exit, even if we have
7301 * loops in the file referencing.
7302 */
7303static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7304{
7305 struct sock *sk = ctx->ring_sock->sk;
7306 struct scm_fp_list *fpl;
7307 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007308 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007309
Jens Axboe6b063142019-01-10 22:13:58 -07007310 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7311 if (!fpl)
7312 return -ENOMEM;
7313
7314 skb = alloc_skb(0, GFP_KERNEL);
7315 if (!skb) {
7316 kfree(fpl);
7317 return -ENOMEM;
7318 }
7319
7320 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007321
Jens Axboe08a45172019-10-03 08:11:03 -06007322 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007323 fpl->user = get_uid(ctx->user);
7324 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007325 struct file *file = io_file_from_index(ctx, i + offset);
7326
7327 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007328 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007329 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007330 unix_inflight(fpl->user, fpl->fp[nr_files]);
7331 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007332 }
7333
Jens Axboe08a45172019-10-03 08:11:03 -06007334 if (nr_files) {
7335 fpl->max = SCM_MAX_FD;
7336 fpl->count = nr_files;
7337 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007338 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007339 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7340 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007341
Jens Axboe08a45172019-10-03 08:11:03 -06007342 for (i = 0; i < nr_files; i++)
7343 fput(fpl->fp[i]);
7344 } else {
7345 kfree_skb(skb);
7346 kfree(fpl);
7347 }
Jens Axboe6b063142019-01-10 22:13:58 -07007348
7349 return 0;
7350}
7351
7352/*
7353 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7354 * causes regular reference counting to break down. We rely on the UNIX
7355 * garbage collection to take care of this problem for us.
7356 */
7357static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7358{
7359 unsigned left, total;
7360 int ret = 0;
7361
7362 total = 0;
7363 left = ctx->nr_user_files;
7364 while (left) {
7365 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007366
7367 ret = __io_sqe_files_scm(ctx, this_files, total);
7368 if (ret)
7369 break;
7370 left -= this_files;
7371 total += this_files;
7372 }
7373
7374 if (!ret)
7375 return 0;
7376
7377 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007378 struct file *file = io_file_from_index(ctx, total);
7379
7380 if (file)
7381 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007382 total++;
7383 }
7384
7385 return ret;
7386}
7387#else
7388static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7389{
7390 return 0;
7391}
7392#endif
7393
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007394static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007395 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007396{
7397 int i;
7398
7399 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007400 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007401 unsigned this_files;
7402
7403 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7404 table->files = kcalloc(this_files, sizeof(struct file *),
7405 GFP_KERNEL);
7406 if (!table->files)
7407 break;
7408 nr_files -= this_files;
7409 }
7410
7411 if (i == nr_tables)
7412 return 0;
7413
7414 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007415 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007416 kfree(table->files);
7417 }
7418 return 1;
7419}
7420
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007421static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007422{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007423 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007424#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007425 struct sock *sock = ctx->ring_sock->sk;
7426 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7427 struct sk_buff *skb;
7428 int i;
7429
7430 __skb_queue_head_init(&list);
7431
7432 /*
7433 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7434 * remove this entry and rearrange the file array.
7435 */
7436 skb = skb_dequeue(head);
7437 while (skb) {
7438 struct scm_fp_list *fp;
7439
7440 fp = UNIXCB(skb).fp;
7441 for (i = 0; i < fp->count; i++) {
7442 int left;
7443
7444 if (fp->fp[i] != file)
7445 continue;
7446
7447 unix_notinflight(fp->user, fp->fp[i]);
7448 left = fp->count - 1 - i;
7449 if (left) {
7450 memmove(&fp->fp[i], &fp->fp[i + 1],
7451 left * sizeof(struct file *));
7452 }
7453 fp->count--;
7454 if (!fp->count) {
7455 kfree_skb(skb);
7456 skb = NULL;
7457 } else {
7458 __skb_queue_tail(&list, skb);
7459 }
7460 fput(file);
7461 file = NULL;
7462 break;
7463 }
7464
7465 if (!file)
7466 break;
7467
7468 __skb_queue_tail(&list, skb);
7469
7470 skb = skb_dequeue(head);
7471 }
7472
7473 if (skb_peek(&list)) {
7474 spin_lock_irq(&head->lock);
7475 while ((skb = __skb_dequeue(&list)) != NULL)
7476 __skb_queue_tail(head, skb);
7477 spin_unlock_irq(&head->lock);
7478 }
7479#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007480 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007481#endif
7482}
7483
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007484static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007485{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007486 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7487 struct io_ring_ctx *ctx = rsrc_data->ctx;
7488 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007489
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007490 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7491 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007492 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007493 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007494 }
7495
Xiaoguang Wang05589552020-03-31 14:05:18 +08007496 percpu_ref_exit(&ref_node->refs);
7497 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007498 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007499}
7500
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007501static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007502{
7503 struct io_ring_ctx *ctx;
7504 struct llist_node *node;
7505
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007506 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7507 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007508
7509 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007510 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007511 struct llist_node *next = node->next;
7512
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007513 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7514 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007515 node = next;
7516 }
7517}
7518
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007519static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7520 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007521{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007522 struct fixed_rsrc_table *table;
7523
7524 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7525 return &table->files[i & IORING_FILE_TABLE_MASK];
7526}
7527
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007528static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007529{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007530 struct fixed_rsrc_ref_node *ref_node;
7531 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007532 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007533 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007534 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007535
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007536 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7537 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007538 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007539
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007540 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007541 ref_node->done = true;
7542
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007543 while (!list_empty(&ctx->rsrc_ref_list)) {
7544 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007545 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007546 /* recycle ref nodes in order */
7547 if (!ref_node->done)
7548 break;
7549 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007550 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007551 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007552 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007553
7554 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007555 delay = 0;
7556
Jens Axboe4a38aed22020-05-14 17:21:15 -06007557 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007558 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007559 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007560 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007561}
7562
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007563static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007564 struct io_ring_ctx *ctx)
7565{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007566 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007567
7568 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7569 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007570 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007571
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007572 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007573 0, GFP_KERNEL)) {
7574 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007575 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007576 }
7577 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007578 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007579 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007580 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007581}
7582
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007583static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7584 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007585{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007586 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007587 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007588}
7589
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007590static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007591{
7592 percpu_ref_exit(&ref_node->refs);
7593 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007594}
7595
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007596
Jens Axboe05f3fb32019-12-09 11:22:50 -07007597static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7598 unsigned nr_args)
7599{
7600 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007601 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007602 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007603 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007604 struct fixed_rsrc_ref_node *ref_node;
7605 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007606
7607 if (ctx->file_data)
7608 return -EBUSY;
7609 if (!nr_args)
7610 return -EINVAL;
7611 if (nr_args > IORING_MAX_FIXED_FILES)
7612 return -EMFILE;
7613
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007614 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007615 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007616 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007617 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007618
7619 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007620 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007621 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007622 if (!file_data->table)
7623 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007624
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007625 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007626 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007627
Jens Axboe05f3fb32019-12-09 11:22:50 -07007628 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007629 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7630 ret = -EFAULT;
7631 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007632 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007633 /* allow sparse sets */
7634 if (fd == -1)
7635 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007636
Jens Axboe05f3fb32019-12-09 11:22:50 -07007637 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007638 ret = -EBADF;
7639 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007640 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007641
7642 /*
7643 * Don't allow io_uring instances to be registered. If UNIX
7644 * isn't enabled, then this causes a reference cycle and this
7645 * instance can never get freed. If UNIX is enabled we'll
7646 * handle it just fine, but there's still no point in allowing
7647 * a ring fd as it doesn't support regular read/write anyway.
7648 */
7649 if (file->f_op == &io_uring_fops) {
7650 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007651 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007652 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007653 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007654 }
7655
Jens Axboe05f3fb32019-12-09 11:22:50 -07007656 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007657 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007658 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007659 return ret;
7660 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007661
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007662 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007663 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007664 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007665 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007666 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007667 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007668
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007669 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007670 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007671out_fput:
7672 for (i = 0; i < ctx->nr_user_files; i++) {
7673 file = io_file_from_index(ctx, i);
7674 if (file)
7675 fput(file);
7676 }
7677 for (i = 0; i < nr_tables; i++)
7678 kfree(file_data->table[i].files);
7679 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007680out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007681 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007682 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007683 return ret;
7684}
7685
Jens Axboec3a31e62019-10-03 13:59:56 -06007686static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7687 int index)
7688{
7689#if defined(CONFIG_UNIX)
7690 struct sock *sock = ctx->ring_sock->sk;
7691 struct sk_buff_head *head = &sock->sk_receive_queue;
7692 struct sk_buff *skb;
7693
7694 /*
7695 * See if we can merge this file into an existing skb SCM_RIGHTS
7696 * file set. If there's no room, fall back to allocating a new skb
7697 * and filling it in.
7698 */
7699 spin_lock_irq(&head->lock);
7700 skb = skb_peek(head);
7701 if (skb) {
7702 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7703
7704 if (fpl->count < SCM_MAX_FD) {
7705 __skb_unlink(skb, head);
7706 spin_unlock_irq(&head->lock);
7707 fpl->fp[fpl->count] = get_file(file);
7708 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7709 fpl->count++;
7710 spin_lock_irq(&head->lock);
7711 __skb_queue_head(head, skb);
7712 } else {
7713 skb = NULL;
7714 }
7715 }
7716 spin_unlock_irq(&head->lock);
7717
7718 if (skb) {
7719 fput(file);
7720 return 0;
7721 }
7722
7723 return __io_sqe_files_scm(ctx, 1, index);
7724#else
7725 return 0;
7726#endif
7727}
7728
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007729static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007730{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007731 struct io_rsrc_put *prsrc;
7732 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007733
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007734 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7735 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007736 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007737
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007738 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007739 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007740
Hillf Dantona5318d32020-03-23 17:47:15 +08007741 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007742}
7743
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007744static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7745 struct file *file)
7746{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007747 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007748}
7749
Jens Axboe05f3fb32019-12-09 11:22:50 -07007750static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007751 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07007752 unsigned nr_args)
7753{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007754 struct fixed_rsrc_data *data = ctx->file_data;
7755 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007756 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06007757 __s32 __user *fds;
7758 int fd, i, err;
7759 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007760 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06007761
Jens Axboe05f3fb32019-12-09 11:22:50 -07007762 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06007763 return -EOVERFLOW;
7764 if (done > ctx->nr_user_files)
7765 return -EINVAL;
7766
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007767 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007768 if (!ref_node)
7769 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007770 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007771
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007772 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00007773 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007774 err = 0;
7775 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
7776 err = -EFAULT;
7777 break;
7778 }
noah4e0377a2021-01-26 15:23:28 -05007779 if (fd == IORING_REGISTER_FILES_SKIP)
7780 continue;
7781
Pavel Begunkov67973b92021-01-26 13:51:09 +00007782 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007783 file_slot = io_fixed_file_slot(ctx->file_data, i);
7784
7785 if (*file_slot) {
7786 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08007787 if (err)
7788 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007789 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007790 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06007791 }
7792 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06007793 file = fget(fd);
7794 if (!file) {
7795 err = -EBADF;
7796 break;
7797 }
7798 /*
7799 * Don't allow io_uring instances to be registered. If
7800 * UNIX isn't enabled, then this causes a reference
7801 * cycle and this instance can never get freed. If UNIX
7802 * is enabled we'll handle it just fine, but there's
7803 * still no point in allowing a ring fd as it doesn't
7804 * support regular read/write anyway.
7805 */
7806 if (file->f_op == &io_uring_fops) {
7807 fput(file);
7808 err = -EBADF;
7809 break;
7810 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07007811 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007812 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007813 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07007814 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007815 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007816 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00007817 }
Jens Axboec3a31e62019-10-03 13:59:56 -06007818 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007819 }
7820
Xiaoguang Wang05589552020-03-31 14:05:18 +08007821 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01007822 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007823 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007824 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007825 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06007826
7827 return done ? done : err;
7828}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007829
Jens Axboe05f3fb32019-12-09 11:22:50 -07007830static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
7831 unsigned nr_args)
7832{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007833 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007834
7835 if (!ctx->file_data)
7836 return -ENXIO;
7837 if (!nr_args)
7838 return -EINVAL;
7839 if (copy_from_user(&up, arg, sizeof(up)))
7840 return -EFAULT;
7841 if (up.resv)
7842 return -EINVAL;
7843
7844 return __io_sqe_files_update(ctx, &up, nr_args);
7845}
Jens Axboec3a31e62019-10-03 13:59:56 -06007846
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007847static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07007848{
7849 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
7850
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00007851 req = io_put_req_find_next(req);
7852 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07007853}
7854
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007855static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03007856{
7857 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007858 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007859
7860 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03007861 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03007862 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03007863
Jens Axboed25e3a32021-02-16 11:41:41 -07007864 /* Do QD, or 4 * CPUS, whatever is smallest */
7865 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03007866
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007867 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03007868}
7869
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007870static int io_uring_alloc_task_context(struct task_struct *task,
7871 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06007872{
7873 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06007874 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06007875
7876 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
7877 if (unlikely(!tctx))
7878 return -ENOMEM;
7879
Jens Axboed8a6df12020-10-15 16:24:45 -06007880 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
7881 if (unlikely(ret)) {
7882 kfree(tctx);
7883 return ret;
7884 }
7885
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007886 tctx->io_wq = io_init_wq_offload(ctx);
7887 if (IS_ERR(tctx->io_wq)) {
7888 ret = PTR_ERR(tctx->io_wq);
7889 percpu_counter_destroy(&tctx->inflight);
7890 kfree(tctx);
7891 return ret;
7892 }
7893
Jens Axboe0f212202020-09-13 13:09:39 -06007894 xa_init(&tctx->xa);
7895 init_waitqueue_head(&tctx->wait);
7896 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06007897 atomic_set(&tctx->in_idle, 0);
7898 tctx->sqpoll = false;
Jens Axboe0f212202020-09-13 13:09:39 -06007899 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00007900 spin_lock_init(&tctx->task_lock);
7901 INIT_WQ_LIST(&tctx->task_list);
7902 tctx->task_state = 0;
7903 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06007904 return 0;
7905}
7906
7907void __io_uring_free(struct task_struct *tsk)
7908{
7909 struct io_uring_task *tctx = tsk->io_uring;
7910
7911 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboed8a6df12020-10-15 16:24:45 -06007912 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06007913 kfree(tctx);
7914 tsk->io_uring = NULL;
7915}
7916
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007917static int io_sq_offload_create(struct io_ring_ctx *ctx,
7918 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007919{
7920 int ret;
7921
Jens Axboed25e3a32021-02-16 11:41:41 -07007922 /* Retain compatibility with failing for an invalid attach attempt */
7923 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
7924 IORING_SETUP_ATTACH_WQ) {
7925 struct fd f;
7926
7927 f = fdget(p->wq_fd);
7928 if (!f.file)
7929 return -ENXIO;
7930 if (f.file->f_op != &io_uring_fops) {
7931 fdput(f);
7932 return -EINVAL;
7933 }
7934 fdput(f);
7935 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07007936 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06007937 struct io_sq_data *sqd;
7938
Jens Axboe3ec482d2019-04-08 10:51:01 -06007939 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06007940 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06007941 goto err;
7942
Jens Axboe534ca6d2020-09-02 13:52:19 -06007943 sqd = io_get_sq_data(p);
7944 if (IS_ERR(sqd)) {
7945 ret = PTR_ERR(sqd);
7946 goto err;
7947 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007948
Jens Axboe534ca6d2020-09-02 13:52:19 -06007949 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06007950 io_sq_thread_park(sqd);
7951 mutex_lock(&sqd->ctx_lock);
7952 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
7953 mutex_unlock(&sqd->ctx_lock);
7954 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007955
Jens Axboe917257d2019-04-13 09:28:55 -06007956 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
7957 if (!ctx->sq_thread_idle)
7958 ctx->sq_thread_idle = HZ;
7959
Jens Axboeaa061652020-09-02 14:50:27 -06007960 if (sqd->thread)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007961 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06007962
Jens Axboe6c271ce2019-01-10 11:22:30 -07007963 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06007964 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007965
Jens Axboe917257d2019-04-13 09:28:55 -06007966 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06007967 if (cpu >= nr_cpu_ids)
7968 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08007969 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06007970 goto err;
7971
Jens Axboe69fb2132020-09-14 11:16:23 -06007972 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06007973 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07007974 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06007975 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07007976 "io_uring-sq");
7977 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06007978 if (IS_ERR(sqd->thread)) {
7979 ret = PTR_ERR(sqd->thread);
7980 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007981 goto err;
7982 }
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007983 ret = io_uring_alloc_task_context(sqd->thread, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06007984 if (ret)
7985 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007986 } else if (p->flags & IORING_SETUP_SQ_AFF) {
7987 /* Can't have SQ_AFF without SQPOLL */
7988 ret = -EINVAL;
7989 goto err;
7990 }
7991
Jens Axboe2b188cc2019-01-07 10:46:33 -07007992 return 0;
7993err:
Jens Axboe5aa75ed2021-02-16 12:56:50 -07007994 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007995 return ret;
7996}
7997
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02007998static void io_sq_offload_start(struct io_ring_ctx *ctx)
7999{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008000 struct io_sq_data *sqd = ctx->sq_data;
8001
8002 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8003 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008004}
8005
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008006static inline void __io_unaccount_mem(struct user_struct *user,
8007 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008008{
8009 atomic_long_sub(nr_pages, &user->locked_vm);
8010}
8011
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008012static inline int __io_account_mem(struct user_struct *user,
8013 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008014{
8015 unsigned long page_limit, cur_pages, new_pages;
8016
8017 /* Don't allow more pages than we can safely lock */
8018 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8019
8020 do {
8021 cur_pages = atomic_long_read(&user->locked_vm);
8022 new_pages = cur_pages + nr_pages;
8023 if (new_pages > page_limit)
8024 return -ENOMEM;
8025 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8026 new_pages) != cur_pages);
8027
8028 return 0;
8029}
8030
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008031static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008032{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008033 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008034 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008035
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008036 if (ctx->mm_account)
8037 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008038}
8039
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008040static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008041{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008042 int ret;
8043
8044 if (ctx->limit_mem) {
8045 ret = __io_account_mem(ctx->user, nr_pages);
8046 if (ret)
8047 return ret;
8048 }
8049
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008050 if (ctx->mm_account)
8051 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008052
8053 return 0;
8054}
8055
Jens Axboe2b188cc2019-01-07 10:46:33 -07008056static void io_mem_free(void *ptr)
8057{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008058 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008059
Mark Rutland52e04ef2019-04-30 17:30:21 +01008060 if (!ptr)
8061 return;
8062
8063 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008064 if (put_page_testzero(page))
8065 free_compound_page(page);
8066}
8067
8068static void *io_mem_alloc(size_t size)
8069{
8070 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008071 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008072
8073 return (void *) __get_free_pages(gfp_flags, get_order(size));
8074}
8075
Hristo Venev75b28af2019-08-26 17:23:46 +00008076static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8077 size_t *sq_offset)
8078{
8079 struct io_rings *rings;
8080 size_t off, sq_array_size;
8081
8082 off = struct_size(rings, cqes, cq_entries);
8083 if (off == SIZE_MAX)
8084 return SIZE_MAX;
8085
8086#ifdef CONFIG_SMP
8087 off = ALIGN(off, SMP_CACHE_BYTES);
8088 if (off == 0)
8089 return SIZE_MAX;
8090#endif
8091
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008092 if (sq_offset)
8093 *sq_offset = off;
8094
Hristo Venev75b28af2019-08-26 17:23:46 +00008095 sq_array_size = array_size(sizeof(u32), sq_entries);
8096 if (sq_array_size == SIZE_MAX)
8097 return SIZE_MAX;
8098
8099 if (check_add_overflow(off, sq_array_size, &off))
8100 return SIZE_MAX;
8101
Hristo Venev75b28af2019-08-26 17:23:46 +00008102 return off;
8103}
8104
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008105static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008106{
8107 int i, j;
8108
8109 if (!ctx->user_bufs)
8110 return -ENXIO;
8111
8112 for (i = 0; i < ctx->nr_user_bufs; i++) {
8113 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8114
8115 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008116 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008117
Jens Axboede293932020-09-17 16:19:16 -06008118 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008119 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008120 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008121 imu->nr_bvecs = 0;
8122 }
8123
8124 kfree(ctx->user_bufs);
8125 ctx->user_bufs = NULL;
8126 ctx->nr_user_bufs = 0;
8127 return 0;
8128}
8129
8130static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8131 void __user *arg, unsigned index)
8132{
8133 struct iovec __user *src;
8134
8135#ifdef CONFIG_COMPAT
8136 if (ctx->compat) {
8137 struct compat_iovec __user *ciovs;
8138 struct compat_iovec ciov;
8139
8140 ciovs = (struct compat_iovec __user *) arg;
8141 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8142 return -EFAULT;
8143
Jens Axboed55e5f52019-12-11 16:12:15 -07008144 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008145 dst->iov_len = ciov.iov_len;
8146 return 0;
8147 }
8148#endif
8149 src = (struct iovec __user *) arg;
8150 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8151 return -EFAULT;
8152 return 0;
8153}
8154
Jens Axboede293932020-09-17 16:19:16 -06008155/*
8156 * Not super efficient, but this is just a registration time. And we do cache
8157 * the last compound head, so generally we'll only do a full search if we don't
8158 * match that one.
8159 *
8160 * We check if the given compound head page has already been accounted, to
8161 * avoid double accounting it. This allows us to account the full size of the
8162 * page, not just the constituent pages of a huge page.
8163 */
8164static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8165 int nr_pages, struct page *hpage)
8166{
8167 int i, j;
8168
8169 /* check current page array */
8170 for (i = 0; i < nr_pages; i++) {
8171 if (!PageCompound(pages[i]))
8172 continue;
8173 if (compound_head(pages[i]) == hpage)
8174 return true;
8175 }
8176
8177 /* check previously registered pages */
8178 for (i = 0; i < ctx->nr_user_bufs; i++) {
8179 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8180
8181 for (j = 0; j < imu->nr_bvecs; j++) {
8182 if (!PageCompound(imu->bvec[j].bv_page))
8183 continue;
8184 if (compound_head(imu->bvec[j].bv_page) == hpage)
8185 return true;
8186 }
8187 }
8188
8189 return false;
8190}
8191
8192static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8193 int nr_pages, struct io_mapped_ubuf *imu,
8194 struct page **last_hpage)
8195{
8196 int i, ret;
8197
8198 for (i = 0; i < nr_pages; i++) {
8199 if (!PageCompound(pages[i])) {
8200 imu->acct_pages++;
8201 } else {
8202 struct page *hpage;
8203
8204 hpage = compound_head(pages[i]);
8205 if (hpage == *last_hpage)
8206 continue;
8207 *last_hpage = hpage;
8208 if (headpage_already_acct(ctx, pages, i, hpage))
8209 continue;
8210 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8211 }
8212 }
8213
8214 if (!imu->acct_pages)
8215 return 0;
8216
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008217 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008218 if (ret)
8219 imu->acct_pages = 0;
8220 return ret;
8221}
8222
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008223static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8224 struct io_mapped_ubuf *imu,
8225 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008226{
8227 struct vm_area_struct **vmas = NULL;
8228 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008229 unsigned long off, start, end, ubuf;
8230 size_t size;
8231 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008232
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008233 ubuf = (unsigned long) iov->iov_base;
8234 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8235 start = ubuf >> PAGE_SHIFT;
8236 nr_pages = end - start;
8237
8238 ret = -ENOMEM;
8239
8240 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8241 if (!pages)
8242 goto done;
8243
8244 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8245 GFP_KERNEL);
8246 if (!vmas)
8247 goto done;
8248
8249 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8250 GFP_KERNEL);
8251 if (!imu->bvec)
8252 goto done;
8253
8254 ret = 0;
8255 mmap_read_lock(current->mm);
8256 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8257 pages, vmas);
8258 if (pret == nr_pages) {
8259 /* don't support file backed memory */
8260 for (i = 0; i < nr_pages; i++) {
8261 struct vm_area_struct *vma = vmas[i];
8262
8263 if (vma->vm_file &&
8264 !is_file_hugepages(vma->vm_file)) {
8265 ret = -EOPNOTSUPP;
8266 break;
8267 }
8268 }
8269 } else {
8270 ret = pret < 0 ? pret : -EFAULT;
8271 }
8272 mmap_read_unlock(current->mm);
8273 if (ret) {
8274 /*
8275 * if we did partial map, or found file backed vmas,
8276 * release any pages we did get
8277 */
8278 if (pret > 0)
8279 unpin_user_pages(pages, pret);
8280 kvfree(imu->bvec);
8281 goto done;
8282 }
8283
8284 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8285 if (ret) {
8286 unpin_user_pages(pages, pret);
8287 kvfree(imu->bvec);
8288 goto done;
8289 }
8290
8291 off = ubuf & ~PAGE_MASK;
8292 size = iov->iov_len;
8293 for (i = 0; i < nr_pages; i++) {
8294 size_t vec_len;
8295
8296 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8297 imu->bvec[i].bv_page = pages[i];
8298 imu->bvec[i].bv_len = vec_len;
8299 imu->bvec[i].bv_offset = off;
8300 off = 0;
8301 size -= vec_len;
8302 }
8303 /* store original address for later verification */
8304 imu->ubuf = ubuf;
8305 imu->len = iov->iov_len;
8306 imu->nr_bvecs = nr_pages;
8307 ret = 0;
8308done:
8309 kvfree(pages);
8310 kvfree(vmas);
8311 return ret;
8312}
8313
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008314static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008315{
Jens Axboeedafcce2019-01-09 09:16:05 -07008316 if (ctx->user_bufs)
8317 return -EBUSY;
8318 if (!nr_args || nr_args > UIO_MAXIOV)
8319 return -EINVAL;
8320
8321 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8322 GFP_KERNEL);
8323 if (!ctx->user_bufs)
8324 return -ENOMEM;
8325
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008326 return 0;
8327}
8328
8329static int io_buffer_validate(struct iovec *iov)
8330{
8331 /*
8332 * Don't impose further limits on the size and buffer
8333 * constraints here, we'll -EINVAL later when IO is
8334 * submitted if they are wrong.
8335 */
8336 if (!iov->iov_base || !iov->iov_len)
8337 return -EFAULT;
8338
8339 /* arbitrary limit, but we need something */
8340 if (iov->iov_len > SZ_1G)
8341 return -EFAULT;
8342
8343 return 0;
8344}
8345
8346static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8347 unsigned int nr_args)
8348{
8349 int i, ret;
8350 struct iovec iov;
8351 struct page *last_hpage = NULL;
8352
8353 ret = io_buffers_map_alloc(ctx, nr_args);
8354 if (ret)
8355 return ret;
8356
Jens Axboeedafcce2019-01-09 09:16:05 -07008357 for (i = 0; i < nr_args; i++) {
8358 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008359
8360 ret = io_copy_iov(ctx, &iov, arg, i);
8361 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008362 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008363
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008364 ret = io_buffer_validate(&iov);
8365 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008366 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008367
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008368 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8369 if (ret)
8370 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008371
8372 ctx->nr_user_bufs++;
8373 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008374
8375 if (ret)
8376 io_sqe_buffers_unregister(ctx);
8377
Jens Axboeedafcce2019-01-09 09:16:05 -07008378 return ret;
8379}
8380
Jens Axboe9b402842019-04-11 11:45:41 -06008381static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8382{
8383 __s32 __user *fds = arg;
8384 int fd;
8385
8386 if (ctx->cq_ev_fd)
8387 return -EBUSY;
8388
8389 if (copy_from_user(&fd, fds, sizeof(*fds)))
8390 return -EFAULT;
8391
8392 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8393 if (IS_ERR(ctx->cq_ev_fd)) {
8394 int ret = PTR_ERR(ctx->cq_ev_fd);
8395 ctx->cq_ev_fd = NULL;
8396 return ret;
8397 }
8398
8399 return 0;
8400}
8401
8402static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8403{
8404 if (ctx->cq_ev_fd) {
8405 eventfd_ctx_put(ctx->cq_ev_fd);
8406 ctx->cq_ev_fd = NULL;
8407 return 0;
8408 }
8409
8410 return -ENXIO;
8411}
8412
Jens Axboe5a2e7452020-02-23 16:23:11 -07008413static int __io_destroy_buffers(int id, void *p, void *data)
8414{
8415 struct io_ring_ctx *ctx = data;
8416 struct io_buffer *buf = p;
8417
Jens Axboe067524e2020-03-02 16:32:28 -07008418 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008419 return 0;
8420}
8421
8422static void io_destroy_buffers(struct io_ring_ctx *ctx)
8423{
8424 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8425 idr_destroy(&ctx->io_buffer_idr);
8426}
8427
Jens Axboe68e68ee2021-02-13 09:00:02 -07008428static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008429{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008430 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008431
Jens Axboe68e68ee2021-02-13 09:00:02 -07008432 list_for_each_entry_safe(req, nxt, list, compl.list) {
8433 if (tsk && req->task != tsk)
8434 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008435 list_del(&req->compl.list);
8436 kmem_cache_free(req_cachep, req);
8437 }
8438}
8439
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008440static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008441{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008442 struct io_submit_state *submit_state = &ctx->submit_state;
8443
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008444 mutex_lock(&ctx->uring_lock);
8445
8446 if (submit_state->free_reqs)
8447 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8448 submit_state->reqs);
8449
8450 io_req_cache_free(&submit_state->comp.free_list, NULL);
8451
8452 spin_lock_irq(&ctx->completion_lock);
8453 io_req_cache_free(&submit_state->comp.locked_free_list, NULL);
8454 spin_unlock_irq(&ctx->completion_lock);
8455
8456 mutex_unlock(&ctx->uring_lock);
8457}
8458
Jens Axboe2b188cc2019-01-07 10:46:33 -07008459static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8460{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008461 /*
8462 * Some may use context even when all refs and requests have been put,
8463 * and they are free to do so while still holding uring_lock, see
8464 * __io_req_task_submit(). Wait for them to finish.
8465 */
8466 mutex_lock(&ctx->uring_lock);
8467 mutex_unlock(&ctx->uring_lock);
8468
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008469 io_sq_thread_stop(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008470 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008471
8472 if (ctx->sqo_task) {
8473 put_task_struct(ctx->sqo_task);
8474 ctx->sqo_task = NULL;
8475 mmdrop(ctx->mm_account);
8476 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008477 }
Jens Axboedef596e2019-01-09 08:59:42 -07008478
Dennis Zhou91d8f512020-09-16 13:41:05 -07008479#ifdef CONFIG_BLK_CGROUP
8480 if (ctx->sqo_blkcg_css)
8481 css_put(ctx->sqo_blkcg_css);
8482#endif
8483
Hao Xu8bad28d2021-02-19 17:19:36 +08008484 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008485 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008486 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008487 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008488 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008489 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008490
Jens Axboe2b188cc2019-01-07 10:46:33 -07008491#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008492 if (ctx->ring_sock) {
8493 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008494 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008495 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008496#endif
8497
Hristo Venev75b28af2019-08-26 17:23:46 +00008498 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008499 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008500
8501 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008502 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008503 put_cred(ctx->creds);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008504 io_req_caches_free(ctx, NULL);
Jens Axboe78076bb2019-12-04 19:56:40 -07008505 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008506 kfree(ctx);
8507}
8508
8509static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8510{
8511 struct io_ring_ctx *ctx = file->private_data;
8512 __poll_t mask = 0;
8513
8514 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008515 /*
8516 * synchronizes with barrier from wq_has_sleeper call in
8517 * io_commit_cqring
8518 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008519 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008520 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008521 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008522
8523 /*
8524 * Don't flush cqring overflow list here, just do a simple check.
8525 * Otherwise there could possible be ABBA deadlock:
8526 * CPU0 CPU1
8527 * ---- ----
8528 * lock(&ctx->uring_lock);
8529 * lock(&ep->mtx);
8530 * lock(&ctx->uring_lock);
8531 * lock(&ep->mtx);
8532 *
8533 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8534 * pushs them to do the flush.
8535 */
8536 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008537 mask |= EPOLLIN | EPOLLRDNORM;
8538
8539 return mask;
8540}
8541
8542static int io_uring_fasync(int fd, struct file *file, int on)
8543{
8544 struct io_ring_ctx *ctx = file->private_data;
8545
8546 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8547}
8548
Yejune Deng0bead8c2020-12-24 11:02:20 +08008549static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008550{
Jens Axboe4379bf82021-02-15 13:40:22 -07008551 const struct cred *creds;
Jens Axboe071698e2020-01-28 10:04:42 -07008552
Jens Axboe4379bf82021-02-15 13:40:22 -07008553 creds = idr_remove(&ctx->personality_idr, id);
8554 if (creds) {
8555 put_cred(creds);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008556 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008557 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008558
8559 return -EINVAL;
8560}
8561
8562static int io_remove_personalities(int id, void *p, void *data)
8563{
8564 struct io_ring_ctx *ctx = data;
8565
8566 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008567 return 0;
8568}
8569
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008570static void io_run_ctx_fallback(struct io_ring_ctx *ctx)
8571{
8572 struct callback_head *work, *head, *next;
8573
8574 do {
8575 do {
8576 head = NULL;
8577 work = READ_ONCE(ctx->exit_task_work);
8578 } while (cmpxchg(&ctx->exit_task_work, work, head) != work);
8579
8580 if (!work)
8581 break;
8582
8583 do {
8584 next = work->next;
8585 work->func(work);
8586 work = next;
8587 cond_resched();
8588 } while (work);
8589 } while (1);
8590}
8591
Jens Axboe85faa7b2020-04-09 18:14:00 -06008592static void io_ring_exit_work(struct work_struct *work)
8593{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008594 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8595 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008596
Jens Axboe56952e92020-06-17 15:00:04 -06008597 /*
8598 * If we're doing polled IO and end up having requests being
8599 * submitted async (out-of-line), then completions can come in while
8600 * we're waiting for refs to drop. We need to reap these manually,
8601 * as nobody else will be looking for them.
8602 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008603 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008604 io_uring_try_cancel_requests(ctx, NULL, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008605 io_run_ctx_fallback(ctx);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008606 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008607 io_ring_ctx_free(ctx);
8608}
8609
Jens Axboe2b188cc2019-01-07 10:46:33 -07008610static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8611{
8612 mutex_lock(&ctx->uring_lock);
8613 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008614
8615 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8616 ctx->sqo_dead = 1;
8617
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008618 /* if force is set, the ring is going away. always drop after that */
8619 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008620 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008621 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008622 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008623 mutex_unlock(&ctx->uring_lock);
8624
Pavel Begunkov6b819282020-11-06 13:00:25 +00008625 io_kill_timeouts(ctx, NULL, NULL);
8626 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008627
Jens Axboe15dff282019-11-13 09:09:23 -07008628 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008629 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008630
Jens Axboe85faa7b2020-04-09 18:14:00 -06008631 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008632 /*
8633 * Use system_unbound_wq to avoid spawning tons of event kworkers
8634 * if we're exiting a ton of rings at the same time. It just adds
8635 * noise and overhead, there's no discernable change in runtime
8636 * over using system_wq.
8637 */
8638 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008639}
8640
8641static int io_uring_release(struct inode *inode, struct file *file)
8642{
8643 struct io_ring_ctx *ctx = file->private_data;
8644
8645 file->private_data = NULL;
8646 io_ring_ctx_wait_and_kill(ctx);
8647 return 0;
8648}
8649
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008650struct io_task_cancel {
8651 struct task_struct *task;
8652 struct files_struct *files;
8653};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008654
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008655static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008656{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008657 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008658 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008659 bool ret;
8660
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008661 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008662 unsigned long flags;
8663 struct io_ring_ctx *ctx = req->ctx;
8664
8665 /* protect against races with linked timeouts */
8666 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008667 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008668 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8669 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008670 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008671 }
8672 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008673}
8674
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008675static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008676 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008677 struct files_struct *files)
8678{
8679 struct io_defer_entry *de = NULL;
8680 LIST_HEAD(list);
8681
8682 spin_lock_irq(&ctx->completion_lock);
8683 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008684 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008685 list_cut_position(&list, &ctx->defer_list, &de->list);
8686 break;
8687 }
8688 }
8689 spin_unlock_irq(&ctx->completion_lock);
8690
8691 while (!list_empty(&list)) {
8692 de = list_first_entry(&list, struct io_defer_entry, list);
8693 list_del_init(&de->list);
8694 req_set_fail_links(de->req);
8695 io_put_req(de->req);
8696 io_req_complete(de->req, -ECANCELED);
8697 kfree(de);
8698 }
8699}
8700
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008701static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8702 struct task_struct *task,
8703 struct files_struct *files)
8704{
8705 struct io_task_cancel cancel = { .task = task, .files = files, };
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008706 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008707
8708 while (1) {
8709 enum io_wq_cancel cret;
8710 bool ret = false;
8711
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008712 if (tctx && tctx->io_wq) {
8713 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008714 &cancel, true);
8715 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8716 }
8717
8718 /* SQPOLL thread does its own polling */
8719 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8720 while (!list_empty_careful(&ctx->iopoll_list)) {
8721 io_iopoll_try_reap_events(ctx);
8722 ret = true;
8723 }
8724 }
8725
8726 ret |= io_poll_remove_all(ctx, task, files);
8727 ret |= io_kill_timeouts(ctx, task, files);
8728 ret |= io_run_task_work();
8729 io_cqring_overflow_flush(ctx, true, task, files);
8730 if (!ret)
8731 break;
8732 cond_resched();
8733 }
8734}
8735
Pavel Begunkovca70f002021-01-26 15:28:27 +00008736static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8737 struct task_struct *task,
8738 struct files_struct *files)
8739{
8740 struct io_kiocb *req;
8741 int cnt = 0;
8742
8743 spin_lock_irq(&ctx->inflight_lock);
8744 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
8745 cnt += io_match_task(req, task, files);
8746 spin_unlock_irq(&ctx->inflight_lock);
8747 return cnt;
8748}
8749
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008750static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008751 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06008752 struct files_struct *files)
8753{
Jens Axboefcb323c2019-10-24 12:39:47 -06008754 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08008755 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008756 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06008757
Pavel Begunkovca70f002021-01-26 15:28:27 +00008758 inflight = io_uring_count_inflight(ctx, task, files);
8759 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06008760 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008761
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008762 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008763
Pavel Begunkov34343782021-02-10 11:45:42 +00008764 if (ctx->sq_data)
8765 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00008766 prepare_to_wait(&task->io_uring->wait, &wait,
8767 TASK_UNINTERRUPTIBLE);
8768 if (inflight == io_uring_count_inflight(ctx, task, files))
8769 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00008770 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00008771 if (ctx->sq_data)
8772 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06008773 }
Jens Axboe0f212202020-09-13 13:09:39 -06008774}
8775
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008776static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
8777{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008778 mutex_lock(&ctx->uring_lock);
8779 ctx->sqo_dead = 1;
8780 mutex_unlock(&ctx->uring_lock);
8781
8782 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00008783 if (ctx->rings)
8784 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008785}
8786
Jens Axboe0f212202020-09-13 13:09:39 -06008787/*
8788 * We need to iteratively cancel requests, in case a request has dependent
8789 * hard links. These persist even for failure of cancelations, hence keep
8790 * looping until none are found.
8791 */
8792static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
8793 struct files_struct *files)
8794{
8795 struct task_struct *task = current;
8796
Jens Axboefdaf0832020-10-30 09:37:30 -06008797 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008798 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008799 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06008800 atomic_inc(&task->io_uring->in_idle);
8801 io_sq_thread_park(ctx->sq_data);
8802 }
Jens Axboe0f212202020-09-13 13:09:39 -06008803
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00008804 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06008805
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00008806 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00008807 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008808 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008809
8810 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
8811 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06008812 io_sq_thread_unpark(ctx->sq_data);
8813 }
Jens Axboe0f212202020-09-13 13:09:39 -06008814}
8815
8816/*
8817 * Note that this task has used io_uring. We use it for cancelation purposes.
8818 */
Jens Axboefdaf0832020-10-30 09:37:30 -06008819static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06008820{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008821 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00008822 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008823
8824 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008825 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008826 if (unlikely(ret))
8827 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008828 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008829 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008830 if (tctx->last != file) {
8831 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008832
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008833 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06008834 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00008835 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
8836 file, GFP_KERNEL));
8837 if (ret) {
8838 fput(file);
8839 return ret;
8840 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00008841
8842 /* one and only SQPOLL file note, held by sqo_task */
8843 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
8844 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06008845 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01008846 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06008847 }
8848
Jens Axboefdaf0832020-10-30 09:37:30 -06008849 /*
8850 * This is race safe in that the task itself is doing this, hence it
8851 * cannot be going through the exit/cancel paths at the same time.
8852 * This cannot be modified while exit/cancel is running.
8853 */
8854 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
8855 tctx->sqpoll = true;
8856
Jens Axboe0f212202020-09-13 13:09:39 -06008857 return 0;
8858}
8859
8860/*
8861 * Remove this io_uring_file -> task mapping.
8862 */
8863static void io_uring_del_task_file(struct file *file)
8864{
8865 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06008866
8867 if (tctx->last == file)
8868 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01008869 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06008870 if (file)
8871 fput(file);
8872}
8873
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008874static void io_uring_remove_task_files(struct io_uring_task *tctx)
8875{
8876 struct file *file;
8877 unsigned long index;
8878
8879 xa_for_each(&tctx->xa, index, file)
8880 io_uring_del_task_file(file);
8881}
8882
Jens Axboe0f212202020-09-13 13:09:39 -06008883void __io_uring_files_cancel(struct files_struct *files)
8884{
8885 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01008886 struct file *file;
8887 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06008888
8889 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008890 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008891 xa_for_each(&tctx->xa, index, file)
8892 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06008893 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008894
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008895 if (files) {
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008896 io_uring_remove_task_files(tctx);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008897 } else if (tctx->io_wq && current->flags & PF_EXITING) {
8898 io_wq_destroy(tctx->io_wq);
8899 tctx->io_wq = NULL;
8900 }
Jens Axboefdaf0832020-10-30 09:37:30 -06008901}
8902
8903static s64 tctx_inflight(struct io_uring_task *tctx)
8904{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008905 return percpu_counter_sum(&tctx->inflight);
8906}
8907
8908static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
8909{
8910 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06008911 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008912 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06008913
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008914 if (!ctx->sq_data)
8915 return;
8916 tctx = ctx->sq_data->thread->io_uring;
8917 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06008918
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008919 atomic_inc(&tctx->in_idle);
8920 do {
8921 /* read completions before cancelations */
8922 inflight = tctx_inflight(tctx);
8923 if (!inflight)
8924 break;
8925 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06008926
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008927 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8928 /*
8929 * If we've seen completions, retry without waiting. This
8930 * avoids a race where a completion comes in before we did
8931 * prepare_to_wait().
8932 */
8933 if (inflight == tctx_inflight(tctx))
8934 schedule();
8935 finish_wait(&tctx->wait, &wait);
8936 } while (1);
8937 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008938}
8939
Jens Axboe0f212202020-09-13 13:09:39 -06008940/*
8941 * Find any io_uring fd that this task has registered or done IO on, and cancel
8942 * requests.
8943 */
8944void __io_uring_task_cancel(void)
8945{
8946 struct io_uring_task *tctx = current->io_uring;
8947 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008948 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06008949
8950 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06008951 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06008952
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008953 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00008954 if (tctx->sqpoll) {
8955 struct file *file;
8956 unsigned long index;
8957
8958 xa_for_each(&tctx->xa, index, file)
8959 io_uring_cancel_sqpoll(file->private_data);
8960 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00008961
Jens Axboed8a6df12020-10-15 16:24:45 -06008962 do {
Jens Axboe0f212202020-09-13 13:09:39 -06008963 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06008964 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06008965 if (!inflight)
8966 break;
Jens Axboe0f212202020-09-13 13:09:39 -06008967 __io_uring_files_cancel(NULL);
8968
8969 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
8970
8971 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008972 * If we've seen completions, retry without waiting. This
8973 * avoids a race where a completion comes in before we did
8974 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06008975 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00008976 if (inflight == tctx_inflight(tctx))
8977 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00008978 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06008979 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06008980
Jens Axboefdaf0832020-10-30 09:37:30 -06008981 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00008982
8983 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03008984}
8985
Jens Axboefcb323c2019-10-24 12:39:47 -06008986static int io_uring_flush(struct file *file, void *data)
8987{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008988 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008989 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00008990
Jens Axboe3bfe6102021-02-16 14:15:30 -07008991 /* Ignore helper thread files exit */
8992 if (current->flags & PF_IO_WORKER)
8993 return 0;
8994
Jens Axboe41be53e2021-02-13 09:11:04 -07008995 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07008996 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe41be53e2021-02-13 09:11:04 -07008997 io_req_caches_free(ctx, current);
8998 }
Jens Axboe84965ff2021-01-23 15:51:11 -07008999
Jens Axboe7c25c0d2021-02-16 07:17:00 -07009000 io_run_ctx_fallback(ctx);
9001
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009002 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009003 return 0;
9004
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009005 /* we should have cancelled and erased it before PF_EXITING */
9006 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9007 xa_load(&tctx->xa, (unsigned long)file));
9008
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009009 /*
9010 * fput() is pending, will be 2 if the only other ref is our potential
9011 * task file note. If the task is exiting, drop regardless of count.
9012 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009013 if (atomic_long_read(&file->f_count) != 2)
9014 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009015
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009016 if (ctx->flags & IORING_SETUP_SQPOLL) {
9017 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009018 WARN_ON_ONCE(ctx->sqo_task != current &&
9019 xa_load(&tctx->xa, (unsigned long)file));
9020 /* sqo_dead check is for when this happens after cancellation */
9021 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009022 !xa_load(&tctx->xa, (unsigned long)file));
9023
9024 io_disable_sqo_submit(ctx);
9025 }
9026
9027 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9028 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009029 return 0;
9030}
9031
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009032static void *io_uring_validate_mmap_request(struct file *file,
9033 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009034{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009035 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009036 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009037 struct page *page;
9038 void *ptr;
9039
9040 switch (offset) {
9041 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009042 case IORING_OFF_CQ_RING:
9043 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009044 break;
9045 case IORING_OFF_SQES:
9046 ptr = ctx->sq_sqes;
9047 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009048 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009049 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009050 }
9051
9052 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009053 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009054 return ERR_PTR(-EINVAL);
9055
9056 return ptr;
9057}
9058
9059#ifdef CONFIG_MMU
9060
9061static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9062{
9063 size_t sz = vma->vm_end - vma->vm_start;
9064 unsigned long pfn;
9065 void *ptr;
9066
9067 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9068 if (IS_ERR(ptr))
9069 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009070
9071 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9072 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9073}
9074
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009075#else /* !CONFIG_MMU */
9076
9077static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9078{
9079 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9080}
9081
9082static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9083{
9084 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9085}
9086
9087static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9088 unsigned long addr, unsigned long len,
9089 unsigned long pgoff, unsigned long flags)
9090{
9091 void *ptr;
9092
9093 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9094 if (IS_ERR(ptr))
9095 return PTR_ERR(ptr);
9096
9097 return (unsigned long) ptr;
9098}
9099
9100#endif /* !CONFIG_MMU */
9101
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009102static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009103{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009104 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009105 DEFINE_WAIT(wait);
9106
9107 do {
9108 if (!io_sqring_full(ctx))
9109 break;
9110
9111 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9112
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009113 if (unlikely(ctx->sqo_dead)) {
9114 ret = -EOWNERDEAD;
9115 goto out;
9116 }
9117
Jens Axboe90554202020-09-03 12:12:41 -06009118 if (!io_sqring_full(ctx))
9119 break;
9120
9121 schedule();
9122 } while (!signal_pending(current));
9123
9124 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009125out:
9126 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009127}
9128
Hao Xuc73ebb62020-11-03 10:54:37 +08009129static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9130 struct __kernel_timespec __user **ts,
9131 const sigset_t __user **sig)
9132{
9133 struct io_uring_getevents_arg arg;
9134
9135 /*
9136 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9137 * is just a pointer to the sigset_t.
9138 */
9139 if (!(flags & IORING_ENTER_EXT_ARG)) {
9140 *sig = (const sigset_t __user *) argp;
9141 *ts = NULL;
9142 return 0;
9143 }
9144
9145 /*
9146 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9147 * timespec and sigset_t pointers if good.
9148 */
9149 if (*argsz != sizeof(arg))
9150 return -EINVAL;
9151 if (copy_from_user(&arg, argp, sizeof(arg)))
9152 return -EFAULT;
9153 *sig = u64_to_user_ptr(arg.sigmask);
9154 *argsz = arg.sigmask_sz;
9155 *ts = u64_to_user_ptr(arg.ts);
9156 return 0;
9157}
9158
Jens Axboe2b188cc2019-01-07 10:46:33 -07009159SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009160 u32, min_complete, u32, flags, const void __user *, argp,
9161 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009162{
9163 struct io_ring_ctx *ctx;
9164 long ret = -EBADF;
9165 int submitted = 0;
9166 struct fd f;
9167
Jens Axboe4c6e2772020-07-01 11:29:10 -06009168 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009169
Jens Axboe90554202020-09-03 12:12:41 -06009170 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009171 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009172 return -EINVAL;
9173
9174 f = fdget(fd);
9175 if (!f.file)
9176 return -EBADF;
9177
9178 ret = -EOPNOTSUPP;
9179 if (f.file->f_op != &io_uring_fops)
9180 goto out_fput;
9181
9182 ret = -ENXIO;
9183 ctx = f.file->private_data;
9184 if (!percpu_ref_tryget(&ctx->refs))
9185 goto out_fput;
9186
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009187 ret = -EBADFD;
9188 if (ctx->flags & IORING_SETUP_R_DISABLED)
9189 goto out;
9190
Jens Axboe6c271ce2019-01-10 11:22:30 -07009191 /*
9192 * For SQ polling, the thread will do all submissions and completions.
9193 * Just return the requested submit count, and wake the thread if
9194 * we were asked to.
9195 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009196 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009197 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009198 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009199
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009200 ret = -EOWNERDEAD;
9201 if (unlikely(ctx->sqo_dead))
9202 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009203 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009204 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009205 if (flags & IORING_ENTER_SQ_WAIT) {
9206 ret = io_sqpoll_wait_sq(ctx);
9207 if (ret)
9208 goto out;
9209 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009210 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009211 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009212 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009213 if (unlikely(ret))
9214 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009215 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009216 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009217 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009218
9219 if (submitted != to_submit)
9220 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009221 }
9222 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009223 const sigset_t __user *sig;
9224 struct __kernel_timespec __user *ts;
9225
9226 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9227 if (unlikely(ret))
9228 goto out;
9229
Jens Axboe2b188cc2019-01-07 10:46:33 -07009230 min_complete = min(min_complete, ctx->cq_entries);
9231
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009232 /*
9233 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9234 * space applications don't need to do io completion events
9235 * polling again, they can rely on io_sq_thread to do polling
9236 * work, which can reduce cpu usage and uring_lock contention.
9237 */
9238 if (ctx->flags & IORING_SETUP_IOPOLL &&
9239 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009240 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009241 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009242 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009243 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009244 }
9245
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009246out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009247 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009248out_fput:
9249 fdput(f);
9250 return submitted ? submitted : ret;
9251}
9252
Tobias Klauserbebdb652020-02-26 18:38:32 +01009253#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009254static int io_uring_show_cred(int id, void *p, void *data)
9255{
Jens Axboe4379bf82021-02-15 13:40:22 -07009256 const struct cred *cred = p;
Jens Axboe87ce9552020-01-30 08:25:34 -07009257 struct seq_file *m = data;
9258 struct user_namespace *uns = seq_user_ns(m);
9259 struct group_info *gi;
9260 kernel_cap_t cap;
9261 unsigned __capi;
9262 int g;
9263
9264 seq_printf(m, "%5d\n", id);
9265 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9266 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9267 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9268 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9269 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9270 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9271 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9272 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9273 seq_puts(m, "\n\tGroups:\t");
9274 gi = cred->group_info;
9275 for (g = 0; g < gi->ngroups; g++) {
9276 seq_put_decimal_ull(m, g ? " " : "",
9277 from_kgid_munged(uns, gi->gid[g]));
9278 }
9279 seq_puts(m, "\n\tCapEff:\t");
9280 cap = cred->cap_effective;
9281 CAP_FOR_EACH_U32(__capi)
9282 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9283 seq_putc(m, '\n');
9284 return 0;
9285}
9286
9287static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9288{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009289 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009290 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009291 int i;
9292
Jens Axboefad8e0d2020-09-28 08:57:48 -06009293 /*
9294 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9295 * since fdinfo case grabs it in the opposite direction of normal use
9296 * cases. If we fail to get the lock, we just don't iterate any
9297 * structures that could be going away outside the io_uring mutex.
9298 */
9299 has_lock = mutex_trylock(&ctx->uring_lock);
9300
Joseph Qidbbe9c62020-09-29 09:01:22 -06009301 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9302 sq = ctx->sq_data;
9303
9304 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9305 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009306 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009307 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009308 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009309
Jens Axboe87ce9552020-01-30 08:25:34 -07009310 if (f)
9311 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9312 else
9313 seq_printf(m, "%5u: <none>\n", i);
9314 }
9315 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009316 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009317 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9318
9319 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9320 (unsigned int) buf->len);
9321 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009322 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009323 seq_printf(m, "Personalities:\n");
9324 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9325 }
Jens Axboed7718a92020-02-14 22:23:12 -07009326 seq_printf(m, "PollList:\n");
9327 spin_lock_irq(&ctx->completion_lock);
9328 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9329 struct hlist_head *list = &ctx->cancel_hash[i];
9330 struct io_kiocb *req;
9331
9332 hlist_for_each_entry(req, list, hash_node)
9333 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9334 req->task->task_works != NULL);
9335 }
9336 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009337 if (has_lock)
9338 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009339}
9340
9341static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9342{
9343 struct io_ring_ctx *ctx = f->private_data;
9344
9345 if (percpu_ref_tryget(&ctx->refs)) {
9346 __io_uring_show_fdinfo(ctx, m);
9347 percpu_ref_put(&ctx->refs);
9348 }
9349}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009350#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009351
Jens Axboe2b188cc2019-01-07 10:46:33 -07009352static const struct file_operations io_uring_fops = {
9353 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009354 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009355 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009356#ifndef CONFIG_MMU
9357 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9358 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9359#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009360 .poll = io_uring_poll,
9361 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009362#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009363 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009364#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009365};
9366
9367static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9368 struct io_uring_params *p)
9369{
Hristo Venev75b28af2019-08-26 17:23:46 +00009370 struct io_rings *rings;
9371 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009372
Jens Axboebd740482020-08-05 12:58:23 -06009373 /* make sure these are sane, as we already accounted them */
9374 ctx->sq_entries = p->sq_entries;
9375 ctx->cq_entries = p->cq_entries;
9376
Hristo Venev75b28af2019-08-26 17:23:46 +00009377 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9378 if (size == SIZE_MAX)
9379 return -EOVERFLOW;
9380
9381 rings = io_mem_alloc(size);
9382 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009383 return -ENOMEM;
9384
Hristo Venev75b28af2019-08-26 17:23:46 +00009385 ctx->rings = rings;
9386 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9387 rings->sq_ring_mask = p->sq_entries - 1;
9388 rings->cq_ring_mask = p->cq_entries - 1;
9389 rings->sq_ring_entries = p->sq_entries;
9390 rings->cq_ring_entries = p->cq_entries;
9391 ctx->sq_mask = rings->sq_ring_mask;
9392 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009393
9394 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009395 if (size == SIZE_MAX) {
9396 io_mem_free(ctx->rings);
9397 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009398 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009399 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009400
9401 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009402 if (!ctx->sq_sqes) {
9403 io_mem_free(ctx->rings);
9404 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009405 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009406 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009407
Jens Axboe2b188cc2019-01-07 10:46:33 -07009408 return 0;
9409}
9410
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009411static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9412{
9413 int ret, fd;
9414
9415 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9416 if (fd < 0)
9417 return fd;
9418
9419 ret = io_uring_add_task_file(ctx, file);
9420 if (ret) {
9421 put_unused_fd(fd);
9422 return ret;
9423 }
9424 fd_install(fd, file);
9425 return fd;
9426}
9427
Jens Axboe2b188cc2019-01-07 10:46:33 -07009428/*
9429 * Allocate an anonymous fd, this is what constitutes the application
9430 * visible backing of an io_uring instance. The application mmaps this
9431 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9432 * we have to tie this fd to a socket for file garbage collection purposes.
9433 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009434static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009435{
9436 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009437#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009438 int ret;
9439
Jens Axboe2b188cc2019-01-07 10:46:33 -07009440 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9441 &ctx->ring_sock);
9442 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009443 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009444#endif
9445
Jens Axboe2b188cc2019-01-07 10:46:33 -07009446 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9447 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009448#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009449 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009450 sock_release(ctx->ring_sock);
9451 ctx->ring_sock = NULL;
9452 } else {
9453 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009454 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009455#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009456 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009457}
9458
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009459static int io_uring_create(unsigned entries, struct io_uring_params *p,
9460 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009461{
9462 struct user_struct *user = NULL;
9463 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009464 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009465 int ret;
9466
Jens Axboe8110c1a2019-12-28 15:39:54 -07009467 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009468 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009469 if (entries > IORING_MAX_ENTRIES) {
9470 if (!(p->flags & IORING_SETUP_CLAMP))
9471 return -EINVAL;
9472 entries = IORING_MAX_ENTRIES;
9473 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009474
9475 /*
9476 * Use twice as many entries for the CQ ring. It's possible for the
9477 * application to drive a higher depth than the size of the SQ ring,
9478 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009479 * some flexibility in overcommitting a bit. If the application has
9480 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9481 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009482 */
9483 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009484 if (p->flags & IORING_SETUP_CQSIZE) {
9485 /*
9486 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9487 * to a power-of-two, if it isn't already. We do NOT impose
9488 * any cq vs sq ring sizing.
9489 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009490 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009491 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009492 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9493 if (!(p->flags & IORING_SETUP_CLAMP))
9494 return -EINVAL;
9495 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9496 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009497 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9498 if (p->cq_entries < p->sq_entries)
9499 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009500 } else {
9501 p->cq_entries = 2 * p->sq_entries;
9502 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009503
9504 user = get_uid(current_user());
Jens Axboe2b188cc2019-01-07 10:46:33 -07009505
9506 ctx = io_ring_ctx_alloc(p);
9507 if (!ctx) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07009508 free_uid(user);
9509 return -ENOMEM;
9510 }
9511 ctx->compat = in_compat_syscall();
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009512 ctx->limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009513 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009514 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009515#ifdef CONFIG_AUDIT
9516 ctx->loginuid = current->loginuid;
9517 ctx->sessionid = current->sessionid;
9518#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009519 ctx->sqo_task = get_task_struct(current);
9520
9521 /*
9522 * This is just grabbed for accounting purposes. When a process exits,
9523 * the mm is exited and dropped before the files, hence we need to hang
9524 * on to this mm purely for the purposes of being able to unaccount
9525 * memory (locked/pinned vm). It's not used for anything else.
9526 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009527 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009528 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009529
Dennis Zhou91d8f512020-09-16 13:41:05 -07009530#ifdef CONFIG_BLK_CGROUP
9531 /*
9532 * The sq thread will belong to the original cgroup it was inited in.
9533 * If the cgroup goes offline (e.g. disabling the io controller), then
9534 * issued bios will be associated with the closest cgroup later in the
9535 * block layer.
9536 */
9537 rcu_read_lock();
9538 ctx->sqo_blkcg_css = blkcg_css();
9539 ret = css_tryget_online(ctx->sqo_blkcg_css);
9540 rcu_read_unlock();
9541 if (!ret) {
9542 /* don't init against a dying cgroup, have the user try again */
9543 ctx->sqo_blkcg_css = NULL;
9544 ret = -ENODEV;
9545 goto err;
9546 }
9547#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009548 ret = io_allocate_scq_urings(ctx, p);
9549 if (ret)
9550 goto err;
9551
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009552 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009553 if (ret)
9554 goto err;
9555
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009556 if (!(p->flags & IORING_SETUP_R_DISABLED))
9557 io_sq_offload_start(ctx);
9558
Jens Axboe2b188cc2019-01-07 10:46:33 -07009559 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009560 p->sq_off.head = offsetof(struct io_rings, sq.head);
9561 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9562 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9563 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9564 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9565 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9566 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009567
9568 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009569 p->cq_off.head = offsetof(struct io_rings, cq.head);
9570 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9571 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9572 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9573 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9574 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009575 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009576
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009577 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9578 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009579 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009580 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9581 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009582
9583 if (copy_to_user(params, p, sizeof(*p))) {
9584 ret = -EFAULT;
9585 goto err;
9586 }
Jens Axboed1719f72020-07-30 13:43:53 -06009587
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009588 file = io_uring_get_file(ctx);
9589 if (IS_ERR(file)) {
9590 ret = PTR_ERR(file);
9591 goto err;
9592 }
9593
Jens Axboed1719f72020-07-30 13:43:53 -06009594 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009595 * Install ring fd as the very last thing, so we don't risk someone
9596 * having closed it before we finish setup
9597 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009598 ret = io_uring_install_fd(ctx, file);
9599 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009600 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009601 /* fput will clean it up */
9602 fput(file);
9603 return ret;
9604 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009605
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009606 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009607 return ret;
9608err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009609 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009610 io_ring_ctx_wait_and_kill(ctx);
9611 return ret;
9612}
9613
9614/*
9615 * Sets up an aio uring context, and returns the fd. Applications asks for a
9616 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9617 * params structure passed in.
9618 */
9619static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9620{
9621 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009622 int i;
9623
9624 if (copy_from_user(&p, params, sizeof(p)))
9625 return -EFAULT;
9626 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9627 if (p.resv[i])
9628 return -EINVAL;
9629 }
9630
Jens Axboe6c271ce2019-01-10 11:22:30 -07009631 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009632 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009633 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9634 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009635 return -EINVAL;
9636
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009637 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009638}
9639
9640SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9641 struct io_uring_params __user *, params)
9642{
9643 return io_uring_setup(entries, params);
9644}
9645
Jens Axboe66f4af92020-01-16 15:36:52 -07009646static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9647{
9648 struct io_uring_probe *p;
9649 size_t size;
9650 int i, ret;
9651
9652 size = struct_size(p, ops, nr_args);
9653 if (size == SIZE_MAX)
9654 return -EOVERFLOW;
9655 p = kzalloc(size, GFP_KERNEL);
9656 if (!p)
9657 return -ENOMEM;
9658
9659 ret = -EFAULT;
9660 if (copy_from_user(p, arg, size))
9661 goto out;
9662 ret = -EINVAL;
9663 if (memchr_inv(p, 0, size))
9664 goto out;
9665
9666 p->last_op = IORING_OP_LAST - 1;
9667 if (nr_args > IORING_OP_LAST)
9668 nr_args = IORING_OP_LAST;
9669
9670 for (i = 0; i < nr_args; i++) {
9671 p->ops[i].op = i;
9672 if (!io_op_defs[i].not_supported)
9673 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9674 }
9675 p->ops_len = i;
9676
9677 ret = 0;
9678 if (copy_to_user(arg, p, size))
9679 ret = -EFAULT;
9680out:
9681 kfree(p);
9682 return ret;
9683}
9684
Jens Axboe071698e2020-01-28 10:04:42 -07009685static int io_register_personality(struct io_ring_ctx *ctx)
9686{
Jens Axboe4379bf82021-02-15 13:40:22 -07009687 const struct cred *creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06009688 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009689
Jens Axboe4379bf82021-02-15 13:40:22 -07009690 creds = get_current_cred();
Jens Axboe1e6fa522020-10-15 08:46:24 -06009691
Jens Axboe4379bf82021-02-15 13:40:22 -07009692 ret = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
9693 USHRT_MAX, GFP_KERNEL);
9694 if (ret < 0)
9695 put_cred(creds);
Jens Axboe1e6fa522020-10-15 08:46:24 -06009696 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009697}
9698
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009699static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9700 unsigned int nr_args)
9701{
9702 struct io_uring_restriction *res;
9703 size_t size;
9704 int i, ret;
9705
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009706 /* Restrictions allowed only if rings started disabled */
9707 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9708 return -EBADFD;
9709
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009710 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009711 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009712 return -EBUSY;
9713
9714 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9715 return -EINVAL;
9716
9717 size = array_size(nr_args, sizeof(*res));
9718 if (size == SIZE_MAX)
9719 return -EOVERFLOW;
9720
9721 res = memdup_user(arg, size);
9722 if (IS_ERR(res))
9723 return PTR_ERR(res);
9724
9725 ret = 0;
9726
9727 for (i = 0; i < nr_args; i++) {
9728 switch (res[i].opcode) {
9729 case IORING_RESTRICTION_REGISTER_OP:
9730 if (res[i].register_op >= IORING_REGISTER_LAST) {
9731 ret = -EINVAL;
9732 goto out;
9733 }
9734
9735 __set_bit(res[i].register_op,
9736 ctx->restrictions.register_op);
9737 break;
9738 case IORING_RESTRICTION_SQE_OP:
9739 if (res[i].sqe_op >= IORING_OP_LAST) {
9740 ret = -EINVAL;
9741 goto out;
9742 }
9743
9744 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
9745 break;
9746 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
9747 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
9748 break;
9749 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
9750 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
9751 break;
9752 default:
9753 ret = -EINVAL;
9754 goto out;
9755 }
9756 }
9757
9758out:
9759 /* Reset all restrictions if an error happened */
9760 if (ret != 0)
9761 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
9762 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009763 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009764
9765 kfree(res);
9766 return ret;
9767}
9768
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009769static int io_register_enable_rings(struct io_ring_ctx *ctx)
9770{
9771 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9772 return -EBADFD;
9773
9774 if (ctx->restrictions.registered)
9775 ctx->restricted = 1;
9776
9777 ctx->flags &= ~IORING_SETUP_R_DISABLED;
9778
9779 io_sq_offload_start(ctx);
9780
9781 return 0;
9782}
9783
Jens Axboe071698e2020-01-28 10:04:42 -07009784static bool io_register_op_must_quiesce(int op)
9785{
9786 switch (op) {
9787 case IORING_UNREGISTER_FILES:
9788 case IORING_REGISTER_FILES_UPDATE:
9789 case IORING_REGISTER_PROBE:
9790 case IORING_REGISTER_PERSONALITY:
9791 case IORING_UNREGISTER_PERSONALITY:
9792 return false;
9793 default:
9794 return true;
9795 }
9796}
9797
Jens Axboeedafcce2019-01-09 09:16:05 -07009798static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
9799 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06009800 __releases(ctx->uring_lock)
9801 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07009802{
9803 int ret;
9804
Jens Axboe35fa71a2019-04-22 10:23:23 -06009805 /*
9806 * We're inside the ring mutex, if the ref is already dying, then
9807 * someone else killed the ctx or is already going through
9808 * io_uring_register().
9809 */
9810 if (percpu_ref_is_dying(&ctx->refs))
9811 return -ENXIO;
9812
Jens Axboe071698e2020-01-28 10:04:42 -07009813 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009814 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06009815
Jens Axboe05f3fb32019-12-09 11:22:50 -07009816 /*
9817 * Drop uring mutex before waiting for references to exit. If
9818 * another thread is currently inside io_uring_enter() it might
9819 * need to grab the uring_lock to make progress. If we hold it
9820 * here across the drain wait, then we can deadlock. It's safe
9821 * to drop the mutex here, since no new references will come in
9822 * after we've killed the percpu ref.
9823 */
9824 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009825 do {
9826 ret = wait_for_completion_interruptible(&ctx->ref_comp);
9827 if (!ret)
9828 break;
Jens Axboeed6930c2020-10-08 19:09:46 -06009829 ret = io_run_task_work_sig();
9830 if (ret < 0)
9831 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009832 } while (1);
9833
Jens Axboe05f3fb32019-12-09 11:22:50 -07009834 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -06009835
Pavel Begunkov88f171a2021-02-20 18:03:50 +00009836 if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp))
9837 return ret;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009838 }
9839
9840 if (ctx->restricted) {
9841 if (opcode >= IORING_REGISTER_LAST) {
9842 ret = -EINVAL;
9843 goto out;
9844 }
9845
9846 if (!test_bit(opcode, ctx->restrictions.register_op)) {
9847 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -07009848 goto out;
9849 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07009850 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009851
9852 switch (opcode) {
9853 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009854 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -07009855 break;
9856 case IORING_UNREGISTER_BUFFERS:
9857 ret = -EINVAL;
9858 if (arg || nr_args)
9859 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08009860 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07009861 break;
Jens Axboe6b063142019-01-10 22:13:58 -07009862 case IORING_REGISTER_FILES:
9863 ret = io_sqe_files_register(ctx, arg, nr_args);
9864 break;
9865 case IORING_UNREGISTER_FILES:
9866 ret = -EINVAL;
9867 if (arg || nr_args)
9868 break;
9869 ret = io_sqe_files_unregister(ctx);
9870 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06009871 case IORING_REGISTER_FILES_UPDATE:
9872 ret = io_sqe_files_update(ctx, arg, nr_args);
9873 break;
Jens Axboe9b402842019-04-11 11:45:41 -06009874 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07009875 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06009876 ret = -EINVAL;
9877 if (nr_args != 1)
9878 break;
9879 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07009880 if (ret)
9881 break;
9882 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
9883 ctx->eventfd_async = 1;
9884 else
9885 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06009886 break;
9887 case IORING_UNREGISTER_EVENTFD:
9888 ret = -EINVAL;
9889 if (arg || nr_args)
9890 break;
9891 ret = io_eventfd_unregister(ctx);
9892 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07009893 case IORING_REGISTER_PROBE:
9894 ret = -EINVAL;
9895 if (!arg || nr_args > 256)
9896 break;
9897 ret = io_probe(ctx, arg, nr_args);
9898 break;
Jens Axboe071698e2020-01-28 10:04:42 -07009899 case IORING_REGISTER_PERSONALITY:
9900 ret = -EINVAL;
9901 if (arg || nr_args)
9902 break;
9903 ret = io_register_personality(ctx);
9904 break;
9905 case IORING_UNREGISTER_PERSONALITY:
9906 ret = -EINVAL;
9907 if (arg)
9908 break;
9909 ret = io_unregister_personality(ctx, nr_args);
9910 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009911 case IORING_REGISTER_ENABLE_RINGS:
9912 ret = -EINVAL;
9913 if (arg || nr_args)
9914 break;
9915 ret = io_register_enable_rings(ctx);
9916 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009917 case IORING_REGISTER_RESTRICTIONS:
9918 ret = io_register_restrictions(ctx, arg, nr_args);
9919 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07009920 default:
9921 ret = -EINVAL;
9922 break;
9923 }
9924
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009925out:
Jens Axboe071698e2020-01-28 10:04:42 -07009926 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07009927 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07009928 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -06009929 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -07009930 }
Jens Axboeedafcce2019-01-09 09:16:05 -07009931 return ret;
9932}
9933
9934SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
9935 void __user *, arg, unsigned int, nr_args)
9936{
9937 struct io_ring_ctx *ctx;
9938 long ret = -EBADF;
9939 struct fd f;
9940
9941 f = fdget(fd);
9942 if (!f.file)
9943 return -EBADF;
9944
9945 ret = -EOPNOTSUPP;
9946 if (f.file->f_op != &io_uring_fops)
9947 goto out_fput;
9948
9949 ctx = f.file->private_data;
9950
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +00009951 io_run_task_work();
9952
Jens Axboeedafcce2019-01-09 09:16:05 -07009953 mutex_lock(&ctx->uring_lock);
9954 ret = __io_uring_register(ctx, opcode, arg, nr_args);
9955 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009956 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
9957 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07009958out_fput:
9959 fdput(f);
9960 return ret;
9961}
9962
Jens Axboe2b188cc2019-01-07 10:46:33 -07009963static int __init io_uring_init(void)
9964{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009965#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
9966 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
9967 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
9968} while (0)
9969
9970#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
9971 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
9972 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
9973 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
9974 BUILD_BUG_SQE_ELEM(1, __u8, flags);
9975 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
9976 BUILD_BUG_SQE_ELEM(4, __s32, fd);
9977 BUILD_BUG_SQE_ELEM(8, __u64, off);
9978 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
9979 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009980 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009981 BUILD_BUG_SQE_ELEM(24, __u32, len);
9982 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
9983 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
9984 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
9985 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +08009986 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
9987 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009988 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
9989 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
9990 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
9991 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
9992 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
9993 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
9994 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
9995 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03009996 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01009997 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
9998 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
9999 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010000 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010001
Jens Axboed3656342019-12-18 09:50:26 -070010002 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010003 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -070010004 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10005 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010006 return 0;
10007};
10008__initcall(io_uring_init);