blob: d6c2ff6124fd0d42c2812f383413983796173add [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060084#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020086#define CREATE_TRACE_POINTS
87#include <trace/events/io_uring.h>
88
Jens Axboe2b188cc2019-01-07 10:46:33 -070089#include <uapi/linux/io_uring.h>
90
91#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060092#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070093
Daniel Xu5277dea2019-09-14 14:23:45 -070094#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060095#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060096
97/*
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
99 */
100#define IORING_FILE_TABLE_SHIFT 9
101#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200104#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700106
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000107#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
108 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
109 IOSQE_BUFFER_SELECT)
110
Jens Axboe2b188cc2019-01-07 10:46:33 -0700111struct io_uring {
112 u32 head ____cacheline_aligned_in_smp;
113 u32 tail ____cacheline_aligned_in_smp;
114};
115
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 * This data is shared with the application through the mmap at offsets
118 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 *
120 * The offsets to the member fields are published through struct
121 * io_sqring_offsets when calling io_uring_setup.
122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Head and tail offsets into the ring; the offsets need to be
126 * masked to get valid indices.
127 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * The kernel controls head of the sq ring and the tail of the cq ring,
129 * and the application controls tail of the sq ring and the head of the
130 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 * ring_entries - 1)
136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 u32 sq_ring_mask, cq_ring_mask;
138 /* Ring sizes (constant, power of 2) */
139 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 /*
141 * Number of invalid entries dropped by the kernel due to
142 * invalid index stored in array
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application (i.e. get number of "new events" by comparing to
146 * cached value).
147 *
148 * After a new SQ head value was read by the application this
149 * counter includes all submissions that were dropped reaching
150 * the new SQ head (and possibly more).
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200154 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 *
156 * Written by the kernel, shouldn't be modified by the
157 * application.
158 *
159 * The application needs a full memory barrier before checking
160 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
161 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000162 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200163 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200164 * Runtime CQ flags
165 *
166 * Written by the application, shouldn't be modified by the
167 * kernel.
168 */
169 u32 cq_flags;
170 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200171 * Number of completion events lost because the queue was full;
172 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800173 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200174 * the completion queue.
175 *
176 * Written by the kernel, shouldn't be modified by the
177 * application (i.e. get number of "new events" by comparing to
178 * cached value).
179 *
180 * As completion events come in out of order this counter is not
181 * ordered with any other data.
182 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000183 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200184 /*
185 * Ring buffer of completion events.
186 *
187 * The kernel writes completion events fresh every time they are
188 * produced, so the application is allowed to modify pending
189 * entries.
190 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000191 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700192};
193
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000194enum io_uring_cmd_flags {
195 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000196 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000197};
198
Jens Axboeedafcce2019-01-09 09:16:05 -0700199struct io_mapped_ubuf {
200 u64 ubuf;
201 size_t len;
202 struct bio_vec *bvec;
203 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600204 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700205};
206
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000207struct io_ring_ctx;
208
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000209struct io_rsrc_put {
210 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000211 union {
212 void *rsrc;
213 struct file *file;
214 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000215};
216
217struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600218 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700219};
220
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000221struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800222 struct percpu_ref refs;
223 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000224 struct list_head rsrc_list;
225 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000226 void (*rsrc_put)(struct io_ring_ctx *ctx,
227 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600228 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000229 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800230};
231
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000232struct fixed_rsrc_data {
233 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700234 struct io_ring_ctx *ctx;
235
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000236 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700237 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700238 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800239 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700240};
241
Jens Axboe5a2e7452020-02-23 16:23:11 -0700242struct io_buffer {
243 struct list_head list;
244 __u64 addr;
245 __s32 len;
246 __u16 bid;
247};
248
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200249struct io_restriction {
250 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
251 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
252 u8 sqe_flags_allowed;
253 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200254 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200255};
256
Jens Axboe534ca6d2020-09-02 13:52:19 -0600257struct io_sq_data {
258 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600259 struct mutex lock;
260
261 /* ctx's that are using this sqd */
262 struct list_head ctx_list;
263 struct list_head ctx_new_list;
264 struct mutex ctx_lock;
265
Jens Axboe534ca6d2020-09-02 13:52:19 -0600266 struct task_struct *thread;
267 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800268
269 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600270};
271
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000272#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000273#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000274#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000275#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000276
277struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000278 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000279 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700280 unsigned int locked_free_nr;
281 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000282 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700283 /* IRQ completion list, under ->completion_lock */
284 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000285};
286
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000287struct io_submit_link {
288 struct io_kiocb *head;
289 struct io_kiocb *last;
290};
291
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000292struct io_submit_state {
293 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000294 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000295
296 /*
297 * io_kiocb alloc cache
298 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000299 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000300 unsigned int free_reqs;
301
302 bool plug_started;
303
304 /*
305 * Batch completion logic
306 */
307 struct io_comp_state comp;
308
309 /*
310 * File reference cache
311 */
312 struct file *file;
313 unsigned int fd;
314 unsigned int file_refs;
315 unsigned int ios_left;
316};
317
Jens Axboe2b188cc2019-01-07 10:46:33 -0700318struct io_ring_ctx {
319 struct {
320 struct percpu_ref refs;
321 } ____cacheline_aligned_in_smp;
322
323 struct {
324 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800325 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700326 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800327 unsigned int cq_overflow_flushed: 1;
328 unsigned int drain_next: 1;
329 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200330 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000331 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332
Hristo Venev75b28af2019-08-26 17:23:46 +0000333 /*
334 * Ring buffer of indices into array of io_uring_sqe, which is
335 * mmapped by the application using the IORING_OFF_SQES offset.
336 *
337 * This indirection could e.g. be used to assign fixed
338 * io_uring_sqe entries to operations and only submit them to
339 * the queue when needed.
340 *
341 * The kernel modifies neither the indices array nor the entries
342 * array.
343 */
344 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345 unsigned cached_sq_head;
346 unsigned sq_entries;
347 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700348 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600349 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100350 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700351 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600352
353 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600354 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700355 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356
Jens Axboead3eb2c2019-12-18 17:12:20 -0700357 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700358 } ____cacheline_aligned_in_smp;
359
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700360 struct {
361 struct mutex uring_lock;
362 wait_queue_head_t wait;
363 } ____cacheline_aligned_in_smp;
364
365 struct io_submit_state submit_state;
366
Hristo Venev75b28af2019-08-26 17:23:46 +0000367 struct io_rings *rings;
368
Jens Axboe2b188cc2019-01-07 10:46:33 -0700369 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600370 struct io_wq *io_wq;
Jens Axboe2aede0e2020-09-14 10:45:53 -0600371
372 /*
373 * For SQPOLL usage - we hold a reference to the parent task, so we
374 * have access to the ->files
375 */
376 struct task_struct *sqo_task;
377
378 /* Only used for accounting purposes */
379 struct mm_struct *mm_account;
380
Dennis Zhou91d8f512020-09-16 13:41:05 -0700381#ifdef CONFIG_BLK_CGROUP
382 struct cgroup_subsys_state *sqo_blkcg_css;
383#endif
384
Jens Axboe534ca6d2020-09-02 13:52:19 -0600385 struct io_sq_data *sq_data; /* if using sq thread polling */
386
Jens Axboe90554202020-09-03 12:12:41 -0600387 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600388 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700389
Jens Axboe6b063142019-01-10 22:13:58 -0700390 /*
391 * If used, fixed file set. Writers must ensure that ->refs is dead,
392 * readers must ensure that ->refs is alive as long as the file* is
393 * used. Only updated through io_uring_register(2).
394 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000395 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700396 unsigned nr_user_files;
397
Jens Axboeedafcce2019-01-09 09:16:05 -0700398 /* if used, fixed mapped user buffers */
399 unsigned nr_user_bufs;
400 struct io_mapped_ubuf *user_bufs;
401
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402 struct user_struct *user;
403
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700404 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700405
Jens Axboe4ea33a92020-10-15 13:46:44 -0600406#ifdef CONFIG_AUDIT
407 kuid_t loginuid;
408 unsigned int sessionid;
409#endif
410
Jens Axboe0f158b42020-05-14 17:18:39 -0600411 struct completion ref_comp;
412 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700413
414#if defined(CONFIG_UNIX)
415 struct socket *ring_sock;
416#endif
417
Jens Axboe5a2e7452020-02-23 16:23:11 -0700418 struct idr io_buffer_idr;
419
Jens Axboe071698e2020-01-28 10:04:42 -0700420 struct idr personality_idr;
421
Jens Axboe206aefd2019-11-07 18:27:42 -0700422 struct {
423 unsigned cached_cq_tail;
424 unsigned cq_entries;
425 unsigned cq_mask;
426 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500427 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700428 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700429 struct wait_queue_head cq_wait;
430 struct fasync_struct *cq_fasync;
431 struct eventfd_ctx *cq_ev_fd;
432 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433
434 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700435 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700436
Jens Axboedef596e2019-01-09 08:59:42 -0700437 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300438 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700439 * io_uring instances that don't use IORING_SETUP_SQPOLL.
440 * For SQPOLL, only the single threaded io_sq_thread() will
441 * manipulate the list, hence no extra locking is needed there.
442 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300443 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700444 struct hlist_head *cancel_hash;
445 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700446 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600447
448 spinlock_t inflight_lock;
449 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700450 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600451
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000452 struct delayed_work rsrc_put_work;
453 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000454 struct list_head rsrc_ref_list;
455 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600456
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200457 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700458
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700459 /* exit task_work */
460 struct callback_head *exit_task_work;
461
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700462 /* Keep this last, we don't need it for the fast path */
463 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464};
465
Jens Axboe09bb8392019-03-13 12:39:28 -0600466/*
467 * First field must be the file pointer in all the
468 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
469 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700470struct io_poll_iocb {
471 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000472 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700473 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600474 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700475 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700476 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700477};
478
Pavel Begunkov018043b2020-10-27 23:17:18 +0000479struct io_poll_remove {
480 struct file *file;
481 u64 addr;
482};
483
Jens Axboeb5dba592019-12-11 14:02:38 -0700484struct io_close {
485 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700486 int fd;
487};
488
Jens Axboead8a48a2019-11-15 08:49:11 -0700489struct io_timeout_data {
490 struct io_kiocb *req;
491 struct hrtimer timer;
492 struct timespec64 ts;
493 enum hrtimer_mode mode;
494};
495
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700496struct io_accept {
497 struct file *file;
498 struct sockaddr __user *addr;
499 int __user *addr_len;
500 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600501 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700502};
503
504struct io_sync {
505 struct file *file;
506 loff_t len;
507 loff_t off;
508 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700509 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700510};
511
Jens Axboefbf23842019-12-17 18:45:56 -0700512struct io_cancel {
513 struct file *file;
514 u64 addr;
515};
516
Jens Axboeb29472e2019-12-17 18:50:29 -0700517struct io_timeout {
518 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300519 u32 off;
520 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300521 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000522 /* head of the link, used by linked timeouts only */
523 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700524};
525
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100526struct io_timeout_rem {
527 struct file *file;
528 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000529
530 /* timeout update */
531 struct timespec64 ts;
532 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100533};
534
Jens Axboe9adbd452019-12-20 08:45:55 -0700535struct io_rw {
536 /* NOTE: kiocb has the file as the first member, so don't do it here */
537 struct kiocb kiocb;
538 u64 addr;
539 u64 len;
540};
541
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700542struct io_connect {
543 struct file *file;
544 struct sockaddr __user *addr;
545 int addr_len;
546};
547
Jens Axboee47293f2019-12-20 08:58:21 -0700548struct io_sr_msg {
549 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700550 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300551 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700552 void __user *buf;
553 };
Jens Axboee47293f2019-12-20 08:58:21 -0700554 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700555 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700556 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700557 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700558};
559
Jens Axboe15b71ab2019-12-11 11:20:36 -0700560struct io_open {
561 struct file *file;
562 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700563 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700564 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600565 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700566};
567
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000568struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700569 struct file *file;
570 u64 arg;
571 u32 nr_args;
572 u32 offset;
573};
574
Jens Axboe4840e412019-12-25 22:03:45 -0700575struct io_fadvise {
576 struct file *file;
577 u64 offset;
578 u32 len;
579 u32 advice;
580};
581
Jens Axboec1ca7572019-12-25 22:18:28 -0700582struct io_madvise {
583 struct file *file;
584 u64 addr;
585 u32 len;
586 u32 advice;
587};
588
Jens Axboe3e4827b2020-01-08 15:18:09 -0700589struct io_epoll {
590 struct file *file;
591 int epfd;
592 int op;
593 int fd;
594 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595};
596
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300597struct io_splice {
598 struct file *file_out;
599 struct file *file_in;
600 loff_t off_out;
601 loff_t off_in;
602 u64 len;
603 unsigned int flags;
604};
605
Jens Axboeddf0322d2020-02-23 16:41:33 -0700606struct io_provide_buf {
607 struct file *file;
608 __u64 addr;
609 __s32 len;
610 __u32 bgid;
611 __u16 nbufs;
612 __u16 bid;
613};
614
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700615struct io_statx {
616 struct file *file;
617 int dfd;
618 unsigned int mask;
619 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700620 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700621 struct statx __user *buffer;
622};
623
Jens Axboe36f4fa62020-09-05 11:14:22 -0600624struct io_shutdown {
625 struct file *file;
626 int how;
627};
628
Jens Axboe80a261f2020-09-28 14:23:58 -0600629struct io_rename {
630 struct file *file;
631 int old_dfd;
632 int new_dfd;
633 struct filename *oldpath;
634 struct filename *newpath;
635 int flags;
636};
637
Jens Axboe14a11432020-09-28 14:27:37 -0600638struct io_unlink {
639 struct file *file;
640 int dfd;
641 int flags;
642 struct filename *filename;
643};
644
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300645struct io_completion {
646 struct file *file;
647 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300648 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300649};
650
Jens Axboef499a022019-12-02 16:28:46 -0700651struct io_async_connect {
652 struct sockaddr_storage address;
653};
654
Jens Axboe03b12302019-12-02 18:50:25 -0700655struct io_async_msghdr {
656 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000657 /* points to an allocated iov, if NULL we use fast_iov instead */
658 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700659 struct sockaddr __user *uaddr;
660 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700661 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700662};
663
Jens Axboef67676d2019-12-02 11:03:47 -0700664struct io_async_rw {
665 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600666 const struct iovec *free_iovec;
667 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600668 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600669 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700670};
671
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300672enum {
673 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
674 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
675 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
676 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
677 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700678 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300679
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300680 REQ_F_FAIL_LINK_BIT,
681 REQ_F_INFLIGHT_BIT,
682 REQ_F_CUR_POS_BIT,
683 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300684 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300685 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300686 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700687 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700688 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600689 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800690 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100691 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000692 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700693
694 /* not a real bit, just to check we're not overflowing the space */
695 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300696};
697
698enum {
699 /* ctx owns file */
700 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
701 /* drain existing IO first */
702 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
703 /* linked sqes */
704 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
705 /* doesn't sever on completion < 0 */
706 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
707 /* IOSQE_ASYNC */
708 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700709 /* IOSQE_BUFFER_SELECT */
710 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300711
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300712 /* fail rest of links */
713 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
714 /* on inflight list */
715 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
716 /* read/write uses file position */
717 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
718 /* must not punt to workers */
719 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100720 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300721 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300722 /* regular file */
723 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300724 /* needs cleanup */
725 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700726 /* already went through poll handler */
727 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700728 /* buffer already selected */
729 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600730 /* doesn't need file table for this request */
731 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800732 /* io_wq_work is initialized */
733 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100734 /* linked timeout is active, i.e. prepared by link's head */
735 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000736 /* completion is deferred through io_comp_state */
737 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700738};
739
740struct async_poll {
741 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600742 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300743};
744
Jens Axboe7cbf1722021-02-10 00:03:20 +0000745struct io_task_work {
746 struct io_wq_work_node node;
747 task_work_func_t func;
748};
749
Jens Axboe09bb8392019-03-13 12:39:28 -0600750/*
751 * NOTE! Each of the iocb union members has the file pointer
752 * as the first entry in their struct definition. So you can
753 * access the file pointer through any of the sub-structs,
754 * or directly as just 'ki_filp' in this struct.
755 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700756struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700757 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600758 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700759 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700760 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000761 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700762 struct io_accept accept;
763 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700764 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700765 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100766 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700767 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700768 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700769 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700770 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000771 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700772 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700773 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700774 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300775 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700776 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700777 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600778 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600779 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600780 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300781 /* use only after cleaning per-op data, see io_clean_op() */
782 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700783 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700784
Jens Axboee8c2bc12020-08-15 18:44:09 -0700785 /* opcode allocated if it needs to store data for async defer */
786 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700787 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800788 /* polled IO has completed */
789 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700790
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700791 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300792 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700793
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300794 struct io_ring_ctx *ctx;
795 unsigned int flags;
796 refcount_t refs;
797 struct task_struct *task;
798 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700799
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000800 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000801 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700802
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300803 /*
804 * 1. used with ctx->iopoll_list with reads/writes
805 * 2. to track reqs with ->files (see io_op_def::file_table)
806 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300807 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000808 union {
809 struct io_task_work io_task_work;
810 struct callback_head task_work;
811 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300812 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
813 struct hlist_node hash_node;
814 struct async_poll *apoll;
815 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700816};
817
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300818struct io_defer_entry {
819 struct list_head list;
820 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300821 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300822};
823
Jens Axboed3656342019-12-18 09:50:26 -0700824struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700825 /* needs req->file assigned */
826 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700827 /* hash wq insertion if file is a regular file */
828 unsigned hash_reg_file : 1;
829 /* unbound wq insertion if file is a non-regular file */
830 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700831 /* opcode is not supported by this kernel */
832 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700833 /* set if opcode supports polled "wait" */
834 unsigned pollin : 1;
835 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700836 /* op supports buffer selection */
837 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700838 /* must always have async data allocated */
839 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600840 /* should block plug */
841 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700842 /* size of async data needed, if any */
843 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600844 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700845};
846
Jens Axboe09186822020-10-13 15:01:40 -0600847static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300848 [IORING_OP_NOP] = {},
849 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700850 .needs_file = 1,
851 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700852 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700853 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700854 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600855 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700856 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600857 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700858 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300859 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700860 .needs_file = 1,
861 .hash_reg_file = 1,
862 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700863 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700864 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600865 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700866 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600867 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
868 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700869 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300870 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700871 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600872 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700873 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300874 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700875 .needs_file = 1,
876 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700877 .pollin = 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 Axboe4017eb92020-10-22 14:14:12 -0600880 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700881 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300882 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700883 .needs_file = 1,
884 .hash_reg_file = 1,
885 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700886 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600887 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700888 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600889 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
890 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700891 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300892 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700893 .needs_file = 1,
894 .unbound_nonreg_file = 1,
895 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300896 [IORING_OP_POLL_REMOVE] = {},
897 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700898 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600899 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700900 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300901 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700902 .needs_file = 1,
903 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700904 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700905 .needs_async_data = 1,
906 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe92c75f72021-02-10 12:37:58 -0700907 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
908 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700909 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300910 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700911 .needs_file = 1,
912 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700913 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700914 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700915 .needs_async_data = 1,
916 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe92c75f72021-02-10 12:37:58 -0700917 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
918 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700919 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300920 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700921 .needs_async_data = 1,
922 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600923 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700924 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000925 [IORING_OP_TIMEOUT_REMOVE] = {
926 /* used by timeout updates' prep() */
927 .work_flags = IO_WQ_WORK_MM,
928 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300929 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700930 .needs_file = 1,
931 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700932 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600933 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700934 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300935 [IORING_OP_ASYNC_CANCEL] = {},
936 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700937 .needs_async_data = 1,
938 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600939 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700940 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300941 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700942 .needs_file = 1,
943 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700944 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700945 .needs_async_data = 1,
946 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600947 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700948 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300949 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700950 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600951 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700952 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300953 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600954 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600955 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600958 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600961 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700962 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300963 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600964 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
965 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700966 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300967 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700968 .needs_file = 1,
969 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700970 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700971 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600972 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700973 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600974 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700975 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300976 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700977 .needs_file = 1,
978 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700979 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600980 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700981 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600982 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
983 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700984 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300985 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700986 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600987 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700988 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300989 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600990 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700991 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300992 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700993 .needs_file = 1,
994 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700995 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600996 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700997 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300998 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700999 .needs_file = 1,
1000 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -07001001 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -07001002 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001003 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -07001004 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001005 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -06001006 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -06001007 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -07001008 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001009 [IORING_OP_EPOLL_CTL] = {
1010 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001011 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001012 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001013 [IORING_OP_SPLICE] = {
1014 .needs_file = 1,
1015 .hash_reg_file = 1,
1016 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001017 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001018 },
1019 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001020 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001021 [IORING_OP_TEE] = {
1022 .needs_file = 1,
1023 .hash_reg_file = 1,
1024 .unbound_nonreg_file = 1,
1025 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001026 [IORING_OP_SHUTDOWN] = {
1027 .needs_file = 1,
1028 },
Jens Axboe80a261f2020-09-28 14:23:58 -06001029 [IORING_OP_RENAMEAT] = {
1030 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1031 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1032 },
Jens Axboe14a11432020-09-28 14:27:37 -06001033 [IORING_OP_UNLINKAT] = {
1034 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1035 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1036 },
Jens Axboed3656342019-12-18 09:50:26 -07001037};
1038
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001039static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1040 struct task_struct *task,
1041 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001042static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001043static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001044 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00001045static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001046
Pavel Begunkov23faba32021-02-11 18:28:22 +00001047static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001048static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001049static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001050static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001051static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001052static void io_dismantle_req(struct io_kiocb *req);
1053static void io_put_task(struct task_struct *task, int nr);
1054static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001055static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001056static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001057static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001058static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001059 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001060 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001061static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001062static struct file *io_file_get(struct io_submit_state *state,
1063 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001064static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001065static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001066
Pavel Begunkov847595d2021-02-04 13:52:06 +00001067static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1068 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001069static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1070 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001071 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001072static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001073static void io_submit_flush_completions(struct io_comp_state *cs,
1074 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001075
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076static struct kmem_cache *req_cachep;
1077
Jens Axboe09186822020-10-13 15:01:40 -06001078static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079
1080struct sock *io_uring_get_socket(struct file *file)
1081{
1082#if defined(CONFIG_UNIX)
1083 if (file->f_op == &io_uring_fops) {
1084 struct io_ring_ctx *ctx = file->private_data;
1085
1086 return ctx->ring_sock->sk;
1087 }
1088#endif
1089 return NULL;
1090}
1091EXPORT_SYMBOL(io_uring_get_socket);
1092
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001093#define io_for_each_link(pos, head) \
1094 for (pos = (head); pos; pos = pos->link)
1095
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001096static inline void io_clean_op(struct io_kiocb *req)
1097{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001098 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001099 __io_clean_op(req);
1100}
1101
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001102static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001103{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001104 struct io_ring_ctx *ctx = req->ctx;
1105
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001106 if (!req->fixed_rsrc_refs) {
1107 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1108 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001109 }
1110}
1111
Pavel Begunkov88f171a2021-02-20 18:03:50 +00001112static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1113{
1114 if (!percpu_ref_tryget(ref)) {
1115 /* already at zero, wait for ->release() */
1116 if (!try_wait_for_completion(compl))
1117 synchronize_rcu();
1118 return false;
1119 }
1120
1121 percpu_ref_resurrect(ref);
1122 reinit_completion(compl);
1123 percpu_ref_put(ref);
1124 return true;
1125}
1126
Pavel Begunkov08d23632020-11-06 13:00:22 +00001127static bool io_match_task(struct io_kiocb *head,
1128 struct task_struct *task,
1129 struct files_struct *files)
1130{
1131 struct io_kiocb *req;
1132
Jens Axboe84965ff2021-01-23 15:51:11 -07001133 if (task && head->task != task) {
1134 /* in terms of cancelation, always match if req task is dead */
1135 if (head->task->flags & PF_EXITING)
1136 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001137 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001138 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001139 if (!files)
1140 return true;
1141
1142 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001143 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1144 continue;
1145 if (req->file && req->file->f_op == &io_uring_fops)
1146 return true;
1147 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001148 req->work.identity->files == files)
1149 return true;
1150 }
1151 return false;
1152}
1153
Jens Axboe28cea78a2020-09-14 10:51:17 -06001154static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001155{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001156 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001157 struct mm_struct *mm = current->mm;
1158
1159 if (mm) {
1160 kthread_unuse_mm(mm);
1161 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001162 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001163 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001164 if (files) {
1165 struct nsproxy *nsproxy = current->nsproxy;
1166
1167 task_lock(current);
1168 current->files = NULL;
1169 current->nsproxy = NULL;
1170 task_unlock(current);
1171 put_files_struct(files);
1172 put_nsproxy(nsproxy);
1173 }
1174}
1175
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001176static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001177{
1178 if (!current->files) {
1179 struct files_struct *files;
1180 struct nsproxy *nsproxy;
1181
1182 task_lock(ctx->sqo_task);
1183 files = ctx->sqo_task->files;
1184 if (!files) {
1185 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001186 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001187 }
1188 atomic_inc(&files->count);
1189 get_nsproxy(ctx->sqo_task->nsproxy);
1190 nsproxy = ctx->sqo_task->nsproxy;
1191 task_unlock(ctx->sqo_task);
1192
1193 task_lock(current);
1194 current->files = files;
1195 current->nsproxy = nsproxy;
1196 task_unlock(current);
1197 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001198 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001199}
1200
1201static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1202{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001203 struct mm_struct *mm;
1204
1205 if (current->mm)
1206 return 0;
1207
Jens Axboe4b70cf92020-11-02 10:39:05 -07001208 task_lock(ctx->sqo_task);
1209 mm = ctx->sqo_task->mm;
1210 if (unlikely(!mm || !mmget_not_zero(mm)))
1211 mm = NULL;
1212 task_unlock(ctx->sqo_task);
1213
1214 if (mm) {
1215 kthread_use_mm(mm);
1216 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001217 }
1218
Jens Axboe4b70cf92020-11-02 10:39:05 -07001219 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001220}
1221
Pavel Begunkov4e326352021-02-12 03:23:52 +00001222static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1223 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001224{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001225 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001226 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001227
1228 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001229 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001230 if (unlikely(ret))
1231 return ret;
1232 }
1233
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001234 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1235 ret = __io_sq_thread_acquire_files(ctx);
1236 if (unlikely(ret))
1237 return ret;
1238 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001239
1240 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001241}
1242
Pavel Begunkov4e326352021-02-12 03:23:52 +00001243static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1244 struct io_kiocb *req)
1245{
Pavel Begunkov4e326352021-02-12 03:23:52 +00001246 if (!(ctx->flags & IORING_SETUP_SQPOLL))
1247 return 0;
1248 return __io_sq_thread_acquire_mm_files(ctx, req);
1249}
1250
Dennis Zhou91d8f512020-09-16 13:41:05 -07001251static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1252 struct cgroup_subsys_state **cur_css)
1253
1254{
1255#ifdef CONFIG_BLK_CGROUP
1256 /* puts the old one when swapping */
1257 if (*cur_css != ctx->sqo_blkcg_css) {
1258 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1259 *cur_css = ctx->sqo_blkcg_css;
1260 }
1261#endif
1262}
1263
1264static void io_sq_thread_unassociate_blkcg(void)
1265{
1266#ifdef CONFIG_BLK_CGROUP
1267 kthread_associate_blkcg(NULL);
1268#endif
1269}
1270
Jens Axboec40f6372020-06-25 15:39:59 -06001271static inline void req_set_fail_links(struct io_kiocb *req)
1272{
1273 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1274 req->flags |= REQ_F_FAIL_LINK;
1275}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001276
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001277/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001278 * None of these are dereferenced, they are simply used to check if any of
1279 * them have changed. If we're under current and check they are still the
1280 * same, we're fine to grab references to them for actual out-of-line use.
1281 */
1282static void io_init_identity(struct io_identity *id)
1283{
1284 id->files = current->files;
1285 id->mm = current->mm;
1286#ifdef CONFIG_BLK_CGROUP
1287 rcu_read_lock();
1288 id->blkcg_css = blkcg_css();
1289 rcu_read_unlock();
1290#endif
1291 id->creds = current_cred();
1292 id->nsproxy = current->nsproxy;
1293 id->fs = current->fs;
1294 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001295#ifdef CONFIG_AUDIT
1296 id->loginuid = current->loginuid;
1297 id->sessionid = current->sessionid;
1298#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001299 refcount_set(&id->count, 1);
1300}
1301
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001302static inline void __io_req_init_async(struct io_kiocb *req)
1303{
1304 memset(&req->work, 0, sizeof(req->work));
1305 req->flags |= REQ_F_WORK_INITIALIZED;
1306}
1307
Jens Axboe1e6fa522020-10-15 08:46:24 -06001308/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001309 * Note: must call io_req_init_async() for the first time you
1310 * touch any members of io_wq_work.
1311 */
1312static inline void io_req_init_async(struct io_kiocb *req)
1313{
Jens Axboe500a3732020-10-15 17:38:03 -06001314 struct io_uring_task *tctx = current->io_uring;
1315
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001316 if (req->flags & REQ_F_WORK_INITIALIZED)
1317 return;
1318
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001319 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001320
1321 /* Grab a ref if this isn't our static identity */
1322 req->work.identity = tctx->identity;
1323 if (tctx->identity != &tctx->__identity)
1324 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001325}
1326
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1328{
1329 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1330
Jens Axboe0f158b42020-05-14 17:18:39 -06001331 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001332}
1333
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001334static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1335{
1336 return !req->timeout.off;
1337}
1338
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1340{
1341 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001342 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001343
1344 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1345 if (!ctx)
1346 return NULL;
1347
Jens Axboe78076bb2019-12-04 19:56:40 -07001348 /*
1349 * Use 5 bits less than the max cq entries, that should give us around
1350 * 32 entries per hash list if totally full and uniformly spread.
1351 */
1352 hash_bits = ilog2(p->cq_entries);
1353 hash_bits -= 5;
1354 if (hash_bits <= 0)
1355 hash_bits = 1;
1356 ctx->cancel_hash_bits = hash_bits;
1357 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1358 GFP_KERNEL);
1359 if (!ctx->cancel_hash)
1360 goto err;
1361 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1362
Roman Gushchin21482892019-05-07 10:01:48 -07001363 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001364 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1365 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366
1367 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001368 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001369 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001370 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001371 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001372 init_completion(&ctx->ref_comp);
1373 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001374 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001375 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001376 mutex_init(&ctx->uring_lock);
1377 init_waitqueue_head(&ctx->wait);
1378 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001379 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001380 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001381 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001382 spin_lock_init(&ctx->inflight_lock);
1383 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001384 spin_lock_init(&ctx->rsrc_ref_lock);
1385 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001386 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1387 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001388 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001389 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001391err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001392 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001393 kfree(ctx);
1394 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001395}
1396
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001397static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001398{
Jens Axboe2bc99302020-07-09 09:43:27 -06001399 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1400 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001401
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001402 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001403 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001404 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001405
Bob Liu9d858b22019-11-13 18:06:25 +08001406 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001407}
1408
Jens Axboe5c3462c2020-10-15 09:02:33 -06001409static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001410{
Jens Axboe500a3732020-10-15 17:38:03 -06001411 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001412 return;
1413 if (refcount_dec_and_test(&req->work.identity->count))
1414 kfree(req->work.identity);
1415}
1416
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001417static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001418{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001419 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001420 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001421
Pavel Begunkove86d0042021-02-01 18:59:54 +00001422 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001423 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001424#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001425 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001426 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001427#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001428 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001429 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001430 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001431 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001432
Jens Axboe98447d62020-10-14 10:48:51 -06001433 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001434 if (--fs->users)
1435 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001436 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001437 if (fs)
1438 free_fs_struct(fs);
1439 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001440 if (req->work.flags & IO_WQ_WORK_FILES) {
1441 put_files_struct(req->work.identity->files);
1442 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001443 }
1444 if (req->flags & REQ_F_INFLIGHT) {
1445 struct io_ring_ctx *ctx = req->ctx;
1446 struct io_uring_task *tctx = req->task->io_uring;
1447 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001448
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001449 spin_lock_irqsave(&ctx->inflight_lock, flags);
1450 list_del(&req->inflight_entry);
1451 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1452 req->flags &= ~REQ_F_INFLIGHT;
1453 if (atomic_read(&tctx->in_idle))
1454 wake_up(&tctx->wait);
1455 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001456
Pavel Begunkove86d0042021-02-01 18:59:54 +00001457 req->flags &= ~REQ_F_WORK_INITIALIZED;
1458 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1459 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001460 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001461}
1462
1463/*
1464 * Create a private copy of io_identity, since some fields don't match
1465 * the current context.
1466 */
1467static bool io_identity_cow(struct io_kiocb *req)
1468{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001469 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001470 const struct cred *creds = NULL;
1471 struct io_identity *id;
1472
1473 if (req->work.flags & IO_WQ_WORK_CREDS)
1474 creds = req->work.identity->creds;
1475
1476 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1477 if (unlikely(!id)) {
1478 req->work.flags |= IO_WQ_WORK_CANCEL;
1479 return false;
1480 }
1481
1482 /*
1483 * We can safely just re-init the creds we copied Either the field
1484 * matches the current one, or we haven't grabbed it yet. The only
1485 * exception is ->creds, through registered personalities, so handle
1486 * that one separately.
1487 */
1488 io_init_identity(id);
1489 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001490 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001491
1492 /* add one for this request */
1493 refcount_inc(&id->count);
1494
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001495 /* drop tctx and req identity references, if needed */
1496 if (tctx->identity != &tctx->__identity &&
1497 refcount_dec_and_test(&tctx->identity->count))
1498 kfree(tctx->identity);
1499 if (req->work.identity != &tctx->__identity &&
1500 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001501 kfree(req->work.identity);
1502
1503 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001504 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001505 return true;
1506}
1507
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001508static void io_req_track_inflight(struct io_kiocb *req)
1509{
1510 struct io_ring_ctx *ctx = req->ctx;
1511
1512 if (!(req->flags & REQ_F_INFLIGHT)) {
1513 io_req_init_async(req);
1514 req->flags |= REQ_F_INFLIGHT;
1515
1516 spin_lock_irq(&ctx->inflight_lock);
1517 list_add(&req->inflight_entry, &ctx->inflight_list);
1518 spin_unlock_irq(&ctx->inflight_lock);
1519 }
1520}
1521
Jens Axboe1e6fa522020-10-15 08:46:24 -06001522static bool io_grab_identity(struct io_kiocb *req)
1523{
1524 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001525 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001526
Jens Axboe69228332020-10-20 14:28:41 -06001527 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1528 if (id->fsize != rlimit(RLIMIT_FSIZE))
1529 return false;
1530 req->work.flags |= IO_WQ_WORK_FSIZE;
1531 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001532#ifdef CONFIG_BLK_CGROUP
1533 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1534 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1535 rcu_read_lock();
1536 if (id->blkcg_css != blkcg_css()) {
1537 rcu_read_unlock();
1538 return false;
1539 }
1540 /*
1541 * This should be rare, either the cgroup is dying or the task
1542 * is moving cgroups. Just punt to root for the handful of ios.
1543 */
1544 if (css_tryget_online(id->blkcg_css))
1545 req->work.flags |= IO_WQ_WORK_BLKCG;
1546 rcu_read_unlock();
1547 }
1548#endif
1549 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1550 if (id->creds != current_cred())
1551 return false;
1552 get_cred(id->creds);
1553 req->work.flags |= IO_WQ_WORK_CREDS;
1554 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001555#ifdef CONFIG_AUDIT
1556 if (!uid_eq(current->loginuid, id->loginuid) ||
1557 current->sessionid != id->sessionid)
1558 return false;
1559#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001560 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1561 (def->work_flags & IO_WQ_WORK_FS)) {
1562 if (current->fs != id->fs)
1563 return false;
1564 spin_lock(&id->fs->lock);
1565 if (!id->fs->in_exec) {
1566 id->fs->users++;
1567 req->work.flags |= IO_WQ_WORK_FS;
1568 } else {
1569 req->work.flags |= IO_WQ_WORK_CANCEL;
1570 }
1571 spin_unlock(&current->fs->lock);
1572 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001573 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1574 (def->work_flags & IO_WQ_WORK_FILES) &&
1575 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1576 if (id->files != current->files ||
1577 id->nsproxy != current->nsproxy)
1578 return false;
1579 atomic_inc(&id->files->count);
1580 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001581 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001582 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001583 }
Jens Axboe77788772020-12-29 10:50:46 -07001584 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1585 (def->work_flags & IO_WQ_WORK_MM)) {
1586 if (id->mm != current->mm)
1587 return false;
1588 mmgrab(id->mm);
1589 req->work.flags |= IO_WQ_WORK_MM;
1590 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001591
1592 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001593}
1594
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001595static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001596{
Jens Axboed3656342019-12-18 09:50:26 -07001597 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001598 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001599
Pavel Begunkov16d59802020-07-12 16:16:47 +03001600 io_req_init_async(req);
1601
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001602 if (req->flags & REQ_F_FORCE_ASYNC)
1603 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1604
Jens Axboed3656342019-12-18 09:50:26 -07001605 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001606 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001607 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001608 } else {
1609 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001610 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001611 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001612
Jens Axboe1e6fa522020-10-15 08:46:24 -06001613 /* if we fail grabbing identity, we must COW, regrab, and retry */
1614 if (io_grab_identity(req))
1615 return;
1616
1617 if (!io_identity_cow(req))
1618 return;
1619
1620 /* can't fail at this point */
1621 if (!io_grab_identity(req))
1622 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001623}
1624
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001625static void io_prep_async_link(struct io_kiocb *req)
1626{
1627 struct io_kiocb *cur;
1628
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001629 io_for_each_link(cur, req)
1630 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001631}
1632
Jens Axboe7271ef32020-08-10 09:55:22 -06001633static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001634{
Jackie Liua197f662019-11-08 08:09:12 -07001635 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001636 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001637
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001638 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1639 &req->work, req->flags);
1640 io_wq_enqueue(ctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001641 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001642}
1643
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001644static void io_queue_async_work(struct io_kiocb *req)
1645{
Jens Axboe7271ef32020-08-10 09:55:22 -06001646 struct io_kiocb *link;
1647
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001648 /* init ->work of the whole link before punting */
1649 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001650 link = __io_queue_async_work(req);
1651
1652 if (link)
1653 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001654}
1655
Jens Axboe5262f562019-09-17 12:26:57 -06001656static void io_kill_timeout(struct io_kiocb *req)
1657{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001658 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001659 int ret;
1660
Jens Axboee8c2bc12020-08-15 18:44:09 -07001661 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001662 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001663 atomic_set(&req->ctx->cq_timeouts,
1664 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001665 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001666 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001667 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001668 }
1669}
1670
Jens Axboe76e1b642020-09-26 15:05:03 -06001671/*
1672 * Returns true if we found and killed one or more timeouts
1673 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001674static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1675 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001676{
1677 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001678 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001679
1680 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001681 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001682 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001683 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001684 canceled++;
1685 }
Jens Axboef3606e32020-09-22 08:18:24 -06001686 }
Jens Axboe5262f562019-09-17 12:26:57 -06001687 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001688 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001689}
1690
Pavel Begunkov04518942020-05-26 20:34:05 +03001691static void __io_queue_deferred(struct io_ring_ctx *ctx)
1692{
1693 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001694 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1695 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001696
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001697 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001698 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001699 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001700 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001701 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001702 } while (!list_empty(&ctx->defer_list));
1703}
1704
Pavel Begunkov360428f2020-05-30 14:54:17 +03001705static void io_flush_timeouts(struct io_ring_ctx *ctx)
1706{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001707 u32 seq;
1708
1709 if (list_empty(&ctx->timeout_list))
1710 return;
1711
1712 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1713
1714 do {
1715 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001716 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001717 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001718
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001719 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001720 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001721
1722 /*
1723 * Since seq can easily wrap around over time, subtract
1724 * the last seq at which timeouts were flushed before comparing.
1725 * Assuming not more than 2^31-1 events have happened since,
1726 * these subtractions won't have wrapped, so we can check if
1727 * target is in [last_seq, current_seq] by comparing the two.
1728 */
1729 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1730 events_got = seq - ctx->cq_last_tm_flush;
1731 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001732 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001733
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001734 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001735 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001736 } while (!list_empty(&ctx->timeout_list));
1737
1738 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001739}
1740
Jens Axboede0617e2019-04-06 21:51:27 -06001741static void io_commit_cqring(struct io_ring_ctx *ctx)
1742{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001743 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001744
1745 /* order cqe stores with ring update */
1746 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001747
Pavel Begunkov04518942020-05-26 20:34:05 +03001748 if (unlikely(!list_empty(&ctx->defer_list)))
1749 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001750}
1751
Jens Axboe90554202020-09-03 12:12:41 -06001752static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1753{
1754 struct io_rings *r = ctx->rings;
1755
1756 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1757}
1758
Pavel Begunkov888aae22021-01-19 13:32:39 +00001759static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1760{
1761 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1762}
1763
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1765{
Hristo Venev75b28af2019-08-26 17:23:46 +00001766 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767 unsigned tail;
1768
Stefan Bühler115e12e2019-04-24 23:54:18 +02001769 /*
1770 * writes to the cq entry need to come after reading head; the
1771 * control dependency is enough as we're using WRITE_ONCE to
1772 * fill the cq entry
1773 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001774 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775 return NULL;
1776
Pavel Begunkov888aae22021-01-19 13:32:39 +00001777 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001778 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779}
1780
Jens Axboef2842ab2020-01-08 11:04:00 -07001781static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1782{
Jens Axboef0b493e2020-02-01 21:30:11 -07001783 if (!ctx->cq_ev_fd)
1784 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001785 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1786 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001787 if (!ctx->eventfd_async)
1788 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001789 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001790}
1791
Jens Axboeb41e9852020-02-17 09:52:41 -07001792static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001793{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001794 /* see waitqueue_active() comment */
1795 smp_mb();
1796
Jens Axboe8c838782019-03-12 15:48:16 -06001797 if (waitqueue_active(&ctx->wait))
1798 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001799 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1800 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001801 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001802 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001803 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001804 wake_up_interruptible(&ctx->cq_wait);
1805 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1806 }
Jens Axboe8c838782019-03-12 15:48:16 -06001807}
1808
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001809static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1810{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001811 /* see waitqueue_active() comment */
1812 smp_mb();
1813
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001814 if (ctx->flags & IORING_SETUP_SQPOLL) {
1815 if (waitqueue_active(&ctx->wait))
1816 wake_up(&ctx->wait);
1817 }
1818 if (io_should_trigger_evfd(ctx))
1819 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001820 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001821 wake_up_interruptible(&ctx->cq_wait);
1822 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1823 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001824}
1825
Jens Axboec4a2ed72019-11-21 21:01:26 -07001826/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001827static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1828 struct task_struct *tsk,
1829 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001831 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001832 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001833 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001834 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001835 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001836 LIST_HEAD(list);
1837
Pavel Begunkove23de152020-12-17 00:24:37 +00001838 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1839 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001840
Jens Axboeb18032b2021-01-24 16:58:56 -07001841 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001842 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001843 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001844 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001845 continue;
1846
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001847 cqe = io_get_cqring(ctx);
1848 if (!cqe && !force)
1849 break;
1850
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001851 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001852 if (cqe) {
1853 WRITE_ONCE(cqe->user_data, req->user_data);
1854 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001855 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001856 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001857 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001858 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001859 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001860 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001861 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001862 }
1863
Pavel Begunkov09e88402020-12-17 00:24:38 +00001864 all_flushed = list_empty(&ctx->cq_overflow_list);
1865 if (all_flushed) {
1866 clear_bit(0, &ctx->sq_check_overflow);
1867 clear_bit(0, &ctx->cq_check_overflow);
1868 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1869 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001870
Jens Axboeb18032b2021-01-24 16:58:56 -07001871 if (posted)
1872 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001873 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001874 if (posted)
1875 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001876
1877 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001878 req = list_first_entry(&list, struct io_kiocb, compl.list);
1879 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001880 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001881 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001882
Pavel Begunkov09e88402020-12-17 00:24:38 +00001883 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001884}
1885
Pavel Begunkov6c503152021-01-04 20:36:36 +00001886static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1887 struct task_struct *tsk,
1888 struct files_struct *files)
1889{
1890 if (test_bit(0, &ctx->cq_check_overflow)) {
1891 /* iopoll syncs against uring_lock, not completion_lock */
1892 if (ctx->flags & IORING_SETUP_IOPOLL)
1893 mutex_lock(&ctx->uring_lock);
1894 __io_cqring_overflow_flush(ctx, force, tsk, files);
1895 if (ctx->flags & IORING_SETUP_IOPOLL)
1896 mutex_unlock(&ctx->uring_lock);
1897 }
1898}
1899
Jens Axboebcda7ba2020-02-23 16:42:51 -07001900static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001902 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903 struct io_uring_cqe *cqe;
1904
Jens Axboe78e19bb2019-11-06 15:21:34 -07001905 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001906
Jens Axboe2b188cc2019-01-07 10:46:33 -07001907 /*
1908 * If we can't get a cq entry, userspace overflowed the
1909 * submission (by quite a lot). Increment the overflow count in
1910 * the ring.
1911 */
1912 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001913 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001914 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001915 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001916 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001917 } else if (ctx->cq_overflow_flushed ||
1918 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001919 /*
1920 * If we're in ring overflow flush mode, or in task cancel mode,
1921 * then we cannot store the request for later flushing, we need
1922 * to drop it on the floor.
1923 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001924 ctx->cached_cq_overflow++;
1925 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001926 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001927 if (list_empty(&ctx->cq_overflow_list)) {
1928 set_bit(0, &ctx->sq_check_overflow);
1929 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001930 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001931 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001932 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001933 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001934 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001935 refcount_inc(&req->refs);
1936 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001937 }
1938}
1939
Jens Axboebcda7ba2020-02-23 16:42:51 -07001940static void io_cqring_fill_event(struct io_kiocb *req, long res)
1941{
1942 __io_cqring_fill_event(req, res, 0);
1943}
1944
Jens Axboec7dae4b2021-02-09 19:53:37 -07001945static inline void io_req_complete_post(struct io_kiocb *req, long res,
1946 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001947{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001948 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001949 unsigned long flags;
1950
1951 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001952 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001953 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001954 /*
1955 * If we're the last reference to this request, add to our locked
1956 * free_list cache.
1957 */
1958 if (refcount_dec_and_test(&req->refs)) {
1959 struct io_comp_state *cs = &ctx->submit_state.comp;
1960
1961 io_dismantle_req(req);
1962 io_put_task(req->task, 1);
1963 list_add(&req->compl.list, &cs->locked_free_list);
1964 cs->locked_free_nr++;
1965 } else
1966 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1968
Jens Axboe8c838782019-03-12 15:48:16 -06001969 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001970 if (req) {
1971 io_queue_next(req);
1972 percpu_ref_put(&ctx->refs);
1973 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001974}
1975
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001976static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001977 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001978{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001979 io_clean_op(req);
1980 req->result = res;
1981 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001982 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001983}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001984
Pavel Begunkov889fca72021-02-10 00:03:09 +00001985static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1986 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001987{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001988 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1989 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001990 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001991 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001992}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001993
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001994static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001995{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001996 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001997}
1998
Jens Axboec7dae4b2021-02-09 19:53:37 -07001999static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002000{
Jens Axboec7dae4b2021-02-09 19:53:37 -07002001 struct io_submit_state *state = &ctx->submit_state;
2002 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002003 struct io_kiocb *req = NULL;
2004
Jens Axboec7dae4b2021-02-09 19:53:37 -07002005 /*
2006 * If we have more than a batch's worth of requests in our IRQ side
2007 * locked cache, grab the lock and move them over to our submission
2008 * side cache.
2009 */
2010 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
2011 spin_lock_irq(&ctx->completion_lock);
2012 list_splice_init(&cs->locked_free_list, &cs->free_list);
2013 cs->locked_free_nr = 0;
2014 spin_unlock_irq(&ctx->completion_lock);
2015 }
2016
2017 while (!list_empty(&cs->free_list)) {
2018 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002019 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002020 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002021 state->reqs[state->free_reqs++] = req;
2022 if (state->free_reqs == ARRAY_SIZE(state->reqs))
2023 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002024 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002026 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002027}
2028
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002029static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002030{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002031 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002032
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002033 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07002034
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002035 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002036 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002037 int ret;
2038
Jens Axboec7dae4b2021-02-09 19:53:37 -07002039 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002040 goto got_req;
2041
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002042 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
2043 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06002044
2045 /*
2046 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2047 * retry single alloc to be on the safe side.
2048 */
2049 if (unlikely(ret <= 0)) {
2050 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2051 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00002052 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06002053 ret = 1;
2054 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002055 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002057got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03002058 state->free_reqs--;
2059 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060}
2061
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002062static inline void io_put_file(struct io_kiocb *req, struct file *file,
2063 bool fixed)
2064{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002065 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002066 fput(file);
2067}
2068
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002069static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002070{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002071 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002072
Jens Axboee8c2bc12020-08-15 18:44:09 -07002073 if (req->async_data)
2074 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002075 if (req->file)
2076 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002077 if (req->fixed_rsrc_refs)
2078 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002079 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002080}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002081
Pavel Begunkov7c660732021-01-25 11:42:21 +00002082static inline void io_put_task(struct task_struct *task, int nr)
2083{
2084 struct io_uring_task *tctx = task->io_uring;
2085
2086 percpu_counter_sub(&tctx->inflight, nr);
2087 if (unlikely(atomic_read(&tctx->in_idle)))
2088 wake_up(&tctx->wait);
2089 put_task_struct_many(task, nr);
2090}
2091
Pavel Begunkov216578e2020-10-13 09:44:00 +01002092static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002093{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002094 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002095
Pavel Begunkov216578e2020-10-13 09:44:00 +01002096 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002097 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002098
Pavel Begunkov3893f392021-02-10 00:03:15 +00002099 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002100 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002101}
2102
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002103static inline void io_remove_next_linked(struct io_kiocb *req)
2104{
2105 struct io_kiocb *nxt = req->link;
2106
2107 req->link = nxt->link;
2108 nxt->link = NULL;
2109}
2110
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002111static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002112{
Jackie Liua197f662019-11-08 08:09:12 -07002113 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002114 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002115 bool cancelled = false;
2116 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002117
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002118 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002119 link = req->link;
2120
Pavel Begunkov900fad42020-10-19 16:39:16 +01002121 /*
2122 * Can happen if a linked timeout fired and link had been like
2123 * req -> link t-out -> link t-out [-> ...]
2124 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002125 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2126 struct io_timeout_data *io = link->async_data;
2127 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002128
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002129 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002130 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002131 ret = hrtimer_try_to_cancel(&io->timer);
2132 if (ret != -1) {
2133 io_cqring_fill_event(link, -ECANCELED);
2134 io_commit_cqring(ctx);
2135 cancelled = true;
2136 }
2137 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002138 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002139 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002140
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002141 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002142 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002143 io_put_req(link);
2144 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002145}
2146
Jens Axboe4d7dd462019-11-20 13:03:52 -07002147
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002148static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002149{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002150 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002151 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002152 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002153
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002154 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002155 link = req->link;
2156 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002157
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002158 while (link) {
2159 nxt = link->link;
2160 link->link = NULL;
2161
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002162 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002163 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002164
2165 /*
2166 * It's ok to free under spinlock as they're not linked anymore,
2167 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2168 * work.fs->lock.
2169 */
2170 if (link->flags & REQ_F_WORK_INITIALIZED)
2171 io_put_req_deferred(link, 2);
2172 else
2173 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002174 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002175 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002176 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002177 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002178
Jens Axboe2665abf2019-11-05 12:40:47 -07002179 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002180}
2181
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002182static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002183{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002184 if (req->flags & REQ_F_LINK_TIMEOUT)
2185 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002186
Jens Axboe9e645e112019-05-10 16:07:28 -06002187 /*
2188 * If LINK is set, we have dependent requests in this chain. If we
2189 * didn't fail this request, queue the first one up, moving any other
2190 * dependencies to the next request. In case of failure, fail the rest
2191 * of the chain.
2192 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002193 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2194 struct io_kiocb *nxt = req->link;
2195
2196 req->link = NULL;
2197 return nxt;
2198 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002199 io_fail_links(req);
2200 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002201}
Jens Axboe2665abf2019-11-05 12:40:47 -07002202
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002203static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002204{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002205 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002206 return NULL;
2207 return __io_req_find_next(req);
2208}
2209
Jens Axboe7cbf1722021-02-10 00:03:20 +00002210static bool __tctx_task_work(struct io_uring_task *tctx)
2211{
Jens Axboe65453d12021-02-10 00:03:21 +00002212 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002213 struct io_wq_work_list list;
2214 struct io_wq_work_node *node;
2215
2216 if (wq_list_empty(&tctx->task_list))
2217 return false;
2218
Jens Axboe0b81e802021-02-16 10:33:53 -07002219 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002220 list = tctx->task_list;
2221 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002222 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002223
2224 node = list.first;
2225 while (node) {
2226 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00002227 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002228 struct io_kiocb *req;
2229
2230 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00002231 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002232 req->task_work.func(&req->task_work);
2233 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00002234
2235 if (!ctx) {
2236 ctx = this_ctx;
2237 } else if (ctx != this_ctx) {
2238 mutex_lock(&ctx->uring_lock);
2239 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2240 mutex_unlock(&ctx->uring_lock);
2241 ctx = this_ctx;
2242 }
2243 }
2244
2245 if (ctx && ctx->submit_state.comp.nr) {
2246 mutex_lock(&ctx->uring_lock);
2247 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2248 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002249 }
2250
2251 return list.first != NULL;
2252}
2253
2254static void tctx_task_work(struct callback_head *cb)
2255{
2256 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
2257
2258 while (__tctx_task_work(tctx))
2259 cond_resched();
2260
2261 clear_bit(0, &tctx->task_state);
2262}
2263
2264static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
2265 enum task_work_notify_mode notify)
2266{
2267 struct io_uring_task *tctx = tsk->io_uring;
2268 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07002269 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002270 int ret;
2271
2272 WARN_ON_ONCE(!tctx);
2273
Jens Axboe0b81e802021-02-16 10:33:53 -07002274 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002275 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002276 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002277
2278 /* task_work already pending, we're done */
2279 if (test_bit(0, &tctx->task_state) ||
2280 test_and_set_bit(0, &tctx->task_state))
2281 return 0;
2282
2283 if (!task_work_add(tsk, &tctx->task_work, notify))
2284 return 0;
2285
2286 /*
2287 * Slow path - we failed, find and delete work. if the work is not
2288 * in the list, it got run and we're fine.
2289 */
2290 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07002291 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002292 wq_list_for_each(node, prev, &tctx->task_list) {
2293 if (&req->io_task_work.node == node) {
2294 wq_list_del(&tctx->task_list, node, prev);
2295 ret = 1;
2296 break;
2297 }
2298 }
Jens Axboe0b81e802021-02-16 10:33:53 -07002299 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002300 clear_bit(0, &tctx->task_state);
2301 return ret;
2302}
2303
Jens Axboe355fb9e2020-10-22 20:19:35 -06002304static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002305{
2306 struct task_struct *tsk = req->task;
2307 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002308 enum task_work_notify_mode notify;
2309 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002310
Jens Axboe6200b0a2020-09-13 14:38:30 -06002311 if (tsk->flags & PF_EXITING)
2312 return -ESRCH;
2313
Jens Axboec2c4c832020-07-01 15:37:11 -06002314 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002315 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2316 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2317 * processing task_work. There's no reliable way to tell if TWA_RESUME
2318 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002319 */
Jens Axboe91989c72020-10-16 09:02:26 -06002320 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002321 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002322 notify = TWA_SIGNAL;
2323
Jens Axboe7cbf1722021-02-10 00:03:20 +00002324 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002325 if (!ret)
2326 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002327
Jens Axboec2c4c832020-07-01 15:37:11 -06002328 return ret;
2329}
2330
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002331static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002332 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002333{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07002334 struct io_ring_ctx *ctx = req->ctx;
2335 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002336
2337 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07002338 do {
2339 head = READ_ONCE(ctx->exit_task_work);
2340 req->task_work.next = head;
2341 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002342}
2343
Jens Axboec40f6372020-06-25 15:39:59 -06002344static void __io_req_task_cancel(struct io_kiocb *req, int error)
2345{
2346 struct io_ring_ctx *ctx = req->ctx;
2347
2348 spin_lock_irq(&ctx->completion_lock);
2349 io_cqring_fill_event(req, error);
2350 io_commit_cqring(ctx);
2351 spin_unlock_irq(&ctx->completion_lock);
2352
2353 io_cqring_ev_posted(ctx);
2354 req_set_fail_links(req);
2355 io_double_put_req(req);
2356}
2357
2358static void io_req_task_cancel(struct callback_head *cb)
2359{
2360 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002361 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002362
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002363 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002364 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002365 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002366 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002367}
2368
2369static void __io_req_task_submit(struct io_kiocb *req)
2370{
2371 struct io_ring_ctx *ctx = req->ctx;
2372
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002373 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002374 mutex_lock(&ctx->uring_lock);
Pavel Begunkovdc0eced2021-02-12 18:41:15 +00002375 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) &&
2376 !io_sq_thread_acquire_mm_files(ctx, req))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002377 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002378 else
Jens Axboec40f6372020-06-25 15:39:59 -06002379 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002380 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovaec18a52021-02-04 19:22:46 +00002381
2382 if (ctx->flags & IORING_SETUP_SQPOLL)
2383 io_sq_thread_drop_mm_files();
Jens Axboe9e645e112019-05-10 16:07:28 -06002384}
2385
Jens Axboec40f6372020-06-25 15:39:59 -06002386static void io_req_task_submit(struct callback_head *cb)
2387{
2388 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2389
2390 __io_req_task_submit(req);
2391}
2392
2393static void io_req_task_queue(struct io_kiocb *req)
2394{
Jens Axboec40f6372020-06-25 15:39:59 -06002395 int ret;
2396
Jens Axboe7cbf1722021-02-10 00:03:20 +00002397 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002398 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002399 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00002400 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002401 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002402 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002403 }
Jens Axboec40f6372020-06-25 15:39:59 -06002404}
2405
Pavel Begunkova3df76982021-02-18 22:32:52 +00002406static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2407{
2408 percpu_ref_get(&req->ctx->refs);
2409 req->result = ret;
2410 req->task_work.func = io_req_task_cancel;
2411
2412 if (unlikely(io_req_task_work_add(req)))
2413 io_req_task_work_add_fallback(req, io_req_task_cancel);
2414}
2415
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002416static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002417{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002418 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002419
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002420 if (nxt)
2421 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002422}
2423
Jens Axboe9e645e112019-05-10 16:07:28 -06002424static void io_free_req(struct io_kiocb *req)
2425{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002426 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002427 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002428}
2429
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002430struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002431 struct task_struct *task;
2432 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002433 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002434};
2435
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002436static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002437{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002438 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002439 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002440 rb->task = NULL;
2441}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002442
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002443static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2444 struct req_batch *rb)
2445{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002446 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002447 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002448 if (rb->ctx_refs)
2449 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002450}
2451
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002452static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2453 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002454{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002455 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002456
Jens Axboee3bc8e92020-09-24 08:45:57 -06002457 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002458 if (rb->task)
2459 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002460 rb->task = req->task;
2461 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002462 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002463 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002464 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002465
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002466 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002467 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002468 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002469 else
2470 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002471}
2472
Pavel Begunkov905c1722021-02-10 00:03:14 +00002473static void io_submit_flush_completions(struct io_comp_state *cs,
2474 struct io_ring_ctx *ctx)
2475{
2476 int i, nr = cs->nr;
2477 struct io_kiocb *req;
2478 struct req_batch rb;
2479
2480 io_init_req_batch(&rb);
2481 spin_lock_irq(&ctx->completion_lock);
2482 for (i = 0; i < nr; i++) {
2483 req = cs->reqs[i];
2484 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2485 }
2486 io_commit_cqring(ctx);
2487 spin_unlock_irq(&ctx->completion_lock);
2488
2489 io_cqring_ev_posted(ctx);
2490 for (i = 0; i < nr; i++) {
2491 req = cs->reqs[i];
2492
2493 /* submission and completion refs */
2494 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002495 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002496 }
2497
2498 io_req_free_batch_finish(ctx, &rb);
2499 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002500}
2501
Jens Axboeba816ad2019-09-28 11:36:45 -06002502/*
2503 * Drop reference to request, return next in chain (if there is one) if this
2504 * was the last reference to this request.
2505 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002506static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002507{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002508 struct io_kiocb *nxt = NULL;
2509
Jens Axboe2a44f462020-02-25 13:25:41 -07002510 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002511 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002512 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002513 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002514 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002515}
2516
Jens Axboe2b188cc2019-01-07 10:46:33 -07002517static void io_put_req(struct io_kiocb *req)
2518{
Jens Axboedef596e2019-01-09 08:59:42 -07002519 if (refcount_dec_and_test(&req->refs))
2520 io_free_req(req);
2521}
2522
Pavel Begunkov216578e2020-10-13 09:44:00 +01002523static void io_put_req_deferred_cb(struct callback_head *cb)
2524{
2525 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2526
2527 io_free_req(req);
2528}
2529
2530static void io_free_req_deferred(struct io_kiocb *req)
2531{
2532 int ret;
2533
Jens Axboe7cbf1722021-02-10 00:03:20 +00002534 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002535 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002536 if (unlikely(ret))
2537 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002538}
2539
2540static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2541{
2542 if (refcount_sub_and_test(refs, &req->refs))
2543 io_free_req_deferred(req);
2544}
2545
Jens Axboe978db572019-11-14 22:39:04 -07002546static void io_double_put_req(struct io_kiocb *req)
2547{
2548 /* drop both submit and complete references */
2549 if (refcount_sub_and_test(2, &req->refs))
2550 io_free_req(req);
2551}
2552
Pavel Begunkov6c503152021-01-04 20:36:36 +00002553static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002554{
2555 /* See comment at the top of this file */
2556 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002557 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002558}
2559
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002560static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2561{
2562 struct io_rings *rings = ctx->rings;
2563
2564 /* make sure SQ entry isn't read before tail */
2565 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2566}
2567
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002568static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002569{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002570 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002571
Jens Axboebcda7ba2020-02-23 16:42:51 -07002572 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2573 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002574 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002575 kfree(kbuf);
2576 return cflags;
2577}
2578
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002579static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2580{
2581 struct io_buffer *kbuf;
2582
2583 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2584 return io_put_kbuf(req, kbuf);
2585}
2586
Jens Axboe4c6e2772020-07-01 11:29:10 -06002587static inline bool io_run_task_work(void)
2588{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002589 /*
2590 * Not safe to run on exiting task, and the task_work handling will
2591 * not add work to such a task.
2592 */
2593 if (unlikely(current->flags & PF_EXITING))
2594 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002595 if (current->task_works) {
2596 __set_current_state(TASK_RUNNING);
2597 task_work_run();
2598 return true;
2599 }
2600
2601 return false;
2602}
2603
Jens Axboedef596e2019-01-09 08:59:42 -07002604/*
2605 * Find and free completed poll iocbs
2606 */
2607static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2608 struct list_head *done)
2609{
Jens Axboe8237e042019-12-28 10:48:22 -07002610 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002611 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002612
2613 /* order with ->result store in io_complete_rw_iopoll() */
2614 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002615
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002616 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002617 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002618 int cflags = 0;
2619
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002620 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002621 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002622
Pavel Begunkovf1613402021-02-11 18:28:21 +00002623 if (READ_ONCE(req->result) == -EAGAIN) {
2624 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002625 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002626 continue;
2627 }
2628
Jens Axboebcda7ba2020-02-23 16:42:51 -07002629 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002630 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002631
2632 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002633 (*nr_events)++;
2634
Pavel Begunkovc3524382020-06-28 12:52:32 +03002635 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002636 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002637 }
Jens Axboedef596e2019-01-09 08:59:42 -07002638
Jens Axboe09bb8392019-03-13 12:39:28 -06002639 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002640 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002641 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002642}
2643
Jens Axboedef596e2019-01-09 08:59:42 -07002644static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2645 long min)
2646{
2647 struct io_kiocb *req, *tmp;
2648 LIST_HEAD(done);
2649 bool spin;
2650 int ret;
2651
2652 /*
2653 * Only spin for completions if we don't have multiple devices hanging
2654 * off our complete list, and we're under the requested amount.
2655 */
2656 spin = !ctx->poll_multi_file && *nr_events < min;
2657
2658 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002659 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002660 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002661
2662 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002663 * Move completed and retryable entries to our local lists.
2664 * If we find a request that requires polling, break out
2665 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002666 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002667 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002668 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002669 continue;
2670 }
2671 if (!list_empty(&done))
2672 break;
2673
2674 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2675 if (ret < 0)
2676 break;
2677
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002678 /* iopoll may have completed current req */
2679 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002680 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002681
Jens Axboedef596e2019-01-09 08:59:42 -07002682 if (ret && spin)
2683 spin = false;
2684 ret = 0;
2685 }
2686
2687 if (!list_empty(&done))
2688 io_iopoll_complete(ctx, nr_events, &done);
2689
2690 return ret;
2691}
2692
2693/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002694 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002695 * non-spinning poll check - we'll still enter the driver poll loop, but only
2696 * as a non-spinning completion check.
2697 */
2698static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2699 long min)
2700{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002701 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002702 int ret;
2703
2704 ret = io_do_iopoll(ctx, nr_events, min);
2705 if (ret < 0)
2706 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002707 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002708 return 0;
2709 }
2710
2711 return 1;
2712}
2713
2714/*
2715 * We can't just wait for polled events to come to us, we have to actively
2716 * find and complete them.
2717 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002718static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002719{
2720 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2721 return;
2722
2723 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002724 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002725 unsigned int nr_events = 0;
2726
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002727 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002728
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002729 /* let it sleep and repeat later if can't complete a request */
2730 if (nr_events == 0)
2731 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002732 /*
2733 * Ensure we allow local-to-the-cpu processing to take place,
2734 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002735 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002736 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002737 if (need_resched()) {
2738 mutex_unlock(&ctx->uring_lock);
2739 cond_resched();
2740 mutex_lock(&ctx->uring_lock);
2741 }
Jens Axboedef596e2019-01-09 08:59:42 -07002742 }
2743 mutex_unlock(&ctx->uring_lock);
2744}
2745
Pavel Begunkov7668b922020-07-07 16:36:21 +03002746static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002747{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002748 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002749 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002750
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002751 /*
2752 * We disallow the app entering submit/complete with polling, but we
2753 * still need to lock the ring to prevent racing with polled issue
2754 * that got punted to a workqueue.
2755 */
2756 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002757 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002758 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002759 * Don't enter poll loop if we already have events pending.
2760 * If we do, we can potentially be spinning for commands that
2761 * already triggered a CQE (eg in error).
2762 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002763 if (test_bit(0, &ctx->cq_check_overflow))
2764 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2765 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002766 break;
2767
2768 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002769 * If a submit got punted to a workqueue, we can have the
2770 * application entering polling for a command before it gets
2771 * issued. That app will hold the uring_lock for the duration
2772 * of the poll right here, so we need to take a breather every
2773 * now and then to ensure that the issue has a chance to add
2774 * the poll to the issued list. Otherwise we can spin here
2775 * forever, while the workqueue is stuck trying to acquire the
2776 * very same mutex.
2777 */
2778 if (!(++iters & 7)) {
2779 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002780 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002781 mutex_lock(&ctx->uring_lock);
2782 }
2783
Pavel Begunkov7668b922020-07-07 16:36:21 +03002784 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002785 if (ret <= 0)
2786 break;
2787 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002788 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002789
Jens Axboe500f9fb2019-08-19 12:15:59 -06002790 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002791 return ret;
2792}
2793
Jens Axboe491381ce2019-10-17 09:20:46 -06002794static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002795{
Jens Axboe491381ce2019-10-17 09:20:46 -06002796 /*
2797 * Tell lockdep we inherited freeze protection from submission
2798 * thread.
2799 */
2800 if (req->flags & REQ_F_ISREG) {
2801 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002802
Jens Axboe491381ce2019-10-17 09:20:46 -06002803 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002804 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002805 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806}
2807
Jens Axboeb63534c2020-06-04 11:28:00 -06002808#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002809static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002810{
2811 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002812 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002813 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002814
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002815 /* already prepared */
2816 if (req->async_data)
2817 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002818
2819 switch (req->opcode) {
2820 case IORING_OP_READV:
2821 case IORING_OP_READ_FIXED:
2822 case IORING_OP_READ:
2823 rw = READ;
2824 break;
2825 case IORING_OP_WRITEV:
2826 case IORING_OP_WRITE_FIXED:
2827 case IORING_OP_WRITE:
2828 rw = WRITE;
2829 break;
2830 default:
2831 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2832 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002833 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002834 }
2835
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002836 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2837 if (ret < 0)
2838 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002839 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002840}
Jens Axboeb63534c2020-06-04 11:28:00 -06002841#endif
2842
Pavel Begunkov23faba32021-02-11 18:28:22 +00002843static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002844{
2845#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002846 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002847 int ret;
2848
Jens Axboe355afae2020-09-02 09:30:31 -06002849 if (!S_ISBLK(mode) && !S_ISREG(mode))
2850 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002851 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002852 return false;
2853
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002854 lockdep_assert_held(&req->ctx->uring_lock);
2855
Jens Axboe28cea78a2020-09-14 10:51:17 -06002856 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002857
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002858 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002859 refcount_inc(&req->refs);
2860 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002861 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002862 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002863 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002864#endif
2865 return false;
2866}
2867
Jens Axboea1d7c392020-06-22 11:09:46 -06002868static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002869 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002870{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002871 int cflags = 0;
2872
Pavel Begunkov23faba32021-02-11 18:28:22 +00002873 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2874 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002875 if (res != req->result)
2876 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002877
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002878 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2879 kiocb_end_write(req);
2880 if (req->flags & REQ_F_BUFFER_SELECTED)
2881 cflags = io_put_rw_kbuf(req);
2882 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002883}
2884
2885static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2886{
Jens Axboe9adbd452019-12-20 08:45:55 -07002887 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002888
Pavel Begunkov889fca72021-02-10 00:03:09 +00002889 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890}
2891
Jens Axboedef596e2019-01-09 08:59:42 -07002892static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2893{
Jens Axboe9adbd452019-12-20 08:45:55 -07002894 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002895
Jens Axboe491381ce2019-10-17 09:20:46 -06002896 if (kiocb->ki_flags & IOCB_WRITE)
2897 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002898
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002899 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002900 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002901
2902 WRITE_ONCE(req->result, res);
2903 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002904 smp_wmb();
2905 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002906}
2907
2908/*
2909 * After the iocb has been issued, it's safe to be found on the poll list.
2910 * Adding the kiocb to the list AFTER submission ensures that we don't
2911 * find it from a io_iopoll_getevents() thread before the issuer is done
2912 * accessing the kiocb cookie.
2913 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002914static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002915{
2916 struct io_ring_ctx *ctx = req->ctx;
2917
2918 /*
2919 * Track whether we have multiple files in our lists. This will impact
2920 * how we do polling eventually, not spinning if we're on potentially
2921 * different devices.
2922 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002923 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002924 ctx->poll_multi_file = false;
2925 } else if (!ctx->poll_multi_file) {
2926 struct io_kiocb *list_req;
2927
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002928 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002929 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002930 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002931 ctx->poll_multi_file = true;
2932 }
2933
2934 /*
2935 * For fast devices, IO may have already completed. If it has, add
2936 * it to the front so we find it first.
2937 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002938 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002939 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002940 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002941 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002942
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002943 /*
2944 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2945 * task context or in io worker task context. If current task context is
2946 * sq thread, we don't need to check whether should wake up sq thread.
2947 */
2948 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002949 wq_has_sleeper(&ctx->sq_data->wait))
2950 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002951}
2952
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002953static inline void io_state_file_put(struct io_submit_state *state)
2954{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002955 if (state->file_refs) {
2956 fput_many(state->file, state->file_refs);
2957 state->file_refs = 0;
2958 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002959}
2960
2961/*
2962 * Get as many references to a file as we have IOs left in this submission,
2963 * assuming most submissions are for one file, or at least that each file
2964 * has more than one submission.
2965 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002966static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002967{
2968 if (!state)
2969 return fget(fd);
2970
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002971 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002972 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002973 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002974 return state->file;
2975 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002976 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002977 }
2978 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002979 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002980 return NULL;
2981
2982 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002983 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002984 return state->file;
2985}
2986
Jens Axboe4503b762020-06-01 10:00:27 -06002987static bool io_bdev_nowait(struct block_device *bdev)
2988{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002989 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002990}
2991
Jens Axboe2b188cc2019-01-07 10:46:33 -07002992/*
2993 * If we tracked the file through the SCM inflight mechanism, we could support
2994 * any file. For now, just ensure that anything potentially problematic is done
2995 * inline.
2996 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002997static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002998{
2999 umode_t mode = file_inode(file)->i_mode;
3000
Jens Axboe4503b762020-06-01 10:00:27 -06003001 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01003002 if (IS_ENABLED(CONFIG_BLOCK) &&
3003 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06003004 return true;
3005 return false;
3006 }
3007 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003008 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06003009 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01003010 if (IS_ENABLED(CONFIG_BLOCK) &&
3011 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06003012 file->f_op != &io_uring_fops)
3013 return true;
3014 return false;
3015 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003016
Jens Axboec5b85622020-06-09 19:23:05 -06003017 /* any ->read/write should understand O_NONBLOCK */
3018 if (file->f_flags & O_NONBLOCK)
3019 return true;
3020
Jens Axboeaf197f52020-04-28 13:15:06 -06003021 if (!(file->f_mode & FMODE_NOWAIT))
3022 return false;
3023
3024 if (rw == READ)
3025 return file->f_op->read_iter != NULL;
3026
3027 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003028}
3029
Pavel Begunkova88fc402020-09-30 22:57:53 +03003030static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003031{
Jens Axboedef596e2019-01-09 08:59:42 -07003032 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07003033 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003034 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06003035 unsigned ioprio;
3036 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003037
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003038 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06003039 req->flags |= REQ_F_ISREG;
3040
Jens Axboe2b188cc2019-01-07 10:46:33 -07003041 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003042 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07003043 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003044 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07003045 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003046 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03003047 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
3048 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
3049 if (unlikely(ret))
3050 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003051
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003052 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
3053 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
3054 req->flags |= REQ_F_NOWAIT;
3055
Jens Axboe2b188cc2019-01-07 10:46:33 -07003056 ioprio = READ_ONCE(sqe->ioprio);
3057 if (ioprio) {
3058 ret = ioprio_check_cap(ioprio);
3059 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06003060 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003061
3062 kiocb->ki_ioprio = ioprio;
3063 } else
3064 kiocb->ki_ioprio = get_current_ioprio();
3065
Jens Axboedef596e2019-01-09 08:59:42 -07003066 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003067 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
3068 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06003069 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003070
Jens Axboedef596e2019-01-09 08:59:42 -07003071 kiocb->ki_flags |= IOCB_HIPRI;
3072 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08003073 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07003074 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06003075 if (kiocb->ki_flags & IOCB_HIPRI)
3076 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07003077 kiocb->ki_complete = io_complete_rw;
3078 }
Jens Axboe9adbd452019-12-20 08:45:55 -07003079
Jens Axboe3529d8c2019-12-19 18:24:38 -07003080 req->rw.addr = READ_ONCE(sqe->addr);
3081 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003082 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003083 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003084}
3085
3086static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3087{
3088 switch (ret) {
3089 case -EIOCBQUEUED:
3090 break;
3091 case -ERESTARTSYS:
3092 case -ERESTARTNOINTR:
3093 case -ERESTARTNOHAND:
3094 case -ERESTART_RESTARTBLOCK:
3095 /*
3096 * We can't just restart the syscall, since previously
3097 * submitted sqes may already be in progress. Just fail this
3098 * IO with EINTR.
3099 */
3100 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003101 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003102 default:
3103 kiocb->ki_complete(kiocb, ret, 0);
3104 }
3105}
3106
Jens Axboea1d7c392020-06-22 11:09:46 -06003107static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003108 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003109{
Jens Axboeba042912019-12-25 16:33:42 -07003110 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07003111 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003112
Jens Axboe227c0c92020-08-13 11:51:40 -06003113 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003114 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003115 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003116 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003117 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003118 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003119 }
3120
Jens Axboeba042912019-12-25 16:33:42 -07003121 if (req->flags & REQ_F_CUR_POS)
3122 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03003123 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00003124 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003125 else
3126 io_rw_done(kiocb, ret);
3127}
3128
Pavel Begunkov847595d2021-02-04 13:52:06 +00003129static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003130{
Jens Axboe9adbd452019-12-20 08:45:55 -07003131 struct io_ring_ctx *ctx = req->ctx;
3132 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003133 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003134 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003135 size_t offset;
3136 u64 buf_addr;
3137
Jens Axboeedafcce2019-01-09 09:16:05 -07003138 if (unlikely(buf_index >= ctx->nr_user_bufs))
3139 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003140 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3141 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003142 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003143
3144 /* overflow */
3145 if (buf_addr + len < buf_addr)
3146 return -EFAULT;
3147 /* not inside the mapped region */
3148 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3149 return -EFAULT;
3150
3151 /*
3152 * May not be a start of buffer, set size appropriately
3153 * and advance us to the beginning.
3154 */
3155 offset = buf_addr - imu->ubuf;
3156 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003157
3158 if (offset) {
3159 /*
3160 * Don't use iov_iter_advance() here, as it's really slow for
3161 * using the latter parts of a big fixed buffer - it iterates
3162 * over each segment manually. We can cheat a bit here, because
3163 * we know that:
3164 *
3165 * 1) it's a BVEC iter, we set it up
3166 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3167 * first and last bvec
3168 *
3169 * So just find our index, and adjust the iterator afterwards.
3170 * If the offset is within the first bvec (or the whole first
3171 * bvec, just use iov_iter_advance(). This makes it easier
3172 * since we can just skip the first segment, which may not
3173 * be PAGE_SIZE aligned.
3174 */
3175 const struct bio_vec *bvec = imu->bvec;
3176
3177 if (offset <= bvec->bv_len) {
3178 iov_iter_advance(iter, offset);
3179 } else {
3180 unsigned long seg_skip;
3181
3182 /* skip first vec */
3183 offset -= bvec->bv_len;
3184 seg_skip = 1 + (offset >> PAGE_SHIFT);
3185
3186 iter->bvec = bvec + seg_skip;
3187 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003188 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003189 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003190 }
3191 }
3192
Pavel Begunkov847595d2021-02-04 13:52:06 +00003193 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003194}
3195
Jens Axboebcda7ba2020-02-23 16:42:51 -07003196static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3197{
3198 if (needs_lock)
3199 mutex_unlock(&ctx->uring_lock);
3200}
3201
3202static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3203{
3204 /*
3205 * "Normal" inline submissions always hold the uring_lock, since we
3206 * grab it from the system call. Same is true for the SQPOLL offload.
3207 * The only exception is when we've detached the request and issue it
3208 * from an async worker thread, grab the lock for that case.
3209 */
3210 if (needs_lock)
3211 mutex_lock(&ctx->uring_lock);
3212}
3213
3214static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3215 int bgid, struct io_buffer *kbuf,
3216 bool needs_lock)
3217{
3218 struct io_buffer *head;
3219
3220 if (req->flags & REQ_F_BUFFER_SELECTED)
3221 return kbuf;
3222
3223 io_ring_submit_lock(req->ctx, needs_lock);
3224
3225 lockdep_assert_held(&req->ctx->uring_lock);
3226
3227 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3228 if (head) {
3229 if (!list_empty(&head->list)) {
3230 kbuf = list_last_entry(&head->list, struct io_buffer,
3231 list);
3232 list_del(&kbuf->list);
3233 } else {
3234 kbuf = head;
3235 idr_remove(&req->ctx->io_buffer_idr, bgid);
3236 }
3237 if (*len > kbuf->len)
3238 *len = kbuf->len;
3239 } else {
3240 kbuf = ERR_PTR(-ENOBUFS);
3241 }
3242
3243 io_ring_submit_unlock(req->ctx, needs_lock);
3244
3245 return kbuf;
3246}
3247
Jens Axboe4d954c22020-02-27 07:31:19 -07003248static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3249 bool needs_lock)
3250{
3251 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003252 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003253
3254 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003255 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003256 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3257 if (IS_ERR(kbuf))
3258 return kbuf;
3259 req->rw.addr = (u64) (unsigned long) kbuf;
3260 req->flags |= REQ_F_BUFFER_SELECTED;
3261 return u64_to_user_ptr(kbuf->addr);
3262}
3263
3264#ifdef CONFIG_COMPAT
3265static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3266 bool needs_lock)
3267{
3268 struct compat_iovec __user *uiov;
3269 compat_ssize_t clen;
3270 void __user *buf;
3271 ssize_t len;
3272
3273 uiov = u64_to_user_ptr(req->rw.addr);
3274 if (!access_ok(uiov, sizeof(*uiov)))
3275 return -EFAULT;
3276 if (__get_user(clen, &uiov->iov_len))
3277 return -EFAULT;
3278 if (clen < 0)
3279 return -EINVAL;
3280
3281 len = clen;
3282 buf = io_rw_buffer_select(req, &len, needs_lock);
3283 if (IS_ERR(buf))
3284 return PTR_ERR(buf);
3285 iov[0].iov_base = buf;
3286 iov[0].iov_len = (compat_size_t) len;
3287 return 0;
3288}
3289#endif
3290
3291static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3292 bool needs_lock)
3293{
3294 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3295 void __user *buf;
3296 ssize_t len;
3297
3298 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3299 return -EFAULT;
3300
3301 len = iov[0].iov_len;
3302 if (len < 0)
3303 return -EINVAL;
3304 buf = io_rw_buffer_select(req, &len, needs_lock);
3305 if (IS_ERR(buf))
3306 return PTR_ERR(buf);
3307 iov[0].iov_base = buf;
3308 iov[0].iov_len = len;
3309 return 0;
3310}
3311
3312static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3313 bool needs_lock)
3314{
Jens Axboedddb3e22020-06-04 11:27:01 -06003315 if (req->flags & REQ_F_BUFFER_SELECTED) {
3316 struct io_buffer *kbuf;
3317
3318 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3319 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3320 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003321 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003322 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003323 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003324 return -EINVAL;
3325
3326#ifdef CONFIG_COMPAT
3327 if (req->ctx->compat)
3328 return io_compat_import(req, iov, needs_lock);
3329#endif
3330
3331 return __io_iov_buffer_select(req, iov, needs_lock);
3332}
3333
Pavel Begunkov847595d2021-02-04 13:52:06 +00003334static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3335 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003336{
Jens Axboe9adbd452019-12-20 08:45:55 -07003337 void __user *buf = u64_to_user_ptr(req->rw.addr);
3338 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003339 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003340 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003341
Pavel Begunkov7d009162019-11-25 23:14:40 +03003342 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003343 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003344 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003345 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003346
Jens Axboebcda7ba2020-02-23 16:42:51 -07003347 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003348 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003349 return -EINVAL;
3350
Jens Axboe3a6820f2019-12-22 15:19:35 -07003351 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003352 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003353 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003354 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003355 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003356 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003357 }
3358
Jens Axboe3a6820f2019-12-22 15:19:35 -07003359 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3360 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003361 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003362 }
3363
Jens Axboe4d954c22020-02-27 07:31:19 -07003364 if (req->flags & REQ_F_BUFFER_SELECT) {
3365 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003366 if (!ret)
3367 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003368 *iovec = NULL;
3369 return ret;
3370 }
3371
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003372 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3373 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003374}
3375
Jens Axboe0fef9482020-08-26 10:36:20 -06003376static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3377{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003378 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003379}
3380
Jens Axboe32960612019-09-23 11:05:34 -06003381/*
3382 * For files that don't have ->read_iter() and ->write_iter(), handle them
3383 * by looping over ->read() or ->write() manually.
3384 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003385static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003386{
Jens Axboe4017eb92020-10-22 14:14:12 -06003387 struct kiocb *kiocb = &req->rw.kiocb;
3388 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003389 ssize_t ret = 0;
3390
3391 /*
3392 * Don't support polled IO through this interface, and we can't
3393 * support non-blocking either. For the latter, this just causes
3394 * the kiocb to be handled from an async context.
3395 */
3396 if (kiocb->ki_flags & IOCB_HIPRI)
3397 return -EOPNOTSUPP;
3398 if (kiocb->ki_flags & IOCB_NOWAIT)
3399 return -EAGAIN;
3400
3401 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003402 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003403 ssize_t nr;
3404
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003405 if (!iov_iter_is_bvec(iter)) {
3406 iovec = iov_iter_iovec(iter);
3407 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003408 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3409 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003410 }
3411
Jens Axboe32960612019-09-23 11:05:34 -06003412 if (rw == READ) {
3413 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003414 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003415 } else {
3416 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003417 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003418 }
3419
3420 if (nr < 0) {
3421 if (!ret)
3422 ret = nr;
3423 break;
3424 }
3425 ret += nr;
3426 if (nr != iovec.iov_len)
3427 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003428 req->rw.len -= nr;
3429 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003430 iov_iter_advance(iter, nr);
3431 }
3432
3433 return ret;
3434}
3435
Jens Axboeff6165b2020-08-13 09:47:43 -06003436static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3437 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003438{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003439 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003440
Jens Axboeff6165b2020-08-13 09:47:43 -06003441 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003442 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003443 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003444 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003445 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003446 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003447 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003448 unsigned iov_off = 0;
3449
3450 rw->iter.iov = rw->fast_iov;
3451 if (iter->iov != fast_iov) {
3452 iov_off = iter->iov - fast_iov;
3453 rw->iter.iov += iov_off;
3454 }
3455 if (rw->fast_iov != fast_iov)
3456 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003457 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003458 } else {
3459 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003460 }
3461}
3462
Jens Axboee8c2bc12020-08-15 18:44:09 -07003463static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003464{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003465 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3466 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3467 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003468}
3469
Jens Axboee8c2bc12020-08-15 18:44:09 -07003470static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003471{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003472 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003473 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003474
Jens Axboee8c2bc12020-08-15 18:44:09 -07003475 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003476}
3477
Jens Axboeff6165b2020-08-13 09:47:43 -06003478static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3479 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003480 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003481{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003482 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003483 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003484 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003485 if (__io_alloc_async_data(req)) {
3486 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003487 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003488 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003489
Jens Axboeff6165b2020-08-13 09:47:43 -06003490 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003491 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003492 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003493}
3494
Pavel Begunkov73debe62020-09-30 22:57:54 +03003495static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003496{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003497 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003498 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003499 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003500
Pavel Begunkov2846c482020-11-07 13:16:27 +00003501 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003502 if (unlikely(ret < 0))
3503 return ret;
3504
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003505 iorw->bytes_done = 0;
3506 iorw->free_iovec = iov;
3507 if (iov)
3508 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003509 return 0;
3510}
3511
Pavel Begunkov73debe62020-09-30 22:57:54 +03003512static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003513{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003514 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3515 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003516 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003517}
3518
Jens Axboec1dd91d2020-08-03 16:43:59 -06003519/*
3520 * This is our waitqueue callback handler, registered through lock_page_async()
3521 * when we initially tried to do the IO with the iocb armed our waitqueue.
3522 * This gets called when the page is unlocked, and we generally expect that to
3523 * happen when the page IO is completed and the page is now uptodate. This will
3524 * queue a task_work based retry of the operation, attempting to copy the data
3525 * again. If the latter fails because the page was NOT uptodate, then we will
3526 * do a thread based blocking retry of the operation. That's the unexpected
3527 * slow path.
3528 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003529static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3530 int sync, void *arg)
3531{
3532 struct wait_page_queue *wpq;
3533 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003534 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003535
3536 wpq = container_of(wait, struct wait_page_queue, wait);
3537
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003538 if (!wake_page_match(wpq, key))
3539 return 0;
3540
Hao Xuc8d317a2020-09-29 20:00:45 +08003541 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003542 list_del_init(&wait->entry);
3543
Jens Axboebcf5a062020-05-22 09:24:42 -06003544 /* submit ref gets dropped, acquire a new one */
3545 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003546 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003547 return 1;
3548}
3549
Jens Axboec1dd91d2020-08-03 16:43:59 -06003550/*
3551 * This controls whether a given IO request should be armed for async page
3552 * based retry. If we return false here, the request is handed to the async
3553 * worker threads for retry. If we're doing buffered reads on a regular file,
3554 * we prepare a private wait_page_queue entry and retry the operation. This
3555 * will either succeed because the page is now uptodate and unlocked, or it
3556 * will register a callback when the page is unlocked at IO completion. Through
3557 * that callback, io_uring uses task_work to setup a retry of the operation.
3558 * That retry will attempt the buffered read again. The retry will generally
3559 * succeed, or in rare cases where it fails, we then fall back to using the
3560 * async worker threads for a blocking retry.
3561 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003562static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003563{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003564 struct io_async_rw *rw = req->async_data;
3565 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003566 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003567
3568 /* never retry for NOWAIT, we just complete with -EAGAIN */
3569 if (req->flags & REQ_F_NOWAIT)
3570 return false;
3571
Jens Axboe227c0c92020-08-13 11:51:40 -06003572 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003573 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003574 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003575
Jens Axboebcf5a062020-05-22 09:24:42 -06003576 /*
3577 * just use poll if we can, and don't attempt if the fs doesn't
3578 * support callback based unlocks
3579 */
3580 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3581 return false;
3582
Jens Axboe3b2a4432020-08-16 10:58:43 -07003583 wait->wait.func = io_async_buf_func;
3584 wait->wait.private = req;
3585 wait->wait.flags = 0;
3586 INIT_LIST_HEAD(&wait->wait.entry);
3587 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003588 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003589 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003590 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003591}
3592
3593static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3594{
3595 if (req->file->f_op->read_iter)
3596 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003597 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003598 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003599 else
3600 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003601}
3602
Pavel Begunkov889fca72021-02-10 00:03:09 +00003603static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003604{
3605 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003606 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003607 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003608 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003609 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003610 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003611
Pavel Begunkov2846c482020-11-07 13:16:27 +00003612 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003613 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003614 iovec = NULL;
3615 } else {
3616 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3617 if (ret < 0)
3618 return ret;
3619 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003620 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003621 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003622
Jens Axboefd6c2e42019-12-18 12:19:41 -07003623 /* Ensure we clear previously set non-block flag */
3624 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003625 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003626 else
3627 kiocb->ki_flags |= IOCB_NOWAIT;
3628
Pavel Begunkov24c74672020-06-21 13:09:51 +03003629 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003630 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3631 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003632 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003633 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003634
Pavel Begunkov632546c2020-11-07 13:16:26 +00003635 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003636 if (unlikely(ret)) {
3637 kfree(iovec);
3638 return ret;
3639 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003640
Jens Axboe227c0c92020-08-13 11:51:40 -06003641 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003642
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003643 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003644 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003645 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003646 /* IOPOLL retry should happen for io-wq threads */
3647 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003648 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003649 /* no retry on NONBLOCK nor RWF_NOWAIT */
3650 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003651 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003652 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003653 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003654 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003655 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003656 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003657 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003658 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003659 }
3660
Jens Axboe227c0c92020-08-13 11:51:40 -06003661 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003662 if (ret2)
3663 return ret2;
3664
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003665 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003666 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003667 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003668 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003669
Pavel Begunkovb23df912021-02-04 13:52:04 +00003670 do {
3671 io_size -= ret;
3672 rw->bytes_done += ret;
3673 /* if we can retry, do so with the callbacks armed */
3674 if (!io_rw_should_retry(req)) {
3675 kiocb->ki_flags &= ~IOCB_WAITQ;
3676 return -EAGAIN;
3677 }
3678
3679 /*
3680 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3681 * we get -EIOCBQUEUED, then we'll get a notification when the
3682 * desired page gets unlocked. We can also get a partial read
3683 * here, and if we do, then just retry at the new offset.
3684 */
3685 ret = io_iter_do_read(req, iter);
3686 if (ret == -EIOCBQUEUED)
3687 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003688 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003689 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003690done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003691 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003692out_free:
3693 /* it's faster to check here then delegate to kfree */
3694 if (iovec)
3695 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003696 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003697}
3698
Pavel Begunkov73debe62020-09-30 22:57:54 +03003699static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003700{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003701 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3702 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003703 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003704}
3705
Pavel Begunkov889fca72021-02-10 00:03:09 +00003706static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003707{
3708 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003709 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003710 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003711 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003712 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003713 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003714
Pavel Begunkov2846c482020-11-07 13:16:27 +00003715 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003716 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003717 iovec = NULL;
3718 } else {
3719 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3720 if (ret < 0)
3721 return ret;
3722 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003723 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003724 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003725
Jens Axboefd6c2e42019-12-18 12:19:41 -07003726 /* Ensure we clear previously set non-block flag */
3727 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003728 kiocb->ki_flags &= ~IOCB_NOWAIT;
3729 else
3730 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003731
Pavel Begunkov24c74672020-06-21 13:09:51 +03003732 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003733 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003734 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003735
Jens Axboe10d59342019-12-09 20:16:22 -07003736 /* file path doesn't support NOWAIT for non-direct_IO */
3737 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3738 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003739 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003740
Pavel Begunkov632546c2020-11-07 13:16:26 +00003741 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003742 if (unlikely(ret))
3743 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003744
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003745 /*
3746 * Open-code file_start_write here to grab freeze protection,
3747 * which will be released by another thread in
3748 * io_complete_rw(). Fool lockdep by telling it the lock got
3749 * released so that it doesn't complain about the held lock when
3750 * we return to userspace.
3751 */
3752 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003753 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003754 __sb_writers_release(file_inode(req->file)->i_sb,
3755 SB_FREEZE_WRITE);
3756 }
3757 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003758
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003759 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003760 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003761 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003762 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003763 else
3764 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003765
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003766 /*
3767 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3768 * retry them without IOCB_NOWAIT.
3769 */
3770 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3771 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003772 /* no retry on NONBLOCK nor RWF_NOWAIT */
3773 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003774 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003775 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003776 /* IOPOLL retry should happen for io-wq threads */
3777 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3778 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003779done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003780 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003781 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003782copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003783 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003784 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003785 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003786 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003787 }
Jens Axboe31b51512019-01-18 22:56:34 -07003788out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003789 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003790 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003791 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003792 return ret;
3793}
3794
Jens Axboe80a261f2020-09-28 14:23:58 -06003795static int io_renameat_prep(struct io_kiocb *req,
3796 const struct io_uring_sqe *sqe)
3797{
3798 struct io_rename *ren = &req->rename;
3799 const char __user *oldf, *newf;
3800
3801 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3802 return -EBADF;
3803
3804 ren->old_dfd = READ_ONCE(sqe->fd);
3805 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3806 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3807 ren->new_dfd = READ_ONCE(sqe->len);
3808 ren->flags = READ_ONCE(sqe->rename_flags);
3809
3810 ren->oldpath = getname(oldf);
3811 if (IS_ERR(ren->oldpath))
3812 return PTR_ERR(ren->oldpath);
3813
3814 ren->newpath = getname(newf);
3815 if (IS_ERR(ren->newpath)) {
3816 putname(ren->oldpath);
3817 return PTR_ERR(ren->newpath);
3818 }
3819
3820 req->flags |= REQ_F_NEED_CLEANUP;
3821 return 0;
3822}
3823
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003824static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003825{
3826 struct io_rename *ren = &req->rename;
3827 int ret;
3828
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003829 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003830 return -EAGAIN;
3831
3832 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3833 ren->newpath, ren->flags);
3834
3835 req->flags &= ~REQ_F_NEED_CLEANUP;
3836 if (ret < 0)
3837 req_set_fail_links(req);
3838 io_req_complete(req, ret);
3839 return 0;
3840}
3841
Jens Axboe14a11432020-09-28 14:27:37 -06003842static int io_unlinkat_prep(struct io_kiocb *req,
3843 const struct io_uring_sqe *sqe)
3844{
3845 struct io_unlink *un = &req->unlink;
3846 const char __user *fname;
3847
3848 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3849 return -EBADF;
3850
3851 un->dfd = READ_ONCE(sqe->fd);
3852
3853 un->flags = READ_ONCE(sqe->unlink_flags);
3854 if (un->flags & ~AT_REMOVEDIR)
3855 return -EINVAL;
3856
3857 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3858 un->filename = getname(fname);
3859 if (IS_ERR(un->filename))
3860 return PTR_ERR(un->filename);
3861
3862 req->flags |= REQ_F_NEED_CLEANUP;
3863 return 0;
3864}
3865
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003866static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003867{
3868 struct io_unlink *un = &req->unlink;
3869 int ret;
3870
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003871 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003872 return -EAGAIN;
3873
3874 if (un->flags & AT_REMOVEDIR)
3875 ret = do_rmdir(un->dfd, un->filename);
3876 else
3877 ret = do_unlinkat(un->dfd, un->filename);
3878
3879 req->flags &= ~REQ_F_NEED_CLEANUP;
3880 if (ret < 0)
3881 req_set_fail_links(req);
3882 io_req_complete(req, ret);
3883 return 0;
3884}
3885
Jens Axboe36f4fa62020-09-05 11:14:22 -06003886static int io_shutdown_prep(struct io_kiocb *req,
3887 const struct io_uring_sqe *sqe)
3888{
3889#if defined(CONFIG_NET)
3890 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3891 return -EINVAL;
3892 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3893 sqe->buf_index)
3894 return -EINVAL;
3895
3896 req->shutdown.how = READ_ONCE(sqe->len);
3897 return 0;
3898#else
3899 return -EOPNOTSUPP;
3900#endif
3901}
3902
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003903static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003904{
3905#if defined(CONFIG_NET)
3906 struct socket *sock;
3907 int ret;
3908
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003909 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003910 return -EAGAIN;
3911
Linus Torvalds48aba792020-12-16 12:44:05 -08003912 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003913 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003914 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003915
3916 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003917 if (ret < 0)
3918 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003919 io_req_complete(req, ret);
3920 return 0;
3921#else
3922 return -EOPNOTSUPP;
3923#endif
3924}
3925
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003926static int __io_splice_prep(struct io_kiocb *req,
3927 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003928{
3929 struct io_splice* sp = &req->splice;
3930 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003931
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003932 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3933 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003934
3935 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003936 sp->len = READ_ONCE(sqe->len);
3937 sp->flags = READ_ONCE(sqe->splice_flags);
3938
3939 if (unlikely(sp->flags & ~valid_flags))
3940 return -EINVAL;
3941
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003942 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3943 (sp->flags & SPLICE_F_FD_IN_FIXED));
3944 if (!sp->file_in)
3945 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003946 req->flags |= REQ_F_NEED_CLEANUP;
3947
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003948 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3949 /*
3950 * Splice operation will be punted aync, and here need to
3951 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3952 */
3953 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003954 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003955 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003956
3957 return 0;
3958}
3959
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003960static int io_tee_prep(struct io_kiocb *req,
3961 const struct io_uring_sqe *sqe)
3962{
3963 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3964 return -EINVAL;
3965 return __io_splice_prep(req, sqe);
3966}
3967
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003968static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003969{
3970 struct io_splice *sp = &req->splice;
3971 struct file *in = sp->file_in;
3972 struct file *out = sp->file_out;
3973 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3974 long ret = 0;
3975
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003976 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003977 return -EAGAIN;
3978 if (sp->len)
3979 ret = do_tee(in, out, sp->len, flags);
3980
3981 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3982 req->flags &= ~REQ_F_NEED_CLEANUP;
3983
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003984 if (ret != sp->len)
3985 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003986 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003987 return 0;
3988}
3989
3990static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3991{
3992 struct io_splice* sp = &req->splice;
3993
3994 sp->off_in = READ_ONCE(sqe->splice_off_in);
3995 sp->off_out = READ_ONCE(sqe->off);
3996 return __io_splice_prep(req, sqe);
3997}
3998
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003999static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004000{
4001 struct io_splice *sp = &req->splice;
4002 struct file *in = sp->file_in;
4003 struct file *out = sp->file_out;
4004 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4005 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004006 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004007
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004008 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004009 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004010
4011 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4012 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004013
Jens Axboe948a7742020-05-17 14:21:38 -06004014 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004015 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004016
4017 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
4018 req->flags &= ~REQ_F_NEED_CLEANUP;
4019
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004020 if (ret != sp->len)
4021 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004022 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004023 return 0;
4024}
4025
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026/*
4027 * IORING_OP_NOP just posts a completion event, nothing else.
4028 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004029static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004030{
4031 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004032
Jens Axboedef596e2019-01-09 08:59:42 -07004033 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4034 return -EINVAL;
4035
Pavel Begunkov889fca72021-02-10 00:03:09 +00004036 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004037 return 0;
4038}
4039
Pavel Begunkov1155c762021-02-18 18:29:38 +00004040static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004041{
Jens Axboe6b063142019-01-10 22:13:58 -07004042 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004043
Jens Axboe09bb8392019-03-13 12:39:28 -06004044 if (!req->file)
4045 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004046
Jens Axboe6b063142019-01-10 22:13:58 -07004047 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004048 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07004049 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004050 return -EINVAL;
4051
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004052 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4053 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4054 return -EINVAL;
4055
4056 req->sync.off = READ_ONCE(sqe->off);
4057 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004058 return 0;
4059}
4060
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004061static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004062{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004063 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004064 int ret;
4065
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004066 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004067 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004068 return -EAGAIN;
4069
Jens Axboe9adbd452019-12-20 08:45:55 -07004070 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004071 end > 0 ? end : LLONG_MAX,
4072 req->sync.flags & IORING_FSYNC_DATASYNC);
4073 if (ret < 0)
4074 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004075 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004076 return 0;
4077}
4078
Jens Axboed63d1b52019-12-10 10:38:56 -07004079static int io_fallocate_prep(struct io_kiocb *req,
4080 const struct io_uring_sqe *sqe)
4081{
4082 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4083 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004084 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4085 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004086
4087 req->sync.off = READ_ONCE(sqe->off);
4088 req->sync.len = READ_ONCE(sqe->addr);
4089 req->sync.mode = READ_ONCE(sqe->len);
4090 return 0;
4091}
4092
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004093static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004094{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004095 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004096
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004097 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004098 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004099 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004100 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4101 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004102 if (ret < 0)
4103 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004104 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004105 return 0;
4106}
4107
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004108static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004109{
Jens Axboef8748882020-01-08 17:47:02 -07004110 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004111 int ret;
4112
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004113 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004114 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004115 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004116 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004117
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004118 /* open.how should be already initialised */
4119 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004120 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004121
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004122 req->open.dfd = READ_ONCE(sqe->fd);
4123 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004124 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004125 if (IS_ERR(req->open.filename)) {
4126 ret = PTR_ERR(req->open.filename);
4127 req->open.filename = NULL;
4128 return ret;
4129 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004130 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004131 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004132 return 0;
4133}
4134
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004135static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4136{
4137 u64 flags, mode;
4138
Jens Axboe14587a462020-09-05 11:36:08 -06004139 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004140 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004141 mode = READ_ONCE(sqe->len);
4142 flags = READ_ONCE(sqe->open_flags);
4143 req->open.how = build_open_how(flags, mode);
4144 return __io_openat_prep(req, sqe);
4145}
4146
Jens Axboecebdb982020-01-08 17:59:24 -07004147static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4148{
4149 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004150 size_t len;
4151 int ret;
4152
Jens Axboe14587a462020-09-05 11:36:08 -06004153 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004154 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004155 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4156 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004157 if (len < OPEN_HOW_SIZE_VER0)
4158 return -EINVAL;
4159
4160 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4161 len);
4162 if (ret)
4163 return ret;
4164
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004165 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004166}
4167
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004168static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004169{
4170 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004171 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004172 bool nonblock_set;
4173 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004174 int ret;
4175
Jens Axboecebdb982020-01-08 17:59:24 -07004176 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004177 if (ret)
4178 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004179 nonblock_set = op.open_flag & O_NONBLOCK;
4180 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004181 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004182 /*
4183 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4184 * it'll always -EAGAIN
4185 */
4186 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4187 return -EAGAIN;
4188 op.lookup_flags |= LOOKUP_CACHED;
4189 op.open_flag |= O_NONBLOCK;
4190 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004191
Jens Axboe4022e7a2020-03-19 19:23:18 -06004192 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004193 if (ret < 0)
4194 goto err;
4195
4196 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004197 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004198 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4199 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004200 /*
4201 * We could hang on to this 'fd', but seems like marginal
4202 * gain for something that is now known to be a slower path.
4203 * So just put it, and we'll get a new one when we retry.
4204 */
4205 put_unused_fd(ret);
4206 return -EAGAIN;
4207 }
4208
Jens Axboe15b71ab2019-12-11 11:20:36 -07004209 if (IS_ERR(file)) {
4210 put_unused_fd(ret);
4211 ret = PTR_ERR(file);
4212 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004213 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004214 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004215 fsnotify_open(file);
4216 fd_install(ret, file);
4217 }
4218err:
4219 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004220 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004221 if (ret < 0)
4222 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004223 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004224 return 0;
4225}
4226
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004227static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004228{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004229 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004230}
4231
Jens Axboe067524e2020-03-02 16:32:28 -07004232static int io_remove_buffers_prep(struct io_kiocb *req,
4233 const struct io_uring_sqe *sqe)
4234{
4235 struct io_provide_buf *p = &req->pbuf;
4236 u64 tmp;
4237
4238 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4239 return -EINVAL;
4240
4241 tmp = READ_ONCE(sqe->fd);
4242 if (!tmp || tmp > USHRT_MAX)
4243 return -EINVAL;
4244
4245 memset(p, 0, sizeof(*p));
4246 p->nbufs = tmp;
4247 p->bgid = READ_ONCE(sqe->buf_group);
4248 return 0;
4249}
4250
4251static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4252 int bgid, unsigned nbufs)
4253{
4254 unsigned i = 0;
4255
4256 /* shouldn't happen */
4257 if (!nbufs)
4258 return 0;
4259
4260 /* the head kbuf is the list itself */
4261 while (!list_empty(&buf->list)) {
4262 struct io_buffer *nxt;
4263
4264 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4265 list_del(&nxt->list);
4266 kfree(nxt);
4267 if (++i == nbufs)
4268 return i;
4269 }
4270 i++;
4271 kfree(buf);
4272 idr_remove(&ctx->io_buffer_idr, bgid);
4273
4274 return i;
4275}
4276
Pavel Begunkov889fca72021-02-10 00:03:09 +00004277static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004278{
4279 struct io_provide_buf *p = &req->pbuf;
4280 struct io_ring_ctx *ctx = req->ctx;
4281 struct io_buffer *head;
4282 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004283 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004284
4285 io_ring_submit_lock(ctx, !force_nonblock);
4286
4287 lockdep_assert_held(&ctx->uring_lock);
4288
4289 ret = -ENOENT;
4290 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4291 if (head)
4292 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004293 if (ret < 0)
4294 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004295
4296 /* need to hold the lock to complete IOPOLL requests */
4297 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004298 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004299 io_ring_submit_unlock(ctx, !force_nonblock);
4300 } else {
4301 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004302 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004303 }
Jens Axboe067524e2020-03-02 16:32:28 -07004304 return 0;
4305}
4306
Jens Axboeddf0322d2020-02-23 16:41:33 -07004307static int io_provide_buffers_prep(struct io_kiocb *req,
4308 const struct io_uring_sqe *sqe)
4309{
4310 struct io_provide_buf *p = &req->pbuf;
4311 u64 tmp;
4312
4313 if (sqe->ioprio || sqe->rw_flags)
4314 return -EINVAL;
4315
4316 tmp = READ_ONCE(sqe->fd);
4317 if (!tmp || tmp > USHRT_MAX)
4318 return -E2BIG;
4319 p->nbufs = tmp;
4320 p->addr = READ_ONCE(sqe->addr);
4321 p->len = READ_ONCE(sqe->len);
4322
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004323 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004324 return -EFAULT;
4325
4326 p->bgid = READ_ONCE(sqe->buf_group);
4327 tmp = READ_ONCE(sqe->off);
4328 if (tmp > USHRT_MAX)
4329 return -E2BIG;
4330 p->bid = tmp;
4331 return 0;
4332}
4333
4334static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4335{
4336 struct io_buffer *buf;
4337 u64 addr = pbuf->addr;
4338 int i, bid = pbuf->bid;
4339
4340 for (i = 0; i < pbuf->nbufs; i++) {
4341 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4342 if (!buf)
4343 break;
4344
4345 buf->addr = addr;
4346 buf->len = pbuf->len;
4347 buf->bid = bid;
4348 addr += pbuf->len;
4349 bid++;
4350 if (!*head) {
4351 INIT_LIST_HEAD(&buf->list);
4352 *head = buf;
4353 } else {
4354 list_add_tail(&buf->list, &(*head)->list);
4355 }
4356 }
4357
4358 return i ? i : -ENOMEM;
4359}
4360
Pavel Begunkov889fca72021-02-10 00:03:09 +00004361static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004362{
4363 struct io_provide_buf *p = &req->pbuf;
4364 struct io_ring_ctx *ctx = req->ctx;
4365 struct io_buffer *head, *list;
4366 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004367 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004368
4369 io_ring_submit_lock(ctx, !force_nonblock);
4370
4371 lockdep_assert_held(&ctx->uring_lock);
4372
4373 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4374
4375 ret = io_add_buffers(p, &head);
4376 if (ret < 0)
4377 goto out;
4378
4379 if (!list) {
4380 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4381 GFP_KERNEL);
4382 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004383 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004384 goto out;
4385 }
4386 }
4387out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004388 if (ret < 0)
4389 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004390
4391 /* need to hold the lock to complete IOPOLL requests */
4392 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004393 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004394 io_ring_submit_unlock(ctx, !force_nonblock);
4395 } else {
4396 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004397 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004398 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004399 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004400}
4401
Jens Axboe3e4827b2020-01-08 15:18:09 -07004402static int io_epoll_ctl_prep(struct io_kiocb *req,
4403 const struct io_uring_sqe *sqe)
4404{
4405#if defined(CONFIG_EPOLL)
4406 if (sqe->ioprio || sqe->buf_index)
4407 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004408 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004409 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004410
4411 req->epoll.epfd = READ_ONCE(sqe->fd);
4412 req->epoll.op = READ_ONCE(sqe->len);
4413 req->epoll.fd = READ_ONCE(sqe->off);
4414
4415 if (ep_op_has_event(req->epoll.op)) {
4416 struct epoll_event __user *ev;
4417
4418 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4419 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4420 return -EFAULT;
4421 }
4422
4423 return 0;
4424#else
4425 return -EOPNOTSUPP;
4426#endif
4427}
4428
Pavel Begunkov889fca72021-02-10 00:03:09 +00004429static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004430{
4431#if defined(CONFIG_EPOLL)
4432 struct io_epoll *ie = &req->epoll;
4433 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004434 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004435
4436 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4437 if (force_nonblock && ret == -EAGAIN)
4438 return -EAGAIN;
4439
4440 if (ret < 0)
4441 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004442 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004443 return 0;
4444#else
4445 return -EOPNOTSUPP;
4446#endif
4447}
4448
Jens Axboec1ca7572019-12-25 22:18:28 -07004449static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4450{
4451#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4452 if (sqe->ioprio || sqe->buf_index || sqe->off)
4453 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004454 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4455 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004456
4457 req->madvise.addr = READ_ONCE(sqe->addr);
4458 req->madvise.len = READ_ONCE(sqe->len);
4459 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4460 return 0;
4461#else
4462 return -EOPNOTSUPP;
4463#endif
4464}
4465
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004466static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004467{
4468#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4469 struct io_madvise *ma = &req->madvise;
4470 int ret;
4471
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004472 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004473 return -EAGAIN;
4474
Minchan Kim0726b012020-10-17 16:14:50 -07004475 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004476 if (ret < 0)
4477 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004478 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004479 return 0;
4480#else
4481 return -EOPNOTSUPP;
4482#endif
4483}
4484
Jens Axboe4840e412019-12-25 22:03:45 -07004485static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4486{
4487 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4488 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004489 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4490 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004491
4492 req->fadvise.offset = READ_ONCE(sqe->off);
4493 req->fadvise.len = READ_ONCE(sqe->len);
4494 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4495 return 0;
4496}
4497
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004498static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004499{
4500 struct io_fadvise *fa = &req->fadvise;
4501 int ret;
4502
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004503 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004504 switch (fa->advice) {
4505 case POSIX_FADV_NORMAL:
4506 case POSIX_FADV_RANDOM:
4507 case POSIX_FADV_SEQUENTIAL:
4508 break;
4509 default:
4510 return -EAGAIN;
4511 }
4512 }
Jens Axboe4840e412019-12-25 22:03:45 -07004513
4514 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4515 if (ret < 0)
4516 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004517 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004518 return 0;
4519}
4520
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004521static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4522{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004523 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004524 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004525 if (sqe->ioprio || sqe->buf_index)
4526 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004527 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004528 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004529
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004530 req->statx.dfd = READ_ONCE(sqe->fd);
4531 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004532 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004533 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4534 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004535
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004536 return 0;
4537}
4538
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004539static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004540{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004541 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004542 int ret;
4543
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004544 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004545 /* only need file table for an actual valid fd */
4546 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4547 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004548 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004549 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004550
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004551 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4552 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004553
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004554 if (ret < 0)
4555 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004556 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004557 return 0;
4558}
4559
Jens Axboeb5dba592019-12-11 14:02:38 -07004560static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4561{
Jens Axboe14587a462020-09-05 11:36:08 -06004562 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004563 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004564 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4565 sqe->rw_flags || sqe->buf_index)
4566 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004567 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004568 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004569
4570 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004571 return 0;
4572}
4573
Pavel Begunkov889fca72021-02-10 00:03:09 +00004574static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004575{
Jens Axboe9eac1902021-01-19 15:50:37 -07004576 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004577 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004578 struct fdtable *fdt;
4579 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004580 int ret;
4581
Jens Axboe9eac1902021-01-19 15:50:37 -07004582 file = NULL;
4583 ret = -EBADF;
4584 spin_lock(&files->file_lock);
4585 fdt = files_fdtable(files);
4586 if (close->fd >= fdt->max_fds) {
4587 spin_unlock(&files->file_lock);
4588 goto err;
4589 }
4590 file = fdt->fd[close->fd];
4591 if (!file) {
4592 spin_unlock(&files->file_lock);
4593 goto err;
4594 }
4595
4596 if (file->f_op == &io_uring_fops) {
4597 spin_unlock(&files->file_lock);
4598 file = NULL;
4599 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004600 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004601
4602 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004603 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004604 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004605 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004606 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004607
Jens Axboe9eac1902021-01-19 15:50:37 -07004608 ret = __close_fd_get_file(close->fd, &file);
4609 spin_unlock(&files->file_lock);
4610 if (ret < 0) {
4611 if (ret == -ENOENT)
4612 ret = -EBADF;
4613 goto err;
4614 }
4615
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004616 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004617 ret = filp_close(file, current->files);
4618err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004619 if (ret < 0)
4620 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004621 if (file)
4622 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004623 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004624 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004625}
4626
Pavel Begunkov1155c762021-02-18 18:29:38 +00004627static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004628{
4629 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004630
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004631 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4632 return -EINVAL;
4633 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4634 return -EINVAL;
4635
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004636 req->sync.off = READ_ONCE(sqe->off);
4637 req->sync.len = READ_ONCE(sqe->len);
4638 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004639 return 0;
4640}
4641
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004642static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004643{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004644 int ret;
4645
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004646 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004647 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004648 return -EAGAIN;
4649
Jens Axboe9adbd452019-12-20 08:45:55 -07004650 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004651 req->sync.flags);
4652 if (ret < 0)
4653 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004654 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004655 return 0;
4656}
4657
YueHaibing469956e2020-03-04 15:53:52 +08004658#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004659static int io_setup_async_msg(struct io_kiocb *req,
4660 struct io_async_msghdr *kmsg)
4661{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004662 struct io_async_msghdr *async_msg = req->async_data;
4663
4664 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004665 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004666 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004667 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004668 return -ENOMEM;
4669 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004670 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004671 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004672 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004673 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004674 /* if were using fast_iov, set it to the new one */
4675 if (!async_msg->free_iov)
4676 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4677
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004678 return -EAGAIN;
4679}
4680
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004681static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4682 struct io_async_msghdr *iomsg)
4683{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004684 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004685 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004686 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004687 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004688}
4689
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004690static int io_sendmsg_prep_async(struct io_kiocb *req)
4691{
4692 int ret;
4693
4694 if (!io_op_defs[req->opcode].needs_async_data)
4695 return 0;
4696 ret = io_sendmsg_copy_hdr(req, req->async_data);
4697 if (!ret)
4698 req->flags |= REQ_F_NEED_CLEANUP;
4699 return ret;
4700}
4701
Jens Axboe3529d8c2019-12-19 18:24:38 -07004702static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004703{
Jens Axboee47293f2019-12-20 08:58:21 -07004704 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004705
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004706 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4707 return -EINVAL;
4708
Jens Axboee47293f2019-12-20 08:58:21 -07004709 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004710 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004711 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004712
Jens Axboed8768362020-02-27 14:17:49 -07004713#ifdef CONFIG_COMPAT
4714 if (req->ctx->compat)
4715 sr->msg_flags |= MSG_CMSG_COMPAT;
4716#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004717 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004718}
4719
Pavel Begunkov889fca72021-02-10 00:03:09 +00004720static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004721{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004722 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004723 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004724 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004725 int ret;
4726
Florent Revestdba4a922020-12-04 12:36:04 +01004727 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004728 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004729 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004730
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004731 kmsg = req->async_data;
4732 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004733 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004734 if (ret)
4735 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004736 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004737 }
4738
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004739 flags = req->sr_msg.msg_flags;
4740 if (flags & MSG_DONTWAIT)
4741 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004742 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004743 flags |= MSG_DONTWAIT;
4744
4745 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004746 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004747 return io_setup_async_msg(req, kmsg);
4748 if (ret == -ERESTARTSYS)
4749 ret = -EINTR;
4750
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004751 /* fast path, check for non-NULL to avoid function call */
4752 if (kmsg->free_iov)
4753 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004754 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004755 if (ret < 0)
4756 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004757 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004758 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004759}
4760
Pavel Begunkov889fca72021-02-10 00:03:09 +00004761static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004762{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004763 struct io_sr_msg *sr = &req->sr_msg;
4764 struct msghdr msg;
4765 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004766 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004767 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004768 int ret;
4769
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 Axboe03b12302019-12-02 18:50:25 -07004773
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004774 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4775 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004776 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004777
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004778 msg.msg_name = NULL;
4779 msg.msg_control = NULL;
4780 msg.msg_controllen = 0;
4781 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004782
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004783 flags = req->sr_msg.msg_flags;
4784 if (flags & MSG_DONTWAIT)
4785 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004786 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004787 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004788
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004789 msg.msg_flags = flags;
4790 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004791 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004792 return -EAGAIN;
4793 if (ret == -ERESTARTSYS)
4794 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004795
Jens Axboe03b12302019-12-02 18:50:25 -07004796 if (ret < 0)
4797 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004798 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004799 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004800}
4801
Pavel Begunkov1400e692020-07-12 20:41:05 +03004802static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4803 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004804{
4805 struct io_sr_msg *sr = &req->sr_msg;
4806 struct iovec __user *uiov;
4807 size_t iov_len;
4808 int ret;
4809
Pavel Begunkov1400e692020-07-12 20:41:05 +03004810 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4811 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004812 if (ret)
4813 return ret;
4814
4815 if (req->flags & REQ_F_BUFFER_SELECT) {
4816 if (iov_len > 1)
4817 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004818 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004819 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004820 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004821 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004822 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004823 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004824 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004825 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004826 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004827 if (ret > 0)
4828 ret = 0;
4829 }
4830
4831 return ret;
4832}
4833
4834#ifdef CONFIG_COMPAT
4835static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004836 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004837{
4838 struct compat_msghdr __user *msg_compat;
4839 struct io_sr_msg *sr = &req->sr_msg;
4840 struct compat_iovec __user *uiov;
4841 compat_uptr_t ptr;
4842 compat_size_t len;
4843 int ret;
4844
Pavel Begunkov270a5942020-07-12 20:41:04 +03004845 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004846 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004847 &ptr, &len);
4848 if (ret)
4849 return ret;
4850
4851 uiov = compat_ptr(ptr);
4852 if (req->flags & REQ_F_BUFFER_SELECT) {
4853 compat_ssize_t clen;
4854
4855 if (len > 1)
4856 return -EINVAL;
4857 if (!access_ok(uiov, sizeof(*uiov)))
4858 return -EFAULT;
4859 if (__get_user(clen, &uiov->iov_len))
4860 return -EFAULT;
4861 if (clen < 0)
4862 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004863 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004864 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004865 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004866 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004867 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004868 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004869 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004870 if (ret < 0)
4871 return ret;
4872 }
4873
4874 return 0;
4875}
Jens Axboe03b12302019-12-02 18:50:25 -07004876#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004877
Pavel Begunkov1400e692020-07-12 20:41:05 +03004878static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4879 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004880{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004881 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004882
4883#ifdef CONFIG_COMPAT
4884 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004885 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004886#endif
4887
Pavel Begunkov1400e692020-07-12 20:41:05 +03004888 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004889}
4890
Jens Axboebcda7ba2020-02-23 16:42:51 -07004891static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004892 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004893{
4894 struct io_sr_msg *sr = &req->sr_msg;
4895 struct io_buffer *kbuf;
4896
Jens Axboebcda7ba2020-02-23 16:42:51 -07004897 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4898 if (IS_ERR(kbuf))
4899 return kbuf;
4900
4901 sr->kbuf = kbuf;
4902 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004903 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004904}
4905
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004906static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4907{
4908 return io_put_kbuf(req, req->sr_msg.kbuf);
4909}
4910
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004911static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004912{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004913 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004914
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004915 if (!io_op_defs[req->opcode].needs_async_data)
4916 return 0;
4917 ret = io_recvmsg_copy_hdr(req, req->async_data);
4918 if (!ret)
4919 req->flags |= REQ_F_NEED_CLEANUP;
4920 return ret;
4921}
4922
4923static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4924{
4925 struct io_sr_msg *sr = &req->sr_msg;
4926
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004927 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4928 return -EINVAL;
4929
Jens Axboe3529d8c2019-12-19 18:24:38 -07004930 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004931 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004932 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004933 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004934
Jens Axboed8768362020-02-27 14:17:49 -07004935#ifdef CONFIG_COMPAT
4936 if (req->ctx->compat)
4937 sr->msg_flags |= MSG_CMSG_COMPAT;
4938#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004939 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004940}
4941
Pavel Begunkov889fca72021-02-10 00:03:09 +00004942static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004943{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004944 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004945 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004946 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004947 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004948 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004949 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004950
Florent Revestdba4a922020-12-04 12:36:04 +01004951 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004952 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004953 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004954
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004955 kmsg = req->async_data;
4956 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004957 ret = io_recvmsg_copy_hdr(req, &iomsg);
4958 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004959 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004960 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004961 }
4962
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004963 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004964 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004965 if (IS_ERR(kbuf))
4966 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004967 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004968 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4969 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004970 1, req->sr_msg.len);
4971 }
4972
4973 flags = req->sr_msg.msg_flags;
4974 if (flags & MSG_DONTWAIT)
4975 req->flags |= REQ_F_NOWAIT;
4976 else if (force_nonblock)
4977 flags |= MSG_DONTWAIT;
4978
4979 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4980 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004981 if (force_nonblock && ret == -EAGAIN)
4982 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004983 if (ret == -ERESTARTSYS)
4984 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004985
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004986 if (req->flags & REQ_F_BUFFER_SELECTED)
4987 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004988 /* fast path, check for non-NULL to avoid function call */
4989 if (kmsg->free_iov)
4990 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004991 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004992 if (ret < 0)
4993 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004994 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004995 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004996}
4997
Pavel Begunkov889fca72021-02-10 00:03:09 +00004998static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07004999{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005000 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005001 struct io_sr_msg *sr = &req->sr_msg;
5002 struct msghdr msg;
5003 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005004 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005005 struct iovec iov;
5006 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005007 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005008 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005009
Florent Revestdba4a922020-12-04 12:36:04 +01005010 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005011 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005012 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005013
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005014 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005015 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005016 if (IS_ERR(kbuf))
5017 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005018 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005019 }
5020
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005021 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005022 if (unlikely(ret))
5023 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005024
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005025 msg.msg_name = NULL;
5026 msg.msg_control = NULL;
5027 msg.msg_controllen = 0;
5028 msg.msg_namelen = 0;
5029 msg.msg_iocb = NULL;
5030 msg.msg_flags = 0;
5031
5032 flags = req->sr_msg.msg_flags;
5033 if (flags & MSG_DONTWAIT)
5034 req->flags |= REQ_F_NOWAIT;
5035 else if (force_nonblock)
5036 flags |= MSG_DONTWAIT;
5037
5038 ret = sock_recvmsg(sock, &msg, flags);
5039 if (force_nonblock && ret == -EAGAIN)
5040 return -EAGAIN;
5041 if (ret == -ERESTARTSYS)
5042 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005043out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005044 if (req->flags & REQ_F_BUFFER_SELECTED)
5045 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07005046 if (ret < 0)
5047 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005048 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005049 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005050}
5051
Jens Axboe3529d8c2019-12-19 18:24:38 -07005052static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005053{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005054 struct io_accept *accept = &req->accept;
5055
Jens Axboe14587a462020-09-05 11:36:08 -06005056 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005057 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05005058 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005059 return -EINVAL;
5060
Jens Axboed55e5f52019-12-11 16:12:15 -07005061 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5062 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005063 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005064 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005065 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005066}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005067
Pavel Begunkov889fca72021-02-10 00:03:09 +00005068static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005069{
5070 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005071 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005072 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005073 int ret;
5074
Jiufei Xuee697dee2020-06-10 13:41:59 +08005075 if (req->file->f_flags & O_NONBLOCK)
5076 req->flags |= REQ_F_NOWAIT;
5077
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005078 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06005079 accept->addr_len, accept->flags,
5080 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005081 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005082 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005083 if (ret < 0) {
5084 if (ret == -ERESTARTSYS)
5085 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005086 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005087 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005088 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005089 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005090}
5091
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005092static int io_connect_prep_async(struct io_kiocb *req)
5093{
5094 struct io_async_connect *io = req->async_data;
5095 struct io_connect *conn = &req->connect;
5096
5097 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5098}
5099
Jens Axboe3529d8c2019-12-19 18:24:38 -07005100static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005101{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005102 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005103
Jens Axboe14587a462020-09-05 11:36:08 -06005104 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005105 return -EINVAL;
5106 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5107 return -EINVAL;
5108
Jens Axboe3529d8c2019-12-19 18:24:38 -07005109 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5110 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005111 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005112}
5113
Pavel Begunkov889fca72021-02-10 00:03:09 +00005114static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005115{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005116 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005117 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005118 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005119 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005120
Jens Axboee8c2bc12020-08-15 18:44:09 -07005121 if (req->async_data) {
5122 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005123 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005124 ret = move_addr_to_kernel(req->connect.addr,
5125 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005126 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005127 if (ret)
5128 goto out;
5129 io = &__io;
5130 }
5131
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005132 file_flags = force_nonblock ? O_NONBLOCK : 0;
5133
Jens Axboee8c2bc12020-08-15 18:44:09 -07005134 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005135 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005136 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005137 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005138 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005139 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005140 ret = -ENOMEM;
5141 goto out;
5142 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005143 io = req->async_data;
5144 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005145 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005146 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005147 if (ret == -ERESTARTSYS)
5148 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005149out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005150 if (ret < 0)
5151 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005152 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005153 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005154}
YueHaibing469956e2020-03-04 15:53:52 +08005155#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005156#define IO_NETOP_FN(op) \
5157static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5158{ \
5159 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005160}
5161
Jens Axboe99a10082021-02-19 09:35:19 -07005162#define IO_NETOP_PREP(op) \
5163IO_NETOP_FN(op) \
5164static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5165{ \
5166 return -EOPNOTSUPP; \
5167} \
5168
5169#define IO_NETOP_PREP_ASYNC(op) \
5170IO_NETOP_PREP(op) \
5171static int io_##op##_prep_async(struct io_kiocb *req) \
5172{ \
5173 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005174}
5175
Jens Axboe99a10082021-02-19 09:35:19 -07005176IO_NETOP_PREP_ASYNC(sendmsg);
5177IO_NETOP_PREP_ASYNC(recvmsg);
5178IO_NETOP_PREP_ASYNC(connect);
5179IO_NETOP_PREP(accept);
5180IO_NETOP_FN(send);
5181IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005182#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005183
Jens Axboed7718a92020-02-14 22:23:12 -07005184struct io_poll_table {
5185 struct poll_table_struct pt;
5186 struct io_kiocb *req;
5187 int error;
5188};
5189
Jens Axboed7718a92020-02-14 22:23:12 -07005190static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5191 __poll_t mask, task_work_func_t func)
5192{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005193 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005194
5195 /* for instances that support it check for an event match first: */
5196 if (mask && !(mask & poll->events))
5197 return 0;
5198
5199 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5200
5201 list_del_init(&poll->wait.entry);
5202
Jens Axboed7718a92020-02-14 22:23:12 -07005203 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00005204 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005205 percpu_ref_get(&req->ctx->refs);
5206
Jens Axboed7718a92020-02-14 22:23:12 -07005207 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005208 * If this fails, then the task is exiting. When a task exits, the
5209 * work gets canceled, so just cancel this request as well instead
5210 * of executing it. We can't safely execute it anyway, as we may not
5211 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005212 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005213 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005214 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005215 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005216 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005217 }
Jens Axboed7718a92020-02-14 22:23:12 -07005218 return 1;
5219}
5220
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005221static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5222 __acquires(&req->ctx->completion_lock)
5223{
5224 struct io_ring_ctx *ctx = req->ctx;
5225
5226 if (!req->result && !READ_ONCE(poll->canceled)) {
5227 struct poll_table_struct pt = { ._key = poll->events };
5228
5229 req->result = vfs_poll(req->file, &pt) & poll->events;
5230 }
5231
5232 spin_lock_irq(&ctx->completion_lock);
5233 if (!req->result && !READ_ONCE(poll->canceled)) {
5234 add_wait_queue(poll->head, &poll->wait);
5235 return true;
5236 }
5237
5238 return false;
5239}
5240
Jens Axboed4e7cd32020-08-15 11:44:50 -07005241static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005242{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005243 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005244 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005245 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005246 return req->apoll->double_poll;
5247}
5248
5249static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5250{
5251 if (req->opcode == IORING_OP_POLL_ADD)
5252 return &req->poll;
5253 return &req->apoll->poll;
5254}
5255
5256static void io_poll_remove_double(struct io_kiocb *req)
5257{
5258 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005259
5260 lockdep_assert_held(&req->ctx->completion_lock);
5261
5262 if (poll && poll->head) {
5263 struct wait_queue_head *head = poll->head;
5264
5265 spin_lock(&head->lock);
5266 list_del_init(&poll->wait.entry);
5267 if (poll->wait.private)
5268 refcount_dec(&req->refs);
5269 poll->head = NULL;
5270 spin_unlock(&head->lock);
5271 }
5272}
5273
5274static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5275{
5276 struct io_ring_ctx *ctx = req->ctx;
5277
Jens Axboed4e7cd32020-08-15 11:44:50 -07005278 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005279 req->poll.done = true;
5280 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5281 io_commit_cqring(ctx);
5282}
5283
Jens Axboe18bceab2020-05-15 11:56:54 -06005284static void io_poll_task_func(struct callback_head *cb)
5285{
5286 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005287 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005288 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005289
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005290 if (io_poll_rewait(req, &req->poll)) {
5291 spin_unlock_irq(&ctx->completion_lock);
5292 } else {
5293 hash_del(&req->hash_node);
5294 io_poll_complete(req, req->result, 0);
5295 spin_unlock_irq(&ctx->completion_lock);
5296
5297 nxt = io_put_req_find_next(req);
5298 io_cqring_ev_posted(ctx);
5299 if (nxt)
5300 __io_req_task_submit(nxt);
5301 }
5302
Jens Axboe6d816e02020-08-11 08:04:14 -06005303 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005304}
5305
5306static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5307 int sync, void *key)
5308{
5309 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005310 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005311 __poll_t mask = key_to_poll(key);
5312
5313 /* for instances that support it check for an event match first: */
5314 if (mask && !(mask & poll->events))
5315 return 0;
5316
Jens Axboe8706e042020-09-28 08:38:54 -06005317 list_del_init(&wait->entry);
5318
Jens Axboe807abcb2020-07-17 17:09:27 -06005319 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005320 bool done;
5321
Jens Axboe807abcb2020-07-17 17:09:27 -06005322 spin_lock(&poll->head->lock);
5323 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005324 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005325 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005326 /* make sure double remove sees this as being gone */
5327 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005328 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005329 if (!done) {
5330 /* use wait func handler, so it matches the rq type */
5331 poll->wait.func(&poll->wait, mode, sync, key);
5332 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005333 }
5334 refcount_dec(&req->refs);
5335 return 1;
5336}
5337
5338static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5339 wait_queue_func_t wake_func)
5340{
5341 poll->head = NULL;
5342 poll->done = false;
5343 poll->canceled = false;
5344 poll->events = events;
5345 INIT_LIST_HEAD(&poll->wait.entry);
5346 init_waitqueue_func_entry(&poll->wait, wake_func);
5347}
5348
5349static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005350 struct wait_queue_head *head,
5351 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005352{
5353 struct io_kiocb *req = pt->req;
5354
5355 /*
5356 * If poll->head is already set, it's because the file being polled
5357 * uses multiple waitqueues for poll handling (eg one for read, one
5358 * for write). Setup a separate io_poll_iocb if this happens.
5359 */
5360 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005361 struct io_poll_iocb *poll_one = poll;
5362
Jens Axboe18bceab2020-05-15 11:56:54 -06005363 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005364 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005365 pt->error = -EINVAL;
5366 return;
5367 }
5368 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5369 if (!poll) {
5370 pt->error = -ENOMEM;
5371 return;
5372 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005373 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005374 refcount_inc(&req->refs);
5375 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005376 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005377 }
5378
5379 pt->error = 0;
5380 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005381
5382 if (poll->events & EPOLLEXCLUSIVE)
5383 add_wait_queue_exclusive(head, &poll->wait);
5384 else
5385 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005386}
5387
5388static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5389 struct poll_table_struct *p)
5390{
5391 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005392 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005393
Jens Axboe807abcb2020-07-17 17:09:27 -06005394 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005395}
5396
Jens Axboed7718a92020-02-14 22:23:12 -07005397static void io_async_task_func(struct callback_head *cb)
5398{
5399 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5400 struct async_poll *apoll = req->apoll;
5401 struct io_ring_ctx *ctx = req->ctx;
5402
5403 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5404
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005405 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005406 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005407 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005408 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005409 }
5410
Jens Axboe31067252020-05-17 17:43:31 -06005411 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005412 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005413 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005414
Jens Axboed4e7cd32020-08-15 11:44:50 -07005415 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005416 spin_unlock_irq(&ctx->completion_lock);
5417
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005418 if (!READ_ONCE(apoll->poll.canceled))
5419 __io_req_task_submit(req);
5420 else
5421 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005422
Jens Axboe6d816e02020-08-11 08:04:14 -06005423 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005424 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005425 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005426}
5427
5428static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5429 void *key)
5430{
5431 struct io_kiocb *req = wait->private;
5432 struct io_poll_iocb *poll = &req->apoll->poll;
5433
5434 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5435 key_to_poll(key));
5436
5437 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5438}
5439
5440static void io_poll_req_insert(struct io_kiocb *req)
5441{
5442 struct io_ring_ctx *ctx = req->ctx;
5443 struct hlist_head *list;
5444
5445 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5446 hlist_add_head(&req->hash_node, list);
5447}
5448
5449static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5450 struct io_poll_iocb *poll,
5451 struct io_poll_table *ipt, __poll_t mask,
5452 wait_queue_func_t wake_func)
5453 __acquires(&ctx->completion_lock)
5454{
5455 struct io_ring_ctx *ctx = req->ctx;
5456 bool cancel = false;
5457
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005458 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005459 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005460 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005461 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005462
5463 ipt->pt._key = mask;
5464 ipt->req = req;
5465 ipt->error = -EINVAL;
5466
Jens Axboed7718a92020-02-14 22:23:12 -07005467 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5468
5469 spin_lock_irq(&ctx->completion_lock);
5470 if (likely(poll->head)) {
5471 spin_lock(&poll->head->lock);
5472 if (unlikely(list_empty(&poll->wait.entry))) {
5473 if (ipt->error)
5474 cancel = true;
5475 ipt->error = 0;
5476 mask = 0;
5477 }
5478 if (mask || ipt->error)
5479 list_del_init(&poll->wait.entry);
5480 else if (cancel)
5481 WRITE_ONCE(poll->canceled, true);
5482 else if (!poll->done) /* actually waiting for an event */
5483 io_poll_req_insert(req);
5484 spin_unlock(&poll->head->lock);
5485 }
5486
5487 return mask;
5488}
5489
5490static bool io_arm_poll_handler(struct io_kiocb *req)
5491{
5492 const struct io_op_def *def = &io_op_defs[req->opcode];
5493 struct io_ring_ctx *ctx = req->ctx;
5494 struct async_poll *apoll;
5495 struct io_poll_table ipt;
5496 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005497 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005498
5499 if (!req->file || !file_can_poll(req->file))
5500 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005501 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005502 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005503 if (def->pollin)
5504 rw = READ;
5505 else if (def->pollout)
5506 rw = WRITE;
5507 else
5508 return false;
5509 /* if we can't nonblock try, then no point in arming a poll handler */
5510 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005511 return false;
5512
5513 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5514 if (unlikely(!apoll))
5515 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005516 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005517
5518 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005519 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005520
Nathan Chancellor8755d972020-03-02 16:01:19 -07005521 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005522 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005523 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005524 if (def->pollout)
5525 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005526
5527 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5528 if ((req->opcode == IORING_OP_RECVMSG) &&
5529 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5530 mask &= ~POLLIN;
5531
Jens Axboed7718a92020-02-14 22:23:12 -07005532 mask |= POLLERR | POLLPRI;
5533
5534 ipt.pt._qproc = io_async_queue_proc;
5535
5536 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5537 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005538 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005539 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005540 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005541 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005542 kfree(apoll);
5543 return false;
5544 }
5545 spin_unlock_irq(&ctx->completion_lock);
5546 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5547 apoll->poll.events);
5548 return true;
5549}
5550
5551static bool __io_poll_remove_one(struct io_kiocb *req,
5552 struct io_poll_iocb *poll)
5553{
Jens Axboeb41e9852020-02-17 09:52:41 -07005554 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005555
5556 spin_lock(&poll->head->lock);
5557 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005558 if (!list_empty(&poll->wait.entry)) {
5559 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005560 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005561 }
5562 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005563 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005564 return do_complete;
5565}
5566
5567static bool io_poll_remove_one(struct io_kiocb *req)
5568{
5569 bool do_complete;
5570
Jens Axboed4e7cd32020-08-15 11:44:50 -07005571 io_poll_remove_double(req);
5572
Jens Axboed7718a92020-02-14 22:23:12 -07005573 if (req->opcode == IORING_OP_POLL_ADD) {
5574 do_complete = __io_poll_remove_one(req, &req->poll);
5575 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005576 struct async_poll *apoll = req->apoll;
5577
Jens Axboed7718a92020-02-14 22:23:12 -07005578 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005579 do_complete = __io_poll_remove_one(req, &apoll->poll);
5580 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005581 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005582 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005583 kfree(apoll);
5584 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005585 }
5586
Jens Axboeb41e9852020-02-17 09:52:41 -07005587 if (do_complete) {
5588 io_cqring_fill_event(req, -ECANCELED);
5589 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005590 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005591 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005592 }
5593
5594 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005595}
5596
Jens Axboe76e1b642020-09-26 15:05:03 -06005597/*
5598 * Returns true if we found and killed one or more poll requests
5599 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005600static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5601 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005602{
Jens Axboe78076bb2019-12-04 19:56:40 -07005603 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005604 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005605 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005606
5607 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005608 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5609 struct hlist_head *list;
5610
5611 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005612 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005613 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005614 posted += io_poll_remove_one(req);
5615 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005616 }
5617 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005618
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005619 if (posted)
5620 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005621
5622 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005623}
5624
Jens Axboe47f46762019-11-09 17:43:02 -07005625static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5626{
Jens Axboe78076bb2019-12-04 19:56:40 -07005627 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005628 struct io_kiocb *req;
5629
Jens Axboe78076bb2019-12-04 19:56:40 -07005630 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5631 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005632 if (sqe_addr != req->user_data)
5633 continue;
5634 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005635 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005636 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005637 }
5638
5639 return -ENOENT;
5640}
5641
Jens Axboe3529d8c2019-12-19 18:24:38 -07005642static int io_poll_remove_prep(struct io_kiocb *req,
5643 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005644{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005645 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5646 return -EINVAL;
5647 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5648 sqe->poll_events)
5649 return -EINVAL;
5650
Pavel Begunkov018043b2020-10-27 23:17:18 +00005651 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005652 return 0;
5653}
5654
5655/*
5656 * Find a running poll command that matches one specified in sqe->addr,
5657 * and remove it if found.
5658 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005659static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005660{
5661 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005662 int ret;
5663
Jens Axboe221c5eb2019-01-17 09:41:58 -07005664 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005665 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005666 spin_unlock_irq(&ctx->completion_lock);
5667
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005668 if (ret < 0)
5669 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005670 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005671 return 0;
5672}
5673
Jens Axboe221c5eb2019-01-17 09:41:58 -07005674static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5675 void *key)
5676{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005677 struct io_kiocb *req = wait->private;
5678 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005679
Jens Axboed7718a92020-02-14 22:23:12 -07005680 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005681}
5682
Jens Axboe221c5eb2019-01-17 09:41:58 -07005683static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5684 struct poll_table_struct *p)
5685{
5686 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5687
Jens Axboee8c2bc12020-08-15 18:44:09 -07005688 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005689}
5690
Jens Axboe3529d8c2019-12-19 18:24:38 -07005691static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005692{
5693 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005694 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005695
5696 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5697 return -EINVAL;
5698 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5699 return -EINVAL;
5700
Jiufei Xue5769a352020-06-17 17:53:55 +08005701 events = READ_ONCE(sqe->poll32_events);
5702#ifdef __BIG_ENDIAN
5703 events = swahw32(events);
5704#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005705 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5706 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005707 return 0;
5708}
5709
Pavel Begunkov61e98202021-02-10 00:03:08 +00005710static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005711{
5712 struct io_poll_iocb *poll = &req->poll;
5713 struct io_ring_ctx *ctx = req->ctx;
5714 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005715 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005716
Jens Axboed7718a92020-02-14 22:23:12 -07005717 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005718
Jens Axboed7718a92020-02-14 22:23:12 -07005719 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5720 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005721
Jens Axboe8c838782019-03-12 15:48:16 -06005722 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005723 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005724 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005725 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005726 spin_unlock_irq(&ctx->completion_lock);
5727
Jens Axboe8c838782019-03-12 15:48:16 -06005728 if (mask) {
5729 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005730 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005731 }
Jens Axboe8c838782019-03-12 15:48:16 -06005732 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005733}
5734
Jens Axboe5262f562019-09-17 12:26:57 -06005735static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5736{
Jens Axboead8a48a2019-11-15 08:49:11 -07005737 struct io_timeout_data *data = container_of(timer,
5738 struct io_timeout_data, timer);
5739 struct io_kiocb *req = data->req;
5740 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005741 unsigned long flags;
5742
Jens Axboe5262f562019-09-17 12:26:57 -06005743 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005744 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005745 atomic_set(&req->ctx->cq_timeouts,
5746 atomic_read(&req->ctx->cq_timeouts) + 1);
5747
Jens Axboe78e19bb2019-11-06 15:21:34 -07005748 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005749 io_commit_cqring(ctx);
5750 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5751
5752 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005753 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005754 io_put_req(req);
5755 return HRTIMER_NORESTART;
5756}
5757
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005758static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5759 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005760{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005761 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005762 struct io_kiocb *req;
5763 int ret = -ENOENT;
5764
5765 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5766 if (user_data == req->user_data) {
5767 ret = 0;
5768 break;
5769 }
5770 }
5771
5772 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005773 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005774
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005775 io = req->async_data;
5776 ret = hrtimer_try_to_cancel(&io->timer);
5777 if (ret == -1)
5778 return ERR_PTR(-EALREADY);
5779 list_del_init(&req->timeout.list);
5780 return req;
5781}
5782
5783static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5784{
5785 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5786
5787 if (IS_ERR(req))
5788 return PTR_ERR(req);
5789
5790 req_set_fail_links(req);
5791 io_cqring_fill_event(req, -ECANCELED);
5792 io_put_req_deferred(req, 1);
5793 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005794}
5795
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005796static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5797 struct timespec64 *ts, enum hrtimer_mode mode)
5798{
5799 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5800 struct io_timeout_data *data;
5801
5802 if (IS_ERR(req))
5803 return PTR_ERR(req);
5804
5805 req->timeout.off = 0; /* noseq */
5806 data = req->async_data;
5807 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5808 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5809 data->timer.function = io_timeout_fn;
5810 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5811 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005812}
5813
Jens Axboe3529d8c2019-12-19 18:24:38 -07005814static int io_timeout_remove_prep(struct io_kiocb *req,
5815 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005816{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005817 struct io_timeout_rem *tr = &req->timeout_rem;
5818
Jens Axboeb29472e2019-12-17 18:50:29 -07005819 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5820 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005821 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5822 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005823 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005824 return -EINVAL;
5825
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005826 tr->addr = READ_ONCE(sqe->addr);
5827 tr->flags = READ_ONCE(sqe->timeout_flags);
5828 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5829 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5830 return -EINVAL;
5831 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5832 return -EFAULT;
5833 } else if (tr->flags) {
5834 /* timeout removal doesn't support flags */
5835 return -EINVAL;
5836 }
5837
Jens Axboeb29472e2019-12-17 18:50:29 -07005838 return 0;
5839}
5840
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005841static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5842{
5843 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5844 : HRTIMER_MODE_REL;
5845}
5846
Jens Axboe11365042019-10-16 09:08:32 -06005847/*
5848 * Remove or update an existing timeout command
5849 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005850static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005851{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005852 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005853 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005854 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005855
Jens Axboe11365042019-10-16 09:08:32 -06005856 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005857 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005858 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005859 else
5860 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5861 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005862
Jens Axboe47f46762019-11-09 17:43:02 -07005863 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005864 io_commit_cqring(ctx);
5865 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005866 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005867 if (ret < 0)
5868 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005869 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005870 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005871}
5872
Jens Axboe3529d8c2019-12-19 18:24:38 -07005873static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005874 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005875{
Jens Axboead8a48a2019-11-15 08:49:11 -07005876 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005877 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005878 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005879
Jens Axboead8a48a2019-11-15 08:49:11 -07005880 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005881 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005882 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005883 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005884 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005885 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005886 flags = READ_ONCE(sqe->timeout_flags);
5887 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005888 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005889
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005890 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005891
Jens Axboee8c2bc12020-08-15 18:44:09 -07005892 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005893 return -ENOMEM;
5894
Jens Axboee8c2bc12020-08-15 18:44:09 -07005895 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005896 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005897
5898 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005899 return -EFAULT;
5900
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005901 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005902 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5903 return 0;
5904}
5905
Pavel Begunkov61e98202021-02-10 00:03:08 +00005906static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005907{
Jens Axboead8a48a2019-11-15 08:49:11 -07005908 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005909 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005910 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005911 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005912
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005913 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005914
Jens Axboe5262f562019-09-17 12:26:57 -06005915 /*
5916 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005917 * timeout event to be satisfied. If it isn't set, then this is
5918 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005919 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005920 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005921 entry = ctx->timeout_list.prev;
5922 goto add;
5923 }
Jens Axboe5262f562019-09-17 12:26:57 -06005924
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005925 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5926 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005927
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005928 /* Update the last seq here in case io_flush_timeouts() hasn't.
5929 * This is safe because ->completion_lock is held, and submissions
5930 * and completions are never mixed in the same ->completion_lock section.
5931 */
5932 ctx->cq_last_tm_flush = tail;
5933
Jens Axboe5262f562019-09-17 12:26:57 -06005934 /*
5935 * Insertion sort, ensuring the first entry in the list is always
5936 * the one we need first.
5937 */
Jens Axboe5262f562019-09-17 12:26:57 -06005938 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005939 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5940 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005941
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005942 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005943 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005944 /* nxt.seq is behind @tail, otherwise would've been completed */
5945 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005946 break;
5947 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005948add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005949 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005950 data->timer.function = io_timeout_fn;
5951 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005952 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005953 return 0;
5954}
5955
Jens Axboe62755e32019-10-28 21:49:21 -06005956static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005957{
Jens Axboe62755e32019-10-28 21:49:21 -06005958 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005959
Jens Axboe62755e32019-10-28 21:49:21 -06005960 return req->user_data == (unsigned long) data;
5961}
5962
Jens Axboee977d6d2019-11-05 12:39:45 -07005963static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005964{
Jens Axboe62755e32019-10-28 21:49:21 -06005965 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005966 int ret = 0;
5967
Pavel Begunkov4f26bda2020-06-15 10:24:03 +03005968 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005969 switch (cancel_ret) {
5970 case IO_WQ_CANCEL_OK:
5971 ret = 0;
5972 break;
5973 case IO_WQ_CANCEL_RUNNING:
5974 ret = -EALREADY;
5975 break;
5976 case IO_WQ_CANCEL_NOTFOUND:
5977 ret = -ENOENT;
5978 break;
5979 }
5980
Jens Axboee977d6d2019-11-05 12:39:45 -07005981 return ret;
5982}
5983
Jens Axboe47f46762019-11-09 17:43:02 -07005984static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5985 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005986 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005987{
5988 unsigned long flags;
5989 int ret;
5990
5991 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
5992 if (ret != -ENOENT) {
5993 spin_lock_irqsave(&ctx->completion_lock, flags);
5994 goto done;
5995 }
5996
5997 spin_lock_irqsave(&ctx->completion_lock, flags);
5998 ret = io_timeout_cancel(ctx, sqe_addr);
5999 if (ret != -ENOENT)
6000 goto done;
6001 ret = io_poll_cancel(ctx, sqe_addr);
6002done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07006003 if (!ret)
6004 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006005 io_cqring_fill_event(req, ret);
6006 io_commit_cqring(ctx);
6007 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6008 io_cqring_ev_posted(ctx);
6009
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006010 if (ret < 0)
6011 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03006012 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07006013}
6014
Jens Axboe3529d8c2019-12-19 18:24:38 -07006015static int io_async_cancel_prep(struct io_kiocb *req,
6016 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006017{
Jens Axboefbf23842019-12-17 18:45:56 -07006018 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006019 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006020 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6021 return -EINVAL;
6022 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07006023 return -EINVAL;
6024
Jens Axboefbf23842019-12-17 18:45:56 -07006025 req->cancel.addr = READ_ONCE(sqe->addr);
6026 return 0;
6027}
6028
Pavel Begunkov61e98202021-02-10 00:03:08 +00006029static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006030{
6031 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07006032
Pavel Begunkov014db002020-03-03 21:33:12 +03006033 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006034 return 0;
6035}
6036
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006037static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006038 const struct io_uring_sqe *sqe)
6039{
Jens Axboe6ca56f82020-09-18 16:51:19 -06006040 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
6041 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006042 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6043 return -EINVAL;
6044 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006045 return -EINVAL;
6046
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006047 req->rsrc_update.offset = READ_ONCE(sqe->off);
6048 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6049 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006050 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006051 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006052 return 0;
6053}
6054
Pavel Begunkov889fca72021-02-10 00:03:09 +00006055static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006056{
6057 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006058 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006059 int ret;
6060
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006061 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006062 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006063
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006064 up.offset = req->rsrc_update.offset;
6065 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006066
6067 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006068 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006069 mutex_unlock(&ctx->uring_lock);
6070
6071 if (ret < 0)
6072 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006073 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006074 return 0;
6075}
6076
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006077static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006078{
Jens Axboed625c6e2019-12-17 19:53:05 -07006079 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006080 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006081 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006082 case IORING_OP_READV:
6083 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006084 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006085 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006086 case IORING_OP_WRITEV:
6087 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006088 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006089 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006090 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006091 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006092 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006093 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006094 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006095 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006096 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006097 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006098 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006099 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006100 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006101 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006102 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006103 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006104 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006105 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006106 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006107 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006108 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006109 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006110 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006111 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006112 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006113 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006114 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006115 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006116 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006117 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006118 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006119 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006120 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006121 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006122 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006123 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006124 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006125 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006126 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006127 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006128 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006129 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006130 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006131 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006132 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006133 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006134 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006135 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006136 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006137 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006138 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006139 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006140 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006141 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006142 case IORING_OP_SHUTDOWN:
6143 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006144 case IORING_OP_RENAMEAT:
6145 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006146 case IORING_OP_UNLINKAT:
6147 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006148 }
6149
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006150 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6151 req->opcode);
6152 return-EINVAL;
6153}
6154
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006155static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006156{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006157 switch (req->opcode) {
6158 case IORING_OP_READV:
6159 case IORING_OP_READ_FIXED:
6160 case IORING_OP_READ:
6161 return io_rw_prep_async(req, READ);
6162 case IORING_OP_WRITEV:
6163 case IORING_OP_WRITE_FIXED:
6164 case IORING_OP_WRITE:
6165 return io_rw_prep_async(req, WRITE);
6166 case IORING_OP_SENDMSG:
6167 case IORING_OP_SEND:
6168 return io_sendmsg_prep_async(req);
6169 case IORING_OP_RECVMSG:
6170 case IORING_OP_RECV:
6171 return io_recvmsg_prep_async(req);
6172 case IORING_OP_CONNECT:
6173 return io_connect_prep_async(req);
6174 }
6175 return 0;
6176}
6177
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006178static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006179{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006180 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006181 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006182 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006183 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006184 return 0;
6185 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006186 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006187 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006188}
6189
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006190static u32 io_get_sequence(struct io_kiocb *req)
6191{
6192 struct io_kiocb *pos;
6193 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006194 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006195
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006196 io_for_each_link(pos, req)
6197 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006198
6199 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6200 return total_submitted - nr_reqs;
6201}
6202
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006203static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204{
6205 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006206 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006207 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006208 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209
6210 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006211 if (likely(list_empty_careful(&ctx->defer_list) &&
6212 !(req->flags & REQ_F_IO_DRAIN)))
6213 return 0;
6214
6215 seq = io_get_sequence(req);
6216 /* Still a chance to pass the sequence check */
6217 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006218 return 0;
6219
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006220 ret = io_req_defer_prep(req);
6221 if (ret)
6222 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006223 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006224 de = kmalloc(sizeof(*de), GFP_KERNEL);
6225 if (!de)
6226 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006227
6228 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006229 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006230 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006231 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006232 io_queue_async_work(req);
6233 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006234 }
6235
6236 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006237 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006238 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006239 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006240 spin_unlock_irq(&ctx->completion_lock);
6241 return -EIOCBQUEUED;
6242}
Jens Axboeedafcce2019-01-09 09:16:05 -07006243
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006244static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006245{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006246 if (req->flags & REQ_F_BUFFER_SELECTED) {
6247 switch (req->opcode) {
6248 case IORING_OP_READV:
6249 case IORING_OP_READ_FIXED:
6250 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006251 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006252 break;
6253 case IORING_OP_RECVMSG:
6254 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006255 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006256 break;
6257 }
6258 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006259 }
6260
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006261 if (req->flags & REQ_F_NEED_CLEANUP) {
6262 switch (req->opcode) {
6263 case IORING_OP_READV:
6264 case IORING_OP_READ_FIXED:
6265 case IORING_OP_READ:
6266 case IORING_OP_WRITEV:
6267 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006268 case IORING_OP_WRITE: {
6269 struct io_async_rw *io = req->async_data;
6270 if (io->free_iovec)
6271 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006272 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006273 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006274 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006275 case IORING_OP_SENDMSG: {
6276 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006277
6278 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006279 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006280 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006281 case IORING_OP_SPLICE:
6282 case IORING_OP_TEE:
6283 io_put_file(req, req->splice.file_in,
6284 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6285 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006286 case IORING_OP_OPENAT:
6287 case IORING_OP_OPENAT2:
6288 if (req->open.filename)
6289 putname(req->open.filename);
6290 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006291 case IORING_OP_RENAMEAT:
6292 putname(req->rename.oldpath);
6293 putname(req->rename.newpath);
6294 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006295 case IORING_OP_UNLINKAT:
6296 putname(req->unlink.filename);
6297 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006298 }
6299 req->flags &= ~REQ_F_NEED_CLEANUP;
6300 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006301}
6302
Pavel Begunkov889fca72021-02-10 00:03:09 +00006303static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006304{
Jens Axboeedafcce2019-01-09 09:16:05 -07006305 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006306 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006307
Jens Axboed625c6e2019-12-17 19:53:05 -07006308 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006309 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006310 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006311 break;
6312 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006313 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006314 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006315 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316 break;
6317 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006318 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006319 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006320 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321 break;
6322 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006323 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006324 break;
6325 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006326 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006327 break;
6328 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006329 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006330 break;
6331 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006332 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006333 break;
6334 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006335 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006336 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006337 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006338 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006339 break;
6340 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006341 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006342 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006343 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006344 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006345 break;
6346 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006347 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006348 break;
6349 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006350 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006351 break;
6352 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006353 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006354 break;
6355 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006356 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006357 break;
6358 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006359 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006360 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006361 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006362 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006363 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006364 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006365 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006366 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006367 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006368 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006369 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006370 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006371 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006372 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006373 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006374 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006375 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006376 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006377 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006378 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006379 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006380 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006381 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006382 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006383 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006384 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006385 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006386 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006387 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006388 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006389 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006390 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006391 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006392 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006393 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006394 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006395 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006397 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006398 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006399 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006400 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006401 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006402 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006403 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006404 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006405 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006406 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006407 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006408 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006409 default:
6410 ret = -EINVAL;
6411 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006412 }
6413
6414 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006415 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006416
Jens Axboeb5325762020-05-19 21:20:27 -06006417 /* If the op doesn't have a file, we're not polling for it */
6418 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006419 const bool in_async = io_wq_current_is_worker();
6420
Jens Axboe11ba8202020-01-15 21:51:17 -07006421 /* workqueue context doesn't hold uring_lock, grab it now */
6422 if (in_async)
6423 mutex_lock(&ctx->uring_lock);
6424
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006425 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006426
6427 if (in_async)
6428 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006429 }
6430
6431 return 0;
6432}
6433
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006434static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006435{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006436 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006437 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006438 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006439
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006440 timeout = io_prep_linked_timeout(req);
6441 if (timeout)
6442 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006443
Jens Axboe4014d942021-01-19 15:53:54 -07006444 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006445 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006446
Jens Axboe561fb042019-10-24 07:25:42 -06006447 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006448 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006449 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006450 /*
6451 * We can get EAGAIN for polled IO even though we're
6452 * forcing a sync submission from here, since we can't
6453 * wait for request slots on the block side.
6454 */
6455 if (ret != -EAGAIN)
6456 break;
6457 cond_resched();
6458 } while (1);
6459 }
Jens Axboe31b51512019-01-18 22:56:34 -07006460
Pavel Begunkova3df76982021-02-18 22:32:52 +00006461 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006462 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006463 /* io-wq is going to take one down */
6464 refcount_inc(&req->refs);
6465 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006466 }
Jens Axboe31b51512019-01-18 22:56:34 -07006467}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006468
Jens Axboe65e19f52019-10-26 07:20:21 -06006469static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6470 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006471{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006472 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006473
Jens Axboe05f3fb32019-12-09 11:22:50 -07006474 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006475 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006476}
6477
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006478static struct file *io_file_get(struct io_submit_state *state,
6479 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006480{
6481 struct io_ring_ctx *ctx = req->ctx;
6482 struct file *file;
6483
6484 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006485 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006486 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006487 fd = array_index_nospec(fd, ctx->nr_user_files);
6488 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006489 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006490 } else {
6491 trace_io_uring_file_get(ctx, fd);
6492 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006493 }
6494
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006495 if (file && unlikely(file->f_op == &io_uring_fops))
6496 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006497 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006498}
6499
Jens Axboe2665abf2019-11-05 12:40:47 -07006500static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6501{
Jens Axboead8a48a2019-11-15 08:49:11 -07006502 struct io_timeout_data *data = container_of(timer,
6503 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006504 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006505 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006506 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006507
6508 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006509 prev = req->timeout.head;
6510 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006511
6512 /*
6513 * We don't expect the list to be empty, that will only happen if we
6514 * race with the completion of the linked work.
6515 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006516 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006517 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006518 else
6519 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006520 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6521
6522 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006523 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006524 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006525 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006526 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006527 io_req_complete_post(req, -ETIME, 0);
6528 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006529 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006530 return HRTIMER_NORESTART;
6531}
6532
Jens Axboe7271ef32020-08-10 09:55:22 -06006533static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006534{
Jens Axboe76a46e02019-11-10 23:34:16 -07006535 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006536 * If the back reference is NULL, then our linked request finished
6537 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006538 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006539 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006540 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006541
Jens Axboead8a48a2019-11-15 08:49:11 -07006542 data->timer.function = io_link_timeout_fn;
6543 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6544 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006545 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006546}
6547
6548static void io_queue_linked_timeout(struct io_kiocb *req)
6549{
6550 struct io_ring_ctx *ctx = req->ctx;
6551
6552 spin_lock_irq(&ctx->completion_lock);
6553 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006554 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006555
Jens Axboe2665abf2019-11-05 12:40:47 -07006556 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006557 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006558}
6559
Jens Axboead8a48a2019-11-15 08:49:11 -07006560static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006561{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006562 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006563
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006564 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6565 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006566 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006567
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006568 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006569 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006570 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006571 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006572}
6573
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006574static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006575{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006576 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe193155c2020-02-22 23:22:19 -07006577 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006578 int ret;
6579
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006580 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6581 (req->work.flags & IO_WQ_WORK_CREDS) &&
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006582 req->work.identity->creds != current_cred())
6583 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006584
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006585 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006586
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006587 if (old_creds)
6588 revert_creds(old_creds);
Jens Axboe491381ce2019-10-17 09:20:46 -06006589
6590 /*
6591 * We async punt it if the file wasn't marked NOWAIT, or if the file
6592 * doesn't support non-blocking read/write attempts
6593 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006594 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006595 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006596 /*
6597 * Queued up for async execution, worker will release
6598 * submit reference when the iocb is actually submitted.
6599 */
6600 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006601 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006602 } else if (likely(!ret)) {
6603 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006604 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006605 struct io_ring_ctx *ctx = req->ctx;
6606 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006607
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006608 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006609 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006610 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006611 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006612 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006613 }
6614 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006615 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006616 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006617 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006618 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006619 if (linked_timeout)
6620 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006621}
6622
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006623static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006624{
6625 int ret;
6626
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006627 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006628 if (ret) {
6629 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006630fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006631 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006632 io_put_req(req);
6633 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006634 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006635 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006636 ret = io_req_defer_prep(req);
6637 if (unlikely(ret))
6638 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006639 io_queue_async_work(req);
6640 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006641 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006642 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006643}
6644
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006645/*
6646 * Check SQE restrictions (opcode and flags).
6647 *
6648 * Returns 'true' if SQE is allowed, 'false' otherwise.
6649 */
6650static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6651 struct io_kiocb *req,
6652 unsigned int sqe_flags)
6653{
6654 if (!ctx->restricted)
6655 return true;
6656
6657 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6658 return false;
6659
6660 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6661 ctx->restrictions.sqe_flags_required)
6662 return false;
6663
6664 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6665 ctx->restrictions.sqe_flags_required))
6666 return false;
6667
6668 return true;
6669}
6670
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006671static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006672 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006673{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006674 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006675 unsigned int sqe_flags;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006676 int id, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006677
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006678 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006679 /* same numerical values with corresponding REQ_F_*, safe to copy */
6680 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006681 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006682 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006683 req->file = NULL;
6684 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006685 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006686 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006687 /* one is dropped after submission, the other at completion */
6688 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006689 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006690 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006691
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006692 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006693 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6694 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006695 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006696 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006697
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006698 if (unlikely(req->opcode >= IORING_OP_LAST))
6699 return -EINVAL;
6700
Jens Axboe28cea78a2020-09-14 10:51:17 -06006701 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006702 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006703
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006704 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6705 return -EACCES;
6706
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006707 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6708 !io_op_defs[req->opcode].buffer_select)
6709 return -EOPNOTSUPP;
6710
6711 id = READ_ONCE(sqe->personality);
6712 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006713 struct io_identity *iod;
6714
Jens Axboe1e6fa522020-10-15 08:46:24 -06006715 iod = idr_find(&ctx->personality_idr, id);
6716 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006717 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006718 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006719
6720 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006721 get_cred(iod->creds);
6722 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006723 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006724 }
6725
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006726 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006727
Jens Axboe27926b62020-10-28 09:33:23 -06006728 /*
6729 * Plug now if we have more than 1 IO left after this, and the target
6730 * is potentially a read/write to block based storage.
6731 */
6732 if (!state->plug_started && state->ios_left > 1 &&
6733 io_op_defs[req->opcode].plug) {
6734 blk_start_plug(&state->plug);
6735 state->plug_started = true;
6736 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006737
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006738 if (io_op_defs[req->opcode].needs_file) {
6739 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006740
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006741 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006742 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006743 ret = -EBADF;
6744 }
6745
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006746 state->ios_left--;
6747 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006748}
6749
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006750static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006751 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006752{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006753 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006754 int ret;
6755
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006756 ret = io_init_req(ctx, req, sqe);
6757 if (unlikely(ret)) {
6758fail_req:
6759 io_put_req(req);
6760 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006761 if (link->head) {
6762 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006763 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006764 io_put_req(link->head);
6765 io_req_complete(link->head, -ECANCELED);
6766 link->head = NULL;
6767 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006768 return ret;
6769 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006770 ret = io_req_prep(req, sqe);
6771 if (unlikely(ret))
6772 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006773
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006774 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006775 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6776 true, ctx->flags & IORING_SETUP_SQPOLL);
6777
Jens Axboe6c271ce2019-01-10 11:22:30 -07006778 /*
6779 * If we already have a head request, queue this one for async
6780 * submittal once the head completes. If we don't have a head but
6781 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6782 * submitted sync once the chain is complete. If none of those
6783 * conditions are true (normal request), then just queue it.
6784 */
6785 if (link->head) {
6786 struct io_kiocb *head = link->head;
6787
6788 /*
6789 * Taking sequential execution of a link, draining both sides
6790 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6791 * requests in the link. So, it drains the head and the
6792 * next after the link request. The last one is done via
6793 * drain_next flag to persist the effect across calls.
6794 */
6795 if (req->flags & REQ_F_IO_DRAIN) {
6796 head->flags |= REQ_F_IO_DRAIN;
6797 ctx->drain_next = 1;
6798 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006799 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006800 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006801 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006802 trace_io_uring_link(ctx, req, head);
6803 link->last->link = req;
6804 link->last = req;
6805
6806 /* last request of a link, enqueue the link */
6807 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006808 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006809 link->head = NULL;
6810 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006811 } else {
6812 if (unlikely(ctx->drain_next)) {
6813 req->flags |= REQ_F_IO_DRAIN;
6814 ctx->drain_next = 0;
6815 }
6816 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006817 link->head = req;
6818 link->last = req;
6819 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006820 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006821 }
6822 }
6823
6824 return 0;
6825}
6826
6827/*
6828 * Batched submission is done, ensure local IO is flushed out.
6829 */
6830static void io_submit_state_end(struct io_submit_state *state,
6831 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006832{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006833 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006834 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006835 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006836 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006837 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006838 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006839 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006840}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006841
Jens Axboe9e645e112019-05-10 16:07:28 -06006842/*
6843 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006844 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006845static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006846 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006847{
6848 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006849 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006850 /* set only head, no need to init link_last in advance */
6851 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006852}
6853
Jens Axboe193155c2020-02-22 23:22:19 -07006854static void io_commit_sqring(struct io_ring_ctx *ctx)
6855{
Jens Axboe75c6a032020-01-28 10:15:23 -07006856 struct io_rings *rings = ctx->rings;
6857
6858 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006859 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006860 * since once we write the new head, the application could
6861 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006862 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006863 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006864}
6865
Jens Axboe9e645e112019-05-10 16:07:28 -06006866/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006867 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006868 * that is mapped by userspace. This means that care needs to be taken to
6869 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006870 * being a good citizen. If members of the sqe are validated and then later
6871 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006872 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006873 */
6874static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006875{
6876 u32 *sq_array = ctx->sq_array;
6877 unsigned head;
6878
6879 /*
6880 * The cached sq head (or cq tail) serves two purposes:
6881 *
6882 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006883 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006884 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006885 * though the application is the one updating it.
6886 */
6887 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6888 if (likely(head < ctx->sq_entries))
6889 return &ctx->sq_sqes[head];
6890
6891 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006892 ctx->cached_sq_dropped++;
6893 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6894 return NULL;
6895}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006896
Jens Axboe0f212202020-09-13 13:09:39 -06006897static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006898{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006899 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006900
Jens Axboec4a2ed72019-11-21 21:01:26 -07006901 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006902 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006903 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006904 return -EBUSY;
6905 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006906
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006907 /* make sure SQ entry isn't read before tail */
6908 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006909
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006910 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6911 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006912
Jens Axboed8a6df12020-10-15 16:24:45 -06006913 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006914 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006915 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006916
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006917 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006918 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006919 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006920
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006921 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006922 if (unlikely(!req)) {
6923 if (!submitted)
6924 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006925 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006926 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006927 sqe = io_get_sqe(ctx);
6928 if (unlikely(!sqe)) {
6929 kmem_cache_free(req_cachep, req);
6930 break;
6931 }
Jens Axboed3656342019-12-18 09:50:26 -07006932 /* will complete beyond this point, count as submitted */
6933 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006934 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006935 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006936 }
6937
Pavel Begunkov9466f432020-01-25 22:34:01 +03006938 if (unlikely(submitted != nr)) {
6939 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006940 struct io_uring_task *tctx = current->io_uring;
6941 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006942
Jens Axboed8a6df12020-10-15 16:24:45 -06006943 percpu_ref_put_many(&ctx->refs, unused);
6944 percpu_counter_sub(&tctx->inflight, unused);
6945 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006946 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006947
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006948 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006949 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6950 io_commit_sqring(ctx);
6951
Jens Axboe6c271ce2019-01-10 11:22:30 -07006952 return submitted;
6953}
6954
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006955static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6956{
6957 /* Tell userspace we may need a wakeup call */
6958 spin_lock_irq(&ctx->completion_lock);
6959 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6960 spin_unlock_irq(&ctx->completion_lock);
6961}
6962
6963static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6964{
6965 spin_lock_irq(&ctx->completion_lock);
6966 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6967 spin_unlock_irq(&ctx->completion_lock);
6968}
6969
Xiaoguang Wang08369242020-11-03 14:15:59 +08006970static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006971{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006972 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006973 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006974
Jens Axboec8d1ba52020-09-14 11:07:26 -06006975 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006976 /* if we're handling multiple rings, cap submit size for fairness */
6977 if (cap_entries && to_submit > 8)
6978 to_submit = 8;
6979
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006980 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6981 unsigned nr_events = 0;
6982
Xiaoguang Wang08369242020-11-03 14:15:59 +08006983 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006984 if (!list_empty(&ctx->iopoll_list))
6985 io_do_iopoll(ctx, &nr_events, 0);
6986
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006987 if (to_submit && !ctx->sqo_dead &&
6988 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006989 ret = io_submit_sqes(ctx, to_submit);
6990 mutex_unlock(&ctx->uring_lock);
6991 }
Jens Axboe90554202020-09-03 12:12:41 -06006992
6993 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6994 wake_up(&ctx->sqo_sq_wait);
6995
Xiaoguang Wang08369242020-11-03 14:15:59 +08006996 return ret;
6997}
6998
6999static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7000{
7001 struct io_ring_ctx *ctx;
7002 unsigned sq_thread_idle = 0;
7003
7004 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7005 if (sq_thread_idle < ctx->sq_thread_idle)
7006 sq_thread_idle = ctx->sq_thread_idle;
7007 }
7008
7009 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007010}
7011
Jens Axboe69fb2132020-09-14 11:16:23 -06007012static void io_sqd_init_new(struct io_sq_data *sqd)
7013{
7014 struct io_ring_ctx *ctx;
7015
7016 while (!list_empty(&sqd->ctx_new_list)) {
7017 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007018 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7019 complete(&ctx->sq_thread_comp);
7020 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007021
7022 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007023}
7024
Jens Axboe6c271ce2019-01-10 11:22:30 -07007025static int io_sq_thread(void *data)
7026{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007027 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007028 struct files_struct *old_files = current->files;
7029 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007030 const struct cred *old_cred = NULL;
7031 struct io_sq_data *sqd = data;
7032 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007033 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007034 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007035
Jens Axboe28cea78a2020-09-14 10:51:17 -06007036 task_lock(current);
7037 current->files = NULL;
7038 current->nsproxy = NULL;
7039 task_unlock(current);
7040
Jens Axboe69fb2132020-09-14 11:16:23 -06007041 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007042 int ret;
7043 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007044
7045 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007046 * Any changes to the sqd lists are synchronized through the
7047 * kthread parking. This synchronizes the thread vs users,
7048 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007049 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007050 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007051 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007052 /*
7053 * When sq thread is unparked, in case the previous park operation
7054 * comes from io_put_sq_data(), which means that sq thread is going
7055 * to be stopped, so here needs to have a check.
7056 */
7057 if (kthread_should_stop())
7058 break;
7059 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007060
Xiaoguang Wang08369242020-11-03 14:15:59 +08007061 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007062 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007063 timeout = jiffies + sqd->sq_thread_idle;
7064 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007065
Xiaoguang Wang08369242020-11-03 14:15:59 +08007066 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007067 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007068 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7069 if (current->cred != ctx->creds) {
7070 if (old_cred)
7071 revert_creds(old_cred);
7072 old_cred = override_creds(ctx->creds);
7073 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007074 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007075#ifdef CONFIG_AUDIT
7076 current->loginuid = ctx->loginuid;
7077 current->sessionid = ctx->sessionid;
7078#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007079
Xiaoguang Wang08369242020-11-03 14:15:59 +08007080 ret = __io_sq_thread(ctx, cap_entries);
7081 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7082 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007083
Jens Axboe28cea78a2020-09-14 10:51:17 -06007084 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007085 }
7086
Xiaoguang Wang08369242020-11-03 14:15:59 +08007087 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007088 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007089 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007090 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007091 if (sqt_spin)
7092 timeout = jiffies + sqd->sq_thread_idle;
7093 continue;
7094 }
7095
Xiaoguang Wang08369242020-11-03 14:15:59 +08007096 needs_sched = true;
7097 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7098 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7099 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7100 !list_empty_careful(&ctx->iopoll_list)) {
7101 needs_sched = false;
7102 break;
7103 }
7104 if (io_sqring_entries(ctx)) {
7105 needs_sched = false;
7106 break;
7107 }
7108 }
7109
Hao Xu8b28fdf2021-01-31 22:39:04 +08007110 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007111 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7112 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007113
Jens Axboe69fb2132020-09-14 11:16:23 -06007114 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007115 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7116 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007117 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007118
7119 finish_wait(&sqd->wait, &wait);
7120 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007121 }
7122
Jens Axboe4c6e2772020-07-01 11:29:10 -06007123 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007124 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007125
Dennis Zhou91d8f512020-09-16 13:41:05 -07007126 if (cur_css)
7127 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007128 if (old_cred)
7129 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007130
Jens Axboe28cea78a2020-09-14 10:51:17 -06007131 task_lock(current);
7132 current->files = old_files;
7133 current->nsproxy = old_nsproxy;
7134 task_unlock(current);
7135
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007136 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007137
Jens Axboe6c271ce2019-01-10 11:22:30 -07007138 return 0;
7139}
7140
Jens Axboebda52162019-09-24 13:47:15 -06007141struct io_wait_queue {
7142 struct wait_queue_entry wq;
7143 struct io_ring_ctx *ctx;
7144 unsigned to_wait;
7145 unsigned nr_timeouts;
7146};
7147
Pavel Begunkov6c503152021-01-04 20:36:36 +00007148static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007149{
7150 struct io_ring_ctx *ctx = iowq->ctx;
7151
7152 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007153 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007154 * started waiting. For timeouts, we always want to return to userspace,
7155 * regardless of event count.
7156 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007157 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007158 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7159}
7160
7161static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7162 int wake_flags, void *key)
7163{
7164 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7165 wq);
7166
Pavel Begunkov6c503152021-01-04 20:36:36 +00007167 /*
7168 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7169 * the task, and the next invocation will do it.
7170 */
7171 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7172 return autoremove_wake_function(curr, mode, wake_flags, key);
7173 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007174}
7175
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007176static int io_run_task_work_sig(void)
7177{
7178 if (io_run_task_work())
7179 return 1;
7180 if (!signal_pending(current))
7181 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007182 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7183 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007184 return -EINTR;
7185}
7186
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007187/* when returns >0, the caller should retry */
7188static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7189 struct io_wait_queue *iowq,
7190 signed long *timeout)
7191{
7192 int ret;
7193
7194 /* make sure we run task_work before checking for signals */
7195 ret = io_run_task_work_sig();
7196 if (ret || io_should_wake(iowq))
7197 return ret;
7198 /* let the caller flush overflows, retry */
7199 if (test_bit(0, &ctx->cq_check_overflow))
7200 return 1;
7201
7202 *timeout = schedule_timeout(*timeout);
7203 return !*timeout ? -ETIME : 1;
7204}
7205
Jens Axboe2b188cc2019-01-07 10:46:33 -07007206/*
7207 * Wait until events become available, if we don't already have some. The
7208 * application must reap them itself, as they reside on the shared cq ring.
7209 */
7210static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007211 const sigset_t __user *sig, size_t sigsz,
7212 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007213{
Jens Axboebda52162019-09-24 13:47:15 -06007214 struct io_wait_queue iowq = {
7215 .wq = {
7216 .private = current,
7217 .func = io_wake_function,
7218 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7219 },
7220 .ctx = ctx,
7221 .to_wait = min_events,
7222 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007223 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007224 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7225 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007226
Jens Axboeb41e9852020-02-17 09:52:41 -07007227 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007228 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7229 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007230 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007231 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007232 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007233 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007234
7235 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007236#ifdef CONFIG_COMPAT
7237 if (in_compat_syscall())
7238 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007239 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007240 else
7241#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007242 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007243
Jens Axboe2b188cc2019-01-07 10:46:33 -07007244 if (ret)
7245 return ret;
7246 }
7247
Hao Xuc73ebb62020-11-03 10:54:37 +08007248 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007249 struct timespec64 ts;
7250
Hao Xuc73ebb62020-11-03 10:54:37 +08007251 if (get_timespec64(&ts, uts))
7252 return -EFAULT;
7253 timeout = timespec64_to_jiffies(&ts);
7254 }
7255
Jens Axboebda52162019-09-24 13:47:15 -06007256 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007257 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007258 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007259 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007260 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7261 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007262 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7263 finish_wait(&ctx->wait, &iowq.wq);
7264 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007265
Jens Axboeb7db41c2020-07-04 08:55:50 -06007266 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007267
Hristo Venev75b28af2019-08-26 17:23:46 +00007268 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007269}
7270
Jens Axboe6b063142019-01-10 22:13:58 -07007271static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7272{
7273#if defined(CONFIG_UNIX)
7274 if (ctx->ring_sock) {
7275 struct sock *sock = ctx->ring_sock->sk;
7276 struct sk_buff *skb;
7277
7278 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7279 kfree_skb(skb);
7280 }
7281#else
7282 int i;
7283
Jens Axboe65e19f52019-10-26 07:20:21 -06007284 for (i = 0; i < ctx->nr_user_files; i++) {
7285 struct file *file;
7286
7287 file = io_file_from_index(ctx, i);
7288 if (file)
7289 fput(file);
7290 }
Jens Axboe6b063142019-01-10 22:13:58 -07007291#endif
7292}
7293
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007294static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007295{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007296 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007297
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007298 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007299 complete(&data->done);
7300}
7301
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007302static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007303{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007304 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007305}
7306
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007307static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07007308{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007309 spin_unlock_bh(&ctx->rsrc_ref_lock);
7310}
7311
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007312static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7313 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007314 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007315{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007316 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007317 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007318 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007319 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007320 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07007321}
7322
Hao Xu8bad28d2021-02-19 17:19:36 +08007323static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07007324{
Hao Xu8bad28d2021-02-19 17:19:36 +08007325 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007326
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007327 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007328 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00007329 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007330 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007331 if (ref_node)
7332 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08007333}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007334
Hao Xu8bad28d2021-02-19 17:19:36 +08007335static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7336 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007337 void (*rsrc_put)(struct io_ring_ctx *ctx,
7338 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08007339{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007340 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08007341 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007342
Hao Xu8bad28d2021-02-19 17:19:36 +08007343 if (data->quiesce)
7344 return -ENXIO;
7345
7346 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007347 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007348 ret = -ENOMEM;
7349 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7350 if (!backup_node)
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007351 break;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007352 backup_node->rsrc_data = data;
7353 backup_node->rsrc_put = rsrc_put;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007354
Hao Xu8bad28d2021-02-19 17:19:36 +08007355 io_sqe_rsrc_kill_node(ctx, data);
7356 percpu_ref_kill(&data->refs);
7357 flush_delayed_work(&ctx->rsrc_put_work);
7358
Jens Axboe6b063142019-01-10 22:13:58 -07007359 ret = wait_for_completion_interruptible(&data->done);
Pavel Begunkov88f171a2021-02-20 18:03:50 +00007360 if (!ret || !io_refs_resurrect(&data->refs, &data->done))
Jens Axboe6b063142019-01-10 22:13:58 -07007361 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007362
Hao Xu8bad28d2021-02-19 17:19:36 +08007363 io_sqe_rsrc_set_node(ctx, data, backup_node);
7364 backup_node = NULL;
Hao Xu8bad28d2021-02-19 17:19:36 +08007365 mutex_unlock(&ctx->uring_lock);
7366 ret = io_run_task_work_sig();
7367 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007368 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007369 data->quiesce = false;
7370
7371 if (backup_node)
7372 destroy_fixed_rsrc_ref_node(backup_node);
7373 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007374}
7375
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007376static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7377{
7378 struct fixed_rsrc_data *data;
7379
7380 data = kzalloc(sizeof(*data), GFP_KERNEL);
7381 if (!data)
7382 return NULL;
7383
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007384 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007385 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7386 kfree(data);
7387 return NULL;
7388 }
7389 data->ctx = ctx;
7390 init_completion(&data->done);
7391 return data;
7392}
7393
7394static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7395{
7396 percpu_ref_exit(&data->refs);
7397 kfree(data->table);
7398 kfree(data);
7399}
7400
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007401static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7402{
7403 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007404 unsigned nr_tables, i;
7405 int ret;
7406
Hao Xu8bad28d2021-02-19 17:19:36 +08007407 /*
7408 * percpu_ref_is_dying() is to stop parallel files unregister
7409 * Since we possibly drop uring lock later in this function to
7410 * run task work.
7411 */
7412 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007413 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007414 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007415 if (ret)
7416 return ret;
7417
Jens Axboe6b063142019-01-10 22:13:58 -07007418 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007419 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7420 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007421 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007422 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007423 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007424 ctx->nr_user_files = 0;
7425 return 0;
7426}
7427
Jens Axboe534ca6d2020-09-02 13:52:19 -06007428static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007429{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007430 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007431 /*
7432 * The park is a bit of a work-around, without it we get
7433 * warning spews on shutdown with SQPOLL set and affinity
7434 * set to a single CPU.
7435 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007436 if (sqd->thread) {
7437 kthread_park(sqd->thread);
7438 kthread_stop(sqd->thread);
7439 }
7440
7441 kfree(sqd);
7442 }
7443}
7444
Jens Axboeaa061652020-09-02 14:50:27 -06007445static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7446{
7447 struct io_ring_ctx *ctx_attach;
7448 struct io_sq_data *sqd;
7449 struct fd f;
7450
7451 f = fdget(p->wq_fd);
7452 if (!f.file)
7453 return ERR_PTR(-ENXIO);
7454 if (f.file->f_op != &io_uring_fops) {
7455 fdput(f);
7456 return ERR_PTR(-EINVAL);
7457 }
7458
7459 ctx_attach = f.file->private_data;
7460 sqd = ctx_attach->sq_data;
7461 if (!sqd) {
7462 fdput(f);
7463 return ERR_PTR(-EINVAL);
7464 }
7465
7466 refcount_inc(&sqd->refs);
7467 fdput(f);
7468 return sqd;
7469}
7470
Jens Axboe534ca6d2020-09-02 13:52:19 -06007471static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7472{
7473 struct io_sq_data *sqd;
7474
Jens Axboeaa061652020-09-02 14:50:27 -06007475 if (p->flags & IORING_SETUP_ATTACH_WQ)
7476 return io_attach_sq_data(p);
7477
Jens Axboe534ca6d2020-09-02 13:52:19 -06007478 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7479 if (!sqd)
7480 return ERR_PTR(-ENOMEM);
7481
7482 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007483 INIT_LIST_HEAD(&sqd->ctx_list);
7484 INIT_LIST_HEAD(&sqd->ctx_new_list);
7485 mutex_init(&sqd->ctx_lock);
7486 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007487 init_waitqueue_head(&sqd->wait);
7488 return sqd;
7489}
7490
Jens Axboe69fb2132020-09-14 11:16:23 -06007491static void io_sq_thread_unpark(struct io_sq_data *sqd)
7492 __releases(&sqd->lock)
7493{
7494 if (!sqd->thread)
7495 return;
7496 kthread_unpark(sqd->thread);
7497 mutex_unlock(&sqd->lock);
7498}
7499
7500static void io_sq_thread_park(struct io_sq_data *sqd)
7501 __acquires(&sqd->lock)
7502{
7503 if (!sqd->thread)
7504 return;
7505 mutex_lock(&sqd->lock);
7506 kthread_park(sqd->thread);
7507}
7508
Jens Axboe534ca6d2020-09-02 13:52:19 -06007509static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7510{
7511 struct io_sq_data *sqd = ctx->sq_data;
7512
7513 if (sqd) {
7514 if (sqd->thread) {
7515 /*
7516 * We may arrive here from the error branch in
7517 * io_sq_offload_create() where the kthread is created
7518 * without being waked up, thus wake it up now to make
7519 * sure the wait will complete.
7520 */
7521 wake_up_process(sqd->thread);
7522 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007523
7524 io_sq_thread_park(sqd);
7525 }
7526
7527 mutex_lock(&sqd->ctx_lock);
7528 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007529 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007530 mutex_unlock(&sqd->ctx_lock);
7531
Xiaoguang Wang08369242020-11-03 14:15:59 +08007532 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007533 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007534
7535 io_put_sq_data(sqd);
7536 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007537 }
7538}
7539
Jens Axboe6b063142019-01-10 22:13:58 -07007540static void io_finish_async(struct io_ring_ctx *ctx)
7541{
Jens Axboe6c271ce2019-01-10 11:22:30 -07007542 io_sq_thread_stop(ctx);
7543
Jens Axboe561fb042019-10-24 07:25:42 -06007544 if (ctx->io_wq) {
7545 io_wq_destroy(ctx->io_wq);
7546 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007547 }
7548}
7549
7550#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007551/*
7552 * Ensure the UNIX gc is aware of our file set, so we are certain that
7553 * the io_uring can be safely unregistered on process exit, even if we have
7554 * loops in the file referencing.
7555 */
7556static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7557{
7558 struct sock *sk = ctx->ring_sock->sk;
7559 struct scm_fp_list *fpl;
7560 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007561 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007562
Jens Axboe6b063142019-01-10 22:13:58 -07007563 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7564 if (!fpl)
7565 return -ENOMEM;
7566
7567 skb = alloc_skb(0, GFP_KERNEL);
7568 if (!skb) {
7569 kfree(fpl);
7570 return -ENOMEM;
7571 }
7572
7573 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007574
Jens Axboe08a45172019-10-03 08:11:03 -06007575 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007576 fpl->user = get_uid(ctx->user);
7577 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007578 struct file *file = io_file_from_index(ctx, i + offset);
7579
7580 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007581 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007582 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007583 unix_inflight(fpl->user, fpl->fp[nr_files]);
7584 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007585 }
7586
Jens Axboe08a45172019-10-03 08:11:03 -06007587 if (nr_files) {
7588 fpl->max = SCM_MAX_FD;
7589 fpl->count = nr_files;
7590 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007591 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007592 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7593 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007594
Jens Axboe08a45172019-10-03 08:11:03 -06007595 for (i = 0; i < nr_files; i++)
7596 fput(fpl->fp[i]);
7597 } else {
7598 kfree_skb(skb);
7599 kfree(fpl);
7600 }
Jens Axboe6b063142019-01-10 22:13:58 -07007601
7602 return 0;
7603}
7604
7605/*
7606 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7607 * causes regular reference counting to break down. We rely on the UNIX
7608 * garbage collection to take care of this problem for us.
7609 */
7610static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7611{
7612 unsigned left, total;
7613 int ret = 0;
7614
7615 total = 0;
7616 left = ctx->nr_user_files;
7617 while (left) {
7618 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007619
7620 ret = __io_sqe_files_scm(ctx, this_files, total);
7621 if (ret)
7622 break;
7623 left -= this_files;
7624 total += this_files;
7625 }
7626
7627 if (!ret)
7628 return 0;
7629
7630 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007631 struct file *file = io_file_from_index(ctx, total);
7632
7633 if (file)
7634 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007635 total++;
7636 }
7637
7638 return ret;
7639}
7640#else
7641static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7642{
7643 return 0;
7644}
7645#endif
7646
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007647static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007648 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007649{
7650 int i;
7651
7652 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007653 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007654 unsigned this_files;
7655
7656 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7657 table->files = kcalloc(this_files, sizeof(struct file *),
7658 GFP_KERNEL);
7659 if (!table->files)
7660 break;
7661 nr_files -= this_files;
7662 }
7663
7664 if (i == nr_tables)
7665 return 0;
7666
7667 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007668 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007669 kfree(table->files);
7670 }
7671 return 1;
7672}
7673
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007674static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007675{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007676 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007677#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007678 struct sock *sock = ctx->ring_sock->sk;
7679 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7680 struct sk_buff *skb;
7681 int i;
7682
7683 __skb_queue_head_init(&list);
7684
7685 /*
7686 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7687 * remove this entry and rearrange the file array.
7688 */
7689 skb = skb_dequeue(head);
7690 while (skb) {
7691 struct scm_fp_list *fp;
7692
7693 fp = UNIXCB(skb).fp;
7694 for (i = 0; i < fp->count; i++) {
7695 int left;
7696
7697 if (fp->fp[i] != file)
7698 continue;
7699
7700 unix_notinflight(fp->user, fp->fp[i]);
7701 left = fp->count - 1 - i;
7702 if (left) {
7703 memmove(&fp->fp[i], &fp->fp[i + 1],
7704 left * sizeof(struct file *));
7705 }
7706 fp->count--;
7707 if (!fp->count) {
7708 kfree_skb(skb);
7709 skb = NULL;
7710 } else {
7711 __skb_queue_tail(&list, skb);
7712 }
7713 fput(file);
7714 file = NULL;
7715 break;
7716 }
7717
7718 if (!file)
7719 break;
7720
7721 __skb_queue_tail(&list, skb);
7722
7723 skb = skb_dequeue(head);
7724 }
7725
7726 if (skb_peek(&list)) {
7727 spin_lock_irq(&head->lock);
7728 while ((skb = __skb_dequeue(&list)) != NULL)
7729 __skb_queue_tail(head, skb);
7730 spin_unlock_irq(&head->lock);
7731 }
7732#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007733 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007734#endif
7735}
7736
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007737static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007738{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007739 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7740 struct io_ring_ctx *ctx = rsrc_data->ctx;
7741 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007742
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007743 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7744 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007745 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007746 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007747 }
7748
Xiaoguang Wang05589552020-03-31 14:05:18 +08007749 percpu_ref_exit(&ref_node->refs);
7750 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007751 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007752}
7753
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007754static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007755{
7756 struct io_ring_ctx *ctx;
7757 struct llist_node *node;
7758
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007759 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7760 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007761
7762 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007763 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007764 struct llist_node *next = node->next;
7765
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007766 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7767 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007768 node = next;
7769 }
7770}
7771
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007772static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7773 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007774{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007775 struct fixed_rsrc_table *table;
7776
7777 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7778 return &table->files[i & IORING_FILE_TABLE_MASK];
7779}
7780
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007781static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007782{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007783 struct fixed_rsrc_ref_node *ref_node;
7784 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007785 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007786 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007787 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007788
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007789 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7790 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007791 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007792
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007793 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007794 ref_node->done = true;
7795
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007796 while (!list_empty(&ctx->rsrc_ref_list)) {
7797 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007798 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007799 /* recycle ref nodes in order */
7800 if (!ref_node->done)
7801 break;
7802 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007803 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007804 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007805 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007806
7807 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007808 delay = 0;
7809
Jens Axboe4a38aed22020-05-14 17:21:15 -06007810 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007811 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007812 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007813 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007814}
7815
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007816static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007817 struct io_ring_ctx *ctx)
7818{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007819 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007820
7821 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7822 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007823 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007824
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007825 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007826 0, GFP_KERNEL)) {
7827 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007828 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007829 }
7830 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007831 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007832 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007833 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007834}
7835
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007836static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7837 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007838{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007839 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007840 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007841}
7842
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007843static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007844{
7845 percpu_ref_exit(&ref_node->refs);
7846 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007847}
7848
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007849
Jens Axboe05f3fb32019-12-09 11:22:50 -07007850static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7851 unsigned nr_args)
7852{
7853 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007854 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007855 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007856 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007857 struct fixed_rsrc_ref_node *ref_node;
7858 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007859
7860 if (ctx->file_data)
7861 return -EBUSY;
7862 if (!nr_args)
7863 return -EINVAL;
7864 if (nr_args > IORING_MAX_FIXED_FILES)
7865 return -EMFILE;
7866
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007867 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007868 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007869 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007870 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007871
7872 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007873 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007874 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007875 if (!file_data->table)
7876 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007877
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007878 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007879 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007880
Jens Axboe05f3fb32019-12-09 11:22:50 -07007881 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007882 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7883 ret = -EFAULT;
7884 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007885 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007886 /* allow sparse sets */
7887 if (fd == -1)
7888 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007889
Jens Axboe05f3fb32019-12-09 11:22:50 -07007890 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007891 ret = -EBADF;
7892 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007893 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007894
7895 /*
7896 * Don't allow io_uring instances to be registered. If UNIX
7897 * isn't enabled, then this causes a reference cycle and this
7898 * instance can never get freed. If UNIX is enabled we'll
7899 * handle it just fine, but there's still no point in allowing
7900 * a ring fd as it doesn't support regular read/write anyway.
7901 */
7902 if (file->f_op == &io_uring_fops) {
7903 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007904 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007905 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007906 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007907 }
7908
Jens Axboe05f3fb32019-12-09 11:22:50 -07007909 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007910 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007911 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007912 return ret;
7913 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007914
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007915 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007916 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007917 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007918 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007919 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007920 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007921
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007922 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007923 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007924out_fput:
7925 for (i = 0; i < ctx->nr_user_files; i++) {
7926 file = io_file_from_index(ctx, i);
7927 if (file)
7928 fput(file);
7929 }
7930 for (i = 0; i < nr_tables; i++)
7931 kfree(file_data->table[i].files);
7932 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007933out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007934 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007935 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007936 return ret;
7937}
7938
Jens Axboec3a31e62019-10-03 13:59:56 -06007939static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7940 int index)
7941{
7942#if defined(CONFIG_UNIX)
7943 struct sock *sock = ctx->ring_sock->sk;
7944 struct sk_buff_head *head = &sock->sk_receive_queue;
7945 struct sk_buff *skb;
7946
7947 /*
7948 * See if we can merge this file into an existing skb SCM_RIGHTS
7949 * file set. If there's no room, fall back to allocating a new skb
7950 * and filling it in.
7951 */
7952 spin_lock_irq(&head->lock);
7953 skb = skb_peek(head);
7954 if (skb) {
7955 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7956
7957 if (fpl->count < SCM_MAX_FD) {
7958 __skb_unlink(skb, head);
7959 spin_unlock_irq(&head->lock);
7960 fpl->fp[fpl->count] = get_file(file);
7961 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7962 fpl->count++;
7963 spin_lock_irq(&head->lock);
7964 __skb_queue_head(head, skb);
7965 } else {
7966 skb = NULL;
7967 }
7968 }
7969 spin_unlock_irq(&head->lock);
7970
7971 if (skb) {
7972 fput(file);
7973 return 0;
7974 }
7975
7976 return __io_sqe_files_scm(ctx, 1, index);
7977#else
7978 return 0;
7979#endif
7980}
7981
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007982static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007983{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007984 struct io_rsrc_put *prsrc;
7985 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007986
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007987 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7988 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007989 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007990
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007991 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007992 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007993
Hillf Dantona5318d32020-03-23 17:47:15 +08007994 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007995}
7996
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007997static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7998 struct file *file)
7999{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00008000 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008001}
8002
Jens Axboe05f3fb32019-12-09 11:22:50 -07008003static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008004 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008005 unsigned nr_args)
8006{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008007 struct fixed_rsrc_data *data = ctx->file_data;
8008 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008009 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06008010 __s32 __user *fds;
8011 int fd, i, err;
8012 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008013 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008014
Jens Axboe05f3fb32019-12-09 11:22:50 -07008015 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06008016 return -EOVERFLOW;
8017 if (done > ctx->nr_user_files)
8018 return -EINVAL;
8019
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008020 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00008021 if (!ref_node)
8022 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008023 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008024
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008025 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00008026 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008027 err = 0;
8028 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
8029 err = -EFAULT;
8030 break;
8031 }
noah4e0377a2021-01-26 15:23:28 -05008032 if (fd == IORING_REGISTER_FILES_SKIP)
8033 continue;
8034
Pavel Begunkov67973b92021-01-26 13:51:09 +00008035 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008036 file_slot = io_fixed_file_slot(ctx->file_data, i);
8037
8038 if (*file_slot) {
8039 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008040 if (err)
8041 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008042 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008043 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008044 }
8045 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008046 file = fget(fd);
8047 if (!file) {
8048 err = -EBADF;
8049 break;
8050 }
8051 /*
8052 * Don't allow io_uring instances to be registered. If
8053 * UNIX isn't enabled, then this causes a reference
8054 * cycle and this instance can never get freed. If UNIX
8055 * is enabled we'll handle it just fine, but there's
8056 * still no point in allowing a ring fd as it doesn't
8057 * support regular read/write anyway.
8058 */
8059 if (file->f_op == &io_uring_fops) {
8060 fput(file);
8061 err = -EBADF;
8062 break;
8063 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07008064 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008065 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008066 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07008067 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008068 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008069 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008070 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008071 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008072 }
8073
Xiaoguang Wang05589552020-03-31 14:05:18 +08008074 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008075 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008076 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008077 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008078 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008079
8080 return done ? done : err;
8081}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008082
Jens Axboe05f3fb32019-12-09 11:22:50 -07008083static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8084 unsigned nr_args)
8085{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008086 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008087
8088 if (!ctx->file_data)
8089 return -ENXIO;
8090 if (!nr_args)
8091 return -EINVAL;
8092 if (copy_from_user(&up, arg, sizeof(up)))
8093 return -EFAULT;
8094 if (up.resv)
8095 return -EINVAL;
8096
8097 return __io_sqe_files_update(ctx, &up, nr_args);
8098}
Jens Axboec3a31e62019-10-03 13:59:56 -06008099
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008100static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008101{
8102 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8103
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008104 req = io_put_req_find_next(req);
8105 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008106}
8107
Jens Axboed25e3a32021-02-16 11:41:41 -07008108static int io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008109{
8110 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008111 unsigned int concurrency;
8112 int ret = 0;
8113
8114 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008115 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008116 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008117
Jens Axboed25e3a32021-02-16 11:41:41 -07008118 /* Do QD, or 4 * CPUS, whatever is smallest */
8119 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008120
Jens Axboed25e3a32021-02-16 11:41:41 -07008121 ctx->io_wq = io_wq_create(concurrency, &data);
8122 if (IS_ERR(ctx->io_wq)) {
8123 ret = PTR_ERR(ctx->io_wq);
8124 ctx->io_wq = NULL;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008125 }
8126
Pavel Begunkov24369c22020-01-28 03:15:48 +03008127 return ret;
8128}
8129
Jens Axboe0f212202020-09-13 13:09:39 -06008130static int io_uring_alloc_task_context(struct task_struct *task)
8131{
8132 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008133 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008134
8135 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8136 if (unlikely(!tctx))
8137 return -ENOMEM;
8138
Jens Axboed8a6df12020-10-15 16:24:45 -06008139 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8140 if (unlikely(ret)) {
8141 kfree(tctx);
8142 return ret;
8143 }
8144
Jens Axboe0f212202020-09-13 13:09:39 -06008145 xa_init(&tctx->xa);
8146 init_waitqueue_head(&tctx->wait);
8147 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008148 atomic_set(&tctx->in_idle, 0);
8149 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008150 io_init_identity(&tctx->__identity);
8151 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008152 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008153 spin_lock_init(&tctx->task_lock);
8154 INIT_WQ_LIST(&tctx->task_list);
8155 tctx->task_state = 0;
8156 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008157 return 0;
8158}
8159
8160void __io_uring_free(struct task_struct *tsk)
8161{
8162 struct io_uring_task *tctx = tsk->io_uring;
8163
8164 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008165 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8166 if (tctx->identity != &tctx->__identity)
8167 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008168 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008169 kfree(tctx);
8170 tsk->io_uring = NULL;
8171}
8172
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008173static int io_sq_offload_create(struct io_ring_ctx *ctx,
8174 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008175{
8176 int ret;
8177
Jens Axboed25e3a32021-02-16 11:41:41 -07008178 /* Retain compatibility with failing for an invalid attach attempt */
8179 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8180 IORING_SETUP_ATTACH_WQ) {
8181 struct fd f;
8182
8183 f = fdget(p->wq_fd);
8184 if (!f.file)
8185 return -ENXIO;
8186 if (f.file->f_op != &io_uring_fops) {
8187 fdput(f);
8188 return -EINVAL;
8189 }
8190 fdput(f);
8191 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008192 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008193 struct io_sq_data *sqd;
8194
Jens Axboe3ec482d2019-04-08 10:51:01 -06008195 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008196 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008197 goto err;
8198
Jens Axboe534ca6d2020-09-02 13:52:19 -06008199 sqd = io_get_sq_data(p);
8200 if (IS_ERR(sqd)) {
8201 ret = PTR_ERR(sqd);
8202 goto err;
8203 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008204
Jens Axboe534ca6d2020-09-02 13:52:19 -06008205 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008206 io_sq_thread_park(sqd);
8207 mutex_lock(&sqd->ctx_lock);
8208 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8209 mutex_unlock(&sqd->ctx_lock);
8210 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008211
Jens Axboe917257d2019-04-13 09:28:55 -06008212 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8213 if (!ctx->sq_thread_idle)
8214 ctx->sq_thread_idle = HZ;
8215
Jens Axboeaa061652020-09-02 14:50:27 -06008216 if (sqd->thread)
8217 goto done;
8218
Jens Axboe6c271ce2019-01-10 11:22:30 -07008219 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008220 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008221
Jens Axboe917257d2019-04-13 09:28:55 -06008222 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008223 if (cpu >= nr_cpu_ids)
8224 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008225 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008226 goto err;
8227
Jens Axboe69fb2132020-09-14 11:16:23 -06008228 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008229 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008230 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008231 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008232 "io_uring-sq");
8233 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008234 if (IS_ERR(sqd->thread)) {
8235 ret = PTR_ERR(sqd->thread);
8236 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008237 goto err;
8238 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008239 ret = io_uring_alloc_task_context(sqd->thread);
Jens Axboe0f212202020-09-13 13:09:39 -06008240 if (ret)
8241 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008242 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8243 /* Can't have SQ_AFF without SQPOLL */
8244 ret = -EINVAL;
8245 goto err;
8246 }
8247
Jens Axboeaa061652020-09-02 14:50:27 -06008248done:
Jens Axboed25e3a32021-02-16 11:41:41 -07008249 ret = io_init_wq_offload(ctx);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008250 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008251 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008252
8253 return 0;
8254err:
Jens Axboe54a91f32019-09-10 09:15:04 -06008255 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008256 return ret;
8257}
8258
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008259static void io_sq_offload_start(struct io_ring_ctx *ctx)
8260{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008261 struct io_sq_data *sqd = ctx->sq_data;
8262
8263 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8264 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008265}
8266
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008267static inline void __io_unaccount_mem(struct user_struct *user,
8268 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008269{
8270 atomic_long_sub(nr_pages, &user->locked_vm);
8271}
8272
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008273static inline int __io_account_mem(struct user_struct *user,
8274 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008275{
8276 unsigned long page_limit, cur_pages, new_pages;
8277
8278 /* Don't allow more pages than we can safely lock */
8279 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8280
8281 do {
8282 cur_pages = atomic_long_read(&user->locked_vm);
8283 new_pages = cur_pages + nr_pages;
8284 if (new_pages > page_limit)
8285 return -ENOMEM;
8286 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8287 new_pages) != cur_pages);
8288
8289 return 0;
8290}
8291
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008292static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008293{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008294 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008295 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008296
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008297 if (ctx->mm_account)
8298 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008299}
8300
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008301static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008302{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008303 int ret;
8304
8305 if (ctx->limit_mem) {
8306 ret = __io_account_mem(ctx->user, nr_pages);
8307 if (ret)
8308 return ret;
8309 }
8310
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008311 if (ctx->mm_account)
8312 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008313
8314 return 0;
8315}
8316
Jens Axboe2b188cc2019-01-07 10:46:33 -07008317static void io_mem_free(void *ptr)
8318{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008319 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008320
Mark Rutland52e04ef2019-04-30 17:30:21 +01008321 if (!ptr)
8322 return;
8323
8324 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008325 if (put_page_testzero(page))
8326 free_compound_page(page);
8327}
8328
8329static void *io_mem_alloc(size_t size)
8330{
8331 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008332 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008333
8334 return (void *) __get_free_pages(gfp_flags, get_order(size));
8335}
8336
Hristo Venev75b28af2019-08-26 17:23:46 +00008337static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8338 size_t *sq_offset)
8339{
8340 struct io_rings *rings;
8341 size_t off, sq_array_size;
8342
8343 off = struct_size(rings, cqes, cq_entries);
8344 if (off == SIZE_MAX)
8345 return SIZE_MAX;
8346
8347#ifdef CONFIG_SMP
8348 off = ALIGN(off, SMP_CACHE_BYTES);
8349 if (off == 0)
8350 return SIZE_MAX;
8351#endif
8352
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008353 if (sq_offset)
8354 *sq_offset = off;
8355
Hristo Venev75b28af2019-08-26 17:23:46 +00008356 sq_array_size = array_size(sizeof(u32), sq_entries);
8357 if (sq_array_size == SIZE_MAX)
8358 return SIZE_MAX;
8359
8360 if (check_add_overflow(off, sq_array_size, &off))
8361 return SIZE_MAX;
8362
Hristo Venev75b28af2019-08-26 17:23:46 +00008363 return off;
8364}
8365
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008366static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008367{
8368 int i, j;
8369
8370 if (!ctx->user_bufs)
8371 return -ENXIO;
8372
8373 for (i = 0; i < ctx->nr_user_bufs; i++) {
8374 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8375
8376 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008377 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008378
Jens Axboede293932020-09-17 16:19:16 -06008379 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008380 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008381 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008382 imu->nr_bvecs = 0;
8383 }
8384
8385 kfree(ctx->user_bufs);
8386 ctx->user_bufs = NULL;
8387 ctx->nr_user_bufs = 0;
8388 return 0;
8389}
8390
8391static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8392 void __user *arg, unsigned index)
8393{
8394 struct iovec __user *src;
8395
8396#ifdef CONFIG_COMPAT
8397 if (ctx->compat) {
8398 struct compat_iovec __user *ciovs;
8399 struct compat_iovec ciov;
8400
8401 ciovs = (struct compat_iovec __user *) arg;
8402 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8403 return -EFAULT;
8404
Jens Axboed55e5f52019-12-11 16:12:15 -07008405 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008406 dst->iov_len = ciov.iov_len;
8407 return 0;
8408 }
8409#endif
8410 src = (struct iovec __user *) arg;
8411 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8412 return -EFAULT;
8413 return 0;
8414}
8415
Jens Axboede293932020-09-17 16:19:16 -06008416/*
8417 * Not super efficient, but this is just a registration time. And we do cache
8418 * the last compound head, so generally we'll only do a full search if we don't
8419 * match that one.
8420 *
8421 * We check if the given compound head page has already been accounted, to
8422 * avoid double accounting it. This allows us to account the full size of the
8423 * page, not just the constituent pages of a huge page.
8424 */
8425static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8426 int nr_pages, struct page *hpage)
8427{
8428 int i, j;
8429
8430 /* check current page array */
8431 for (i = 0; i < nr_pages; i++) {
8432 if (!PageCompound(pages[i]))
8433 continue;
8434 if (compound_head(pages[i]) == hpage)
8435 return true;
8436 }
8437
8438 /* check previously registered pages */
8439 for (i = 0; i < ctx->nr_user_bufs; i++) {
8440 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8441
8442 for (j = 0; j < imu->nr_bvecs; j++) {
8443 if (!PageCompound(imu->bvec[j].bv_page))
8444 continue;
8445 if (compound_head(imu->bvec[j].bv_page) == hpage)
8446 return true;
8447 }
8448 }
8449
8450 return false;
8451}
8452
8453static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8454 int nr_pages, struct io_mapped_ubuf *imu,
8455 struct page **last_hpage)
8456{
8457 int i, ret;
8458
8459 for (i = 0; i < nr_pages; i++) {
8460 if (!PageCompound(pages[i])) {
8461 imu->acct_pages++;
8462 } else {
8463 struct page *hpage;
8464
8465 hpage = compound_head(pages[i]);
8466 if (hpage == *last_hpage)
8467 continue;
8468 *last_hpage = hpage;
8469 if (headpage_already_acct(ctx, pages, i, hpage))
8470 continue;
8471 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8472 }
8473 }
8474
8475 if (!imu->acct_pages)
8476 return 0;
8477
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008478 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008479 if (ret)
8480 imu->acct_pages = 0;
8481 return ret;
8482}
8483
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008484static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8485 struct io_mapped_ubuf *imu,
8486 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008487{
8488 struct vm_area_struct **vmas = NULL;
8489 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008490 unsigned long off, start, end, ubuf;
8491 size_t size;
8492 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008493
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008494 ubuf = (unsigned long) iov->iov_base;
8495 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8496 start = ubuf >> PAGE_SHIFT;
8497 nr_pages = end - start;
8498
8499 ret = -ENOMEM;
8500
8501 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8502 if (!pages)
8503 goto done;
8504
8505 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8506 GFP_KERNEL);
8507 if (!vmas)
8508 goto done;
8509
8510 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8511 GFP_KERNEL);
8512 if (!imu->bvec)
8513 goto done;
8514
8515 ret = 0;
8516 mmap_read_lock(current->mm);
8517 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8518 pages, vmas);
8519 if (pret == nr_pages) {
8520 /* don't support file backed memory */
8521 for (i = 0; i < nr_pages; i++) {
8522 struct vm_area_struct *vma = vmas[i];
8523
8524 if (vma->vm_file &&
8525 !is_file_hugepages(vma->vm_file)) {
8526 ret = -EOPNOTSUPP;
8527 break;
8528 }
8529 }
8530 } else {
8531 ret = pret < 0 ? pret : -EFAULT;
8532 }
8533 mmap_read_unlock(current->mm);
8534 if (ret) {
8535 /*
8536 * if we did partial map, or found file backed vmas,
8537 * release any pages we did get
8538 */
8539 if (pret > 0)
8540 unpin_user_pages(pages, pret);
8541 kvfree(imu->bvec);
8542 goto done;
8543 }
8544
8545 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8546 if (ret) {
8547 unpin_user_pages(pages, pret);
8548 kvfree(imu->bvec);
8549 goto done;
8550 }
8551
8552 off = ubuf & ~PAGE_MASK;
8553 size = iov->iov_len;
8554 for (i = 0; i < nr_pages; i++) {
8555 size_t vec_len;
8556
8557 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8558 imu->bvec[i].bv_page = pages[i];
8559 imu->bvec[i].bv_len = vec_len;
8560 imu->bvec[i].bv_offset = off;
8561 off = 0;
8562 size -= vec_len;
8563 }
8564 /* store original address for later verification */
8565 imu->ubuf = ubuf;
8566 imu->len = iov->iov_len;
8567 imu->nr_bvecs = nr_pages;
8568 ret = 0;
8569done:
8570 kvfree(pages);
8571 kvfree(vmas);
8572 return ret;
8573}
8574
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008575static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008576{
Jens Axboeedafcce2019-01-09 09:16:05 -07008577 if (ctx->user_bufs)
8578 return -EBUSY;
8579 if (!nr_args || nr_args > UIO_MAXIOV)
8580 return -EINVAL;
8581
8582 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8583 GFP_KERNEL);
8584 if (!ctx->user_bufs)
8585 return -ENOMEM;
8586
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008587 return 0;
8588}
8589
8590static int io_buffer_validate(struct iovec *iov)
8591{
8592 /*
8593 * Don't impose further limits on the size and buffer
8594 * constraints here, we'll -EINVAL later when IO is
8595 * submitted if they are wrong.
8596 */
8597 if (!iov->iov_base || !iov->iov_len)
8598 return -EFAULT;
8599
8600 /* arbitrary limit, but we need something */
8601 if (iov->iov_len > SZ_1G)
8602 return -EFAULT;
8603
8604 return 0;
8605}
8606
8607static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8608 unsigned int nr_args)
8609{
8610 int i, ret;
8611 struct iovec iov;
8612 struct page *last_hpage = NULL;
8613
8614 ret = io_buffers_map_alloc(ctx, nr_args);
8615 if (ret)
8616 return ret;
8617
Jens Axboeedafcce2019-01-09 09:16:05 -07008618 for (i = 0; i < nr_args; i++) {
8619 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008620
8621 ret = io_copy_iov(ctx, &iov, arg, i);
8622 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008623 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008624
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008625 ret = io_buffer_validate(&iov);
8626 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008627 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008628
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008629 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8630 if (ret)
8631 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008632
8633 ctx->nr_user_bufs++;
8634 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008635
8636 if (ret)
8637 io_sqe_buffers_unregister(ctx);
8638
Jens Axboeedafcce2019-01-09 09:16:05 -07008639 return ret;
8640}
8641
Jens Axboe9b402842019-04-11 11:45:41 -06008642static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8643{
8644 __s32 __user *fds = arg;
8645 int fd;
8646
8647 if (ctx->cq_ev_fd)
8648 return -EBUSY;
8649
8650 if (copy_from_user(&fd, fds, sizeof(*fds)))
8651 return -EFAULT;
8652
8653 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8654 if (IS_ERR(ctx->cq_ev_fd)) {
8655 int ret = PTR_ERR(ctx->cq_ev_fd);
8656 ctx->cq_ev_fd = NULL;
8657 return ret;
8658 }
8659
8660 return 0;
8661}
8662
8663static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8664{
8665 if (ctx->cq_ev_fd) {
8666 eventfd_ctx_put(ctx->cq_ev_fd);
8667 ctx->cq_ev_fd = NULL;
8668 return 0;
8669 }
8670
8671 return -ENXIO;
8672}
8673
Jens Axboe5a2e7452020-02-23 16:23:11 -07008674static int __io_destroy_buffers(int id, void *p, void *data)
8675{
8676 struct io_ring_ctx *ctx = data;
8677 struct io_buffer *buf = p;
8678
Jens Axboe067524e2020-03-02 16:32:28 -07008679 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008680 return 0;
8681}
8682
8683static void io_destroy_buffers(struct io_ring_ctx *ctx)
8684{
8685 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8686 idr_destroy(&ctx->io_buffer_idr);
8687}
8688
Jens Axboe68e68ee2021-02-13 09:00:02 -07008689static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008690{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008691 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008692
Jens Axboe68e68ee2021-02-13 09:00:02 -07008693 list_for_each_entry_safe(req, nxt, list, compl.list) {
8694 if (tsk && req->task != tsk)
8695 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008696 list_del(&req->compl.list);
8697 kmem_cache_free(req_cachep, req);
8698 }
8699}
8700
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008701static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008702{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008703 struct io_submit_state *submit_state = &ctx->submit_state;
8704
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008705 mutex_lock(&ctx->uring_lock);
8706
8707 if (submit_state->free_reqs)
8708 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8709 submit_state->reqs);
8710
8711 io_req_cache_free(&submit_state->comp.free_list, NULL);
8712
8713 spin_lock_irq(&ctx->completion_lock);
8714 io_req_cache_free(&submit_state->comp.locked_free_list, NULL);
8715 spin_unlock_irq(&ctx->completion_lock);
8716
8717 mutex_unlock(&ctx->uring_lock);
8718}
8719
Jens Axboe2b188cc2019-01-07 10:46:33 -07008720static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8721{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008722 /*
8723 * Some may use context even when all refs and requests have been put,
8724 * and they are free to do so while still holding uring_lock, see
8725 * __io_req_task_submit(). Wait for them to finish.
8726 */
8727 mutex_lock(&ctx->uring_lock);
8728 mutex_unlock(&ctx->uring_lock);
8729
Jens Axboe6b063142019-01-10 22:13:58 -07008730 io_finish_async(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008731 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008732
8733 if (ctx->sqo_task) {
8734 put_task_struct(ctx->sqo_task);
8735 ctx->sqo_task = NULL;
8736 mmdrop(ctx->mm_account);
8737 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008738 }
Jens Axboedef596e2019-01-09 08:59:42 -07008739
Dennis Zhou91d8f512020-09-16 13:41:05 -07008740#ifdef CONFIG_BLK_CGROUP
8741 if (ctx->sqo_blkcg_css)
8742 css_put(ctx->sqo_blkcg_css);
8743#endif
8744
Hao Xu8bad28d2021-02-19 17:19:36 +08008745 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008746 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008747 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008748 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008749 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008750 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008751
Jens Axboe2b188cc2019-01-07 10:46:33 -07008752#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008753 if (ctx->ring_sock) {
8754 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008755 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008756 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008757#endif
8758
Hristo Venev75b28af2019-08-26 17:23:46 +00008759 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008760 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008761
8762 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008763 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008764 put_cred(ctx->creds);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008765 io_req_caches_free(ctx, NULL);
Jens Axboe78076bb2019-12-04 19:56:40 -07008766 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008767 kfree(ctx);
8768}
8769
8770static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8771{
8772 struct io_ring_ctx *ctx = file->private_data;
8773 __poll_t mask = 0;
8774
8775 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008776 /*
8777 * synchronizes with barrier from wq_has_sleeper call in
8778 * io_commit_cqring
8779 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008780 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008781 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008782 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008783
8784 /*
8785 * Don't flush cqring overflow list here, just do a simple check.
8786 * Otherwise there could possible be ABBA deadlock:
8787 * CPU0 CPU1
8788 * ---- ----
8789 * lock(&ctx->uring_lock);
8790 * lock(&ep->mtx);
8791 * lock(&ctx->uring_lock);
8792 * lock(&ep->mtx);
8793 *
8794 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8795 * pushs them to do the flush.
8796 */
8797 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008798 mask |= EPOLLIN | EPOLLRDNORM;
8799
8800 return mask;
8801}
8802
8803static int io_uring_fasync(int fd, struct file *file, int on)
8804{
8805 struct io_ring_ctx *ctx = file->private_data;
8806
8807 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8808}
8809
Yejune Deng0bead8c2020-12-24 11:02:20 +08008810static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008811{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008812 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008813
Jens Axboe1e6fa522020-10-15 08:46:24 -06008814 iod = idr_remove(&ctx->personality_idr, id);
8815 if (iod) {
8816 put_cred(iod->creds);
8817 if (refcount_dec_and_test(&iod->count))
8818 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008819 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008820 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008821
8822 return -EINVAL;
8823}
8824
8825static int io_remove_personalities(int id, void *p, void *data)
8826{
8827 struct io_ring_ctx *ctx = data;
8828
8829 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008830 return 0;
8831}
8832
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008833static void io_run_ctx_fallback(struct io_ring_ctx *ctx)
8834{
8835 struct callback_head *work, *head, *next;
8836
8837 do {
8838 do {
8839 head = NULL;
8840 work = READ_ONCE(ctx->exit_task_work);
8841 } while (cmpxchg(&ctx->exit_task_work, work, head) != work);
8842
8843 if (!work)
8844 break;
8845
8846 do {
8847 next = work->next;
8848 work->func(work);
8849 work = next;
8850 cond_resched();
8851 } while (work);
8852 } while (1);
8853}
8854
Jens Axboe85faa7b2020-04-09 18:14:00 -06008855static void io_ring_exit_work(struct work_struct *work)
8856{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008857 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8858 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008859
Jens Axboe56952e92020-06-17 15:00:04 -06008860 /*
8861 * If we're doing polled IO and end up having requests being
8862 * submitted async (out-of-line), then completions can come in while
8863 * we're waiting for refs to drop. We need to reap these manually,
8864 * as nobody else will be looking for them.
8865 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008866 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008867 io_uring_try_cancel_requests(ctx, NULL, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008868 io_run_ctx_fallback(ctx);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008869 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008870 io_ring_ctx_free(ctx);
8871}
8872
Jens Axboe00c18642020-12-20 10:45:02 -07008873static bool io_cancel_ctx_cb(struct io_wq_work *work, void *data)
8874{
8875 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8876
8877 return req->ctx == data;
8878}
8879
Jens Axboe2b188cc2019-01-07 10:46:33 -07008880static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8881{
8882 mutex_lock(&ctx->uring_lock);
8883 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008884
8885 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8886 ctx->sqo_dead = 1;
8887
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008888 /* if force is set, the ring is going away. always drop after that */
8889 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008890 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008891 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008892 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008893 mutex_unlock(&ctx->uring_lock);
8894
Pavel Begunkov6b819282020-11-06 13:00:25 +00008895 io_kill_timeouts(ctx, NULL, NULL);
8896 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008897
8898 if (ctx->io_wq)
Jens Axboe00c18642020-12-20 10:45:02 -07008899 io_wq_cancel_cb(ctx->io_wq, io_cancel_ctx_cb, ctx, true);
Jens Axboe561fb042019-10-24 07:25:42 -06008900
Jens Axboe15dff282019-11-13 09:09:23 -07008901 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008902 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008903
Jens Axboe85faa7b2020-04-09 18:14:00 -06008904 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008905 /*
8906 * Use system_unbound_wq to avoid spawning tons of event kworkers
8907 * if we're exiting a ton of rings at the same time. It just adds
8908 * noise and overhead, there's no discernable change in runtime
8909 * over using system_wq.
8910 */
8911 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008912}
8913
8914static int io_uring_release(struct inode *inode, struct file *file)
8915{
8916 struct io_ring_ctx *ctx = file->private_data;
8917
8918 file->private_data = NULL;
8919 io_ring_ctx_wait_and_kill(ctx);
8920 return 0;
8921}
8922
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008923struct io_task_cancel {
8924 struct task_struct *task;
8925 struct files_struct *files;
8926};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008927
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008928static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008929{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008930 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008931 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008932 bool ret;
8933
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008934 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008935 unsigned long flags;
8936 struct io_ring_ctx *ctx = req->ctx;
8937
8938 /* protect against races with linked timeouts */
8939 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008940 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008941 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8942 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008943 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008944 }
8945 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008946}
8947
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008948static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008949 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008950 struct files_struct *files)
8951{
8952 struct io_defer_entry *de = NULL;
8953 LIST_HEAD(list);
8954
8955 spin_lock_irq(&ctx->completion_lock);
8956 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008957 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008958 list_cut_position(&list, &ctx->defer_list, &de->list);
8959 break;
8960 }
8961 }
8962 spin_unlock_irq(&ctx->completion_lock);
8963
8964 while (!list_empty(&list)) {
8965 de = list_first_entry(&list, struct io_defer_entry, list);
8966 list_del_init(&de->list);
8967 req_set_fail_links(de->req);
8968 io_put_req(de->req);
8969 io_req_complete(de->req, -ECANCELED);
8970 kfree(de);
8971 }
8972}
8973
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008974static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8975 struct task_struct *task,
8976 struct files_struct *files)
8977{
8978 struct io_task_cancel cancel = { .task = task, .files = files, };
8979
8980 while (1) {
8981 enum io_wq_cancel cret;
8982 bool ret = false;
8983
8984 if (ctx->io_wq) {
8985 cret = io_wq_cancel_cb(ctx->io_wq, io_cancel_task_cb,
8986 &cancel, true);
8987 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8988 }
8989
8990 /* SQPOLL thread does its own polling */
8991 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8992 while (!list_empty_careful(&ctx->iopoll_list)) {
8993 io_iopoll_try_reap_events(ctx);
8994 ret = true;
8995 }
8996 }
8997
8998 ret |= io_poll_remove_all(ctx, task, files);
8999 ret |= io_kill_timeouts(ctx, task, files);
9000 ret |= io_run_task_work();
9001 io_cqring_overflow_flush(ctx, true, task, files);
9002 if (!ret)
9003 break;
9004 cond_resched();
9005 }
9006}
9007
Pavel Begunkovca70f002021-01-26 15:28:27 +00009008static int io_uring_count_inflight(struct io_ring_ctx *ctx,
9009 struct task_struct *task,
9010 struct files_struct *files)
9011{
9012 struct io_kiocb *req;
9013 int cnt = 0;
9014
9015 spin_lock_irq(&ctx->inflight_lock);
9016 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
9017 cnt += io_match_task(req, task, files);
9018 spin_unlock_irq(&ctx->inflight_lock);
9019 return cnt;
9020}
9021
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009022static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009023 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06009024 struct files_struct *files)
9025{
Jens Axboefcb323c2019-10-24 12:39:47 -06009026 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08009027 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009028 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06009029
Pavel Begunkovca70f002021-01-26 15:28:27 +00009030 inflight = io_uring_count_inflight(ctx, task, files);
9031 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06009032 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009033
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009034 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009035
Pavel Begunkov34343782021-02-10 11:45:42 +00009036 if (ctx->sq_data)
9037 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009038 prepare_to_wait(&task->io_uring->wait, &wait,
9039 TASK_UNINTERRUPTIBLE);
9040 if (inflight == io_uring_count_inflight(ctx, task, files))
9041 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00009042 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00009043 if (ctx->sq_data)
9044 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06009045 }
Jens Axboe0f212202020-09-13 13:09:39 -06009046}
9047
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009048static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
9049{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009050 mutex_lock(&ctx->uring_lock);
9051 ctx->sqo_dead = 1;
9052 mutex_unlock(&ctx->uring_lock);
9053
9054 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00009055 if (ctx->rings)
9056 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009057}
9058
Jens Axboe0f212202020-09-13 13:09:39 -06009059/*
9060 * We need to iteratively cancel requests, in case a request has dependent
9061 * hard links. These persist even for failure of cancelations, hence keep
9062 * looping until none are found.
9063 */
9064static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9065 struct files_struct *files)
9066{
9067 struct task_struct *task = current;
9068
Jens Axboefdaf0832020-10-30 09:37:30 -06009069 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009070 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06009071 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06009072 atomic_inc(&task->io_uring->in_idle);
9073 io_sq_thread_park(ctx->sq_data);
9074 }
Jens Axboe0f212202020-09-13 13:09:39 -06009075
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009076 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06009077
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009078 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009079 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009080 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009081
9082 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9083 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06009084 io_sq_thread_unpark(ctx->sq_data);
9085 }
Jens Axboe0f212202020-09-13 13:09:39 -06009086}
9087
9088/*
9089 * Note that this task has used io_uring. We use it for cancelation purposes.
9090 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009091static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009092{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009093 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009094 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009095
9096 if (unlikely(!tctx)) {
Jens Axboe0f212202020-09-13 13:09:39 -06009097 ret = io_uring_alloc_task_context(current);
9098 if (unlikely(ret))
9099 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009100 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009101 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009102 if (tctx->last != file) {
9103 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009104
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009105 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009106 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009107 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9108 file, GFP_KERNEL));
9109 if (ret) {
9110 fput(file);
9111 return ret;
9112 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009113
9114 /* one and only SQPOLL file note, held by sqo_task */
9115 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9116 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009117 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009118 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009119 }
9120
Jens Axboefdaf0832020-10-30 09:37:30 -06009121 /*
9122 * This is race safe in that the task itself is doing this, hence it
9123 * cannot be going through the exit/cancel paths at the same time.
9124 * This cannot be modified while exit/cancel is running.
9125 */
9126 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9127 tctx->sqpoll = true;
9128
Jens Axboe0f212202020-09-13 13:09:39 -06009129 return 0;
9130}
9131
9132/*
9133 * Remove this io_uring_file -> task mapping.
9134 */
9135static void io_uring_del_task_file(struct file *file)
9136{
9137 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009138
9139 if (tctx->last == file)
9140 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009141 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009142 if (file)
9143 fput(file);
9144}
9145
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009146static void io_uring_remove_task_files(struct io_uring_task *tctx)
9147{
9148 struct file *file;
9149 unsigned long index;
9150
9151 xa_for_each(&tctx->xa, index, file)
9152 io_uring_del_task_file(file);
9153}
9154
Jens Axboe0f212202020-09-13 13:09:39 -06009155void __io_uring_files_cancel(struct files_struct *files)
9156{
9157 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009158 struct file *file;
9159 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009160
9161 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009162 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009163 xa_for_each(&tctx->xa, index, file)
9164 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009165 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009166
9167 if (files)
9168 io_uring_remove_task_files(tctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009169}
9170
9171static s64 tctx_inflight(struct io_uring_task *tctx)
9172{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009173 return percpu_counter_sum(&tctx->inflight);
9174}
9175
9176static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9177{
9178 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009179 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009180 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009181
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009182 if (!ctx->sq_data)
9183 return;
9184 tctx = ctx->sq_data->thread->io_uring;
9185 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009186
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009187 atomic_inc(&tctx->in_idle);
9188 do {
9189 /* read completions before cancelations */
9190 inflight = tctx_inflight(tctx);
9191 if (!inflight)
9192 break;
9193 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009194
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009195 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9196 /*
9197 * If we've seen completions, retry without waiting. This
9198 * avoids a race where a completion comes in before we did
9199 * prepare_to_wait().
9200 */
9201 if (inflight == tctx_inflight(tctx))
9202 schedule();
9203 finish_wait(&tctx->wait, &wait);
9204 } while (1);
9205 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009206}
9207
Jens Axboe0f212202020-09-13 13:09:39 -06009208/*
9209 * Find any io_uring fd that this task has registered or done IO on, and cancel
9210 * requests.
9211 */
9212void __io_uring_task_cancel(void)
9213{
9214 struct io_uring_task *tctx = current->io_uring;
9215 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009216 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009217
9218 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009219 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009220
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009221 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009222 if (tctx->sqpoll) {
9223 struct file *file;
9224 unsigned long index;
9225
9226 xa_for_each(&tctx->xa, index, file)
9227 io_uring_cancel_sqpoll(file->private_data);
9228 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009229
Jens Axboed8a6df12020-10-15 16:24:45 -06009230 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009231 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009232 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009233 if (!inflight)
9234 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009235 __io_uring_files_cancel(NULL);
9236
9237 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9238
9239 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009240 * If we've seen completions, retry without waiting. This
9241 * avoids a race where a completion comes in before we did
9242 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009243 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009244 if (inflight == tctx_inflight(tctx))
9245 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009246 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009247 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009248
Jens Axboefdaf0832020-10-30 09:37:30 -06009249 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009250
9251 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009252}
9253
Jens Axboefcb323c2019-10-24 12:39:47 -06009254static int io_uring_flush(struct file *file, void *data)
9255{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009256 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009257 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009258
Jens Axboe41be53e2021-02-13 09:11:04 -07009259 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07009260 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe41be53e2021-02-13 09:11:04 -07009261 io_req_caches_free(ctx, current);
9262 }
Jens Axboe84965ff2021-01-23 15:51:11 -07009263
Jens Axboe7c25c0d2021-02-16 07:17:00 -07009264 io_run_ctx_fallback(ctx);
9265
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009266 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009267 return 0;
9268
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009269 /* we should have cancelled and erased it before PF_EXITING */
9270 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9271 xa_load(&tctx->xa, (unsigned long)file));
9272
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009273 /*
9274 * fput() is pending, will be 2 if the only other ref is our potential
9275 * task file note. If the task is exiting, drop regardless of count.
9276 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009277 if (atomic_long_read(&file->f_count) != 2)
9278 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009279
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009280 if (ctx->flags & IORING_SETUP_SQPOLL) {
9281 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009282 WARN_ON_ONCE(ctx->sqo_task != current &&
9283 xa_load(&tctx->xa, (unsigned long)file));
9284 /* sqo_dead check is for when this happens after cancellation */
9285 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009286 !xa_load(&tctx->xa, (unsigned long)file));
9287
9288 io_disable_sqo_submit(ctx);
9289 }
9290
9291 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9292 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009293 return 0;
9294}
9295
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009296static void *io_uring_validate_mmap_request(struct file *file,
9297 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009298{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009299 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009300 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009301 struct page *page;
9302 void *ptr;
9303
9304 switch (offset) {
9305 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009306 case IORING_OFF_CQ_RING:
9307 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009308 break;
9309 case IORING_OFF_SQES:
9310 ptr = ctx->sq_sqes;
9311 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009312 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009313 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009314 }
9315
9316 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009317 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009318 return ERR_PTR(-EINVAL);
9319
9320 return ptr;
9321}
9322
9323#ifdef CONFIG_MMU
9324
9325static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9326{
9327 size_t sz = vma->vm_end - vma->vm_start;
9328 unsigned long pfn;
9329 void *ptr;
9330
9331 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9332 if (IS_ERR(ptr))
9333 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009334
9335 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9336 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9337}
9338
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009339#else /* !CONFIG_MMU */
9340
9341static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9342{
9343 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9344}
9345
9346static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9347{
9348 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9349}
9350
9351static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9352 unsigned long addr, unsigned long len,
9353 unsigned long pgoff, unsigned long flags)
9354{
9355 void *ptr;
9356
9357 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9358 if (IS_ERR(ptr))
9359 return PTR_ERR(ptr);
9360
9361 return (unsigned long) ptr;
9362}
9363
9364#endif /* !CONFIG_MMU */
9365
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009366static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009367{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009368 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009369 DEFINE_WAIT(wait);
9370
9371 do {
9372 if (!io_sqring_full(ctx))
9373 break;
9374
9375 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9376
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009377 if (unlikely(ctx->sqo_dead)) {
9378 ret = -EOWNERDEAD;
9379 goto out;
9380 }
9381
Jens Axboe90554202020-09-03 12:12:41 -06009382 if (!io_sqring_full(ctx))
9383 break;
9384
9385 schedule();
9386 } while (!signal_pending(current));
9387
9388 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009389out:
9390 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009391}
9392
Hao Xuc73ebb62020-11-03 10:54:37 +08009393static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9394 struct __kernel_timespec __user **ts,
9395 const sigset_t __user **sig)
9396{
9397 struct io_uring_getevents_arg arg;
9398
9399 /*
9400 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9401 * is just a pointer to the sigset_t.
9402 */
9403 if (!(flags & IORING_ENTER_EXT_ARG)) {
9404 *sig = (const sigset_t __user *) argp;
9405 *ts = NULL;
9406 return 0;
9407 }
9408
9409 /*
9410 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9411 * timespec and sigset_t pointers if good.
9412 */
9413 if (*argsz != sizeof(arg))
9414 return -EINVAL;
9415 if (copy_from_user(&arg, argp, sizeof(arg)))
9416 return -EFAULT;
9417 *sig = u64_to_user_ptr(arg.sigmask);
9418 *argsz = arg.sigmask_sz;
9419 *ts = u64_to_user_ptr(arg.ts);
9420 return 0;
9421}
9422
Jens Axboe2b188cc2019-01-07 10:46:33 -07009423SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009424 u32, min_complete, u32, flags, const void __user *, argp,
9425 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009426{
9427 struct io_ring_ctx *ctx;
9428 long ret = -EBADF;
9429 int submitted = 0;
9430 struct fd f;
9431
Jens Axboe4c6e2772020-07-01 11:29:10 -06009432 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009433
Jens Axboe90554202020-09-03 12:12:41 -06009434 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009435 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009436 return -EINVAL;
9437
9438 f = fdget(fd);
9439 if (!f.file)
9440 return -EBADF;
9441
9442 ret = -EOPNOTSUPP;
9443 if (f.file->f_op != &io_uring_fops)
9444 goto out_fput;
9445
9446 ret = -ENXIO;
9447 ctx = f.file->private_data;
9448 if (!percpu_ref_tryget(&ctx->refs))
9449 goto out_fput;
9450
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009451 ret = -EBADFD;
9452 if (ctx->flags & IORING_SETUP_R_DISABLED)
9453 goto out;
9454
Jens Axboe6c271ce2019-01-10 11:22:30 -07009455 /*
9456 * For SQ polling, the thread will do all submissions and completions.
9457 * Just return the requested submit count, and wake the thread if
9458 * we were asked to.
9459 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009460 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009461 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009462 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009463
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009464 ret = -EOWNERDEAD;
9465 if (unlikely(ctx->sqo_dead))
9466 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009467 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009468 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009469 if (flags & IORING_ENTER_SQ_WAIT) {
9470 ret = io_sqpoll_wait_sq(ctx);
9471 if (ret)
9472 goto out;
9473 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009474 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009475 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009476 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009477 if (unlikely(ret))
9478 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009479 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009480 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009481 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009482
9483 if (submitted != to_submit)
9484 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485 }
9486 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009487 const sigset_t __user *sig;
9488 struct __kernel_timespec __user *ts;
9489
9490 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9491 if (unlikely(ret))
9492 goto out;
9493
Jens Axboe2b188cc2019-01-07 10:46:33 -07009494 min_complete = min(min_complete, ctx->cq_entries);
9495
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009496 /*
9497 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9498 * space applications don't need to do io completion events
9499 * polling again, they can rely on io_sq_thread to do polling
9500 * work, which can reduce cpu usage and uring_lock contention.
9501 */
9502 if (ctx->flags & IORING_SETUP_IOPOLL &&
9503 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009504 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009505 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009506 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009507 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009508 }
9509
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009510out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009511 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009512out_fput:
9513 fdput(f);
9514 return submitted ? submitted : ret;
9515}
9516
Tobias Klauserbebdb652020-02-26 18:38:32 +01009517#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009518static int io_uring_show_cred(int id, void *p, void *data)
9519{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009520 struct io_identity *iod = p;
9521 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009522 struct seq_file *m = data;
9523 struct user_namespace *uns = seq_user_ns(m);
9524 struct group_info *gi;
9525 kernel_cap_t cap;
9526 unsigned __capi;
9527 int g;
9528
9529 seq_printf(m, "%5d\n", id);
9530 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9531 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9532 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9533 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9534 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9535 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9536 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9537 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9538 seq_puts(m, "\n\tGroups:\t");
9539 gi = cred->group_info;
9540 for (g = 0; g < gi->ngroups; g++) {
9541 seq_put_decimal_ull(m, g ? " " : "",
9542 from_kgid_munged(uns, gi->gid[g]));
9543 }
9544 seq_puts(m, "\n\tCapEff:\t");
9545 cap = cred->cap_effective;
9546 CAP_FOR_EACH_U32(__capi)
9547 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9548 seq_putc(m, '\n');
9549 return 0;
9550}
9551
9552static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9553{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009554 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009555 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009556 int i;
9557
Jens Axboefad8e0d2020-09-28 08:57:48 -06009558 /*
9559 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9560 * since fdinfo case grabs it in the opposite direction of normal use
9561 * cases. If we fail to get the lock, we just don't iterate any
9562 * structures that could be going away outside the io_uring mutex.
9563 */
9564 has_lock = mutex_trylock(&ctx->uring_lock);
9565
Joseph Qidbbe9c62020-09-29 09:01:22 -06009566 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9567 sq = ctx->sq_data;
9568
9569 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9570 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009571 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009572 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009573 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009574
Jens Axboe87ce9552020-01-30 08:25:34 -07009575 if (f)
9576 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9577 else
9578 seq_printf(m, "%5u: <none>\n", i);
9579 }
9580 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009581 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009582 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9583
9584 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9585 (unsigned int) buf->len);
9586 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009587 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009588 seq_printf(m, "Personalities:\n");
9589 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9590 }
Jens Axboed7718a92020-02-14 22:23:12 -07009591 seq_printf(m, "PollList:\n");
9592 spin_lock_irq(&ctx->completion_lock);
9593 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9594 struct hlist_head *list = &ctx->cancel_hash[i];
9595 struct io_kiocb *req;
9596
9597 hlist_for_each_entry(req, list, hash_node)
9598 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9599 req->task->task_works != NULL);
9600 }
9601 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009602 if (has_lock)
9603 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009604}
9605
9606static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9607{
9608 struct io_ring_ctx *ctx = f->private_data;
9609
9610 if (percpu_ref_tryget(&ctx->refs)) {
9611 __io_uring_show_fdinfo(ctx, m);
9612 percpu_ref_put(&ctx->refs);
9613 }
9614}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009615#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009616
Jens Axboe2b188cc2019-01-07 10:46:33 -07009617static const struct file_operations io_uring_fops = {
9618 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009619 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009620 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009621#ifndef CONFIG_MMU
9622 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9623 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9624#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009625 .poll = io_uring_poll,
9626 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009627#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009628 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009629#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009630};
9631
9632static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9633 struct io_uring_params *p)
9634{
Hristo Venev75b28af2019-08-26 17:23:46 +00009635 struct io_rings *rings;
9636 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009637
Jens Axboebd740482020-08-05 12:58:23 -06009638 /* make sure these are sane, as we already accounted them */
9639 ctx->sq_entries = p->sq_entries;
9640 ctx->cq_entries = p->cq_entries;
9641
Hristo Venev75b28af2019-08-26 17:23:46 +00009642 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9643 if (size == SIZE_MAX)
9644 return -EOVERFLOW;
9645
9646 rings = io_mem_alloc(size);
9647 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009648 return -ENOMEM;
9649
Hristo Venev75b28af2019-08-26 17:23:46 +00009650 ctx->rings = rings;
9651 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9652 rings->sq_ring_mask = p->sq_entries - 1;
9653 rings->cq_ring_mask = p->cq_entries - 1;
9654 rings->sq_ring_entries = p->sq_entries;
9655 rings->cq_ring_entries = p->cq_entries;
9656 ctx->sq_mask = rings->sq_ring_mask;
9657 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009658
9659 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009660 if (size == SIZE_MAX) {
9661 io_mem_free(ctx->rings);
9662 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009663 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009664 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009665
9666 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009667 if (!ctx->sq_sqes) {
9668 io_mem_free(ctx->rings);
9669 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009670 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009671 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009672
Jens Axboe2b188cc2019-01-07 10:46:33 -07009673 return 0;
9674}
9675
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009676static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9677{
9678 int ret, fd;
9679
9680 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9681 if (fd < 0)
9682 return fd;
9683
9684 ret = io_uring_add_task_file(ctx, file);
9685 if (ret) {
9686 put_unused_fd(fd);
9687 return ret;
9688 }
9689 fd_install(fd, file);
9690 return fd;
9691}
9692
Jens Axboe2b188cc2019-01-07 10:46:33 -07009693/*
9694 * Allocate an anonymous fd, this is what constitutes the application
9695 * visible backing of an io_uring instance. The application mmaps this
9696 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9697 * we have to tie this fd to a socket for file garbage collection purposes.
9698 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009699static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009700{
9701 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009702#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009703 int ret;
9704
Jens Axboe2b188cc2019-01-07 10:46:33 -07009705 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9706 &ctx->ring_sock);
9707 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009708 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009709#endif
9710
Jens Axboe2b188cc2019-01-07 10:46:33 -07009711 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9712 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009713#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009714 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009715 sock_release(ctx->ring_sock);
9716 ctx->ring_sock = NULL;
9717 } else {
9718 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009719 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009720#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009721 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009722}
9723
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009724static int io_uring_create(unsigned entries, struct io_uring_params *p,
9725 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009726{
9727 struct user_struct *user = NULL;
9728 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009729 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009730 int ret;
9731
Jens Axboe8110c1a2019-12-28 15:39:54 -07009732 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009733 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009734 if (entries > IORING_MAX_ENTRIES) {
9735 if (!(p->flags & IORING_SETUP_CLAMP))
9736 return -EINVAL;
9737 entries = IORING_MAX_ENTRIES;
9738 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009739
9740 /*
9741 * Use twice as many entries for the CQ ring. It's possible for the
9742 * application to drive a higher depth than the size of the SQ ring,
9743 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009744 * some flexibility in overcommitting a bit. If the application has
9745 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9746 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009747 */
9748 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009749 if (p->flags & IORING_SETUP_CQSIZE) {
9750 /*
9751 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9752 * to a power-of-two, if it isn't already. We do NOT impose
9753 * any cq vs sq ring sizing.
9754 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009755 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009756 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009757 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9758 if (!(p->flags & IORING_SETUP_CLAMP))
9759 return -EINVAL;
9760 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9761 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009762 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9763 if (p->cq_entries < p->sq_entries)
9764 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009765 } else {
9766 p->cq_entries = 2 * p->sq_entries;
9767 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009768
9769 user = get_uid(current_user());
Jens Axboe2b188cc2019-01-07 10:46:33 -07009770
9771 ctx = io_ring_ctx_alloc(p);
9772 if (!ctx) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07009773 free_uid(user);
9774 return -ENOMEM;
9775 }
9776 ctx->compat = in_compat_syscall();
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009777 ctx->limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009778 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009779 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009780#ifdef CONFIG_AUDIT
9781 ctx->loginuid = current->loginuid;
9782 ctx->sessionid = current->sessionid;
9783#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009784 ctx->sqo_task = get_task_struct(current);
9785
9786 /*
9787 * This is just grabbed for accounting purposes. When a process exits,
9788 * the mm is exited and dropped before the files, hence we need to hang
9789 * on to this mm purely for the purposes of being able to unaccount
9790 * memory (locked/pinned vm). It's not used for anything else.
9791 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009792 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009793 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009794
Dennis Zhou91d8f512020-09-16 13:41:05 -07009795#ifdef CONFIG_BLK_CGROUP
9796 /*
9797 * The sq thread will belong to the original cgroup it was inited in.
9798 * If the cgroup goes offline (e.g. disabling the io controller), then
9799 * issued bios will be associated with the closest cgroup later in the
9800 * block layer.
9801 */
9802 rcu_read_lock();
9803 ctx->sqo_blkcg_css = blkcg_css();
9804 ret = css_tryget_online(ctx->sqo_blkcg_css);
9805 rcu_read_unlock();
9806 if (!ret) {
9807 /* don't init against a dying cgroup, have the user try again */
9808 ctx->sqo_blkcg_css = NULL;
9809 ret = -ENODEV;
9810 goto err;
9811 }
9812#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009813 ret = io_allocate_scq_urings(ctx, p);
9814 if (ret)
9815 goto err;
9816
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009817 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009818 if (ret)
9819 goto err;
9820
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009821 if (!(p->flags & IORING_SETUP_R_DISABLED))
9822 io_sq_offload_start(ctx);
9823
Jens Axboe2b188cc2019-01-07 10:46:33 -07009824 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009825 p->sq_off.head = offsetof(struct io_rings, sq.head);
9826 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9827 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9828 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9829 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9830 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9831 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009832
9833 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009834 p->cq_off.head = offsetof(struct io_rings, cq.head);
9835 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9836 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9837 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9838 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9839 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009840 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009841
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009842 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9843 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009844 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009845 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9846 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009847
9848 if (copy_to_user(params, p, sizeof(*p))) {
9849 ret = -EFAULT;
9850 goto err;
9851 }
Jens Axboed1719f72020-07-30 13:43:53 -06009852
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009853 file = io_uring_get_file(ctx);
9854 if (IS_ERR(file)) {
9855 ret = PTR_ERR(file);
9856 goto err;
9857 }
9858
Jens Axboed1719f72020-07-30 13:43:53 -06009859 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009860 * Install ring fd as the very last thing, so we don't risk someone
9861 * having closed it before we finish setup
9862 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009863 ret = io_uring_install_fd(ctx, file);
9864 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009865 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009866 /* fput will clean it up */
9867 fput(file);
9868 return ret;
9869 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009870
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009871 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009872 return ret;
9873err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009874 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009875 io_ring_ctx_wait_and_kill(ctx);
9876 return ret;
9877}
9878
9879/*
9880 * Sets up an aio uring context, and returns the fd. Applications asks for a
9881 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9882 * params structure passed in.
9883 */
9884static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9885{
9886 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009887 int i;
9888
9889 if (copy_from_user(&p, params, sizeof(p)))
9890 return -EFAULT;
9891 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9892 if (p.resv[i])
9893 return -EINVAL;
9894 }
9895
Jens Axboe6c271ce2019-01-10 11:22:30 -07009896 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009897 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009898 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9899 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009900 return -EINVAL;
9901
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009902 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009903}
9904
9905SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9906 struct io_uring_params __user *, params)
9907{
9908 return io_uring_setup(entries, params);
9909}
9910
Jens Axboe66f4af92020-01-16 15:36:52 -07009911static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9912{
9913 struct io_uring_probe *p;
9914 size_t size;
9915 int i, ret;
9916
9917 size = struct_size(p, ops, nr_args);
9918 if (size == SIZE_MAX)
9919 return -EOVERFLOW;
9920 p = kzalloc(size, GFP_KERNEL);
9921 if (!p)
9922 return -ENOMEM;
9923
9924 ret = -EFAULT;
9925 if (copy_from_user(p, arg, size))
9926 goto out;
9927 ret = -EINVAL;
9928 if (memchr_inv(p, 0, size))
9929 goto out;
9930
9931 p->last_op = IORING_OP_LAST - 1;
9932 if (nr_args > IORING_OP_LAST)
9933 nr_args = IORING_OP_LAST;
9934
9935 for (i = 0; i < nr_args; i++) {
9936 p->ops[i].op = i;
9937 if (!io_op_defs[i].not_supported)
9938 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9939 }
9940 p->ops_len = i;
9941
9942 ret = 0;
9943 if (copy_to_user(arg, p, size))
9944 ret = -EFAULT;
9945out:
9946 kfree(p);
9947 return ret;
9948}
9949
Jens Axboe071698e2020-01-28 10:04:42 -07009950static int io_register_personality(struct io_ring_ctx *ctx)
9951{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009952 struct io_identity *id;
9953 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009954
Jens Axboe1e6fa522020-10-15 08:46:24 -06009955 id = kmalloc(sizeof(*id), GFP_KERNEL);
9956 if (unlikely(!id))
9957 return -ENOMEM;
9958
9959 io_init_identity(id);
9960 id->creds = get_current_cred();
9961
9962 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9963 if (ret < 0) {
9964 put_cred(id->creds);
9965 kfree(id);
9966 }
9967 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009968}
9969
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009970static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9971 unsigned int nr_args)
9972{
9973 struct io_uring_restriction *res;
9974 size_t size;
9975 int i, ret;
9976
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009977 /* Restrictions allowed only if rings started disabled */
9978 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9979 return -EBADFD;
9980
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009981 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009982 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009983 return -EBUSY;
9984
9985 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9986 return -EINVAL;
9987
9988 size = array_size(nr_args, sizeof(*res));
9989 if (size == SIZE_MAX)
9990 return -EOVERFLOW;
9991
9992 res = memdup_user(arg, size);
9993 if (IS_ERR(res))
9994 return PTR_ERR(res);
9995
9996 ret = 0;
9997
9998 for (i = 0; i < nr_args; i++) {
9999 switch (res[i].opcode) {
10000 case IORING_RESTRICTION_REGISTER_OP:
10001 if (res[i].register_op >= IORING_REGISTER_LAST) {
10002 ret = -EINVAL;
10003 goto out;
10004 }
10005
10006 __set_bit(res[i].register_op,
10007 ctx->restrictions.register_op);
10008 break;
10009 case IORING_RESTRICTION_SQE_OP:
10010 if (res[i].sqe_op >= IORING_OP_LAST) {
10011 ret = -EINVAL;
10012 goto out;
10013 }
10014
10015 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10016 break;
10017 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10018 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10019 break;
10020 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10021 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10022 break;
10023 default:
10024 ret = -EINVAL;
10025 goto out;
10026 }
10027 }
10028
10029out:
10030 /* Reset all restrictions if an error happened */
10031 if (ret != 0)
10032 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10033 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010034 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010035
10036 kfree(res);
10037 return ret;
10038}
10039
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010040static int io_register_enable_rings(struct io_ring_ctx *ctx)
10041{
10042 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10043 return -EBADFD;
10044
10045 if (ctx->restrictions.registered)
10046 ctx->restricted = 1;
10047
10048 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10049
10050 io_sq_offload_start(ctx);
10051
10052 return 0;
10053}
10054
Jens Axboe071698e2020-01-28 10:04:42 -070010055static bool io_register_op_must_quiesce(int op)
10056{
10057 switch (op) {
10058 case IORING_UNREGISTER_FILES:
10059 case IORING_REGISTER_FILES_UPDATE:
10060 case IORING_REGISTER_PROBE:
10061 case IORING_REGISTER_PERSONALITY:
10062 case IORING_UNREGISTER_PERSONALITY:
10063 return false;
10064 default:
10065 return true;
10066 }
10067}
10068
Jens Axboeedafcce2019-01-09 09:16:05 -070010069static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10070 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010071 __releases(ctx->uring_lock)
10072 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010073{
10074 int ret;
10075
Jens Axboe35fa71a2019-04-22 10:23:23 -060010076 /*
10077 * We're inside the ring mutex, if the ref is already dying, then
10078 * someone else killed the ctx or is already going through
10079 * io_uring_register().
10080 */
10081 if (percpu_ref_is_dying(&ctx->refs))
10082 return -ENXIO;
10083
Jens Axboe071698e2020-01-28 10:04:42 -070010084 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010085 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010086
Jens Axboe05f3fb32019-12-09 11:22:50 -070010087 /*
10088 * Drop uring mutex before waiting for references to exit. If
10089 * another thread is currently inside io_uring_enter() it might
10090 * need to grab the uring_lock to make progress. If we hold it
10091 * here across the drain wait, then we can deadlock. It's safe
10092 * to drop the mutex here, since no new references will come in
10093 * after we've killed the percpu ref.
10094 */
10095 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010096 do {
10097 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10098 if (!ret)
10099 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010100 ret = io_run_task_work_sig();
10101 if (ret < 0)
10102 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010103 } while (1);
10104
Jens Axboe05f3fb32019-12-09 11:22:50 -070010105 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010106
Pavel Begunkov88f171a2021-02-20 18:03:50 +000010107 if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp))
10108 return ret;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010109 }
10110
10111 if (ctx->restricted) {
10112 if (opcode >= IORING_REGISTER_LAST) {
10113 ret = -EINVAL;
10114 goto out;
10115 }
10116
10117 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10118 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010119 goto out;
10120 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010121 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010122
10123 switch (opcode) {
10124 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010125 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010126 break;
10127 case IORING_UNREGISTER_BUFFERS:
10128 ret = -EINVAL;
10129 if (arg || nr_args)
10130 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010131 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010132 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010133 case IORING_REGISTER_FILES:
10134 ret = io_sqe_files_register(ctx, arg, nr_args);
10135 break;
10136 case IORING_UNREGISTER_FILES:
10137 ret = -EINVAL;
10138 if (arg || nr_args)
10139 break;
10140 ret = io_sqe_files_unregister(ctx);
10141 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010142 case IORING_REGISTER_FILES_UPDATE:
10143 ret = io_sqe_files_update(ctx, arg, nr_args);
10144 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010145 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010146 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010147 ret = -EINVAL;
10148 if (nr_args != 1)
10149 break;
10150 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010151 if (ret)
10152 break;
10153 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10154 ctx->eventfd_async = 1;
10155 else
10156 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010157 break;
10158 case IORING_UNREGISTER_EVENTFD:
10159 ret = -EINVAL;
10160 if (arg || nr_args)
10161 break;
10162 ret = io_eventfd_unregister(ctx);
10163 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010164 case IORING_REGISTER_PROBE:
10165 ret = -EINVAL;
10166 if (!arg || nr_args > 256)
10167 break;
10168 ret = io_probe(ctx, arg, nr_args);
10169 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010170 case IORING_REGISTER_PERSONALITY:
10171 ret = -EINVAL;
10172 if (arg || nr_args)
10173 break;
10174 ret = io_register_personality(ctx);
10175 break;
10176 case IORING_UNREGISTER_PERSONALITY:
10177 ret = -EINVAL;
10178 if (arg)
10179 break;
10180 ret = io_unregister_personality(ctx, nr_args);
10181 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010182 case IORING_REGISTER_ENABLE_RINGS:
10183 ret = -EINVAL;
10184 if (arg || nr_args)
10185 break;
10186 ret = io_register_enable_rings(ctx);
10187 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010188 case IORING_REGISTER_RESTRICTIONS:
10189 ret = io_register_restrictions(ctx, arg, nr_args);
10190 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010191 default:
10192 ret = -EINVAL;
10193 break;
10194 }
10195
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010196out:
Jens Axboe071698e2020-01-28 10:04:42 -070010197 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010198 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010199 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010200 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010201 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010202 return ret;
10203}
10204
10205SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10206 void __user *, arg, unsigned int, nr_args)
10207{
10208 struct io_ring_ctx *ctx;
10209 long ret = -EBADF;
10210 struct fd f;
10211
10212 f = fdget(fd);
10213 if (!f.file)
10214 return -EBADF;
10215
10216 ret = -EOPNOTSUPP;
10217 if (f.file->f_op != &io_uring_fops)
10218 goto out_fput;
10219
10220 ctx = f.file->private_data;
10221
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010222 io_run_task_work();
10223
Jens Axboeedafcce2019-01-09 09:16:05 -070010224 mutex_lock(&ctx->uring_lock);
10225 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10226 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010227 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10228 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010229out_fput:
10230 fdput(f);
10231 return ret;
10232}
10233
Jens Axboe2b188cc2019-01-07 10:46:33 -070010234static int __init io_uring_init(void)
10235{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010236#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10237 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10238 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10239} while (0)
10240
10241#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10242 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10243 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10244 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10245 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10246 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10247 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10248 BUILD_BUG_SQE_ELEM(8, __u64, off);
10249 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10250 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010251 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010252 BUILD_BUG_SQE_ELEM(24, __u32, len);
10253 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10254 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10255 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10256 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010257 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10258 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010259 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10260 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10261 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10262 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10263 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10264 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10265 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10266 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010267 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010268 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10269 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10270 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010271 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010272
Jens Axboed3656342019-12-18 09:50:26 -070010273 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010274 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -070010275 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10276 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010277 return 0;
10278};
10279__initcall(io_uring_init);