blob: 9d22ec9d94066373d6d896c5ad22dcd00e1b5820 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
Jens Axboe52de1fe2020-02-27 10:15:42 -070047#include <net/compat.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070048#include <linux/refcount.h>
49#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030050#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070051
52#include <linux/sched/signal.h>
53#include <linux/fs.h>
54#include <linux/file.h>
55#include <linux/fdtable.h>
56#include <linux/mm.h>
57#include <linux/mman.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070058#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboeb41e9852020-02-17 09:52:41 -070080#include <linux/task_work.h>
Jens Axboebcf5a062020-05-22 09:24:42 -060081#include <linux/pagemap.h>
Jens Axboe0f212202020-09-13 13:09:39 -060082#include <linux/io_uring.h>
Dennis Zhou91d8f512020-09-16 13:41:05 -070083#include <linux/blk-cgroup.h>
Jens Axboe4ea33a92020-10-15 13:46:44 -060084#include <linux/audit.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020086#define CREATE_TRACE_POINTS
87#include <trace/events/io_uring.h>
88
Jens Axboe2b188cc2019-01-07 10:46:33 -070089#include <uapi/linux/io_uring.h>
90
91#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060092#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070093
Daniel Xu5277dea2019-09-14 14:23:45 -070094#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060095#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060096
97/*
98 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
99 */
100#define IORING_FILE_TABLE_SHIFT 9
101#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
102#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
103#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200104#define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \
105 IORING_REGISTER_LAST + IORING_OP_LAST)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700106
Pavel Begunkovb16fed662021-02-18 18:29:40 +0000107#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
108 IOSQE_IO_HARDLINK | IOSQE_ASYNC | \
109 IOSQE_BUFFER_SELECT)
110
Jens Axboe2b188cc2019-01-07 10:46:33 -0700111struct io_uring {
112 u32 head ____cacheline_aligned_in_smp;
113 u32 tail ____cacheline_aligned_in_smp;
114};
115
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 * This data is shared with the application through the mmap at offsets
118 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 *
120 * The offsets to the member fields are published through struct
121 * io_sqring_offsets when calling io_uring_setup.
122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Head and tail offsets into the ring; the offsets need to be
126 * masked to get valid indices.
127 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000128 * The kernel controls head of the sq ring and the tail of the cq ring,
129 * and the application controls tail of the sq ring and the head of the
130 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 * ring_entries - 1)
136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 u32 sq_ring_mask, cq_ring_mask;
138 /* Ring sizes (constant, power of 2) */
139 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 /*
141 * Number of invalid entries dropped by the kernel due to
142 * invalid index stored in array
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application (i.e. get number of "new events" by comparing to
146 * cached value).
147 *
148 * After a new SQ head value was read by the application this
149 * counter includes all submissions that were dropped reaching
150 * the new SQ head (and possibly more).
151 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000152 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200154 * Runtime SQ flags
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 *
156 * Written by the kernel, shouldn't be modified by the
157 * application.
158 *
159 * The application needs a full memory barrier before checking
160 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
161 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000162 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200163 /*
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +0200164 * Runtime CQ flags
165 *
166 * Written by the application, shouldn't be modified by the
167 * kernel.
168 */
169 u32 cq_flags;
170 /*
Stefan Bühler1e84b972019-04-24 23:54:16 +0200171 * Number of completion events lost because the queue was full;
172 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800173 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200174 * the completion queue.
175 *
176 * Written by the kernel, shouldn't be modified by the
177 * application (i.e. get number of "new events" by comparing to
178 * cached value).
179 *
180 * As completion events come in out of order this counter is not
181 * ordered with any other data.
182 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000183 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200184 /*
185 * Ring buffer of completion events.
186 *
187 * The kernel writes completion events fresh every time they are
188 * produced, so the application is allowed to modify pending
189 * entries.
190 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000191 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700192};
193
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000194enum io_uring_cmd_flags {
195 IO_URING_F_NONBLOCK = 1,
Pavel Begunkov889fca72021-02-10 00:03:09 +0000196 IO_URING_F_COMPLETE_DEFER = 2,
Pavel Begunkov45d189c2021-02-10 00:03:07 +0000197};
198
Jens Axboeedafcce2019-01-09 09:16:05 -0700199struct io_mapped_ubuf {
200 u64 ubuf;
201 size_t len;
202 struct bio_vec *bvec;
203 unsigned int nr_bvecs;
Jens Axboede293932020-09-17 16:19:16 -0600204 unsigned long acct_pages;
Jens Axboeedafcce2019-01-09 09:16:05 -0700205};
206
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000207struct io_ring_ctx;
208
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000209struct io_rsrc_put {
210 struct list_head list;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000211 union {
212 void *rsrc;
213 struct file *file;
214 };
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000215};
216
217struct fixed_rsrc_table {
Jens Axboe65e19f52019-10-26 07:20:21 -0600218 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700219};
220
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000221struct fixed_rsrc_ref_node {
Xiaoguang Wang05589552020-03-31 14:05:18 +0800222 struct percpu_ref refs;
223 struct list_head node;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000224 struct list_head rsrc_list;
225 struct fixed_rsrc_data *rsrc_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +0000226 void (*rsrc_put)(struct io_ring_ctx *ctx,
227 struct io_rsrc_put *prsrc);
Jens Axboe4a38aed22020-05-14 17:21:15 -0600228 struct llist_node llist;
Pavel Begunkove2978222020-11-18 14:56:26 +0000229 bool done;
Xiaoguang Wang05589552020-03-31 14:05:18 +0800230};
231
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000232struct fixed_rsrc_data {
233 struct fixed_rsrc_table *table;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700234 struct io_ring_ctx *ctx;
235
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000236 struct fixed_rsrc_ref_node *node;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700237 struct percpu_ref refs;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700238 struct completion done;
Hao Xu8bad28d2021-02-19 17:19:36 +0800239 bool quiesce;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700240};
241
Jens Axboe5a2e7452020-02-23 16:23:11 -0700242struct io_buffer {
243 struct list_head list;
244 __u64 addr;
245 __s32 len;
246 __u16 bid;
247};
248
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200249struct io_restriction {
250 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
251 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
252 u8 sqe_flags_allowed;
253 u8 sqe_flags_required;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +0200254 bool registered;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200255};
256
Jens Axboe534ca6d2020-09-02 13:52:19 -0600257struct io_sq_data {
258 refcount_t refs;
Jens Axboe69fb2132020-09-14 11:16:23 -0600259 struct mutex lock;
260
261 /* ctx's that are using this sqd */
262 struct list_head ctx_list;
263 struct list_head ctx_new_list;
264 struct mutex ctx_lock;
265
Jens Axboe534ca6d2020-09-02 13:52:19 -0600266 struct task_struct *thread;
267 struct wait_queue_head wait;
Xiaoguang Wang08369242020-11-03 14:15:59 +0800268
269 unsigned sq_thread_idle;
Jens Axboe534ca6d2020-09-02 13:52:19 -0600270};
271
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000272#define IO_IOPOLL_BATCH 8
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000273#define IO_COMPL_BATCH 32
Pavel Begunkov6ff119a2021-02-10 00:03:18 +0000274#define IO_REQ_CACHE_SIZE 32
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000275#define IO_REQ_ALLOC_BATCH 8
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000276
277struct io_comp_state {
Pavel Begunkov6dd0be12021-02-10 00:03:13 +0000278 struct io_kiocb *reqs[IO_COMPL_BATCH];
Jens Axboe1b4c3512021-02-10 00:03:19 +0000279 unsigned int nr;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700280 unsigned int locked_free_nr;
281 /* inline/task_work completion list, under ->uring_lock */
Jens Axboe1b4c3512021-02-10 00:03:19 +0000282 struct list_head free_list;
Jens Axboec7dae4b2021-02-09 19:53:37 -0700283 /* IRQ completion list, under ->completion_lock */
284 struct list_head locked_free_list;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000285};
286
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000287struct io_submit_link {
288 struct io_kiocb *head;
289 struct io_kiocb *last;
290};
291
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000292struct io_submit_state {
293 struct blk_plug plug;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +0000294 struct io_submit_link link;
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000295
296 /*
297 * io_kiocb alloc cache
298 */
Pavel Begunkovbf019da2021-02-10 00:03:17 +0000299 void *reqs[IO_REQ_CACHE_SIZE];
Pavel Begunkov258b29a2021-02-10 00:03:10 +0000300 unsigned int free_reqs;
301
302 bool plug_started;
303
304 /*
305 * Batch completion logic
306 */
307 struct io_comp_state comp;
308
309 /*
310 * File reference cache
311 */
312 struct file *file;
313 unsigned int fd;
314 unsigned int file_refs;
315 unsigned int ios_left;
316};
317
Jens Axboe2b188cc2019-01-07 10:46:33 -0700318struct io_ring_ctx {
319 struct {
320 struct percpu_ref refs;
321 } ____cacheline_aligned_in_smp;
322
323 struct {
324 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800325 unsigned int compat: 1;
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -0700326 unsigned int limit_mem: 1;
Randy Dunlape1d85332020-02-05 20:57:10 -0800327 unsigned int cq_overflow_flushed: 1;
328 unsigned int drain_next: 1;
329 unsigned int eventfd_async: 1;
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200330 unsigned int restricted: 1;
Pavel Begunkovd9d05212021-01-08 20:57:25 +0000331 unsigned int sqo_dead: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332
Hristo Venev75b28af2019-08-26 17:23:46 +0000333 /*
334 * Ring buffer of indices into array of io_uring_sqe, which is
335 * mmapped by the application using the IORING_OFF_SQES offset.
336 *
337 * This indirection could e.g. be used to assign fixed
338 * io_uring_sqe entries to operations and only submit them to
339 * the queue when needed.
340 *
341 * The kernel modifies neither the indices array nor the entries
342 * array.
343 */
344 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345 unsigned cached_sq_head;
346 unsigned sq_entries;
347 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700348 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600349 unsigned cached_sq_dropped;
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +0100350 unsigned cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700351 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600352
353 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600354 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700355 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356
Jens Axboead3eb2c2019-12-18 17:12:20 -0700357 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700358 } ____cacheline_aligned_in_smp;
359
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700360 struct {
361 struct mutex uring_lock;
362 wait_queue_head_t wait;
363 } ____cacheline_aligned_in_smp;
364
365 struct io_submit_state submit_state;
366
Hristo Venev75b28af2019-08-26 17:23:46 +0000367 struct io_rings *rings;
368
Jens Axboe2aede0e2020-09-14 10:45:53 -0600369 /*
370 * For SQPOLL usage - we hold a reference to the parent task, so we
371 * have access to the ->files
372 */
373 struct task_struct *sqo_task;
374
375 /* Only used for accounting purposes */
376 struct mm_struct *mm_account;
377
Dennis Zhou91d8f512020-09-16 13:41:05 -0700378#ifdef CONFIG_BLK_CGROUP
379 struct cgroup_subsys_state *sqo_blkcg_css;
380#endif
381
Jens Axboe534ca6d2020-09-02 13:52:19 -0600382 struct io_sq_data *sq_data; /* if using sq thread polling */
383
Jens Axboe90554202020-09-03 12:12:41 -0600384 struct wait_queue_head sqo_sq_wait;
Jens Axboe69fb2132020-09-14 11:16:23 -0600385 struct list_head sqd_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700386
Jens Axboe6b063142019-01-10 22:13:58 -0700387 /*
388 * If used, fixed file set. Writers must ensure that ->refs is dead,
389 * readers must ensure that ->refs is alive as long as the file* is
390 * used. Only updated through io_uring_register(2).
391 */
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000392 struct fixed_rsrc_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700393 unsigned nr_user_files;
394
Jens Axboeedafcce2019-01-09 09:16:05 -0700395 /* if used, fixed mapped user buffers */
396 unsigned nr_user_bufs;
397 struct io_mapped_ubuf *user_bufs;
398
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399 struct user_struct *user;
400
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700401 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700402
Jens Axboe4ea33a92020-10-15 13:46:44 -0600403#ifdef CONFIG_AUDIT
404 kuid_t loginuid;
405 unsigned int sessionid;
406#endif
407
Jens Axboe0f158b42020-05-14 17:18:39 -0600408 struct completion ref_comp;
409 struct completion sq_thread_comp;
Jens Axboe206aefd2019-11-07 18:27:42 -0700410
411#if defined(CONFIG_UNIX)
412 struct socket *ring_sock;
413#endif
414
Jens Axboe5a2e7452020-02-23 16:23:11 -0700415 struct idr io_buffer_idr;
416
Jens Axboe071698e2020-01-28 10:04:42 -0700417 struct idr personality_idr;
418
Jens Axboe206aefd2019-11-07 18:27:42 -0700419 struct {
420 unsigned cached_cq_tail;
421 unsigned cq_entries;
422 unsigned cq_mask;
423 atomic_t cq_timeouts;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -0500424 unsigned cq_last_tm_flush;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700425 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700426 struct wait_queue_head cq_wait;
427 struct fasync_struct *cq_fasync;
428 struct eventfd_ctx *cq_ev_fd;
429 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430
431 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700433
Jens Axboedef596e2019-01-09 08:59:42 -0700434 /*
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300435 * ->iopoll_list is protected by the ctx->uring_lock for
Jens Axboedef596e2019-01-09 08:59:42 -0700436 * io_uring instances that don't use IORING_SETUP_SQPOLL.
437 * For SQPOLL, only the single threaded io_sq_thread() will
438 * manipulate the list, hence no extra locking is needed there.
439 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +0300440 struct list_head iopoll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700441 struct hlist_head *cancel_hash;
442 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700443 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600444
445 spinlock_t inflight_lock;
446 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700447 } ____cacheline_aligned_in_smp;
Jens Axboe85faa7b2020-04-09 18:14:00 -0600448
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000449 struct delayed_work rsrc_put_work;
450 struct llist_head rsrc_put_llist;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +0000451 struct list_head rsrc_ref_list;
452 spinlock_t rsrc_ref_lock;
Jens Axboe4a38aed22020-05-14 17:21:15 -0600453
Stefano Garzarella21b55db2020-08-27 16:58:30 +0200454 struct io_restriction restrictions;
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700455
Jens Axboe7c25c0d2021-02-16 07:17:00 -0700456 /* exit task_work */
457 struct callback_head *exit_task_work;
458
Jens Axboe3c1a2ea2021-02-11 10:48:03 -0700459 /* Keep this last, we don't need it for the fast path */
460 struct work_struct exit_work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461};
462
Jens Axboe09bb8392019-03-13 12:39:28 -0600463/*
464 * First field must be the file pointer in all the
465 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
466 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700467struct io_poll_iocb {
468 struct file *file;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000469 struct wait_queue_head *head;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700470 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600471 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700472 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700473 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700474};
475
Pavel Begunkov018043b2020-10-27 23:17:18 +0000476struct io_poll_remove {
477 struct file *file;
478 u64 addr;
479};
480
Jens Axboeb5dba592019-12-11 14:02:38 -0700481struct io_close {
482 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -0700483 int fd;
484};
485
Jens Axboead8a48a2019-11-15 08:49:11 -0700486struct io_timeout_data {
487 struct io_kiocb *req;
488 struct hrtimer timer;
489 struct timespec64 ts;
490 enum hrtimer_mode mode;
491};
492
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700493struct io_accept {
494 struct file *file;
495 struct sockaddr __user *addr;
496 int __user *addr_len;
497 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600498 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700499};
500
501struct io_sync {
502 struct file *file;
503 loff_t len;
504 loff_t off;
505 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700506 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700507};
508
Jens Axboefbf23842019-12-17 18:45:56 -0700509struct io_cancel {
510 struct file *file;
511 u64 addr;
512};
513
Jens Axboeb29472e2019-12-17 18:50:29 -0700514struct io_timeout {
515 struct file *file;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +0300516 u32 off;
517 u32 target_seq;
Pavel Begunkov135fcde2020-07-13 23:37:12 +0300518 struct list_head list;
Pavel Begunkov90cd7e42020-10-27 23:25:36 +0000519 /* head of the link, used by linked timeouts only */
520 struct io_kiocb *head;
Jens Axboeb29472e2019-12-17 18:50:29 -0700521};
522
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100523struct io_timeout_rem {
524 struct file *file;
525 u64 addr;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000526
527 /* timeout update */
528 struct timespec64 ts;
529 u32 flags;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100530};
531
Jens Axboe9adbd452019-12-20 08:45:55 -0700532struct io_rw {
533 /* NOTE: kiocb has the file as the first member, so don't do it here */
534 struct kiocb kiocb;
535 u64 addr;
536 u64 len;
537};
538
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700539struct io_connect {
540 struct file *file;
541 struct sockaddr __user *addr;
542 int addr_len;
543};
544
Jens Axboee47293f2019-12-20 08:58:21 -0700545struct io_sr_msg {
546 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700547 union {
Pavel Begunkov270a5942020-07-12 20:41:04 +0300548 struct user_msghdr __user *umsg;
Jens Axboefddafac2020-01-04 20:19:44 -0700549 void __user *buf;
550 };
Jens Axboee47293f2019-12-20 08:58:21 -0700551 int msg_flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700552 int bgid;
Jens Axboefddafac2020-01-04 20:19:44 -0700553 size_t len;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700554 struct io_buffer *kbuf;
Jens Axboee47293f2019-12-20 08:58:21 -0700555};
556
Jens Axboe15b71ab2019-12-11 11:20:36 -0700557struct io_open {
558 struct file *file;
559 int dfd;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700560 struct filename *filename;
Jens Axboec12cedf2020-01-08 17:41:21 -0700561 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600562 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700563};
564
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000565struct io_rsrc_update {
Jens Axboe05f3fb32019-12-09 11:22:50 -0700566 struct file *file;
567 u64 arg;
568 u32 nr_args;
569 u32 offset;
570};
571
Jens Axboe4840e412019-12-25 22:03:45 -0700572struct io_fadvise {
573 struct file *file;
574 u64 offset;
575 u32 len;
576 u32 advice;
577};
578
Jens Axboec1ca7572019-12-25 22:18:28 -0700579struct io_madvise {
580 struct file *file;
581 u64 addr;
582 u32 len;
583 u32 advice;
584};
585
Jens Axboe3e4827b2020-01-08 15:18:09 -0700586struct io_epoll {
587 struct file *file;
588 int epfd;
589 int op;
590 int fd;
591 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700592};
593
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300594struct io_splice {
595 struct file *file_out;
596 struct file *file_in;
597 loff_t off_out;
598 loff_t off_in;
599 u64 len;
600 unsigned int flags;
601};
602
Jens Axboeddf0322d2020-02-23 16:41:33 -0700603struct io_provide_buf {
604 struct file *file;
605 __u64 addr;
606 __s32 len;
607 __u32 bgid;
608 __u16 nbufs;
609 __u16 bid;
610};
611
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700612struct io_statx {
613 struct file *file;
614 int dfd;
615 unsigned int mask;
616 unsigned int flags;
Bijan Mottahedehe62753e2020-05-22 21:31:18 -0700617 const char __user *filename;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700618 struct statx __user *buffer;
619};
620
Jens Axboe36f4fa62020-09-05 11:14:22 -0600621struct io_shutdown {
622 struct file *file;
623 int how;
624};
625
Jens Axboe80a261f2020-09-28 14:23:58 -0600626struct io_rename {
627 struct file *file;
628 int old_dfd;
629 int new_dfd;
630 struct filename *oldpath;
631 struct filename *newpath;
632 int flags;
633};
634
Jens Axboe14a11432020-09-28 14:27:37 -0600635struct io_unlink {
636 struct file *file;
637 int dfd;
638 int flags;
639 struct filename *filename;
640};
641
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300642struct io_completion {
643 struct file *file;
644 struct list_head list;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +0300645 int cflags;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300646};
647
Jens Axboef499a022019-12-02 16:28:46 -0700648struct io_async_connect {
649 struct sockaddr_storage address;
650};
651
Jens Axboe03b12302019-12-02 18:50:25 -0700652struct io_async_msghdr {
653 struct iovec fast_iov[UIO_FASTIOV];
Pavel Begunkov257e84a2021-02-05 00:58:00 +0000654 /* points to an allocated iov, if NULL we use fast_iov instead */
655 struct iovec *free_iov;
Jens Axboe03b12302019-12-02 18:50:25 -0700656 struct sockaddr __user *uaddr;
657 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700658 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700659};
660
Jens Axboef67676d2019-12-02 11:03:47 -0700661struct io_async_rw {
662 struct iovec fast_iov[UIO_FASTIOV];
Jens Axboeff6165b2020-08-13 09:47:43 -0600663 const struct iovec *free_iovec;
664 struct iov_iter iter;
Jens Axboe227c0c92020-08-13 11:51:40 -0600665 size_t bytes_done;
Jens Axboebcf5a062020-05-22 09:24:42 -0600666 struct wait_page_queue wpq;
Jens Axboef67676d2019-12-02 11:03:47 -0700667};
668
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300669enum {
670 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
671 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
672 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
673 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
674 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700675 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300676
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300677 REQ_F_FAIL_LINK_BIT,
678 REQ_F_INFLIGHT_BIT,
679 REQ_F_CUR_POS_BIT,
680 REQ_F_NOWAIT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300681 REQ_F_LINK_TIMEOUT_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300682 REQ_F_ISREG_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300683 REQ_F_NEED_CLEANUP_BIT,
Jens Axboed7718a92020-02-14 22:23:12 -0700684 REQ_F_POLLED_BIT,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700685 REQ_F_BUFFER_SELECTED_BIT,
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600686 REQ_F_NO_FILE_TABLE_BIT,
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800687 REQ_F_WORK_INITIALIZED_BIT,
Pavel Begunkov900fad42020-10-19 16:39:16 +0100688 REQ_F_LTIMEOUT_ACTIVE_BIT,
Pavel Begunkove342c802021-01-19 13:32:47 +0000689 REQ_F_COMPLETE_INLINE_BIT,
Jens Axboe84557872020-03-03 15:28:17 -0700690
691 /* not a real bit, just to check we're not overflowing the space */
692 __REQ_F_LAST_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300693};
694
695enum {
696 /* ctx owns file */
697 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
698 /* drain existing IO first */
699 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
700 /* linked sqes */
701 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
702 /* doesn't sever on completion < 0 */
703 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
704 /* IOSQE_ASYNC */
705 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700706 /* IOSQE_BUFFER_SELECT */
707 REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300708
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300709 /* fail rest of links */
710 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
711 /* on inflight list */
712 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
713 /* read/write uses file position */
714 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
715 /* must not punt to workers */
716 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100717 /* has or had linked timeout */
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300718 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300719 /* regular file */
720 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300721 /* needs cleanup */
722 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700723 /* already went through poll handler */
724 REQ_F_POLLED = BIT(REQ_F_POLLED_BIT),
Jens Axboebcda7ba2020-02-23 16:42:51 -0700725 /* buffer already selected */
726 REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT),
Jens Axboe5b0bbee2020-04-27 10:41:22 -0600727 /* doesn't need file table for this request */
728 REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT),
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +0800729 /* io_wq_work is initialized */
730 REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT),
Pavel Begunkov900fad42020-10-19 16:39:16 +0100731 /* linked timeout is active, i.e. prepared by link's head */
732 REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT),
Pavel Begunkove342c802021-01-19 13:32:47 +0000733 /* completion is deferred through io_comp_state */
734 REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT),
Jens Axboed7718a92020-02-14 22:23:12 -0700735};
736
737struct async_poll {
738 struct io_poll_iocb poll;
Jens Axboe807abcb2020-07-17 17:09:27 -0600739 struct io_poll_iocb *double_poll;
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300740};
741
Jens Axboe7cbf1722021-02-10 00:03:20 +0000742struct io_task_work {
743 struct io_wq_work_node node;
744 task_work_func_t func;
745};
746
Jens Axboe09bb8392019-03-13 12:39:28 -0600747/*
748 * NOTE! Each of the iocb union members has the file pointer
749 * as the first entry in their struct definition. So you can
750 * access the file pointer through any of the sub-structs,
751 * or directly as just 'ki_filp' in this struct.
752 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700753struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700754 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600755 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700756 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700757 struct io_poll_iocb poll;
Pavel Begunkov018043b2020-10-27 23:17:18 +0000758 struct io_poll_remove poll_remove;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700759 struct io_accept accept;
760 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700761 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700762 struct io_timeout timeout;
Pavel Begunkov0bdf7a22020-10-10 18:34:10 +0100763 struct io_timeout_rem timeout_rem;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700764 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700765 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700766 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700767 struct io_close close;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000768 struct io_rsrc_update rsrc_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700769 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700770 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700771 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300772 struct io_splice splice;
Jens Axboeddf0322d2020-02-23 16:41:33 -0700773 struct io_provide_buf pbuf;
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -0700774 struct io_statx statx;
Jens Axboe36f4fa62020-09-05 11:14:22 -0600775 struct io_shutdown shutdown;
Jens Axboe80a261f2020-09-28 14:23:58 -0600776 struct io_rename rename;
Jens Axboe14a11432020-09-28 14:27:37 -0600777 struct io_unlink unlink;
Pavel Begunkov3ca405e2020-07-13 23:37:08 +0300778 /* use only after cleaning per-op data, see io_clean_op() */
779 struct io_completion compl;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700780 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700781
Jens Axboee8c2bc12020-08-15 18:44:09 -0700782 /* opcode allocated if it needs to store data for async defer */
783 void *async_data;
Jens Axboed625c6e2019-12-17 19:53:05 -0700784 u8 opcode;
Xiaoguang Wang65a65432020-06-11 23:39:36 +0800785 /* polled IO has completed */
786 u8 iopoll_completed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700788 u16 buf_index;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300789 u32 result;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -0700790
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300791 struct io_ring_ctx *ctx;
792 unsigned int flags;
793 refcount_t refs;
794 struct task_struct *task;
795 u64 user_data;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700796
Pavel Begunkovf2f87372020-10-27 23:25:37 +0000797 struct io_kiocb *link;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +0000798 struct percpu_ref *fixed_rsrc_refs;
Jens Axboed7718a92020-02-14 22:23:12 -0700799
Pavel Begunkovd21ffe72020-07-13 23:37:10 +0300800 /*
801 * 1. used with ctx->iopoll_list with reads/writes
802 * 2. to track reqs with ->files (see io_op_def::file_table)
803 */
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300804 struct list_head inflight_entry;
Jens Axboe7cbf1722021-02-10 00:03:20 +0000805 union {
806 struct io_task_work io_task_work;
807 struct callback_head task_work;
808 };
Pavel Begunkov010e8e62020-07-30 18:43:45 +0300809 /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
810 struct hlist_node hash_node;
811 struct async_poll *apoll;
812 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813};
814
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300815struct io_defer_entry {
816 struct list_head list;
817 struct io_kiocb *req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +0300818 u32 seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +0300819};
820
Jens Axboed3656342019-12-18 09:50:26 -0700821struct io_op_def {
Jens Axboed3656342019-12-18 09:50:26 -0700822 /* needs req->file assigned */
823 unsigned needs_file : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700824 /* hash wq insertion if file is a regular file */
825 unsigned hash_reg_file : 1;
826 /* unbound wq insertion if file is a non-regular file */
827 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700828 /* opcode is not supported by this kernel */
829 unsigned not_supported : 1;
Jens Axboe8a727582020-02-20 09:59:44 -0700830 /* set if opcode supports polled "wait" */
831 unsigned pollin : 1;
832 unsigned pollout : 1;
Jens Axboebcda7ba2020-02-23 16:42:51 -0700833 /* op supports buffer selection */
834 unsigned buffer_select : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700835 /* must always have async data allocated */
836 unsigned needs_async_data : 1;
Jens Axboe27926b62020-10-28 09:33:23 -0600837 /* should block plug */
838 unsigned plug : 1;
Jens Axboee8c2bc12020-08-15 18:44:09 -0700839 /* size of async data needed, if any */
840 unsigned short async_size;
Jens Axboe0f203762020-10-14 09:23:55 -0600841 unsigned work_flags;
Jens Axboed3656342019-12-18 09:50:26 -0700842};
843
Jens Axboe09186822020-10-13 15:01:40 -0600844static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300845 [IORING_OP_NOP] = {},
846 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700847 .needs_file = 1,
848 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700849 .pollin = 1,
Jens Axboe4d954c22020-02-27 07:31:19 -0700850 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700851 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600852 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700853 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600854 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700855 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300856 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700857 .needs_file = 1,
858 .hash_reg_file = 1,
859 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700860 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700861 .needs_async_data = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600862 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700863 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600864 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
865 IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700866 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300867 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700868 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600869 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700870 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300871 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700872 .needs_file = 1,
873 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700874 .pollin = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600875 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700876 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600877 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700878 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300879 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700880 .needs_file = 1,
881 .hash_reg_file = 1,
882 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700883 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600884 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700885 .async_size = sizeof(struct io_async_rw),
Jens Axboe4017eb92020-10-22 14:14:12 -0600886 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE |
887 IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700888 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300889 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700890 .needs_file = 1,
891 .unbound_nonreg_file = 1,
892 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300893 [IORING_OP_POLL_REMOVE] = {},
894 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700895 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600896 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700897 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300898 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700899 .needs_file = 1,
900 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700901 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700902 .needs_async_data = 1,
903 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe92c75f72021-02-10 12:37:58 -0700904 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
905 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700906 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300907 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700908 .needs_file = 1,
909 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700910 .pollin = 1,
Jens Axboe52de1fe2020-02-27 10:15:42 -0700911 .buffer_select = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700912 .needs_async_data = 1,
913 .async_size = sizeof(struct io_async_msghdr),
Jens Axboe92c75f72021-02-10 12:37:58 -0700914 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
915 IO_WQ_WORK_FS,
Jens Axboed3656342019-12-18 09:50:26 -0700916 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300917 [IORING_OP_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700918 .needs_async_data = 1,
919 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600920 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700921 },
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +0000922 [IORING_OP_TIMEOUT_REMOVE] = {
923 /* used by timeout updates' prep() */
924 .work_flags = IO_WQ_WORK_MM,
925 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300926 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700927 .needs_file = 1,
928 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700929 .pollin = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600930 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES,
Jens Axboed3656342019-12-18 09:50:26 -0700931 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300932 [IORING_OP_ASYNC_CANCEL] = {},
933 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboee8c2bc12020-08-15 18:44:09 -0700934 .needs_async_data = 1,
935 .async_size = sizeof(struct io_timeout_data),
Jens Axboe0f203762020-10-14 09:23:55 -0600936 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700937 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300938 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700939 .needs_file = 1,
940 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700941 .pollout = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700942 .needs_async_data = 1,
943 .async_size = sizeof(struct io_async_connect),
Jens Axboe0f203762020-10-14 09:23:55 -0600944 .work_flags = IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700945 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300946 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700947 .needs_file = 1,
Jens Axboe69228332020-10-20 14:28:41 -0600948 .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE,
Jens Axboed3656342019-12-18 09:50:26 -0700949 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300950 [IORING_OP_OPENAT] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600951 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG |
Jens Axboe14587a462020-09-05 11:36:08 -0600952 IO_WQ_WORK_FS | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700953 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300954 [IORING_OP_CLOSE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600955 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700956 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300957 [IORING_OP_FILES_UPDATE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600958 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM,
Jens Axboed3656342019-12-18 09:50:26 -0700959 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300960 [IORING_OP_STATX] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600961 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM |
962 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
Jens Axboed3656342019-12-18 09:50:26 -0700963 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300964 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700965 .needs_file = 1,
966 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700967 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700968 .buffer_select = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600969 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700970 .async_size = sizeof(struct io_async_rw),
Jens Axboe0f203762020-10-14 09:23:55 -0600971 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700972 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300973 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700974 .needs_file = 1,
975 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700976 .pollout = 1,
Jens Axboe27926b62020-10-28 09:33:23 -0600977 .plug = 1,
Jens Axboee8c2bc12020-08-15 18:44:09 -0700978 .async_size = sizeof(struct io_async_rw),
Jens Axboe69228332020-10-20 14:28:41 -0600979 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG |
980 IO_WQ_WORK_FSIZE,
Jens Axboe3a6820f2019-12-22 15:19:35 -0700981 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300982 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700983 .needs_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600984 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboe4840e412019-12-25 22:03:45 -0700985 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300986 [IORING_OP_MADVISE] = {
Jens Axboe0f203762020-10-14 09:23:55 -0600987 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboec1ca7572019-12-25 22:18:28 -0700988 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300989 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700990 .needs_file = 1,
991 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700992 .pollout = 1,
Jens Axboe0f203762020-10-14 09:23:55 -0600993 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -0700994 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300995 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700996 .needs_file = 1,
997 .unbound_nonreg_file = 1,
Jens Axboe8a727582020-02-20 09:59:44 -0700998 .pollin = 1,
Jens Axboebcda7ba2020-02-23 16:42:51 -0700999 .buffer_select = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001000 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG,
Jens Axboefddafac2020-01-04 20:19:44 -07001001 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +03001002 [IORING_OP_OPENAT2] = {
Jens Axboe0f203762020-10-14 09:23:55 -06001003 .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS |
Jens Axboe14587a462020-09-05 11:36:08 -06001004 IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM,
Jens Axboecebdb982020-01-08 17:59:24 -07001005 },
Jens Axboe3e4827b2020-01-08 15:18:09 -07001006 [IORING_OP_EPOLL_CTL] = {
1007 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001008 .work_flags = IO_WQ_WORK_FILES,
Jens Axboe3e4827b2020-01-08 15:18:09 -07001009 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +03001010 [IORING_OP_SPLICE] = {
1011 .needs_file = 1,
1012 .hash_reg_file = 1,
1013 .unbound_nonreg_file = 1,
Jens Axboe0f203762020-10-14 09:23:55 -06001014 .work_flags = IO_WQ_WORK_BLKCG,
Jens Axboeddf0322d2020-02-23 16:41:33 -07001015 },
1016 [IORING_OP_PROVIDE_BUFFERS] = {},
Jens Axboe067524e2020-03-02 16:32:28 -07001017 [IORING_OP_REMOVE_BUFFERS] = {},
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03001018 [IORING_OP_TEE] = {
1019 .needs_file = 1,
1020 .hash_reg_file = 1,
1021 .unbound_nonreg_file = 1,
1022 },
Jens Axboe36f4fa62020-09-05 11:14:22 -06001023 [IORING_OP_SHUTDOWN] = {
1024 .needs_file = 1,
1025 },
Jens Axboe80a261f2020-09-28 14:23:58 -06001026 [IORING_OP_RENAMEAT] = {
1027 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1028 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1029 },
Jens Axboe14a11432020-09-28 14:27:37 -06001030 [IORING_OP_UNLINKAT] = {
1031 .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES |
1032 IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG,
1033 },
Jens Axboed3656342019-12-18 09:50:26 -07001034};
1035
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00001036static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
1037 struct task_struct *task,
1038 struct files_struct *files);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001039static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node);
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00001040static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001041 struct io_ring_ctx *ctx);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00001042static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00001043
Pavel Begunkov23faba32021-02-11 18:28:22 +00001044static bool io_rw_reissue(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001045static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001046static void io_put_req(struct io_kiocb *req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001047static void io_put_req_deferred(struct io_kiocb *req, int nr);
Jens Axboec40f6372020-06-25 15:39:59 -06001048static void io_double_put_req(struct io_kiocb *req);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001049static void io_dismantle_req(struct io_kiocb *req);
1050static void io_put_task(struct task_struct *task, int nr);
1051static void io_queue_next(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001052static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001053static void __io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001054static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001055static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001056 struct io_uring_rsrc_update *ip,
Jens Axboe05f3fb32019-12-09 11:22:50 -07001057 unsigned nr_args);
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001058static void __io_clean_op(struct io_kiocb *req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01001059static struct file *io_file_get(struct io_submit_state *state,
1060 struct io_kiocb *req, int fd, bool fixed);
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00001061static void __io_queue_sqe(struct io_kiocb *req);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001062static void io_rsrc_put_work(struct work_struct *work);
Jens Axboede0617e2019-04-06 21:51:27 -06001063
Pavel Begunkov847595d2021-02-04 13:52:06 +00001064static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
1065 struct iov_iter *iter, bool needs_lock);
Jens Axboeff6165b2020-08-13 09:47:43 -06001066static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
1067 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06001068 struct iov_iter *iter, bool force);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001069static void io_req_task_queue(struct io_kiocb *req);
Jens Axboe65453d12021-02-10 00:03:21 +00001070static void io_submit_flush_completions(struct io_comp_state *cs,
1071 struct io_ring_ctx *ctx);
Jens Axboe9a56a232019-01-09 09:06:50 -07001072
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073static struct kmem_cache *req_cachep;
1074
Jens Axboe09186822020-10-13 15:01:40 -06001075static const struct file_operations io_uring_fops;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076
1077struct sock *io_uring_get_socket(struct file *file)
1078{
1079#if defined(CONFIG_UNIX)
1080 if (file->f_op == &io_uring_fops) {
1081 struct io_ring_ctx *ctx = file->private_data;
1082
1083 return ctx->ring_sock->sk;
1084 }
1085#endif
1086 return NULL;
1087}
1088EXPORT_SYMBOL(io_uring_get_socket);
1089
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001090#define io_for_each_link(pos, head) \
1091 for (pos = (head); pos; pos = pos->link)
1092
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001093static inline void io_clean_op(struct io_kiocb *req)
1094{
Pavel Begunkov9d5c8192021-01-24 15:08:14 +00001095 if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED))
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03001096 __io_clean_op(req);
1097}
1098
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001099static inline void io_set_resource_node(struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001100{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001101 struct io_ring_ctx *ctx = req->ctx;
1102
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001103 if (!req->fixed_rsrc_refs) {
1104 req->fixed_rsrc_refs = &ctx->file_data->node->refs;
1105 percpu_ref_get(req->fixed_rsrc_refs);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00001106 }
1107}
1108
Pavel Begunkov88f171a2021-02-20 18:03:50 +00001109static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
1110{
1111 if (!percpu_ref_tryget(ref)) {
1112 /* already at zero, wait for ->release() */
1113 if (!try_wait_for_completion(compl))
1114 synchronize_rcu();
1115 return false;
1116 }
1117
1118 percpu_ref_resurrect(ref);
1119 reinit_completion(compl);
1120 percpu_ref_put(ref);
1121 return true;
1122}
1123
Pavel Begunkov08d23632020-11-06 13:00:22 +00001124static bool io_match_task(struct io_kiocb *head,
1125 struct task_struct *task,
1126 struct files_struct *files)
1127{
1128 struct io_kiocb *req;
1129
Jens Axboe84965ff2021-01-23 15:51:11 -07001130 if (task && head->task != task) {
1131 /* in terms of cancelation, always match if req task is dead */
1132 if (head->task->flags & PF_EXITING)
1133 return true;
Pavel Begunkov08d23632020-11-06 13:00:22 +00001134 return false;
Jens Axboe84965ff2021-01-23 15:51:11 -07001135 }
Pavel Begunkov08d23632020-11-06 13:00:22 +00001136 if (!files)
1137 return true;
1138
1139 io_for_each_link(req, head) {
Jens Axboe02a13672021-01-23 15:49:31 -07001140 if (!(req->flags & REQ_F_WORK_INITIALIZED))
1141 continue;
1142 if (req->file && req->file->f_op == &io_uring_fops)
1143 return true;
1144 if ((req->work.flags & IO_WQ_WORK_FILES) &&
Pavel Begunkov08d23632020-11-06 13:00:22 +00001145 req->work.identity->files == files)
1146 return true;
1147 }
1148 return false;
1149}
1150
Jens Axboe28cea78a2020-09-14 10:51:17 -06001151static void io_sq_thread_drop_mm_files(void)
Jens Axboec40f6372020-06-25 15:39:59 -06001152{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001153 struct files_struct *files = current->files;
Jens Axboec40f6372020-06-25 15:39:59 -06001154 struct mm_struct *mm = current->mm;
1155
1156 if (mm) {
1157 kthread_unuse_mm(mm);
1158 mmput(mm);
Jens Axboe4b70cf92020-11-02 10:39:05 -07001159 current->mm = NULL;
Jens Axboec40f6372020-06-25 15:39:59 -06001160 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001161 if (files) {
1162 struct nsproxy *nsproxy = current->nsproxy;
1163
1164 task_lock(current);
1165 current->files = NULL;
1166 current->nsproxy = NULL;
1167 task_unlock(current);
1168 put_files_struct(files);
1169 put_nsproxy(nsproxy);
1170 }
1171}
1172
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001173static int __io_sq_thread_acquire_files(struct io_ring_ctx *ctx)
Jens Axboe28cea78a2020-09-14 10:51:17 -06001174{
1175 if (!current->files) {
1176 struct files_struct *files;
1177 struct nsproxy *nsproxy;
1178
1179 task_lock(ctx->sqo_task);
1180 files = ctx->sqo_task->files;
1181 if (!files) {
1182 task_unlock(ctx->sqo_task);
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001183 return -EOWNERDEAD;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001184 }
1185 atomic_inc(&files->count);
1186 get_nsproxy(ctx->sqo_task->nsproxy);
1187 nsproxy = ctx->sqo_task->nsproxy;
1188 task_unlock(ctx->sqo_task);
1189
1190 task_lock(current);
1191 current->files = files;
1192 current->nsproxy = nsproxy;
1193 task_unlock(current);
1194 }
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001195 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001196}
1197
1198static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx)
1199{
Jens Axboe4b70cf92020-11-02 10:39:05 -07001200 struct mm_struct *mm;
1201
1202 if (current->mm)
1203 return 0;
1204
Jens Axboe4b70cf92020-11-02 10:39:05 -07001205 task_lock(ctx->sqo_task);
1206 mm = ctx->sqo_task->mm;
1207 if (unlikely(!mm || !mmget_not_zero(mm)))
1208 mm = NULL;
1209 task_unlock(ctx->sqo_task);
1210
1211 if (mm) {
1212 kthread_use_mm(mm);
1213 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001214 }
1215
Jens Axboe4b70cf92020-11-02 10:39:05 -07001216 return -EFAULT;
Jens Axboec40f6372020-06-25 15:39:59 -06001217}
1218
Pavel Begunkov4e326352021-02-12 03:23:52 +00001219static int __io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1220 struct io_kiocb *req)
Jens Axboec40f6372020-06-25 15:39:59 -06001221{
Jens Axboe28cea78a2020-09-14 10:51:17 -06001222 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001223 int ret;
Jens Axboe28cea78a2020-09-14 10:51:17 -06001224
1225 if (def->work_flags & IO_WQ_WORK_MM) {
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001226 ret = __io_sq_thread_acquire_mm(ctx);
Jens Axboe28cea78a2020-09-14 10:51:17 -06001227 if (unlikely(ret))
1228 return ret;
1229 }
1230
Pavel Begunkov1a38ffc2020-11-08 12:55:55 +00001231 if (def->needs_file || (def->work_flags & IO_WQ_WORK_FILES)) {
1232 ret = __io_sq_thread_acquire_files(ctx);
1233 if (unlikely(ret))
1234 return ret;
1235 }
Jens Axboe28cea78a2020-09-14 10:51:17 -06001236
1237 return 0;
Jens Axboec40f6372020-06-25 15:39:59 -06001238}
1239
Pavel Begunkov4e326352021-02-12 03:23:52 +00001240static inline int io_sq_thread_acquire_mm_files(struct io_ring_ctx *ctx,
1241 struct io_kiocb *req)
1242{
Pavel Begunkov4e326352021-02-12 03:23:52 +00001243 if (!(ctx->flags & IORING_SETUP_SQPOLL))
1244 return 0;
1245 return __io_sq_thread_acquire_mm_files(ctx, req);
1246}
1247
Dennis Zhou91d8f512020-09-16 13:41:05 -07001248static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx,
1249 struct cgroup_subsys_state **cur_css)
1250
1251{
1252#ifdef CONFIG_BLK_CGROUP
1253 /* puts the old one when swapping */
1254 if (*cur_css != ctx->sqo_blkcg_css) {
1255 kthread_associate_blkcg(ctx->sqo_blkcg_css);
1256 *cur_css = ctx->sqo_blkcg_css;
1257 }
1258#endif
1259}
1260
1261static void io_sq_thread_unassociate_blkcg(void)
1262{
1263#ifdef CONFIG_BLK_CGROUP
1264 kthread_associate_blkcg(NULL);
1265#endif
1266}
1267
Jens Axboec40f6372020-06-25 15:39:59 -06001268static inline void req_set_fail_links(struct io_kiocb *req)
1269{
1270 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1271 req->flags |= REQ_F_FAIL_LINK;
1272}
Jens Axboe4a38aed22020-05-14 17:21:15 -06001273
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001274/*
Jens Axboe1e6fa522020-10-15 08:46:24 -06001275 * None of these are dereferenced, they are simply used to check if any of
1276 * them have changed. If we're under current and check they are still the
1277 * same, we're fine to grab references to them for actual out-of-line use.
1278 */
1279static void io_init_identity(struct io_identity *id)
1280{
1281 id->files = current->files;
1282 id->mm = current->mm;
1283#ifdef CONFIG_BLK_CGROUP
1284 rcu_read_lock();
1285 id->blkcg_css = blkcg_css();
1286 rcu_read_unlock();
1287#endif
1288 id->creds = current_cred();
1289 id->nsproxy = current->nsproxy;
1290 id->fs = current->fs;
1291 id->fsize = rlimit(RLIMIT_FSIZE);
Jens Axboe4ea33a92020-10-15 13:46:44 -06001292#ifdef CONFIG_AUDIT
1293 id->loginuid = current->loginuid;
1294 id->sessionid = current->sessionid;
1295#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001296 refcount_set(&id->count, 1);
1297}
1298
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001299static inline void __io_req_init_async(struct io_kiocb *req)
1300{
1301 memset(&req->work, 0, sizeof(req->work));
1302 req->flags |= REQ_F_WORK_INITIALIZED;
1303}
1304
Jens Axboe1e6fa522020-10-15 08:46:24 -06001305/*
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001306 * Note: must call io_req_init_async() for the first time you
1307 * touch any members of io_wq_work.
1308 */
1309static inline void io_req_init_async(struct io_kiocb *req)
1310{
Jens Axboe500a3732020-10-15 17:38:03 -06001311 struct io_uring_task *tctx = current->io_uring;
1312
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001313 if (req->flags & REQ_F_WORK_INITIALIZED)
1314 return;
1315
Pavel Begunkovec99ca62020-10-18 10:17:38 +01001316 __io_req_init_async(req);
Jens Axboe500a3732020-10-15 17:38:03 -06001317
1318 /* Grab a ref if this isn't our static identity */
1319 req->work.identity = tctx->identity;
1320 if (tctx->identity != &tctx->__identity)
1321 refcount_inc(&req->work.identity->count);
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001322}
1323
Jens Axboe2b188cc2019-01-07 10:46:33 -07001324static void io_ring_ctx_ref_free(struct percpu_ref *ref)
1325{
1326 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
1327
Jens Axboe0f158b42020-05-14 17:18:39 -06001328 complete(&ctx->ref_comp);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329}
1330
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001331static inline bool io_is_timeout_noseq(struct io_kiocb *req)
1332{
1333 return !req->timeout.off;
1334}
1335
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
1337{
1338 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07001339 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001340
1341 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1342 if (!ctx)
1343 return NULL;
1344
Jens Axboe78076bb2019-12-04 19:56:40 -07001345 /*
1346 * Use 5 bits less than the max cq entries, that should give us around
1347 * 32 entries per hash list if totally full and uniformly spread.
1348 */
1349 hash_bits = ilog2(p->cq_entries);
1350 hash_bits -= 5;
1351 if (hash_bits <= 0)
1352 hash_bits = 1;
1353 ctx->cancel_hash_bits = hash_bits;
1354 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
1355 GFP_KERNEL);
1356 if (!ctx->cancel_hash)
1357 goto err;
1358 __hash_init(ctx->cancel_hash, 1U << hash_bits);
1359
Roman Gushchin21482892019-05-07 10:01:48 -07001360 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -07001361 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
1362 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001363
1364 ctx->flags = p->flags;
Jens Axboe90554202020-09-03 12:12:41 -06001365 init_waitqueue_head(&ctx->sqo_sq_wait);
Jens Axboe69fb2132020-09-14 11:16:23 -06001366 INIT_LIST_HEAD(&ctx->sqd_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001368 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe0f158b42020-05-14 17:18:39 -06001369 init_completion(&ctx->ref_comp);
1370 init_completion(&ctx->sq_thread_comp);
Jens Axboe5a2e7452020-02-23 16:23:11 -07001371 idr_init(&ctx->io_buffer_idr);
Jens Axboe071698e2020-01-28 10:04:42 -07001372 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001373 mutex_init(&ctx->uring_lock);
1374 init_waitqueue_head(&ctx->wait);
1375 spin_lock_init(&ctx->completion_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03001376 INIT_LIST_HEAD(&ctx->iopoll_list);
Jens Axboede0617e2019-04-06 21:51:27 -06001377 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -06001378 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -06001379 spin_lock_init(&ctx->inflight_lock);
1380 INIT_LIST_HEAD(&ctx->inflight_list);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00001381 spin_lock_init(&ctx->rsrc_ref_lock);
1382 INIT_LIST_HEAD(&ctx->rsrc_ref_list);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00001383 INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work);
1384 init_llist_head(&ctx->rsrc_put_llist);
Jens Axboe1b4c3512021-02-10 00:03:19 +00001385 INIT_LIST_HEAD(&ctx->submit_state.comp.free_list);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001386 INIT_LIST_HEAD(&ctx->submit_state.comp.locked_free_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001387 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -07001388err:
Jens Axboe78076bb2019-12-04 19:56:40 -07001389 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -07001390 kfree(ctx);
1391 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392}
1393
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001394static bool req_need_defer(struct io_kiocb *req, u32 seq)
Jens Axboede0617e2019-04-06 21:51:27 -06001395{
Jens Axboe2bc99302020-07-09 09:43:27 -06001396 if (unlikely(req->flags & REQ_F_IO_DRAIN)) {
1397 struct io_ring_ctx *ctx = req->ctx;
Jackie Liua197f662019-11-08 08:09:12 -07001398
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001399 return seq != ctx->cached_cq_tail
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001400 + READ_ONCE(ctx->cached_cq_overflow);
Jens Axboe2bc99302020-07-09 09:43:27 -06001401 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001402
Bob Liu9d858b22019-11-13 18:06:25 +08001403 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -06001404}
1405
Jens Axboe5c3462c2020-10-15 09:02:33 -06001406static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001407{
Jens Axboe500a3732020-10-15 17:38:03 -06001408 if (req->work.identity == &tctx->__identity)
Jens Axboe1e6fa522020-10-15 08:46:24 -06001409 return;
1410 if (refcount_dec_and_test(&req->work.identity->count))
1411 kfree(req->work.identity);
1412}
1413
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001414static void io_req_clean_work(struct io_kiocb *req)
Jens Axboecccf0ee2020-01-27 16:34:48 -07001415{
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08001416 if (!(req->flags & REQ_F_WORK_INITIALIZED))
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01001417 return;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001418
Pavel Begunkove86d0042021-02-01 18:59:54 +00001419 if (req->work.flags & IO_WQ_WORK_MM)
Jens Axboe98447d62020-10-14 10:48:51 -06001420 mmdrop(req->work.identity->mm);
Dennis Zhou91d8f512020-09-16 13:41:05 -07001421#ifdef CONFIG_BLK_CGROUP
Pavel Begunkove86d0042021-02-01 18:59:54 +00001422 if (req->work.flags & IO_WQ_WORK_BLKCG)
Jens Axboe98447d62020-10-14 10:48:51 -06001423 css_put(req->work.identity->blkcg_css);
Jens Axboedfead8a2020-10-14 10:12:37 -06001424#endif
Pavel Begunkove86d0042021-02-01 18:59:54 +00001425 if (req->work.flags & IO_WQ_WORK_CREDS)
Jens Axboe98447d62020-10-14 10:48:51 -06001426 put_cred(req->work.identity->creds);
Jens Axboedfead8a2020-10-14 10:12:37 -06001427 if (req->work.flags & IO_WQ_WORK_FS) {
Jens Axboe98447d62020-10-14 10:48:51 -06001428 struct fs_struct *fs = req->work.identity->fs;
Jens Axboeff002b32020-02-07 16:05:21 -07001429
Jens Axboe98447d62020-10-14 10:48:51 -06001430 spin_lock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001431 if (--fs->users)
1432 fs = NULL;
Jens Axboe98447d62020-10-14 10:48:51 -06001433 spin_unlock(&req->work.identity->fs->lock);
Jens Axboeff002b32020-02-07 16:05:21 -07001434 if (fs)
1435 free_fs_struct(fs);
1436 }
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001437 if (req->work.flags & IO_WQ_WORK_FILES) {
1438 put_files_struct(req->work.identity->files);
1439 put_nsproxy(req->work.identity->nsproxy);
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001440 }
1441 if (req->flags & REQ_F_INFLIGHT) {
1442 struct io_ring_ctx *ctx = req->ctx;
1443 struct io_uring_task *tctx = req->task->io_uring;
1444 unsigned long flags;
Jens Axboe51a4cc12020-08-10 10:55:56 -06001445
Pavel Begunkov34e08fe2021-02-01 18:59:53 +00001446 spin_lock_irqsave(&ctx->inflight_lock, flags);
1447 list_del(&req->inflight_entry);
1448 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1449 req->flags &= ~REQ_F_INFLIGHT;
1450 if (atomic_read(&tctx->in_idle))
1451 wake_up(&tctx->wait);
1452 }
Jens Axboe51a4cc12020-08-10 10:55:56 -06001453
Pavel Begunkove86d0042021-02-01 18:59:54 +00001454 req->flags &= ~REQ_F_WORK_INITIALIZED;
1455 req->work.flags &= ~(IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | IO_WQ_WORK_FS |
1456 IO_WQ_WORK_CREDS | IO_WQ_WORK_FILES);
Jens Axboe5c3462c2020-10-15 09:02:33 -06001457 io_put_identity(req->task->io_uring, req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06001458}
1459
1460/*
1461 * Create a private copy of io_identity, since some fields don't match
1462 * the current context.
1463 */
1464static bool io_identity_cow(struct io_kiocb *req)
1465{
Jens Axboe5c3462c2020-10-15 09:02:33 -06001466 struct io_uring_task *tctx = current->io_uring;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001467 const struct cred *creds = NULL;
1468 struct io_identity *id;
1469
1470 if (req->work.flags & IO_WQ_WORK_CREDS)
1471 creds = req->work.identity->creds;
1472
1473 id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL);
1474 if (unlikely(!id)) {
1475 req->work.flags |= IO_WQ_WORK_CANCEL;
1476 return false;
1477 }
1478
1479 /*
1480 * We can safely just re-init the creds we copied Either the field
1481 * matches the current one, or we haven't grabbed it yet. The only
1482 * exception is ->creds, through registered personalities, so handle
1483 * that one separately.
1484 */
1485 io_init_identity(id);
1486 if (creds)
Pavel Begunkove8c954d2020-12-06 22:22:46 +00001487 id->creds = creds;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001488
1489 /* add one for this request */
1490 refcount_inc(&id->count);
1491
Jens Axboecb8a8ae2020-11-03 12:19:07 -07001492 /* drop tctx and req identity references, if needed */
1493 if (tctx->identity != &tctx->__identity &&
1494 refcount_dec_and_test(&tctx->identity->count))
1495 kfree(tctx->identity);
1496 if (req->work.identity != &tctx->__identity &&
1497 refcount_dec_and_test(&req->work.identity->count))
Jens Axboe1e6fa522020-10-15 08:46:24 -06001498 kfree(req->work.identity);
1499
1500 req->work.identity = id;
Jens Axboe500a3732020-10-15 17:38:03 -06001501 tctx->identity = id;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001502 return true;
1503}
1504
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001505static void io_req_track_inflight(struct io_kiocb *req)
1506{
1507 struct io_ring_ctx *ctx = req->ctx;
1508
1509 if (!(req->flags & REQ_F_INFLIGHT)) {
1510 io_req_init_async(req);
1511 req->flags |= REQ_F_INFLIGHT;
1512
1513 spin_lock_irq(&ctx->inflight_lock);
1514 list_add(&req->inflight_entry, &ctx->inflight_list);
1515 spin_unlock_irq(&ctx->inflight_lock);
1516 }
1517}
1518
Jens Axboe1e6fa522020-10-15 08:46:24 -06001519static bool io_grab_identity(struct io_kiocb *req)
1520{
1521 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe5c3462c2020-10-15 09:02:33 -06001522 struct io_identity *id = req->work.identity;
Jens Axboe1e6fa522020-10-15 08:46:24 -06001523
Jens Axboe69228332020-10-20 14:28:41 -06001524 if (def->work_flags & IO_WQ_WORK_FSIZE) {
1525 if (id->fsize != rlimit(RLIMIT_FSIZE))
1526 return false;
1527 req->work.flags |= IO_WQ_WORK_FSIZE;
1528 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001529#ifdef CONFIG_BLK_CGROUP
1530 if (!(req->work.flags & IO_WQ_WORK_BLKCG) &&
1531 (def->work_flags & IO_WQ_WORK_BLKCG)) {
1532 rcu_read_lock();
1533 if (id->blkcg_css != blkcg_css()) {
1534 rcu_read_unlock();
1535 return false;
1536 }
1537 /*
1538 * This should be rare, either the cgroup is dying or the task
1539 * is moving cgroups. Just punt to root for the handful of ios.
1540 */
1541 if (css_tryget_online(id->blkcg_css))
1542 req->work.flags |= IO_WQ_WORK_BLKCG;
1543 rcu_read_unlock();
1544 }
1545#endif
1546 if (!(req->work.flags & IO_WQ_WORK_CREDS)) {
1547 if (id->creds != current_cred())
1548 return false;
1549 get_cred(id->creds);
1550 req->work.flags |= IO_WQ_WORK_CREDS;
1551 }
Jens Axboe4ea33a92020-10-15 13:46:44 -06001552#ifdef CONFIG_AUDIT
1553 if (!uid_eq(current->loginuid, id->loginuid) ||
1554 current->sessionid != id->sessionid)
1555 return false;
1556#endif
Jens Axboe1e6fa522020-10-15 08:46:24 -06001557 if (!(req->work.flags & IO_WQ_WORK_FS) &&
1558 (def->work_flags & IO_WQ_WORK_FS)) {
1559 if (current->fs != id->fs)
1560 return false;
1561 spin_lock(&id->fs->lock);
1562 if (!id->fs->in_exec) {
1563 id->fs->users++;
1564 req->work.flags |= IO_WQ_WORK_FS;
1565 } else {
1566 req->work.flags |= IO_WQ_WORK_CANCEL;
1567 }
1568 spin_unlock(&current->fs->lock);
1569 }
Pavel Begunkovaf604702020-11-25 18:41:28 +00001570 if (!(req->work.flags & IO_WQ_WORK_FILES) &&
1571 (def->work_flags & IO_WQ_WORK_FILES) &&
1572 !(req->flags & REQ_F_NO_FILE_TABLE)) {
1573 if (id->files != current->files ||
1574 id->nsproxy != current->nsproxy)
1575 return false;
1576 atomic_inc(&id->files->count);
1577 get_nsproxy(id->nsproxy);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001578 req->work.flags |= IO_WQ_WORK_FILES;
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00001579 io_req_track_inflight(req);
Pavel Begunkovaf604702020-11-25 18:41:28 +00001580 }
Jens Axboe77788772020-12-29 10:50:46 -07001581 if (!(req->work.flags & IO_WQ_WORK_MM) &&
1582 (def->work_flags & IO_WQ_WORK_MM)) {
1583 if (id->mm != current->mm)
1584 return false;
1585 mmgrab(id->mm);
1586 req->work.flags |= IO_WQ_WORK_MM;
1587 }
Jens Axboe1e6fa522020-10-15 08:46:24 -06001588
1589 return true;
Jens Axboe561fb042019-10-24 07:25:42 -06001590}
1591
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001592static void io_prep_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001593{
Jens Axboed3656342019-12-18 09:50:26 -07001594 const struct io_op_def *def = &io_op_defs[req->opcode];
Pavel Begunkov23329512020-10-10 18:34:06 +01001595 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe54a91f32019-09-10 09:15:04 -06001596
Pavel Begunkov16d59802020-07-12 16:16:47 +03001597 io_req_init_async(req);
1598
Pavel Begunkovfeaadc42020-10-22 16:47:16 +01001599 if (req->flags & REQ_F_FORCE_ASYNC)
1600 req->work.flags |= IO_WQ_WORK_CONCURRENT;
1601
Jens Axboed3656342019-12-18 09:50:26 -07001602 if (req->flags & REQ_F_ISREG) {
Pavel Begunkov23329512020-10-10 18:34:06 +01001603 if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001604 io_wq_hash_work(&req->work, file_inode(req->file));
Jens Axboed3656342019-12-18 09:50:26 -07001605 } else {
1606 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001607 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -06001608 }
Pavel Begunkov23329512020-10-10 18:34:06 +01001609
Jens Axboe1e6fa522020-10-15 08:46:24 -06001610 /* if we fail grabbing identity, we must COW, regrab, and retry */
1611 if (io_grab_identity(req))
1612 return;
1613
1614 if (!io_identity_cow(req))
1615 return;
1616
1617 /* can't fail at this point */
1618 if (!io_grab_identity(req))
1619 WARN_ON(1);
Jens Axboe561fb042019-10-24 07:25:42 -06001620}
1621
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001622static void io_prep_async_link(struct io_kiocb *req)
1623{
1624 struct io_kiocb *cur;
1625
Pavel Begunkovf2f87372020-10-27 23:25:37 +00001626 io_for_each_link(cur, req)
1627 io_prep_async_work(cur);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001628}
1629
Jens Axboe7271ef32020-08-10 09:55:22 -06001630static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001631{
Jackie Liua197f662019-11-08 08:09:12 -07001632 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001633 struct io_kiocb *link = io_prep_linked_timeout(req);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001634 struct io_uring_task *tctx = req->task->io_uring;
Jens Axboe561fb042019-10-24 07:25:42 -06001635
Jens Axboe3bfe6102021-02-16 14:15:30 -07001636 BUG_ON(!tctx);
1637 BUG_ON(!tctx->io_wq);
1638
Pavel Begunkov8766dd52020-03-14 00:31:04 +03001639 trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req,
1640 &req->work, req->flags);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07001641 io_wq_enqueue(tctx->io_wq, &req->work);
Jens Axboe7271ef32020-08-10 09:55:22 -06001642 return link;
Jens Axboe18d9be12019-09-10 09:13:05 -06001643}
1644
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001645static void io_queue_async_work(struct io_kiocb *req)
1646{
Jens Axboe7271ef32020-08-10 09:55:22 -06001647 struct io_kiocb *link;
1648
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001649 /* init ->work of the whole link before punting */
1650 io_prep_async_link(req);
Jens Axboe7271ef32020-08-10 09:55:22 -06001651 link = __io_queue_async_work(req);
1652
1653 if (link)
1654 io_queue_linked_timeout(link);
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03001655}
1656
Jens Axboe5262f562019-09-17 12:26:57 -06001657static void io_kill_timeout(struct io_kiocb *req)
1658{
Jens Axboee8c2bc12020-08-15 18:44:09 -07001659 struct io_timeout_data *io = req->async_data;
Jens Axboe5262f562019-09-17 12:26:57 -06001660 int ret;
1661
Jens Axboee8c2bc12020-08-15 18:44:09 -07001662 ret = hrtimer_try_to_cancel(&io->timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001663 if (ret != -1) {
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03001664 atomic_set(&req->ctx->cq_timeouts,
1665 atomic_read(&req->ctx->cq_timeouts) + 1);
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001666 list_del_init(&req->timeout.list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001667 io_cqring_fill_event(req, 0);
Pavel Begunkov216578e2020-10-13 09:44:00 +01001668 io_put_req_deferred(req, 1);
Jens Axboe5262f562019-09-17 12:26:57 -06001669 }
1670}
1671
Jens Axboe76e1b642020-09-26 15:05:03 -06001672/*
1673 * Returns true if we found and killed one or more timeouts
1674 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00001675static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk,
1676 struct files_struct *files)
Jens Axboe5262f562019-09-17 12:26:57 -06001677{
1678 struct io_kiocb *req, *tmp;
Jens Axboe76e1b642020-09-26 15:05:03 -06001679 int canceled = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001680
1681 spin_lock_irq(&ctx->completion_lock);
Jens Axboef3606e32020-09-22 08:18:24 -06001682 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00001683 if (io_match_task(req, tsk, files)) {
Jens Axboef3606e32020-09-22 08:18:24 -06001684 io_kill_timeout(req);
Jens Axboe76e1b642020-09-26 15:05:03 -06001685 canceled++;
1686 }
Jens Axboef3606e32020-09-22 08:18:24 -06001687 }
Jens Axboe5262f562019-09-17 12:26:57 -06001688 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe76e1b642020-09-26 15:05:03 -06001689 return canceled != 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001690}
1691
Pavel Begunkov04518942020-05-26 20:34:05 +03001692static void __io_queue_deferred(struct io_ring_ctx *ctx)
1693{
1694 do {
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001695 struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
1696 struct io_defer_entry, list);
Pavel Begunkov04518942020-05-26 20:34:05 +03001697
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03001698 if (req_need_defer(de->req, de->seq))
Pavel Begunkov04518942020-05-26 20:34:05 +03001699 break;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001700 list_del_init(&de->list);
Pavel Begunkov907d1df2021-01-26 23:35:10 +00001701 io_req_task_queue(de->req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03001702 kfree(de);
Pavel Begunkov04518942020-05-26 20:34:05 +03001703 } while (!list_empty(&ctx->defer_list));
1704}
1705
Pavel Begunkov360428f2020-05-30 14:54:17 +03001706static void io_flush_timeouts(struct io_ring_ctx *ctx)
1707{
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001708 u32 seq;
1709
1710 if (list_empty(&ctx->timeout_list))
1711 return;
1712
1713 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
1714
1715 do {
1716 u32 events_needed, events_got;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001717 struct io_kiocb *req = list_first_entry(&ctx->timeout_list,
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001718 struct io_kiocb, timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001719
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03001720 if (io_is_timeout_noseq(req))
Pavel Begunkov360428f2020-05-30 14:54:17 +03001721 break;
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001722
1723 /*
1724 * Since seq can easily wrap around over time, subtract
1725 * the last seq at which timeouts were flushed before comparing.
1726 * Assuming not more than 2^31-1 events have happened since,
1727 * these subtractions won't have wrapped, so we can check if
1728 * target is in [last_seq, current_seq] by comparing the two.
1729 */
1730 events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush;
1731 events_got = seq - ctx->cq_last_tm_flush;
1732 if (events_got < events_needed)
Pavel Begunkov360428f2020-05-30 14:54:17 +03001733 break;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03001734
Pavel Begunkov135fcde2020-07-13 23:37:12 +03001735 list_del_init(&req->timeout.list);
Pavel Begunkov360428f2020-05-30 14:54:17 +03001736 io_kill_timeout(req);
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05001737 } while (!list_empty(&ctx->timeout_list));
1738
1739 ctx->cq_last_tm_flush = seq;
Pavel Begunkov360428f2020-05-30 14:54:17 +03001740}
1741
Jens Axboede0617e2019-04-06 21:51:27 -06001742static void io_commit_cqring(struct io_ring_ctx *ctx)
1743{
Pavel Begunkov360428f2020-05-30 14:54:17 +03001744 io_flush_timeouts(ctx);
Pavel Begunkovec30e042021-01-19 13:32:38 +00001745
1746 /* order cqe stores with ring update */
1747 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail);
Jens Axboede0617e2019-04-06 21:51:27 -06001748
Pavel Begunkov04518942020-05-26 20:34:05 +03001749 if (unlikely(!list_empty(&ctx->defer_list)))
1750 __io_queue_deferred(ctx);
Jens Axboede0617e2019-04-06 21:51:27 -06001751}
1752
Jens Axboe90554202020-09-03 12:12:41 -06001753static inline bool io_sqring_full(struct io_ring_ctx *ctx)
1754{
1755 struct io_rings *r = ctx->rings;
1756
1757 return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries;
1758}
1759
Pavel Begunkov888aae22021-01-19 13:32:39 +00001760static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx)
1761{
1762 return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head);
1763}
1764
Jens Axboe2b188cc2019-01-07 10:46:33 -07001765static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1766{
Hristo Venev75b28af2019-08-26 17:23:46 +00001767 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768 unsigned tail;
1769
Stefan Bühler115e12e2019-04-24 23:54:18 +02001770 /*
1771 * writes to the cq entry need to come after reading head; the
1772 * control dependency is enough as we're using WRITE_ONCE to
1773 * fill the cq entry
1774 */
Pavel Begunkov888aae22021-01-19 13:32:39 +00001775 if (__io_cqring_events(ctx) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 return NULL;
1777
Pavel Begunkov888aae22021-01-19 13:32:39 +00001778 tail = ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001779 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001780}
1781
Jens Axboef2842ab2020-01-08 11:04:00 -07001782static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1783{
Jens Axboef0b493e2020-02-01 21:30:11 -07001784 if (!ctx->cq_ev_fd)
1785 return false;
Stefano Garzarella7e55a192020-05-15 18:38:05 +02001786 if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED)
1787 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001788 if (!ctx->eventfd_async)
1789 return true;
Jens Axboeb41e9852020-02-17 09:52:41 -07001790 return io_wq_current_is_worker();
Jens Axboef2842ab2020-01-08 11:04:00 -07001791}
1792
Jens Axboeb41e9852020-02-17 09:52:41 -07001793static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
Jens Axboe8c838782019-03-12 15:48:16 -06001794{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001795 /* see waitqueue_active() comment */
1796 smp_mb();
1797
Jens Axboe8c838782019-03-12 15:48:16 -06001798 if (waitqueue_active(&ctx->wait))
1799 wake_up(&ctx->wait);
Jens Axboe534ca6d2020-09-02 13:52:19 -06001800 if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait))
1801 wake_up(&ctx->sq_data->wait);
Jens Axboeb41e9852020-02-17 09:52:41 -07001802 if (io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001803 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001804 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001805 wake_up_interruptible(&ctx->cq_wait);
1806 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1807 }
Jens Axboe8c838782019-03-12 15:48:16 -06001808}
1809
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001810static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
1811{
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001812 /* see waitqueue_active() comment */
1813 smp_mb();
1814
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001815 if (ctx->flags & IORING_SETUP_SQPOLL) {
1816 if (waitqueue_active(&ctx->wait))
1817 wake_up(&ctx->wait);
1818 }
1819 if (io_should_trigger_evfd(ctx))
1820 eventfd_signal(ctx->cq_ev_fd, 1);
Pavel Begunkovb1445e52021-01-07 03:15:43 +00001821 if (waitqueue_active(&ctx->cq_wait)) {
Pavel Begunkov4aa84f22021-01-07 03:15:42 +00001822 wake_up_interruptible(&ctx->cq_wait);
1823 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
1824 }
Pavel Begunkov80c18e42021-01-07 03:15:41 +00001825}
1826
Jens Axboec4a2ed72019-11-21 21:01:26 -07001827/* Returns true if there are no backlogged entries after the flush */
Pavel Begunkov6c503152021-01-04 20:36:36 +00001828static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1829 struct task_struct *tsk,
1830 struct files_struct *files)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001831{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001832 struct io_rings *rings = ctx->rings;
Jens Axboee6c8aa92020-09-28 13:10:13 -06001833 struct io_kiocb *req, *tmp;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001834 struct io_uring_cqe *cqe;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001835 unsigned long flags;
Jens Axboeb18032b2021-01-24 16:58:56 -07001836 bool all_flushed, posted;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001837 LIST_HEAD(list);
1838
Pavel Begunkove23de152020-12-17 00:24:37 +00001839 if (!force && __io_cqring_events(ctx) == rings->cq_ring_entries)
1840 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001841
Jens Axboeb18032b2021-01-24 16:58:56 -07001842 posted = false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001843 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboee6c8aa92020-09-28 13:10:13 -06001844 list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00001845 if (!io_match_task(req, tsk, files))
Jens Axboee6c8aa92020-09-28 13:10:13 -06001846 continue;
1847
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001848 cqe = io_get_cqring(ctx);
1849 if (!cqe && !force)
1850 break;
1851
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001852 list_move(&req->compl.list, &list);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001853 if (cqe) {
1854 WRITE_ONCE(cqe->user_data, req->user_data);
1855 WRITE_ONCE(cqe->res, req->result);
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001856 WRITE_ONCE(cqe->flags, req->compl.cflags);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001857 } else {
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001858 ctx->cached_cq_overflow++;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001859 WRITE_ONCE(ctx->rings->cq_overflow,
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001860 ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001861 }
Jens Axboeb18032b2021-01-24 16:58:56 -07001862 posted = true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001863 }
1864
Pavel Begunkov09e88402020-12-17 00:24:38 +00001865 all_flushed = list_empty(&ctx->cq_overflow_list);
1866 if (all_flushed) {
1867 clear_bit(0, &ctx->sq_check_overflow);
1868 clear_bit(0, &ctx->cq_check_overflow);
1869 ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW;
1870 }
Pavel Begunkov46930142020-07-30 18:43:49 +03001871
Jens Axboeb18032b2021-01-24 16:58:56 -07001872 if (posted)
1873 io_commit_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001874 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeb18032b2021-01-24 16:58:56 -07001875 if (posted)
1876 io_cqring_ev_posted(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001877
1878 while (!list_empty(&list)) {
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001879 req = list_first_entry(&list, struct io_kiocb, compl.list);
1880 list_del(&req->compl.list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001881 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001882 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001883
Pavel Begunkov09e88402020-12-17 00:24:38 +00001884 return all_flushed;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001885}
1886
Pavel Begunkov6c503152021-01-04 20:36:36 +00001887static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1888 struct task_struct *tsk,
1889 struct files_struct *files)
1890{
1891 if (test_bit(0, &ctx->cq_check_overflow)) {
1892 /* iopoll syncs against uring_lock, not completion_lock */
1893 if (ctx->flags & IORING_SETUP_IOPOLL)
1894 mutex_lock(&ctx->uring_lock);
1895 __io_cqring_overflow_flush(ctx, force, tsk, files);
1896 if (ctx->flags & IORING_SETUP_IOPOLL)
1897 mutex_unlock(&ctx->uring_lock);
1898 }
1899}
1900
Jens Axboebcda7ba2020-02-23 16:42:51 -07001901static void __io_cqring_fill_event(struct io_kiocb *req, long res, long cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001904 struct io_uring_cqe *cqe;
1905
Jens Axboe78e19bb2019-11-06 15:21:34 -07001906 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001907
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908 /*
1909 * If we can't get a cq entry, userspace overflowed the
1910 * submission (by quite a lot). Increment the overflow count in
1911 * the ring.
1912 */
1913 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001914 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001915 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916 WRITE_ONCE(cqe->res, res);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001917 WRITE_ONCE(cqe->flags, cflags);
Jens Axboefdaf0832020-10-30 09:37:30 -06001918 } else if (ctx->cq_overflow_flushed ||
1919 atomic_read(&req->task->io_uring->in_idle)) {
Jens Axboe0f212202020-09-13 13:09:39 -06001920 /*
1921 * If we're in ring overflow flush mode, or in task cancel mode,
1922 * then we cannot store the request for later flushing, we need
1923 * to drop it on the floor.
1924 */
Pavel Begunkov2c3bac6d2020-10-18 10:17:40 +01001925 ctx->cached_cq_overflow++;
1926 WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001927 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001928 if (list_empty(&ctx->cq_overflow_list)) {
1929 set_bit(0, &ctx->sq_check_overflow);
1930 set_bit(0, &ctx->cq_check_overflow);
Xiaoguang Wang6d5f9042020-07-09 09:15:29 +08001931 ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW;
Jens Axboead3eb2c2019-12-18 17:12:20 -07001932 }
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001933 io_clean_op(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001934 req->result = res;
Pavel Begunkov0f7e4662020-07-13 23:37:16 +03001935 req->compl.cflags = cflags;
Pavel Begunkov40d8ddd2020-07-13 23:37:11 +03001936 refcount_inc(&req->refs);
1937 list_add_tail(&req->compl.list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001938 }
1939}
1940
Jens Axboebcda7ba2020-02-23 16:42:51 -07001941static void io_cqring_fill_event(struct io_kiocb *req, long res)
1942{
1943 __io_cqring_fill_event(req, res, 0);
1944}
1945
Jens Axboec7dae4b2021-02-09 19:53:37 -07001946static inline void io_req_complete_post(struct io_kiocb *req, long res,
1947 unsigned int cflags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001948{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001949 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001950 unsigned long flags;
1951
1952 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001953 __io_cqring_fill_event(req, res, cflags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001954 io_commit_cqring(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001955 /*
1956 * If we're the last reference to this request, add to our locked
1957 * free_list cache.
1958 */
1959 if (refcount_dec_and_test(&req->refs)) {
1960 struct io_comp_state *cs = &ctx->submit_state.comp;
1961
1962 io_dismantle_req(req);
1963 io_put_task(req->task, 1);
1964 list_add(&req->compl.list, &cs->locked_free_list);
1965 cs->locked_free_nr++;
1966 } else
1967 req = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001968 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1969
Jens Axboe8c838782019-03-12 15:48:16 -06001970 io_cqring_ev_posted(ctx);
Jens Axboec7dae4b2021-02-09 19:53:37 -07001971 if (req) {
1972 io_queue_next(req);
1973 percpu_ref_put(&ctx->refs);
1974 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975}
1976
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001977static void io_req_complete_state(struct io_kiocb *req, long res,
Pavel Begunkov889fca72021-02-10 00:03:09 +00001978 unsigned int cflags)
Jens Axboebcda7ba2020-02-23 16:42:51 -07001979{
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001980 io_clean_op(req);
1981 req->result = res;
1982 req->compl.cflags = cflags;
Pavel Begunkove342c802021-01-19 13:32:47 +00001983 req->flags |= REQ_F_COMPLETE_INLINE;
Jens Axboee1e16092020-06-22 09:17:17 -06001984}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985
Pavel Begunkov889fca72021-02-10 00:03:09 +00001986static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags,
1987 long res, unsigned cflags)
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001988{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001989 if (issue_flags & IO_URING_F_COMPLETE_DEFER)
1990 io_req_complete_state(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001991 else
Jens Axboec7dae4b2021-02-09 19:53:37 -07001992 io_req_complete_post(req, res, cflags);
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001993}
Jens Axboebcda7ba2020-02-23 16:42:51 -07001994
Pavel Begunkova38d68d2021-01-19 13:32:45 +00001995static inline void io_req_complete(struct io_kiocb *req, long res)
Jens Axboee1e16092020-06-22 09:17:17 -06001996{
Pavel Begunkov889fca72021-02-10 00:03:09 +00001997 __io_req_complete(req, 0, res, 0);
Jens Axboebcda7ba2020-02-23 16:42:51 -07001998}
1999
Jens Axboec7dae4b2021-02-09 19:53:37 -07002000static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002001{
Jens Axboec7dae4b2021-02-09 19:53:37 -07002002 struct io_submit_state *state = &ctx->submit_state;
2003 struct io_comp_state *cs = &state->comp;
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002004 struct io_kiocb *req = NULL;
2005
Jens Axboec7dae4b2021-02-09 19:53:37 -07002006 /*
2007 * If we have more than a batch's worth of requests in our IRQ side
2008 * locked cache, grab the lock and move them over to our submission
2009 * side cache.
2010 */
2011 if (READ_ONCE(cs->locked_free_nr) > IO_COMPL_BATCH) {
2012 spin_lock_irq(&ctx->completion_lock);
2013 list_splice_init(&cs->locked_free_list, &cs->free_list);
2014 cs->locked_free_nr = 0;
2015 spin_unlock_irq(&ctx->completion_lock);
2016 }
2017
2018 while (!list_empty(&cs->free_list)) {
2019 req = list_first_entry(&cs->free_list, struct io_kiocb,
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002020 compl.list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002021 list_del(&req->compl.list);
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002022 state->reqs[state->free_reqs++] = req;
2023 if (state->free_reqs == ARRAY_SIZE(state->reqs))
2024 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002026
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002027 return req != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028}
2029
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002030static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002031{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00002032 struct io_submit_state *state = &ctx->submit_state;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002033
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002034 BUILD_BUG_ON(IO_REQ_ALLOC_BATCH > ARRAY_SIZE(state->reqs));
Jens Axboe2b188cc2019-01-07 10:46:33 -07002035
Pavel Begunkovf6b6c7d2020-06-21 13:09:53 +03002036 if (!state->free_reqs) {
Pavel Begunkov291b2822020-09-30 22:57:01 +03002037 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2579f912019-01-09 09:10:43 -07002038 int ret;
2039
Jens Axboec7dae4b2021-02-09 19:53:37 -07002040 if (io_flush_cached_reqs(ctx))
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002041 goto got_req;
2042
Pavel Begunkovbf019da2021-02-10 00:03:17 +00002043 ret = kmem_cache_alloc_bulk(req_cachep, gfp, IO_REQ_ALLOC_BATCH,
2044 state->reqs);
Jens Axboefd6fab22019-03-14 16:30:06 -06002045
2046 /*
2047 * Bulk alloc is all-or-nothing. If we fail to get a batch,
2048 * retry single alloc to be on the safe side.
2049 */
2050 if (unlikely(ret <= 0)) {
2051 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
2052 if (!state->reqs[0])
Pavel Begunkov3893f392021-02-10 00:03:15 +00002053 return NULL;
Jens Axboefd6fab22019-03-14 16:30:06 -06002054 ret = 1;
2055 }
Pavel Begunkov291b2822020-09-30 22:57:01 +03002056 state->free_reqs = ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002057 }
Pavel Begunkove5d1bc02021-02-10 00:03:23 +00002058got_req:
Pavel Begunkov291b2822020-09-30 22:57:01 +03002059 state->free_reqs--;
2060 return state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07002061}
2062
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002063static inline void io_put_file(struct io_kiocb *req, struct file *file,
2064 bool fixed)
2065{
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00002066 if (!fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002067 fput(file);
2068}
2069
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002070static void io_dismantle_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002071{
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03002072 io_clean_op(req);
Pavel Begunkov929a3af2020-02-19 00:19:09 +03002073
Jens Axboee8c2bc12020-08-15 18:44:09 -07002074 if (req->async_data)
2075 kfree(req->async_data);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002076 if (req->file)
2077 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00002078 if (req->fixed_rsrc_refs)
2079 percpu_ref_put(req->fixed_rsrc_refs);
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002080 io_req_clean_work(req);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002081}
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03002082
Pavel Begunkov7c660732021-01-25 11:42:21 +00002083static inline void io_put_task(struct task_struct *task, int nr)
2084{
2085 struct io_uring_task *tctx = task->io_uring;
2086
2087 percpu_counter_sub(&tctx->inflight, nr);
2088 if (unlikely(atomic_read(&tctx->in_idle)))
2089 wake_up(&tctx->wait);
2090 put_task_struct_many(task, nr);
2091}
2092
Pavel Begunkov216578e2020-10-13 09:44:00 +01002093static void __io_free_req(struct io_kiocb *req)
Pavel Begunkove6543a82020-06-28 12:52:30 +03002094{
Jens Axboe51a4cc12020-08-10 10:55:56 -06002095 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002096
Pavel Begunkov216578e2020-10-13 09:44:00 +01002097 io_dismantle_req(req);
Pavel Begunkov7c660732021-01-25 11:42:21 +00002098 io_put_task(req->task, 1);
Pavel Begunkove6543a82020-06-28 12:52:30 +03002099
Pavel Begunkov3893f392021-02-10 00:03:15 +00002100 kmem_cache_free(req_cachep, req);
Pavel Begunkovecfc5172020-06-29 13:13:03 +03002101 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -06002102}
2103
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002104static inline void io_remove_next_linked(struct io_kiocb *req)
2105{
2106 struct io_kiocb *nxt = req->link;
2107
2108 req->link = nxt->link;
2109 nxt->link = NULL;
2110}
2111
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002112static void io_kill_linked_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002113{
Jackie Liua197f662019-11-08 08:09:12 -07002114 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002115 struct io_kiocb *link;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002116 bool cancelled = false;
2117 unsigned long flags;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002118
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002119 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002120 link = req->link;
2121
Pavel Begunkov900fad42020-10-19 16:39:16 +01002122 /*
2123 * Can happen if a linked timeout fired and link had been like
2124 * req -> link t-out -> link t-out [-> ...]
2125 */
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002126 if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) {
2127 struct io_timeout_data *io = link->async_data;
2128 int ret;
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002129
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002130 io_remove_next_linked(req);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00002131 link->timeout.head = NULL;
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002132 ret = hrtimer_try_to_cancel(&io->timer);
2133 if (ret != -1) {
2134 io_cqring_fill_event(link, -ECANCELED);
2135 io_commit_cqring(ctx);
2136 cancelled = true;
2137 }
2138 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002139 req->flags &= ~REQ_F_LINK_TIMEOUT;
Pavel Begunkov216578e2020-10-13 09:44:00 +01002140 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Jens Axboeab0b6452020-06-30 08:43:15 -06002141
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002142 if (cancelled) {
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002143 io_cqring_ev_posted(ctx);
Pavel Begunkovc9abd7a2020-10-22 16:43:11 +01002144 io_put_req(link);
2145 }
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002146}
2147
Jens Axboe4d7dd462019-11-20 13:03:52 -07002148
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002149static void io_fail_links(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002150{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002151 struct io_kiocb *link, *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002152 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002153 unsigned long flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06002154
Pavel Begunkovd148ca42020-10-18 10:17:39 +01002155 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002156 link = req->link;
2157 req->link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002158
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002159 while (link) {
2160 nxt = link->link;
2161 link->link = NULL;
2162
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002163 trace_io_uring_fail_link(req, link);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002164 io_cqring_fill_event(link, -ECANCELED);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002165
2166 /*
2167 * It's ok to free under spinlock as they're not linked anymore,
2168 * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on
2169 * work.fs->lock.
2170 */
2171 if (link->flags & REQ_F_WORK_INITIALIZED)
2172 io_put_req_deferred(link, 2);
2173 else
2174 io_double_put_req(link);
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002175 link = nxt;
Jens Axboe9e645e112019-05-10 16:07:28 -06002176 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002177 io_commit_cqring(ctx);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002178 spin_unlock_irqrestore(&ctx->completion_lock, flags);
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002179
Jens Axboe2665abf2019-11-05 12:40:47 -07002180 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06002181}
2182
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002183static struct io_kiocb *__io_req_find_next(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06002184{
Pavel Begunkov7c86ffe2020-06-29 13:12:59 +03002185 if (req->flags & REQ_F_LINK_TIMEOUT)
2186 io_kill_linked_timeout(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002187
Jens Axboe9e645e112019-05-10 16:07:28 -06002188 /*
2189 * If LINK is set, we have dependent requests in this chain. If we
2190 * didn't fail this request, queue the first one up, moving any other
2191 * dependencies to the next request. In case of failure, fail the rest
2192 * of the chain.
2193 */
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002194 if (likely(!(req->flags & REQ_F_FAIL_LINK))) {
2195 struct io_kiocb *nxt = req->link;
2196
2197 req->link = NULL;
2198 return nxt;
2199 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002200 io_fail_links(req);
2201 return NULL;
Jens Axboe4d7dd462019-11-20 13:03:52 -07002202}
Jens Axboe2665abf2019-11-05 12:40:47 -07002203
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002204static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req)
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002205{
Pavel Begunkovcdbff982021-02-12 18:41:16 +00002206 if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK))))
Pavel Begunkov3fa5e0f2020-06-30 15:20:43 +03002207 return NULL;
2208 return __io_req_find_next(req);
2209}
2210
Jens Axboe7cbf1722021-02-10 00:03:20 +00002211static bool __tctx_task_work(struct io_uring_task *tctx)
2212{
Jens Axboe65453d12021-02-10 00:03:21 +00002213 struct io_ring_ctx *ctx = NULL;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002214 struct io_wq_work_list list;
2215 struct io_wq_work_node *node;
2216
2217 if (wq_list_empty(&tctx->task_list))
2218 return false;
2219
Jens Axboe0b81e802021-02-16 10:33:53 -07002220 spin_lock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002221 list = tctx->task_list;
2222 INIT_WQ_LIST(&tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002223 spin_unlock_irq(&tctx->task_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002224
2225 node = list.first;
2226 while (node) {
2227 struct io_wq_work_node *next = node->next;
Jens Axboe65453d12021-02-10 00:03:21 +00002228 struct io_ring_ctx *this_ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002229 struct io_kiocb *req;
2230
2231 req = container_of(node, struct io_kiocb, io_task_work.node);
Jens Axboe65453d12021-02-10 00:03:21 +00002232 this_ctx = req->ctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002233 req->task_work.func(&req->task_work);
2234 node = next;
Jens Axboe65453d12021-02-10 00:03:21 +00002235
2236 if (!ctx) {
2237 ctx = this_ctx;
2238 } else if (ctx != this_ctx) {
2239 mutex_lock(&ctx->uring_lock);
2240 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2241 mutex_unlock(&ctx->uring_lock);
2242 ctx = this_ctx;
2243 }
2244 }
2245
2246 if (ctx && ctx->submit_state.comp.nr) {
2247 mutex_lock(&ctx->uring_lock);
2248 io_submit_flush_completions(&ctx->submit_state.comp, ctx);
2249 mutex_unlock(&ctx->uring_lock);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002250 }
2251
2252 return list.first != NULL;
2253}
2254
2255static void tctx_task_work(struct callback_head *cb)
2256{
2257 struct io_uring_task *tctx = container_of(cb, struct io_uring_task, task_work);
2258
2259 while (__tctx_task_work(tctx))
2260 cond_resched();
2261
2262 clear_bit(0, &tctx->task_state);
2263}
2264
2265static int io_task_work_add(struct task_struct *tsk, struct io_kiocb *req,
2266 enum task_work_notify_mode notify)
2267{
2268 struct io_uring_task *tctx = tsk->io_uring;
2269 struct io_wq_work_node *node, *prev;
Jens Axboe0b81e802021-02-16 10:33:53 -07002270 unsigned long flags;
Jens Axboe7cbf1722021-02-10 00:03:20 +00002271 int ret;
2272
2273 WARN_ON_ONCE(!tctx);
2274
Jens Axboe0b81e802021-02-16 10:33:53 -07002275 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002276 wq_list_add_tail(&req->io_task_work.node, &tctx->task_list);
Jens Axboe0b81e802021-02-16 10:33:53 -07002277 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002278
2279 /* task_work already pending, we're done */
2280 if (test_bit(0, &tctx->task_state) ||
2281 test_and_set_bit(0, &tctx->task_state))
2282 return 0;
2283
2284 if (!task_work_add(tsk, &tctx->task_work, notify))
2285 return 0;
2286
2287 /*
2288 * Slow path - we failed, find and delete work. if the work is not
2289 * in the list, it got run and we're fine.
2290 */
2291 ret = 0;
Jens Axboe0b81e802021-02-16 10:33:53 -07002292 spin_lock_irqsave(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002293 wq_list_for_each(node, prev, &tctx->task_list) {
2294 if (&req->io_task_work.node == node) {
2295 wq_list_del(&tctx->task_list, node, prev);
2296 ret = 1;
2297 break;
2298 }
2299 }
Jens Axboe0b81e802021-02-16 10:33:53 -07002300 spin_unlock_irqrestore(&tctx->task_lock, flags);
Jens Axboe7cbf1722021-02-10 00:03:20 +00002301 clear_bit(0, &tctx->task_state);
2302 return ret;
2303}
2304
Jens Axboe355fb9e2020-10-22 20:19:35 -06002305static int io_req_task_work_add(struct io_kiocb *req)
Jens Axboec2c4c832020-07-01 15:37:11 -06002306{
2307 struct task_struct *tsk = req->task;
2308 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe91989c72020-10-16 09:02:26 -06002309 enum task_work_notify_mode notify;
2310 int ret;
Jens Axboec2c4c832020-07-01 15:37:11 -06002311
Jens Axboe6200b0a2020-09-13 14:38:30 -06002312 if (tsk->flags & PF_EXITING)
2313 return -ESRCH;
2314
Jens Axboec2c4c832020-07-01 15:37:11 -06002315 /*
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002316 * SQPOLL kernel thread doesn't need notification, just a wakeup. For
2317 * all other cases, use TWA_SIGNAL unconditionally to ensure we're
2318 * processing task_work. There's no reliable way to tell if TWA_RESUME
2319 * will do the job.
Jens Axboec2c4c832020-07-01 15:37:11 -06002320 */
Jens Axboe91989c72020-10-16 09:02:26 -06002321 notify = TWA_NONE;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002322 if (!(ctx->flags & IORING_SETUP_SQPOLL))
Jens Axboec2c4c832020-07-01 15:37:11 -06002323 notify = TWA_SIGNAL;
2324
Jens Axboe7cbf1722021-02-10 00:03:20 +00002325 ret = io_task_work_add(tsk, req, notify);
Jens Axboec2c4c832020-07-01 15:37:11 -06002326 if (!ret)
2327 wake_up_process(tsk);
Jens Axboe0ba9c9e2020-08-06 19:41:50 -06002328
Jens Axboec2c4c832020-07-01 15:37:11 -06002329 return ret;
2330}
2331
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002332static void io_req_task_work_add_fallback(struct io_kiocb *req,
Jens Axboe7cbf1722021-02-10 00:03:20 +00002333 task_work_func_t cb)
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002334{
Jens Axboe7c25c0d2021-02-16 07:17:00 -07002335 struct io_ring_ctx *ctx = req->ctx;
2336 struct callback_head *head;
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002337
2338 init_task_work(&req->task_work, cb);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07002339 do {
2340 head = READ_ONCE(ctx->exit_task_work);
2341 req->task_work.next = head;
2342 } while (cmpxchg(&ctx->exit_task_work, head, &req->task_work) != head);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002343}
2344
Jens Axboec40f6372020-06-25 15:39:59 -06002345static void __io_req_task_cancel(struct io_kiocb *req, int error)
2346{
2347 struct io_ring_ctx *ctx = req->ctx;
2348
2349 spin_lock_irq(&ctx->completion_lock);
2350 io_cqring_fill_event(req, error);
2351 io_commit_cqring(ctx);
2352 spin_unlock_irq(&ctx->completion_lock);
2353
2354 io_cqring_ev_posted(ctx);
2355 req_set_fail_links(req);
2356 io_double_put_req(req);
2357}
2358
2359static void io_req_task_cancel(struct callback_head *cb)
2360{
2361 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002362 struct io_ring_ctx *ctx = req->ctx;
Jens Axboec40f6372020-06-25 15:39:59 -06002363
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002364 mutex_lock(&ctx->uring_lock);
Pavel Begunkova3df76982021-02-18 22:32:52 +00002365 __io_req_task_cancel(req, req->result);
Pavel Begunkov792bb6e2021-02-18 22:32:51 +00002366 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ceb6a2020-09-14 08:20:12 -06002367 percpu_ref_put(&ctx->refs);
Jens Axboec40f6372020-06-25 15:39:59 -06002368}
2369
2370static void __io_req_task_submit(struct io_kiocb *req)
2371{
2372 struct io_ring_ctx *ctx = req->ctx;
2373
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002374 /* ctx stays valid until unlock, even if we drop all ours ctx->refs */
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002375 mutex_lock(&ctx->uring_lock);
Pavel Begunkovdc0eced2021-02-12 18:41:15 +00002376 if (!ctx->sqo_dead && !(current->flags & PF_EXITING) &&
2377 !io_sq_thread_acquire_mm_files(ctx, req))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00002378 __io_queue_sqe(req);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002379 else
Jens Axboec40f6372020-06-25 15:39:59 -06002380 __io_req_task_cancel(req, -EFAULT);
Pavel Begunkov81b6d052021-01-04 20:36:35 +00002381 mutex_unlock(&ctx->uring_lock);
Pavel Begunkovaec18a52021-02-04 19:22:46 +00002382
2383 if (ctx->flags & IORING_SETUP_SQPOLL)
2384 io_sq_thread_drop_mm_files();
Jens Axboe9e645e112019-05-10 16:07:28 -06002385}
2386
Jens Axboec40f6372020-06-25 15:39:59 -06002387static void io_req_task_submit(struct callback_head *cb)
2388{
2389 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2390
2391 __io_req_task_submit(req);
2392}
2393
2394static void io_req_task_queue(struct io_kiocb *req)
2395{
Jens Axboec40f6372020-06-25 15:39:59 -06002396 int ret;
2397
Jens Axboe7cbf1722021-02-10 00:03:20 +00002398 req->task_work.func = io_req_task_submit;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002399 ret = io_req_task_work_add(req);
Jens Axboec40f6372020-06-25 15:39:59 -06002400 if (unlikely(ret)) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00002401 req->result = -ECANCELED;
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00002402 percpu_ref_get(&req->ctx->refs);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002403 io_req_task_work_add_fallback(req, io_req_task_cancel);
Jens Axboec40f6372020-06-25 15:39:59 -06002404 }
Jens Axboec40f6372020-06-25 15:39:59 -06002405}
2406
Pavel Begunkova3df76982021-02-18 22:32:52 +00002407static void io_req_task_queue_fail(struct io_kiocb *req, int ret)
2408{
2409 percpu_ref_get(&req->ctx->refs);
2410 req->result = ret;
2411 req->task_work.func = io_req_task_cancel;
2412
2413 if (unlikely(io_req_task_work_add(req)))
2414 io_req_task_work_add_fallback(req, io_req_task_cancel);
2415}
2416
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002417static inline void io_queue_next(struct io_kiocb *req)
Jackie Liuc69f8db2019-11-09 11:00:08 +08002418{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002419 struct io_kiocb *nxt = io_req_find_next(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03002420
Pavel Begunkov906a8c32020-06-27 14:04:55 +03002421 if (nxt)
2422 io_req_task_queue(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08002423}
2424
Jens Axboe9e645e112019-05-10 16:07:28 -06002425static void io_free_req(struct io_kiocb *req)
2426{
Pavel Begunkovc3524382020-06-28 12:52:32 +03002427 io_queue_next(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002428 __io_free_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002429}
2430
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002431struct req_batch {
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002432 struct task_struct *task;
2433 int task_refs;
Jens Axboe1b4c3512021-02-10 00:03:19 +00002434 int ctx_refs;
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002435};
2436
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002437static inline void io_init_req_batch(struct req_batch *rb)
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002438{
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002439 rb->task_refs = 0;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002440 rb->ctx_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002441 rb->task = NULL;
2442}
Pavel Begunkov8766dd52020-03-14 00:31:04 +03002443
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002444static void io_req_free_batch_finish(struct io_ring_ctx *ctx,
2445 struct req_batch *rb)
2446{
Pavel Begunkov6e833d52021-02-11 18:28:20 +00002447 if (rb->task)
Pavel Begunkov7c660732021-01-25 11:42:21 +00002448 io_put_task(rb->task, rb->task_refs);
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002449 if (rb->ctx_refs)
2450 percpu_ref_put_many(&ctx->refs, rb->ctx_refs);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002451}
2452
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002453static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req,
2454 struct io_submit_state *state)
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002455{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00002456 io_queue_next(req);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002457
Jens Axboee3bc8e92020-09-24 08:45:57 -06002458 if (req->task != rb->task) {
Pavel Begunkov7c660732021-01-25 11:42:21 +00002459 if (rb->task)
2460 io_put_task(rb->task, rb->task_refs);
Jens Axboee3bc8e92020-09-24 08:45:57 -06002461 rb->task = req->task;
2462 rb->task_refs = 0;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002463 }
Jens Axboee3bc8e92020-09-24 08:45:57 -06002464 rb->task_refs++;
Pavel Begunkov9ae72462021-02-10 00:03:16 +00002465 rb->ctx_refs++;
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002466
Pavel Begunkov4edf20f2020-10-13 09:43:59 +01002467 io_dismantle_req(req);
Pavel Begunkovbd759042021-02-12 03:23:50 +00002468 if (state->free_reqs != ARRAY_SIZE(state->reqs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002469 state->reqs[state->free_reqs++] = req;
Pavel Begunkovbd759042021-02-12 03:23:50 +00002470 else
2471 list_add(&req->compl.list, &state->comp.free_list);
Pavel Begunkov7a743e22020-03-03 21:33:13 +03002472}
2473
Pavel Begunkov905c1722021-02-10 00:03:14 +00002474static void io_submit_flush_completions(struct io_comp_state *cs,
2475 struct io_ring_ctx *ctx)
2476{
2477 int i, nr = cs->nr;
2478 struct io_kiocb *req;
2479 struct req_batch rb;
2480
2481 io_init_req_batch(&rb);
2482 spin_lock_irq(&ctx->completion_lock);
2483 for (i = 0; i < nr; i++) {
2484 req = cs->reqs[i];
2485 __io_cqring_fill_event(req, req->result, req->compl.cflags);
2486 }
2487 io_commit_cqring(ctx);
2488 spin_unlock_irq(&ctx->completion_lock);
2489
2490 io_cqring_ev_posted(ctx);
2491 for (i = 0; i < nr; i++) {
2492 req = cs->reqs[i];
2493
2494 /* submission and completion refs */
2495 if (refcount_sub_and_test(2, &req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002496 io_req_free_batch(&rb, req, &ctx->submit_state);
Pavel Begunkov905c1722021-02-10 00:03:14 +00002497 }
2498
2499 io_req_free_batch_finish(ctx, &rb);
2500 cs->nr = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06002501}
2502
Jens Axboeba816ad2019-09-28 11:36:45 -06002503/*
2504 * Drop reference to request, return next in chain (if there is one) if this
2505 * was the last reference to this request.
2506 */
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002507static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -06002508{
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002509 struct io_kiocb *nxt = NULL;
2510
Jens Axboe2a44f462020-02-25 13:25:41 -07002511 if (refcount_dec_and_test(&req->refs)) {
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002512 nxt = io_req_find_next(req);
Jens Axboe4d7dd462019-11-20 13:03:52 -07002513 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07002514 }
Pavel Begunkov9b5f7bd2020-06-29 13:13:00 +03002515 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002516}
2517
Jens Axboe2b188cc2019-01-07 10:46:33 -07002518static void io_put_req(struct io_kiocb *req)
2519{
Jens Axboedef596e2019-01-09 08:59:42 -07002520 if (refcount_dec_and_test(&req->refs))
2521 io_free_req(req);
2522}
2523
Pavel Begunkov216578e2020-10-13 09:44:00 +01002524static void io_put_req_deferred_cb(struct callback_head *cb)
2525{
2526 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
2527
2528 io_free_req(req);
2529}
2530
2531static void io_free_req_deferred(struct io_kiocb *req)
2532{
2533 int ret;
2534
Jens Axboe7cbf1722021-02-10 00:03:20 +00002535 req->task_work.func = io_put_req_deferred_cb;
Jens Axboe355fb9e2020-10-22 20:19:35 -06002536 ret = io_req_task_work_add(req);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00002537 if (unlikely(ret))
2538 io_req_task_work_add_fallback(req, io_put_req_deferred_cb);
Pavel Begunkov216578e2020-10-13 09:44:00 +01002539}
2540
2541static inline void io_put_req_deferred(struct io_kiocb *req, int refs)
2542{
2543 if (refcount_sub_and_test(refs, &req->refs))
2544 io_free_req_deferred(req);
2545}
2546
Jens Axboe978db572019-11-14 22:39:04 -07002547static void io_double_put_req(struct io_kiocb *req)
2548{
2549 /* drop both submit and complete references */
2550 if (refcount_sub_and_test(2, &req->refs))
2551 io_free_req(req);
2552}
2553
Pavel Begunkov6c503152021-01-04 20:36:36 +00002554static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -06002555{
2556 /* See comment at the top of this file */
2557 smp_rmb();
Pavel Begunkove23de152020-12-17 00:24:37 +00002558 return __io_cqring_events(ctx);
Jens Axboea3a0e432019-08-20 11:03:11 -06002559}
2560
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002561static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
2562{
2563 struct io_rings *rings = ctx->rings;
2564
2565 /* make sure SQ entry isn't read before tail */
2566 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
2567}
2568
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002569static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf)
Jens Axboee94f1412019-12-19 12:06:02 -07002570{
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002571 unsigned int cflags;
Jens Axboee94f1412019-12-19 12:06:02 -07002572
Jens Axboebcda7ba2020-02-23 16:42:51 -07002573 cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT;
2574 cflags |= IORING_CQE_F_BUFFER;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03002575 req->flags &= ~REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07002576 kfree(kbuf);
2577 return cflags;
2578}
2579
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002580static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req)
2581{
2582 struct io_buffer *kbuf;
2583
2584 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
2585 return io_put_kbuf(req, kbuf);
2586}
2587
Jens Axboe4c6e2772020-07-01 11:29:10 -06002588static inline bool io_run_task_work(void)
2589{
Jens Axboe6200b0a2020-09-13 14:38:30 -06002590 /*
2591 * Not safe to run on exiting task, and the task_work handling will
2592 * not add work to such a task.
2593 */
2594 if (unlikely(current->flags & PF_EXITING))
2595 return false;
Jens Axboe4c6e2772020-07-01 11:29:10 -06002596 if (current->task_works) {
2597 __set_current_state(TASK_RUNNING);
2598 task_work_run();
2599 return true;
2600 }
2601
2602 return false;
2603}
2604
Jens Axboedef596e2019-01-09 08:59:42 -07002605/*
2606 * Find and free completed poll iocbs
2607 */
2608static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
2609 struct list_head *done)
2610{
Jens Axboe8237e042019-12-28 10:48:22 -07002611 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07002612 struct io_kiocb *req;
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002613
2614 /* order with ->result store in io_complete_rw_iopoll() */
2615 smp_rmb();
Jens Axboedef596e2019-01-09 08:59:42 -07002616
Pavel Begunkov5af1d132020-07-18 11:32:52 +03002617 io_init_req_batch(&rb);
Jens Axboedef596e2019-01-09 08:59:42 -07002618 while (!list_empty(done)) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07002619 int cflags = 0;
2620
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002621 req = list_first_entry(done, struct io_kiocb, inflight_entry);
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002622 list_del(&req->inflight_entry);
Jens Axboedef596e2019-01-09 08:59:42 -07002623
Pavel Begunkovf1613402021-02-11 18:28:21 +00002624 if (READ_ONCE(req->result) == -EAGAIN) {
2625 req->iopoll_completed = 0;
Pavel Begunkov23faba32021-02-11 18:28:22 +00002626 if (io_rw_reissue(req))
Pavel Begunkovf1613402021-02-11 18:28:21 +00002627 continue;
2628 }
2629
Jens Axboebcda7ba2020-02-23 16:42:51 -07002630 if (req->flags & REQ_F_BUFFER_SELECTED)
Pavel Begunkov8ff069b2020-07-16 23:28:04 +03002631 cflags = io_put_rw_kbuf(req);
Jens Axboebcda7ba2020-02-23 16:42:51 -07002632
2633 __io_cqring_fill_event(req, req->result, cflags);
Jens Axboedef596e2019-01-09 08:59:42 -07002634 (*nr_events)++;
2635
Pavel Begunkovc3524382020-06-28 12:52:32 +03002636 if (refcount_dec_and_test(&req->refs))
Pavel Begunkov6ff119a2021-02-10 00:03:18 +00002637 io_req_free_batch(&rb, req, &ctx->submit_state);
Jens Axboedef596e2019-01-09 08:59:42 -07002638 }
Jens Axboedef596e2019-01-09 08:59:42 -07002639
Jens Axboe09bb8392019-03-13 12:39:28 -06002640 io_commit_cqring(ctx);
Pavel Begunkov80c18e42021-01-07 03:15:41 +00002641 io_cqring_ev_posted_iopoll(ctx);
Pavel Begunkov2d6500d2020-06-28 12:52:33 +03002642 io_req_free_batch_finish(ctx, &rb);
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002643}
2644
Jens Axboedef596e2019-01-09 08:59:42 -07002645static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
2646 long min)
2647{
2648 struct io_kiocb *req, *tmp;
2649 LIST_HEAD(done);
2650 bool spin;
2651 int ret;
2652
2653 /*
2654 * Only spin for completions if we don't have multiple devices hanging
2655 * off our complete list, and we're under the requested amount.
2656 */
2657 spin = !ctx->poll_multi_file && *nr_events < min;
2658
2659 ret = 0;
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002660 list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002661 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07002662
2663 /*
Bijan Mottahedeh581f9812020-04-03 13:51:33 -07002664 * Move completed and retryable entries to our local lists.
2665 * If we find a request that requires polling, break out
2666 * and complete those lists first, if we have entries there.
Jens Axboedef596e2019-01-09 08:59:42 -07002667 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002668 if (READ_ONCE(req->iopoll_completed)) {
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002669 list_move_tail(&req->inflight_entry, &done);
Jens Axboedef596e2019-01-09 08:59:42 -07002670 continue;
2671 }
2672 if (!list_empty(&done))
2673 break;
2674
2675 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
2676 if (ret < 0)
2677 break;
2678
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002679 /* iopoll may have completed current req */
2680 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002681 list_move_tail(&req->inflight_entry, &done);
Pavel Begunkov3aadc232020-07-06 17:59:29 +03002682
Jens Axboedef596e2019-01-09 08:59:42 -07002683 if (ret && spin)
2684 spin = false;
2685 ret = 0;
2686 }
2687
2688 if (!list_empty(&done))
2689 io_iopoll_complete(ctx, nr_events, &done);
2690
2691 return ret;
2692}
2693
2694/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08002695 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07002696 * non-spinning poll check - we'll still enter the driver poll loop, but only
2697 * as a non-spinning completion check.
2698 */
2699static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
2700 long min)
2701{
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002702 while (!list_empty(&ctx->iopoll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07002703 int ret;
2704
2705 ret = io_do_iopoll(ctx, nr_events, min);
2706 if (ret < 0)
2707 return ret;
Pavel Begunkoveba0a4d2020-07-06 17:59:30 +03002708 if (*nr_events >= min)
Jens Axboedef596e2019-01-09 08:59:42 -07002709 return 0;
2710 }
2711
2712 return 1;
2713}
2714
2715/*
2716 * We can't just wait for polled events to come to us, we have to actively
2717 * find and complete them.
2718 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002719static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx)
Jens Axboedef596e2019-01-09 08:59:42 -07002720{
2721 if (!(ctx->flags & IORING_SETUP_IOPOLL))
2722 return;
2723
2724 mutex_lock(&ctx->uring_lock);
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002725 while (!list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002726 unsigned int nr_events = 0;
2727
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002728 io_do_iopoll(ctx, &nr_events, 0);
Jens Axboe08f54392019-08-21 22:19:11 -06002729
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03002730 /* let it sleep and repeat later if can't complete a request */
2731 if (nr_events == 0)
2732 break;
Jens Axboe08f54392019-08-21 22:19:11 -06002733 /*
2734 * Ensure we allow local-to-the-cpu processing to take place,
2735 * in this case we need to ensure that we reap all events.
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002736 * Also let task_work, etc. to progress by releasing the mutex
Jens Axboe08f54392019-08-21 22:19:11 -06002737 */
Pavel Begunkov3fcee5a2020-07-06 17:59:31 +03002738 if (need_resched()) {
2739 mutex_unlock(&ctx->uring_lock);
2740 cond_resched();
2741 mutex_lock(&ctx->uring_lock);
2742 }
Jens Axboedef596e2019-01-09 08:59:42 -07002743 }
2744 mutex_unlock(&ctx->uring_lock);
2745}
2746
Pavel Begunkov7668b922020-07-07 16:36:21 +03002747static int io_iopoll_check(struct io_ring_ctx *ctx, long min)
Jens Axboedef596e2019-01-09 08:59:42 -07002748{
Pavel Begunkov7668b922020-07-07 16:36:21 +03002749 unsigned int nr_events = 0;
Jens Axboe2b2ed972019-10-25 10:06:15 -06002750 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07002751
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08002752 /*
2753 * We disallow the app entering submit/complete with polling, but we
2754 * still need to lock the ring to prevent racing with polled issue
2755 * that got punted to a workqueue.
2756 */
2757 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002758 do {
Jens Axboe500f9fb2019-08-19 12:15:59 -06002759 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06002760 * Don't enter poll loop if we already have events pending.
2761 * If we do, we can potentially be spinning for commands that
2762 * already triggered a CQE (eg in error).
2763 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00002764 if (test_bit(0, &ctx->cq_check_overflow))
2765 __io_cqring_overflow_flush(ctx, false, NULL, NULL);
2766 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06002767 break;
2768
2769 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06002770 * If a submit got punted to a workqueue, we can have the
2771 * application entering polling for a command before it gets
2772 * issued. That app will hold the uring_lock for the duration
2773 * of the poll right here, so we need to take a breather every
2774 * now and then to ensure that the issue has a chance to add
2775 * the poll to the issued list. Otherwise we can spin here
2776 * forever, while the workqueue is stuck trying to acquire the
2777 * very same mutex.
2778 */
2779 if (!(++iters & 7)) {
2780 mutex_unlock(&ctx->uring_lock);
Jens Axboe4c6e2772020-07-01 11:29:10 -06002781 io_run_task_work();
Jens Axboe500f9fb2019-08-19 12:15:59 -06002782 mutex_lock(&ctx->uring_lock);
2783 }
2784
Pavel Begunkov7668b922020-07-07 16:36:21 +03002785 ret = io_iopoll_getevents(ctx, &nr_events, min);
Jens Axboedef596e2019-01-09 08:59:42 -07002786 if (ret <= 0)
2787 break;
2788 ret = 0;
Pavel Begunkov7668b922020-07-07 16:36:21 +03002789 } while (min && !nr_events && !need_resched());
Jens Axboedef596e2019-01-09 08:59:42 -07002790
Jens Axboe500f9fb2019-08-19 12:15:59 -06002791 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07002792 return ret;
2793}
2794
Jens Axboe491381ce2019-10-17 09:20:46 -06002795static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002796{
Jens Axboe491381ce2019-10-17 09:20:46 -06002797 /*
2798 * Tell lockdep we inherited freeze protection from submission
2799 * thread.
2800 */
2801 if (req->flags & REQ_F_ISREG) {
2802 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002803
Jens Axboe491381ce2019-10-17 09:20:46 -06002804 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002805 }
Jens Axboe491381ce2019-10-17 09:20:46 -06002806 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002807}
2808
Jens Axboeb63534c2020-06-04 11:28:00 -06002809#ifdef CONFIG_BLOCK
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002810static bool io_resubmit_prep(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002811{
2812 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Colin Ian King4a245472021-02-10 20:00:07 +00002813 int rw, ret;
Jens Axboeb63534c2020-06-04 11:28:00 -06002814 struct iov_iter iter;
Jens Axboeb63534c2020-06-04 11:28:00 -06002815
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002816 /* already prepared */
2817 if (req->async_data)
2818 return true;
Jens Axboeb63534c2020-06-04 11:28:00 -06002819
2820 switch (req->opcode) {
2821 case IORING_OP_READV:
2822 case IORING_OP_READ_FIXED:
2823 case IORING_OP_READ:
2824 rw = READ;
2825 break;
2826 case IORING_OP_WRITEV:
2827 case IORING_OP_WRITE_FIXED:
2828 case IORING_OP_WRITE:
2829 rw = WRITE;
2830 break;
2831 default:
2832 printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n",
2833 req->opcode);
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002834 return false;
Jens Axboeb63534c2020-06-04 11:28:00 -06002835 }
2836
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002837 ret = io_import_iovec(rw, req, &iovec, &iter, false);
2838 if (ret < 0)
2839 return false;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00002840 return !io_setup_async_rw(req, iovec, inline_vecs, &iter, false);
Jens Axboeb63534c2020-06-04 11:28:00 -06002841}
Jens Axboeb63534c2020-06-04 11:28:00 -06002842#endif
2843
Pavel Begunkov23faba32021-02-11 18:28:22 +00002844static bool io_rw_reissue(struct io_kiocb *req)
Jens Axboeb63534c2020-06-04 11:28:00 -06002845{
2846#ifdef CONFIG_BLOCK
Jens Axboe355afae2020-09-02 09:30:31 -06002847 umode_t mode = file_inode(req->file)->i_mode;
Jens Axboeb63534c2020-06-04 11:28:00 -06002848 int ret;
2849
Jens Axboe355afae2020-09-02 09:30:31 -06002850 if (!S_ISBLK(mode) && !S_ISREG(mode))
2851 return false;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00002852 if ((req->flags & REQ_F_NOWAIT) || io_wq_current_is_worker())
Jens Axboeb63534c2020-06-04 11:28:00 -06002853 return false;
2854
Pavel Begunkov55e6ac12021-01-08 20:57:22 +00002855 lockdep_assert_held(&req->ctx->uring_lock);
2856
Jens Axboe28cea78a2020-09-14 10:51:17 -06002857 ret = io_sq_thread_acquire_mm_files(req->ctx, req);
Jens Axboe6d816e02020-08-11 08:04:14 -06002858
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002859 if (!ret && io_resubmit_prep(req)) {
Jens Axboefdee9462020-08-27 16:46:24 -06002860 refcount_inc(&req->refs);
2861 io_queue_async_work(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002862 return true;
Jens Axboefdee9462020-08-27 16:46:24 -06002863 }
Pavel Begunkovdc2a6e92021-01-19 13:32:35 +00002864 req_set_fail_links(req);
Jens Axboeb63534c2020-06-04 11:28:00 -06002865#endif
2866 return false;
2867}
2868
Jens Axboea1d7c392020-06-22 11:09:46 -06002869static void __io_complete_rw(struct io_kiocb *req, long res, long res2,
Pavel Begunkov889fca72021-02-10 00:03:09 +00002870 unsigned int issue_flags)
Jens Axboea1d7c392020-06-22 11:09:46 -06002871{
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002872 int cflags = 0;
2873
Pavel Begunkov23faba32021-02-11 18:28:22 +00002874 if ((res == -EAGAIN || res == -EOPNOTSUPP) && io_rw_reissue(req))
2875 return;
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002876 if (res != req->result)
2877 req_set_fail_links(req);
Pavel Begunkov23faba32021-02-11 18:28:22 +00002878
Pavel Begunkov2f8e45f2021-02-11 18:28:23 +00002879 if (req->rw.kiocb.ki_flags & IOCB_WRITE)
2880 kiocb_end_write(req);
2881 if (req->flags & REQ_F_BUFFER_SELECTED)
2882 cflags = io_put_rw_kbuf(req);
2883 __io_req_complete(req, issue_flags, res, cflags);
Jens Axboeba816ad2019-09-28 11:36:45 -06002884}
2885
2886static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
2887{
Jens Axboe9adbd452019-12-20 08:45:55 -07002888 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06002889
Pavel Begunkov889fca72021-02-10 00:03:09 +00002890 __io_complete_rw(req, res, res2, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002891}
2892
Jens Axboedef596e2019-01-09 08:59:42 -07002893static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
2894{
Jens Axboe9adbd452019-12-20 08:45:55 -07002895 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07002896
Jens Axboe491381ce2019-10-17 09:20:46 -06002897 if (kiocb->ki_flags & IOCB_WRITE)
2898 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002899
Xiaoguang Wang2d7d6792020-06-16 02:06:37 +08002900 if (res != -EAGAIN && res != req->result)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002901 req_set_fail_links(req);
Xiaoguang Wangbbde0172020-06-16 02:06:38 +08002902
2903 WRITE_ONCE(req->result, res);
2904 /* order with io_poll_complete() checking ->result */
Pavel Begunkovcd664b02020-06-25 12:37:10 +03002905 smp_wmb();
2906 WRITE_ONCE(req->iopoll_completed, 1);
Jens Axboedef596e2019-01-09 08:59:42 -07002907}
2908
2909/*
2910 * After the iocb has been issued, it's safe to be found on the poll list.
2911 * Adding the kiocb to the list AFTER submission ensures that we don't
2912 * find it from a io_iopoll_getevents() thread before the issuer is done
2913 * accessing the kiocb cookie.
2914 */
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002915static void io_iopoll_req_issued(struct io_kiocb *req, bool in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002916{
2917 struct io_ring_ctx *ctx = req->ctx;
2918
2919 /*
2920 * Track whether we have multiple files in our lists. This will impact
2921 * how we do polling eventually, not spinning if we're on potentially
2922 * different devices.
2923 */
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002924 if (list_empty(&ctx->iopoll_list)) {
Jens Axboedef596e2019-01-09 08:59:42 -07002925 ctx->poll_multi_file = false;
2926 } else if (!ctx->poll_multi_file) {
2927 struct io_kiocb *list_req;
2928
Pavel Begunkov540e32a2020-07-13 23:37:09 +03002929 list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb,
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002930 inflight_entry);
Jens Axboe9adbd452019-12-20 08:45:55 -07002931 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07002932 ctx->poll_multi_file = true;
2933 }
2934
2935 /*
2936 * For fast devices, IO may have already completed. If it has, add
2937 * it to the front so we find it first.
2938 */
Xiaoguang Wang65a65432020-06-11 23:39:36 +08002939 if (READ_ONCE(req->iopoll_completed))
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002940 list_add(&req->inflight_entry, &ctx->iopoll_list);
Jens Axboedef596e2019-01-09 08:59:42 -07002941 else
Pavel Begunkovd21ffe72020-07-13 23:37:10 +03002942 list_add_tail(&req->inflight_entry, &ctx->iopoll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08002943
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08002944 /*
2945 * If IORING_SETUP_SQPOLL is enabled, sqes are either handled in sq thread
2946 * task context or in io worker task context. If current task context is
2947 * sq thread, we don't need to check whether should wake up sq thread.
2948 */
2949 if (in_async && (ctx->flags & IORING_SETUP_SQPOLL) &&
Jens Axboe534ca6d2020-09-02 13:52:19 -06002950 wq_has_sleeper(&ctx->sq_data->wait))
2951 wake_up(&ctx->sq_data->wait);
Jens Axboedef596e2019-01-09 08:59:42 -07002952}
2953
Pavel Begunkov9f13c352020-05-17 14:13:41 +03002954static inline void io_state_file_put(struct io_submit_state *state)
2955{
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002956 if (state->file_refs) {
2957 fput_many(state->file, state->file_refs);
2958 state->file_refs = 0;
2959 }
Jens Axboe9a56a232019-01-09 09:06:50 -07002960}
2961
2962/*
2963 * Get as many references to a file as we have IOs left in this submission,
2964 * assuming most submissions are for one file, or at least that each file
2965 * has more than one submission.
2966 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03002967static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07002968{
2969 if (!state)
2970 return fget(fd);
2971
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002972 if (state->file_refs) {
Jens Axboe9a56a232019-01-09 09:06:50 -07002973 if (state->fd == fd) {
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002974 state->file_refs--;
Jens Axboe9a56a232019-01-09 09:06:50 -07002975 return state->file;
2976 }
Pavel Begunkov02b23a92021-01-19 13:32:41 +00002977 io_state_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07002978 }
2979 state->file = fget_many(fd, state->ios_left);
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002980 if (unlikely(!state->file))
Jens Axboe9a56a232019-01-09 09:06:50 -07002981 return NULL;
2982
2983 state->fd = fd;
Pavel Begunkov6e1271e2020-11-20 15:50:50 +00002984 state->file_refs = state->ios_left - 1;
Jens Axboe9a56a232019-01-09 09:06:50 -07002985 return state->file;
2986}
2987
Jens Axboe4503b762020-06-01 10:00:27 -06002988static bool io_bdev_nowait(struct block_device *bdev)
2989{
Jeffle Xu9ba0d0c2020-10-19 16:59:42 +08002990 return !bdev || blk_queue_nowait(bdev_get_queue(bdev));
Jens Axboe4503b762020-06-01 10:00:27 -06002991}
2992
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993/*
2994 * If we tracked the file through the SCM inflight mechanism, we could support
2995 * any file. For now, just ensure that anything potentially problematic is done
2996 * inline.
2997 */
Jens Axboeaf197f52020-04-28 13:15:06 -06002998static bool io_file_supports_async(struct file *file, int rw)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002999{
3000 umode_t mode = file_inode(file)->i_mode;
3001
Jens Axboe4503b762020-06-01 10:00:27 -06003002 if (S_ISBLK(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01003003 if (IS_ENABLED(CONFIG_BLOCK) &&
3004 io_bdev_nowait(I_BDEV(file->f_mapping->host)))
Jens Axboe4503b762020-06-01 10:00:27 -06003005 return true;
3006 return false;
3007 }
3008 if (S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003009 return true;
Jens Axboe4503b762020-06-01 10:00:27 -06003010 if (S_ISREG(mode)) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01003011 if (IS_ENABLED(CONFIG_BLOCK) &&
3012 io_bdev_nowait(file->f_inode->i_sb->s_bdev) &&
Jens Axboe4503b762020-06-01 10:00:27 -06003013 file->f_op != &io_uring_fops)
3014 return true;
3015 return false;
3016 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003017
Jens Axboec5b85622020-06-09 19:23:05 -06003018 /* any ->read/write should understand O_NONBLOCK */
3019 if (file->f_flags & O_NONBLOCK)
3020 return true;
3021
Jens Axboeaf197f52020-04-28 13:15:06 -06003022 if (!(file->f_mode & FMODE_NOWAIT))
3023 return false;
3024
3025 if (rw == READ)
3026 return file->f_op->read_iter != NULL;
3027
3028 return file->f_op->write_iter != NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003029}
3030
Pavel Begunkova88fc402020-09-30 22:57:53 +03003031static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003032{
Jens Axboedef596e2019-01-09 08:59:42 -07003033 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07003034 struct kiocb *kiocb = &req->rw.kiocb;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003035 struct file *file = req->file;
Jens Axboe09bb8392019-03-13 12:39:28 -06003036 unsigned ioprio;
3037 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003038
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003039 if (S_ISREG(file_inode(file)->i_mode))
Jens Axboe491381ce2019-10-17 09:20:46 -06003040 req->flags |= REQ_F_ISREG;
3041
Jens Axboe2b188cc2019-01-07 10:46:33 -07003042 kiocb->ki_pos = READ_ONCE(sqe->off);
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003043 if (kiocb->ki_pos == -1 && !(file->f_mode & FMODE_STREAM)) {
Jens Axboeba042912019-12-25 16:33:42 -07003044 req->flags |= REQ_F_CUR_POS;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003045 kiocb->ki_pos = file->f_pos;
Jens Axboeba042912019-12-25 16:33:42 -07003046 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003047 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03003048 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
3049 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
3050 if (unlikely(ret))
3051 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003052
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003053 /* don't allow async punt for O_NONBLOCK or RWF_NOWAIT */
3054 if ((kiocb->ki_flags & IOCB_NOWAIT) || (file->f_flags & O_NONBLOCK))
3055 req->flags |= REQ_F_NOWAIT;
3056
Jens Axboe2b188cc2019-01-07 10:46:33 -07003057 ioprio = READ_ONCE(sqe->ioprio);
3058 if (ioprio) {
3059 ret = ioprio_check_cap(ioprio);
3060 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06003061 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003062
3063 kiocb->ki_ioprio = ioprio;
3064 } else
3065 kiocb->ki_ioprio = get_current_ioprio();
3066
Jens Axboedef596e2019-01-09 08:59:42 -07003067 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003068 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
3069 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06003070 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003071
Jens Axboedef596e2019-01-09 08:59:42 -07003072 kiocb->ki_flags |= IOCB_HIPRI;
3073 kiocb->ki_complete = io_complete_rw_iopoll;
Xiaoguang Wang65a65432020-06-11 23:39:36 +08003074 req->iopoll_completed = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07003075 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06003076 if (kiocb->ki_flags & IOCB_HIPRI)
3077 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07003078 kiocb->ki_complete = io_complete_rw;
3079 }
Jens Axboe9adbd452019-12-20 08:45:55 -07003080
Jens Axboe3529d8c2019-12-19 18:24:38 -07003081 req->rw.addr = READ_ONCE(sqe->addr);
3082 req->rw.len = READ_ONCE(sqe->len);
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003083 req->buf_index = READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003084 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003085}
3086
3087static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
3088{
3089 switch (ret) {
3090 case -EIOCBQUEUED:
3091 break;
3092 case -ERESTARTSYS:
3093 case -ERESTARTNOINTR:
3094 case -ERESTARTNOHAND:
3095 case -ERESTART_RESTARTBLOCK:
3096 /*
3097 * We can't just restart the syscall, since previously
3098 * submitted sqes may already be in progress. Just fail this
3099 * IO with EINTR.
3100 */
3101 ret = -EINTR;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003102 fallthrough;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003103 default:
3104 kiocb->ki_complete(kiocb, ret, 0);
3105 }
3106}
3107
Jens Axboea1d7c392020-06-22 11:09:46 -06003108static void kiocb_done(struct kiocb *kiocb, ssize_t ret,
Pavel Begunkov889fca72021-02-10 00:03:09 +00003109 unsigned int issue_flags)
Jens Axboeba816ad2019-09-28 11:36:45 -06003110{
Jens Axboeba042912019-12-25 16:33:42 -07003111 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboee8c2bc12020-08-15 18:44:09 -07003112 struct io_async_rw *io = req->async_data;
Jens Axboeba042912019-12-25 16:33:42 -07003113
Jens Axboe227c0c92020-08-13 11:51:40 -06003114 /* add previously done IO, if any */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003115 if (io && io->bytes_done > 0) {
Jens Axboe227c0c92020-08-13 11:51:40 -06003116 if (ret < 0)
Jens Axboee8c2bc12020-08-15 18:44:09 -07003117 ret = io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003118 else
Jens Axboee8c2bc12020-08-15 18:44:09 -07003119 ret += io->bytes_done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003120 }
3121
Jens Axboeba042912019-12-25 16:33:42 -07003122 if (req->flags & REQ_F_CUR_POS)
3123 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03003124 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Pavel Begunkov889fca72021-02-10 00:03:09 +00003125 __io_complete_rw(req, ret, 0, issue_flags);
Jens Axboeba816ad2019-09-28 11:36:45 -06003126 else
3127 io_rw_done(kiocb, ret);
3128}
3129
Pavel Begunkov847595d2021-02-04 13:52:06 +00003130static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07003131{
Jens Axboe9adbd452019-12-20 08:45:55 -07003132 struct io_ring_ctx *ctx = req->ctx;
3133 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07003134 struct io_mapped_ubuf *imu;
Pavel Begunkov4be1c612020-09-06 00:45:48 +03003135 u16 index, buf_index = req->buf_index;
Jens Axboeedafcce2019-01-09 09:16:05 -07003136 size_t offset;
3137 u64 buf_addr;
3138
Jens Axboeedafcce2019-01-09 09:16:05 -07003139 if (unlikely(buf_index >= ctx->nr_user_bufs))
3140 return -EFAULT;
Jens Axboeedafcce2019-01-09 09:16:05 -07003141 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
3142 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07003143 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07003144
3145 /* overflow */
3146 if (buf_addr + len < buf_addr)
3147 return -EFAULT;
3148 /* not inside the mapped region */
3149 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
3150 return -EFAULT;
3151
3152 /*
3153 * May not be a start of buffer, set size appropriately
3154 * and advance us to the beginning.
3155 */
3156 offset = buf_addr - imu->ubuf;
3157 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06003158
3159 if (offset) {
3160 /*
3161 * Don't use iov_iter_advance() here, as it's really slow for
3162 * using the latter parts of a big fixed buffer - it iterates
3163 * over each segment manually. We can cheat a bit here, because
3164 * we know that:
3165 *
3166 * 1) it's a BVEC iter, we set it up
3167 * 2) all bvecs are PAGE_SIZE in size, except potentially the
3168 * first and last bvec
3169 *
3170 * So just find our index, and adjust the iterator afterwards.
3171 * If the offset is within the first bvec (or the whole first
3172 * bvec, just use iov_iter_advance(). This makes it easier
3173 * since we can just skip the first segment, which may not
3174 * be PAGE_SIZE aligned.
3175 */
3176 const struct bio_vec *bvec = imu->bvec;
3177
3178 if (offset <= bvec->bv_len) {
3179 iov_iter_advance(iter, offset);
3180 } else {
3181 unsigned long seg_skip;
3182
3183 /* skip first vec */
3184 offset -= bvec->bv_len;
3185 seg_skip = 1 + (offset >> PAGE_SHIFT);
3186
3187 iter->bvec = bvec + seg_skip;
3188 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02003189 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003190 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06003191 }
3192 }
3193
Pavel Begunkov847595d2021-02-04 13:52:06 +00003194 return 0;
Jens Axboeedafcce2019-01-09 09:16:05 -07003195}
3196
Jens Axboebcda7ba2020-02-23 16:42:51 -07003197static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock)
3198{
3199 if (needs_lock)
3200 mutex_unlock(&ctx->uring_lock);
3201}
3202
3203static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock)
3204{
3205 /*
3206 * "Normal" inline submissions always hold the uring_lock, since we
3207 * grab it from the system call. Same is true for the SQPOLL offload.
3208 * The only exception is when we've detached the request and issue it
3209 * from an async worker thread, grab the lock for that case.
3210 */
3211 if (needs_lock)
3212 mutex_lock(&ctx->uring_lock);
3213}
3214
3215static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len,
3216 int bgid, struct io_buffer *kbuf,
3217 bool needs_lock)
3218{
3219 struct io_buffer *head;
3220
3221 if (req->flags & REQ_F_BUFFER_SELECTED)
3222 return kbuf;
3223
3224 io_ring_submit_lock(req->ctx, needs_lock);
3225
3226 lockdep_assert_held(&req->ctx->uring_lock);
3227
3228 head = idr_find(&req->ctx->io_buffer_idr, bgid);
3229 if (head) {
3230 if (!list_empty(&head->list)) {
3231 kbuf = list_last_entry(&head->list, struct io_buffer,
3232 list);
3233 list_del(&kbuf->list);
3234 } else {
3235 kbuf = head;
3236 idr_remove(&req->ctx->io_buffer_idr, bgid);
3237 }
3238 if (*len > kbuf->len)
3239 *len = kbuf->len;
3240 } else {
3241 kbuf = ERR_PTR(-ENOBUFS);
3242 }
3243
3244 io_ring_submit_unlock(req->ctx, needs_lock);
3245
3246 return kbuf;
3247}
3248
Jens Axboe4d954c22020-02-27 07:31:19 -07003249static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len,
3250 bool needs_lock)
3251{
3252 struct io_buffer *kbuf;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003253 u16 bgid;
Jens Axboe4d954c22020-02-27 07:31:19 -07003254
3255 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003256 bgid = req->buf_index;
Jens Axboe4d954c22020-02-27 07:31:19 -07003257 kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock);
3258 if (IS_ERR(kbuf))
3259 return kbuf;
3260 req->rw.addr = (u64) (unsigned long) kbuf;
3261 req->flags |= REQ_F_BUFFER_SELECTED;
3262 return u64_to_user_ptr(kbuf->addr);
3263}
3264
3265#ifdef CONFIG_COMPAT
3266static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov,
3267 bool needs_lock)
3268{
3269 struct compat_iovec __user *uiov;
3270 compat_ssize_t clen;
3271 void __user *buf;
3272 ssize_t len;
3273
3274 uiov = u64_to_user_ptr(req->rw.addr);
3275 if (!access_ok(uiov, sizeof(*uiov)))
3276 return -EFAULT;
3277 if (__get_user(clen, &uiov->iov_len))
3278 return -EFAULT;
3279 if (clen < 0)
3280 return -EINVAL;
3281
3282 len = clen;
3283 buf = io_rw_buffer_select(req, &len, needs_lock);
3284 if (IS_ERR(buf))
3285 return PTR_ERR(buf);
3286 iov[0].iov_base = buf;
3287 iov[0].iov_len = (compat_size_t) len;
3288 return 0;
3289}
3290#endif
3291
3292static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3293 bool needs_lock)
3294{
3295 struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr);
3296 void __user *buf;
3297 ssize_t len;
3298
3299 if (copy_from_user(iov, uiov, sizeof(*uiov)))
3300 return -EFAULT;
3301
3302 len = iov[0].iov_len;
3303 if (len < 0)
3304 return -EINVAL;
3305 buf = io_rw_buffer_select(req, &len, needs_lock);
3306 if (IS_ERR(buf))
3307 return PTR_ERR(buf);
3308 iov[0].iov_base = buf;
3309 iov[0].iov_len = len;
3310 return 0;
3311}
3312
3313static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov,
3314 bool needs_lock)
3315{
Jens Axboedddb3e22020-06-04 11:27:01 -06003316 if (req->flags & REQ_F_BUFFER_SELECTED) {
3317 struct io_buffer *kbuf;
3318
3319 kbuf = (struct io_buffer *) (unsigned long) req->rw.addr;
3320 iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
3321 iov[0].iov_len = kbuf->len;
Jens Axboe4d954c22020-02-27 07:31:19 -07003322 return 0;
Jens Axboedddb3e22020-06-04 11:27:01 -06003323 }
Pavel Begunkovdd201662020-12-19 03:15:43 +00003324 if (req->rw.len != 1)
Jens Axboe4d954c22020-02-27 07:31:19 -07003325 return -EINVAL;
3326
3327#ifdef CONFIG_COMPAT
3328 if (req->ctx->compat)
3329 return io_compat_import(req, iov, needs_lock);
3330#endif
3331
3332 return __io_iov_buffer_select(req, iov, needs_lock);
3333}
3334
Pavel Begunkov847595d2021-02-04 13:52:06 +00003335static int io_import_iovec(int rw, struct io_kiocb *req, struct iovec **iovec,
3336 struct iov_iter *iter, bool needs_lock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003337{
Jens Axboe9adbd452019-12-20 08:45:55 -07003338 void __user *buf = u64_to_user_ptr(req->rw.addr);
3339 size_t sqe_len = req->rw.len;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003340 u8 opcode = req->opcode;
Jens Axboe4d954c22020-02-27 07:31:19 -07003341 ssize_t ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07003342
Pavel Begunkov7d009162019-11-25 23:14:40 +03003343 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07003344 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07003345 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07003346 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003347
Jens Axboebcda7ba2020-02-23 16:42:51 -07003348 /* buffer index only valid with fixed read/write, or buffer select */
Bijan Mottahedeh4f4eeba2020-05-19 14:52:49 -07003349 if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))
Jens Axboe9adbd452019-12-20 08:45:55 -07003350 return -EINVAL;
3351
Jens Axboe3a6820f2019-12-22 15:19:35 -07003352 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
Jens Axboebcda7ba2020-02-23 16:42:51 -07003353 if (req->flags & REQ_F_BUFFER_SELECT) {
Jens Axboe4d954c22020-02-27 07:31:19 -07003354 buf = io_rw_buffer_select(req, &sqe_len, needs_lock);
Pavel Begunkov867a23e2020-08-20 11:34:39 +03003355 if (IS_ERR(buf))
Jens Axboe4d954c22020-02-27 07:31:19 -07003356 return PTR_ERR(buf);
Jens Axboe3f9d6442020-03-11 12:27:04 -06003357 req->rw.len = sqe_len;
Jens Axboebcda7ba2020-02-23 16:42:51 -07003358 }
3359
Jens Axboe3a6820f2019-12-22 15:19:35 -07003360 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
3361 *iovec = NULL;
David Laight10fc72e2020-11-07 13:16:25 +00003362 return ret;
Jens Axboe3a6820f2019-12-22 15:19:35 -07003363 }
3364
Jens Axboe4d954c22020-02-27 07:31:19 -07003365 if (req->flags & REQ_F_BUFFER_SELECT) {
3366 ret = io_iov_buffer_select(req, *iovec, needs_lock);
Pavel Begunkov847595d2021-02-04 13:52:06 +00003367 if (!ret)
3368 iov_iter_init(iter, rw, *iovec, 1, (*iovec)->iov_len);
Jens Axboe4d954c22020-02-27 07:31:19 -07003369 *iovec = NULL;
3370 return ret;
3371 }
3372
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02003373 return __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter,
3374 req->ctx->compat);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003375}
3376
Jens Axboe0fef9482020-08-26 10:36:20 -06003377static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
3378{
Pavel Begunkov5b09e372020-09-30 22:57:15 +03003379 return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
Jens Axboe0fef9482020-08-26 10:36:20 -06003380}
3381
Jens Axboe32960612019-09-23 11:05:34 -06003382/*
3383 * For files that don't have ->read_iter() and ->write_iter(), handle them
3384 * by looping over ->read() or ->write() manually.
3385 */
Jens Axboe4017eb92020-10-22 14:14:12 -06003386static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter)
Jens Axboe32960612019-09-23 11:05:34 -06003387{
Jens Axboe4017eb92020-10-22 14:14:12 -06003388 struct kiocb *kiocb = &req->rw.kiocb;
3389 struct file *file = req->file;
Jens Axboe32960612019-09-23 11:05:34 -06003390 ssize_t ret = 0;
3391
3392 /*
3393 * Don't support polled IO through this interface, and we can't
3394 * support non-blocking either. For the latter, this just causes
3395 * the kiocb to be handled from an async context.
3396 */
3397 if (kiocb->ki_flags & IOCB_HIPRI)
3398 return -EOPNOTSUPP;
3399 if (kiocb->ki_flags & IOCB_NOWAIT)
3400 return -EAGAIN;
3401
3402 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003403 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06003404 ssize_t nr;
3405
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003406 if (!iov_iter_is_bvec(iter)) {
3407 iovec = iov_iter_iovec(iter);
3408 } else {
Jens Axboe4017eb92020-10-22 14:14:12 -06003409 iovec.iov_base = u64_to_user_ptr(req->rw.addr);
3410 iovec.iov_len = req->rw.len;
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03003411 }
3412
Jens Axboe32960612019-09-23 11:05:34 -06003413 if (rw == READ) {
3414 nr = file->f_op->read(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003415 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003416 } else {
3417 nr = file->f_op->write(file, iovec.iov_base,
Jens Axboe0fef9482020-08-26 10:36:20 -06003418 iovec.iov_len, io_kiocb_ppos(kiocb));
Jens Axboe32960612019-09-23 11:05:34 -06003419 }
3420
3421 if (nr < 0) {
3422 if (!ret)
3423 ret = nr;
3424 break;
3425 }
3426 ret += nr;
3427 if (nr != iovec.iov_len)
3428 break;
Jens Axboe4017eb92020-10-22 14:14:12 -06003429 req->rw.len -= nr;
3430 req->rw.addr += nr;
Jens Axboe32960612019-09-23 11:05:34 -06003431 iov_iter_advance(iter, nr);
3432 }
3433
3434 return ret;
3435}
3436
Jens Axboeff6165b2020-08-13 09:47:43 -06003437static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
3438 const struct iovec *fast_iov, struct iov_iter *iter)
Jens Axboef67676d2019-12-02 11:03:47 -07003439{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003440 struct io_async_rw *rw = req->async_data;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003441
Jens Axboeff6165b2020-08-13 09:47:43 -06003442 memcpy(&rw->iter, iter, sizeof(*iter));
Pavel Begunkovafb87652020-09-06 00:45:46 +03003443 rw->free_iovec = iovec;
Jens Axboe227c0c92020-08-13 11:51:40 -06003444 rw->bytes_done = 0;
Jens Axboeff6165b2020-08-13 09:47:43 -06003445 /* can only be fixed buffers, no need to do anything */
Pavel Begunkov9c3a2052020-11-23 23:20:27 +00003446 if (iov_iter_is_bvec(iter))
Jens Axboeff6165b2020-08-13 09:47:43 -06003447 return;
Pavel Begunkovb64e3442020-07-13 22:59:18 +03003448 if (!iovec) {
Jens Axboeff6165b2020-08-13 09:47:43 -06003449 unsigned iov_off = 0;
3450
3451 rw->iter.iov = rw->fast_iov;
3452 if (iter->iov != fast_iov) {
3453 iov_off = iter->iov - fast_iov;
3454 rw->iter.iov += iov_off;
3455 }
3456 if (rw->fast_iov != fast_iov)
3457 memcpy(rw->fast_iov + iov_off, fast_iov + iov_off,
Xiaoguang Wang45097da2020-04-08 22:29:58 +08003458 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003459 } else {
3460 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07003461 }
3462}
3463
Jens Axboee8c2bc12020-08-15 18:44:09 -07003464static inline int __io_alloc_async_data(struct io_kiocb *req)
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003465{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003466 WARN_ON_ONCE(!io_op_defs[req->opcode].async_size);
3467 req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL);
3468 return req->async_data == NULL;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003469}
3470
Jens Axboee8c2bc12020-08-15 18:44:09 -07003471static int io_alloc_async_data(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003472{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003473 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboed3656342019-12-18 09:50:26 -07003474 return 0;
Xiaoguang Wang3d9932a2020-03-27 15:36:52 +08003475
Jens Axboee8c2bc12020-08-15 18:44:09 -07003476 return __io_alloc_async_data(req);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003477}
3478
Jens Axboeff6165b2020-08-13 09:47:43 -06003479static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
3480 const struct iovec *fast_iov,
Jens Axboe227c0c92020-08-13 11:51:40 -06003481 struct iov_iter *iter, bool force)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003482{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003483 if (!force && !io_op_defs[req->opcode].needs_async_data)
Jens Axboe74566df2020-01-13 19:23:24 -07003484 return 0;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003485 if (!req->async_data) {
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003486 if (__io_alloc_async_data(req)) {
3487 kfree(iovec);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003488 return -ENOMEM;
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003489 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003490
Jens Axboeff6165b2020-08-13 09:47:43 -06003491 io_req_map_rw(req, iovec, fast_iov, iter);
Jens Axboe5d204bc2020-01-31 12:06:52 -07003492 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003493 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003494}
3495
Pavel Begunkov73debe62020-09-30 22:57:54 +03003496static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003497{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003498 struct io_async_rw *iorw = req->async_data;
Pavel Begunkovf4bff102020-09-06 00:45:45 +03003499 struct iovec *iov = iorw->fast_iov;
Pavel Begunkov847595d2021-02-04 13:52:06 +00003500 int ret;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003501
Pavel Begunkov2846c482020-11-07 13:16:27 +00003502 ret = io_import_iovec(rw, req, &iov, &iorw->iter, false);
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003503 if (unlikely(ret < 0))
3504 return ret;
3505
Pavel Begunkovab0b1962020-09-06 00:45:47 +03003506 iorw->bytes_done = 0;
3507 iorw->free_iovec = iov;
3508 if (iov)
3509 req->flags |= REQ_F_NEED_CLEANUP;
Pavel Begunkovc3e330a2020-07-13 22:59:19 +03003510 return 0;
3511}
3512
Pavel Begunkov73debe62020-09-30 22:57:54 +03003513static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003514{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003515 if (unlikely(!(req->file->f_mode & FMODE_READ)))
3516 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003517 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003518}
3519
Jens Axboec1dd91d2020-08-03 16:43:59 -06003520/*
3521 * This is our waitqueue callback handler, registered through lock_page_async()
3522 * when we initially tried to do the IO with the iocb armed our waitqueue.
3523 * This gets called when the page is unlocked, and we generally expect that to
3524 * happen when the page IO is completed and the page is now uptodate. This will
3525 * queue a task_work based retry of the operation, attempting to copy the data
3526 * again. If the latter fails because the page was NOT uptodate, then we will
3527 * do a thread based blocking retry of the operation. That's the unexpected
3528 * slow path.
3529 */
Jens Axboebcf5a062020-05-22 09:24:42 -06003530static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
3531 int sync, void *arg)
3532{
3533 struct wait_page_queue *wpq;
3534 struct io_kiocb *req = wait->private;
Jens Axboebcf5a062020-05-22 09:24:42 -06003535 struct wait_page_key *key = arg;
Jens Axboebcf5a062020-05-22 09:24:42 -06003536
3537 wpq = container_of(wait, struct wait_page_queue, wait);
3538
Linus Torvaldscdc8fcb2020-08-03 13:01:22 -07003539 if (!wake_page_match(wpq, key))
3540 return 0;
3541
Hao Xuc8d317a2020-09-29 20:00:45 +08003542 req->rw.kiocb.ki_flags &= ~IOCB_WAITQ;
Jens Axboebcf5a062020-05-22 09:24:42 -06003543 list_del_init(&wait->entry);
3544
Jens Axboebcf5a062020-05-22 09:24:42 -06003545 /* submit ref gets dropped, acquire a new one */
3546 refcount_inc(&req->refs);
Pavel Begunkov921b9052021-02-12 03:23:53 +00003547 io_req_task_queue(req);
Jens Axboebcf5a062020-05-22 09:24:42 -06003548 return 1;
3549}
3550
Jens Axboec1dd91d2020-08-03 16:43:59 -06003551/*
3552 * This controls whether a given IO request should be armed for async page
3553 * based retry. If we return false here, the request is handed to the async
3554 * worker threads for retry. If we're doing buffered reads on a regular file,
3555 * we prepare a private wait_page_queue entry and retry the operation. This
3556 * will either succeed because the page is now uptodate and unlocked, or it
3557 * will register a callback when the page is unlocked at IO completion. Through
3558 * that callback, io_uring uses task_work to setup a retry of the operation.
3559 * That retry will attempt the buffered read again. The retry will generally
3560 * succeed, or in rare cases where it fails, we then fall back to using the
3561 * async worker threads for a blocking retry.
3562 */
Jens Axboe227c0c92020-08-13 11:51:40 -06003563static bool io_rw_should_retry(struct io_kiocb *req)
Jens Axboebcf5a062020-05-22 09:24:42 -06003564{
Jens Axboee8c2bc12020-08-15 18:44:09 -07003565 struct io_async_rw *rw = req->async_data;
3566 struct wait_page_queue *wait = &rw->wpq;
Jens Axboebcf5a062020-05-22 09:24:42 -06003567 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboebcf5a062020-05-22 09:24:42 -06003568
3569 /* never retry for NOWAIT, we just complete with -EAGAIN */
3570 if (req->flags & REQ_F_NOWAIT)
3571 return false;
3572
Jens Axboe227c0c92020-08-13 11:51:40 -06003573 /* Only for buffered IO */
Jens Axboe3b2a4432020-08-16 10:58:43 -07003574 if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
Jens Axboebcf5a062020-05-22 09:24:42 -06003575 return false;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003576
Jens Axboebcf5a062020-05-22 09:24:42 -06003577 /*
3578 * just use poll if we can, and don't attempt if the fs doesn't
3579 * support callback based unlocks
3580 */
3581 if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
3582 return false;
3583
Jens Axboe3b2a4432020-08-16 10:58:43 -07003584 wait->wait.func = io_async_buf_func;
3585 wait->wait.private = req;
3586 wait->wait.flags = 0;
3587 INIT_LIST_HEAD(&wait->wait.entry);
3588 kiocb->ki_flags |= IOCB_WAITQ;
Hao Xuc8d317a2020-09-29 20:00:45 +08003589 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003590 kiocb->ki_waitq = wait;
Jens Axboe3b2a4432020-08-16 10:58:43 -07003591 return true;
Jens Axboebcf5a062020-05-22 09:24:42 -06003592}
3593
3594static int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter)
3595{
3596 if (req->file->f_op->read_iter)
3597 return call_read_iter(req->file, &req->rw.kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003598 else if (req->file->f_op->read)
Jens Axboe4017eb92020-10-22 14:14:12 -06003599 return loop_rw_iter(READ, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003600 else
3601 return -EINVAL;
Jens Axboebcf5a062020-05-22 09:24:42 -06003602}
3603
Pavel Begunkov889fca72021-02-10 00:03:09 +00003604static int io_read(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003605{
3606 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003607 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003608 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003609 struct io_async_rw *rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003610 ssize_t io_size, ret, ret2;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003611 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003612
Pavel Begunkov2846c482020-11-07 13:16:27 +00003613 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003614 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003615 iovec = NULL;
3616 } else {
3617 ret = io_import_iovec(READ, req, &iovec, iter, !force_nonblock);
3618 if (ret < 0)
3619 return ret;
3620 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003621 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003622 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003623
Jens Axboefd6c2e42019-12-18 12:19:41 -07003624 /* Ensure we clear previously set non-block flag */
3625 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07003626 kiocb->ki_flags &= ~IOCB_NOWAIT;
Pavel Begunkova88fc402020-09-30 22:57:53 +03003627 else
3628 kiocb->ki_flags |= IOCB_NOWAIT;
3629
Pavel Begunkov24c74672020-06-21 13:09:51 +03003630 /* If the file doesn't support async, just async punt */
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003631 if (force_nonblock && !io_file_supports_async(req->file, READ)) {
3632 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003633 return ret ?: -EAGAIN;
Pavel Begunkov6713e7a2021-02-04 13:51:59 +00003634 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003635
Pavel Begunkov632546c2020-11-07 13:16:26 +00003636 ret = rw_verify_area(READ, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003637 if (unlikely(ret)) {
3638 kfree(iovec);
3639 return ret;
3640 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003641
Jens Axboe227c0c92020-08-13 11:51:40 -06003642 ret = io_iter_do_read(req, iter);
Jens Axboe32960612019-09-23 11:05:34 -06003643
Pavel Begunkov57cd6572021-02-01 18:59:56 +00003644 if (ret == -EIOCBQUEUED) {
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003645 goto out_free;
Jens Axboe227c0c92020-08-13 11:51:40 -06003646 } else if (ret == -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003647 /* IOPOLL retry should happen for io-wq threads */
3648 if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboef91daf52020-08-15 15:58:42 -07003649 goto done;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003650 /* no retry on NONBLOCK nor RWF_NOWAIT */
3651 if (req->flags & REQ_F_NOWAIT)
Jens Axboe355afae2020-09-02 09:30:31 -06003652 goto done;
Jens Axboe84216312020-08-24 11:45:26 -06003653 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003654 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboef38c7e32020-09-25 15:23:43 -06003655 ret = 0;
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003656 } else if (ret <= 0 || ret == io_size || !force_nonblock ||
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003657 (req->flags & REQ_F_NOWAIT) || !(req->flags & REQ_F_ISREG)) {
Pavel Begunkov7335e3b2021-02-04 13:52:02 +00003658 /* read all, failed, already did sync or don't want to retry */
Jens Axboe00d23d52020-08-25 12:59:22 -06003659 goto done;
Jens Axboe227c0c92020-08-13 11:51:40 -06003660 }
3661
Jens Axboe227c0c92020-08-13 11:51:40 -06003662 ret2 = io_setup_async_rw(req, iovec, inline_vecs, iter, true);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003663 if (ret2)
3664 return ret2;
3665
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003666 iovec = NULL;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003667 rw = req->async_data;
Jens Axboe227c0c92020-08-13 11:51:40 -06003668 /* now use our persistent iterator, if we aren't already */
Jens Axboee8c2bc12020-08-15 18:44:09 -07003669 iter = &rw->iter;
Jens Axboe227c0c92020-08-13 11:51:40 -06003670
Pavel Begunkovb23df912021-02-04 13:52:04 +00003671 do {
3672 io_size -= ret;
3673 rw->bytes_done += ret;
3674 /* if we can retry, do so with the callbacks armed */
3675 if (!io_rw_should_retry(req)) {
3676 kiocb->ki_flags &= ~IOCB_WAITQ;
3677 return -EAGAIN;
3678 }
3679
3680 /*
3681 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
3682 * we get -EIOCBQUEUED, then we'll get a notification when the
3683 * desired page gets unlocked. We can also get a partial read
3684 * here, and if we do, then just retry at the new offset.
3685 */
3686 ret = io_iter_do_read(req, iter);
3687 if (ret == -EIOCBQUEUED)
3688 return 0;
Jens Axboe227c0c92020-08-13 11:51:40 -06003689 /* we got some bytes, but not all. retry. */
Pavel Begunkovb23df912021-02-04 13:52:04 +00003690 } while (ret > 0 && ret < io_size);
Jens Axboe227c0c92020-08-13 11:51:40 -06003691done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003692 kiocb_done(kiocb, ret, issue_flags);
Pavel Begunkovfe1cdd52021-02-17 21:02:36 +00003693out_free:
3694 /* it's faster to check here then delegate to kfree */
3695 if (iovec)
3696 kfree(iovec);
Pavel Begunkov5ea5dd42021-02-04 13:52:03 +00003697 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003698}
3699
Pavel Begunkov73debe62020-09-30 22:57:54 +03003700static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003701{
Jens Axboe3529d8c2019-12-19 18:24:38 -07003702 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
3703 return -EBADF;
Pavel Begunkov93642ef2021-02-18 18:29:44 +00003704 return io_prep_rw(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07003705}
3706
Pavel Begunkov889fca72021-02-10 00:03:09 +00003707static int io_write(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003708{
3709 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07003710 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboeff6165b2020-08-13 09:47:43 -06003711 struct iov_iter __iter, *iter = &__iter;
Jens Axboee8c2bc12020-08-15 18:44:09 -07003712 struct io_async_rw *rw = req->async_data;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003713 ssize_t ret, ret2, io_size;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003714 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003715
Pavel Begunkov2846c482020-11-07 13:16:27 +00003716 if (rw) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07003717 iter = &rw->iter;
Pavel Begunkov2846c482020-11-07 13:16:27 +00003718 iovec = NULL;
3719 } else {
3720 ret = io_import_iovec(WRITE, req, &iovec, iter, !force_nonblock);
3721 if (ret < 0)
3722 return ret;
3723 }
Pavel Begunkov632546c2020-11-07 13:16:26 +00003724 io_size = iov_iter_count(iter);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003725 req->result = io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003726
Jens Axboefd6c2e42019-12-18 12:19:41 -07003727 /* Ensure we clear previously set non-block flag */
3728 if (!force_nonblock)
Pavel Begunkova88fc402020-09-30 22:57:53 +03003729 kiocb->ki_flags &= ~IOCB_NOWAIT;
3730 else
3731 kiocb->ki_flags |= IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07003732
Pavel Begunkov24c74672020-06-21 13:09:51 +03003733 /* If the file doesn't support async, just async punt */
Jens Axboeaf197f52020-04-28 13:15:06 -06003734 if (force_nonblock && !io_file_supports_async(req->file, WRITE))
Jens Axboef67676d2019-12-02 11:03:47 -07003735 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07003736
Jens Axboe10d59342019-12-09 20:16:22 -07003737 /* file path doesn't support NOWAIT for non-direct_IO */
3738 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
3739 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07003740 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06003741
Pavel Begunkov632546c2020-11-07 13:16:26 +00003742 ret = rw_verify_area(WRITE, req->file, io_kiocb_ppos(kiocb), io_size);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003743 if (unlikely(ret))
3744 goto out_free;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003745
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003746 /*
3747 * Open-code file_start_write here to grab freeze protection,
3748 * which will be released by another thread in
3749 * io_complete_rw(). Fool lockdep by telling it the lock got
3750 * released so that it doesn't complain about the held lock when
3751 * we return to userspace.
3752 */
3753 if (req->flags & REQ_F_ISREG) {
Darrick J. Wong8a3c84b2020-11-10 16:50:21 -08003754 sb_start_write(file_inode(req->file)->i_sb);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003755 __sb_writers_release(file_inode(req->file)->i_sb,
3756 SB_FREEZE_WRITE);
3757 }
3758 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01003759
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003760 if (req->file->f_op->write_iter)
Jens Axboeff6165b2020-08-13 09:47:43 -06003761 ret2 = call_write_iter(req->file, kiocb, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003762 else if (req->file->f_op->write)
Jens Axboe4017eb92020-10-22 14:14:12 -06003763 ret2 = loop_rw_iter(WRITE, req, iter);
Guoyu Huang2dd21112020-08-05 03:53:50 -07003764 else
3765 ret2 = -EINVAL;
Jens Axboe4ed734b2020-03-20 11:23:41 -06003766
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003767 /*
3768 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
3769 * retry them without IOCB_NOWAIT.
3770 */
3771 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
3772 ret2 = -EAGAIN;
Pavel Begunkov75c668c2021-02-04 13:52:05 +00003773 /* no retry on NONBLOCK nor RWF_NOWAIT */
3774 if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
Jens Axboe355afae2020-09-02 09:30:31 -06003775 goto done;
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003776 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboeeefdf302020-08-27 16:40:19 -06003777 /* IOPOLL retry should happen for io-wq threads */
3778 if ((req->ctx->flags & IORING_SETUP_IOPOLL) && ret2 == -EAGAIN)
3779 goto copy_iov;
Jens Axboe355afae2020-09-02 09:30:31 -06003780done:
Pavel Begunkov889fca72021-02-10 00:03:09 +00003781 kiocb_done(kiocb, ret2, issue_flags);
Pavel Begunkovfa15baf2020-08-01 13:50:02 +03003782 } else {
Jens Axboef67676d2019-12-02 11:03:47 -07003783copy_iov:
Jens Axboe84216312020-08-24 11:45:26 -06003784 /* some cases will consume bytes even on error returns */
Pavel Begunkov632546c2020-11-07 13:16:26 +00003785 iov_iter_revert(iter, io_size - iov_iter_count(iter));
Jens Axboe227c0c92020-08-13 11:51:40 -06003786 ret = io_setup_async_rw(req, iovec, inline_vecs, iter, false);
Pavel Begunkov6bf985d2021-02-04 13:52:01 +00003787 return ret ?: -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003788 }
Jens Axboe31b51512019-01-18 22:56:34 -07003789out_free:
Pavel Begunkovf261c162020-08-20 11:34:10 +03003790 /* it's reportedly faster than delegating the null check to kfree() */
Pavel Begunkov252917c2020-07-13 22:59:20 +03003791 if (iovec)
Xiaoguang Wang6f2cc162020-06-18 15:01:56 +08003792 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003793 return ret;
3794}
3795
Jens Axboe80a261f2020-09-28 14:23:58 -06003796static int io_renameat_prep(struct io_kiocb *req,
3797 const struct io_uring_sqe *sqe)
3798{
3799 struct io_rename *ren = &req->rename;
3800 const char __user *oldf, *newf;
3801
3802 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3803 return -EBADF;
3804
3805 ren->old_dfd = READ_ONCE(sqe->fd);
3806 oldf = u64_to_user_ptr(READ_ONCE(sqe->addr));
3807 newf = u64_to_user_ptr(READ_ONCE(sqe->addr2));
3808 ren->new_dfd = READ_ONCE(sqe->len);
3809 ren->flags = READ_ONCE(sqe->rename_flags);
3810
3811 ren->oldpath = getname(oldf);
3812 if (IS_ERR(ren->oldpath))
3813 return PTR_ERR(ren->oldpath);
3814
3815 ren->newpath = getname(newf);
3816 if (IS_ERR(ren->newpath)) {
3817 putname(ren->oldpath);
3818 return PTR_ERR(ren->newpath);
3819 }
3820
3821 req->flags |= REQ_F_NEED_CLEANUP;
3822 return 0;
3823}
3824
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003825static int io_renameat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe80a261f2020-09-28 14:23:58 -06003826{
3827 struct io_rename *ren = &req->rename;
3828 int ret;
3829
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003830 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe80a261f2020-09-28 14:23:58 -06003831 return -EAGAIN;
3832
3833 ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd,
3834 ren->newpath, ren->flags);
3835
3836 req->flags &= ~REQ_F_NEED_CLEANUP;
3837 if (ret < 0)
3838 req_set_fail_links(req);
3839 io_req_complete(req, ret);
3840 return 0;
3841}
3842
Jens Axboe14a11432020-09-28 14:27:37 -06003843static int io_unlinkat_prep(struct io_kiocb *req,
3844 const struct io_uring_sqe *sqe)
3845{
3846 struct io_unlink *un = &req->unlink;
3847 const char __user *fname;
3848
3849 if (unlikely(req->flags & REQ_F_FIXED_FILE))
3850 return -EBADF;
3851
3852 un->dfd = READ_ONCE(sqe->fd);
3853
3854 un->flags = READ_ONCE(sqe->unlink_flags);
3855 if (un->flags & ~AT_REMOVEDIR)
3856 return -EINVAL;
3857
3858 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
3859 un->filename = getname(fname);
3860 if (IS_ERR(un->filename))
3861 return PTR_ERR(un->filename);
3862
3863 req->flags |= REQ_F_NEED_CLEANUP;
3864 return 0;
3865}
3866
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003867static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe14a11432020-09-28 14:27:37 -06003868{
3869 struct io_unlink *un = &req->unlink;
3870 int ret;
3871
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003872 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe14a11432020-09-28 14:27:37 -06003873 return -EAGAIN;
3874
3875 if (un->flags & AT_REMOVEDIR)
3876 ret = do_rmdir(un->dfd, un->filename);
3877 else
3878 ret = do_unlinkat(un->dfd, un->filename);
3879
3880 req->flags &= ~REQ_F_NEED_CLEANUP;
3881 if (ret < 0)
3882 req_set_fail_links(req);
3883 io_req_complete(req, ret);
3884 return 0;
3885}
3886
Jens Axboe36f4fa62020-09-05 11:14:22 -06003887static int io_shutdown_prep(struct io_kiocb *req,
3888 const struct io_uring_sqe *sqe)
3889{
3890#if defined(CONFIG_NET)
3891 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3892 return -EINVAL;
3893 if (sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags ||
3894 sqe->buf_index)
3895 return -EINVAL;
3896
3897 req->shutdown.how = READ_ONCE(sqe->len);
3898 return 0;
3899#else
3900 return -EOPNOTSUPP;
3901#endif
3902}
3903
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003904static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003905{
3906#if defined(CONFIG_NET)
3907 struct socket *sock;
3908 int ret;
3909
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003910 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe36f4fa62020-09-05 11:14:22 -06003911 return -EAGAIN;
3912
Linus Torvalds48aba792020-12-16 12:44:05 -08003913 sock = sock_from_file(req->file);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003914 if (unlikely(!sock))
Linus Torvalds48aba792020-12-16 12:44:05 -08003915 return -ENOTSOCK;
Jens Axboe36f4fa62020-09-05 11:14:22 -06003916
3917 ret = __sys_shutdown_sock(sock, req->shutdown.how);
Jens Axboea1464682020-12-14 20:57:27 -07003918 if (ret < 0)
3919 req_set_fail_links(req);
Jens Axboe36f4fa62020-09-05 11:14:22 -06003920 io_req_complete(req, ret);
3921 return 0;
3922#else
3923 return -EOPNOTSUPP;
3924#endif
3925}
3926
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003927static int __io_splice_prep(struct io_kiocb *req,
3928 const struct io_uring_sqe *sqe)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003929{
3930 struct io_splice* sp = &req->splice;
3931 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003932
Pavel Begunkov3232dd02020-06-03 18:03:22 +03003933 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3934 return -EINVAL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003935
3936 sp->file_in = NULL;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003937 sp->len = READ_ONCE(sqe->len);
3938 sp->flags = READ_ONCE(sqe->splice_flags);
3939
3940 if (unlikely(sp->flags & ~valid_flags))
3941 return -EINVAL;
3942
Pavel Begunkov8371adf2020-10-10 18:34:08 +01003943 sp->file_in = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in),
3944 (sp->flags & SPLICE_F_FD_IN_FIXED));
3945 if (!sp->file_in)
3946 return -EBADF;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003947 req->flags |= REQ_F_NEED_CLEANUP;
3948
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003949 if (!S_ISREG(file_inode(sp->file_in)->i_mode)) {
3950 /*
3951 * Splice operation will be punted aync, and here need to
3952 * modify io_wq_work.flags, so initialize io_wq_work firstly.
3953 */
3954 io_req_init_async(req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003955 req->work.flags |= IO_WQ_WORK_UNBOUND;
Xiaoguang Wang7cdaf582020-06-10 19:41:19 +08003956 }
Pavel Begunkov7d67af22020-02-24 11:32:45 +03003957
3958 return 0;
3959}
3960
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003961static int io_tee_prep(struct io_kiocb *req,
3962 const struct io_uring_sqe *sqe)
3963{
3964 if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off))
3965 return -EINVAL;
3966 return __io_splice_prep(req, sqe);
3967}
3968
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003969static int io_tee(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003970{
3971 struct io_splice *sp = &req->splice;
3972 struct file *in = sp->file_in;
3973 struct file *out = sp->file_out;
3974 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
3975 long ret = 0;
3976
Pavel Begunkov45d189c2021-02-10 00:03:07 +00003977 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003978 return -EAGAIN;
3979 if (sp->len)
3980 ret = do_tee(in, out, sp->len, flags);
3981
3982 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
3983 req->flags &= ~REQ_F_NEED_CLEANUP;
3984
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003985 if (ret != sp->len)
3986 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06003987 io_req_complete(req, ret);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03003988 return 0;
3989}
3990
3991static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
3992{
3993 struct io_splice* sp = &req->splice;
3994
3995 sp->off_in = READ_ONCE(sqe->splice_off_in);
3996 sp->off_out = READ_ONCE(sqe->off);
3997 return __io_splice_prep(req, sqe);
3998}
3999
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004000static int io_splice(struct io_kiocb *req, unsigned int issue_flags)
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004001{
4002 struct io_splice *sp = &req->splice;
4003 struct file *in = sp->file_in;
4004 struct file *out = sp->file_out;
4005 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
4006 loff_t *poff_in, *poff_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004007 long ret = 0;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004008
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004009 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov2fb3e822020-05-01 17:09:38 +03004010 return -EAGAIN;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004011
4012 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
4013 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
Pavel Begunkovc9687422020-05-04 23:00:54 +03004014
Jens Axboe948a7742020-05-17 14:21:38 -06004015 if (sp->len)
Pavel Begunkovc9687422020-05-04 23:00:54 +03004016 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004017
4018 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
4019 req->flags &= ~REQ_F_NEED_CLEANUP;
4020
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004021 if (ret != sp->len)
4022 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004023 io_req_complete(req, ret);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004024 return 0;
4025}
4026
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027/*
4028 * IORING_OP_NOP just posts a completion event, nothing else.
4029 */
Pavel Begunkov889fca72021-02-10 00:03:09 +00004030static int io_nop(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004031{
4032 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004033
Jens Axboedef596e2019-01-09 08:59:42 -07004034 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4035 return -EINVAL;
4036
Pavel Begunkov889fca72021-02-10 00:03:09 +00004037 __io_req_complete(req, issue_flags, 0, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038 return 0;
4039}
4040
Pavel Begunkov1155c762021-02-18 18:29:38 +00004041static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004042{
Jens Axboe6b063142019-01-10 22:13:58 -07004043 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004044
Jens Axboe09bb8392019-03-13 12:39:28 -06004045 if (!req->file)
4046 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004047
Jens Axboe6b063142019-01-10 22:13:58 -07004048 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07004049 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07004050 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004051 return -EINVAL;
4052
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004053 req->sync.flags = READ_ONCE(sqe->fsync_flags);
4054 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
4055 return -EINVAL;
4056
4057 req->sync.off = READ_ONCE(sqe->off);
4058 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004059 return 0;
4060}
4061
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004062static int io_fsync(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe78912932020-01-14 22:09:06 -07004063{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004064 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004065 int ret;
4066
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004067 /* fsync always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004068 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004069 return -EAGAIN;
4070
Jens Axboe9adbd452019-12-20 08:45:55 -07004071 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004072 end > 0 ? end : LLONG_MAX,
4073 req->sync.flags & IORING_FSYNC_DATASYNC);
4074 if (ret < 0)
4075 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004076 io_req_complete(req, ret);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004077 return 0;
4078}
4079
Jens Axboed63d1b52019-12-10 10:38:56 -07004080static int io_fallocate_prep(struct io_kiocb *req,
4081 const struct io_uring_sqe *sqe)
4082{
4083 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
4084 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004085 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4086 return -EINVAL;
Jens Axboed63d1b52019-12-10 10:38:56 -07004087
4088 req->sync.off = READ_ONCE(sqe->off);
4089 req->sync.len = READ_ONCE(sqe->addr);
4090 req->sync.mode = READ_ONCE(sqe->len);
4091 return 0;
4092}
4093
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004094static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboed63d1b52019-12-10 10:38:56 -07004095{
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004096 int ret;
Jens Axboed63d1b52019-12-10 10:38:56 -07004097
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004098 /* fallocate always requiring blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004099 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004100 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004101 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
4102 req->sync.len);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004103 if (ret < 0)
4104 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004105 io_req_complete(req, ret);
Jens Axboed63d1b52019-12-10 10:38:56 -07004106 return 0;
4107}
4108
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004109static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004110{
Jens Axboef8748882020-01-08 17:47:02 -07004111 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004112 int ret;
4113
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004114 if (unlikely(sqe->ioprio || sqe->buf_index))
Jens Axboe15b71ab2019-12-11 11:20:36 -07004115 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004116 if (unlikely(req->flags & REQ_F_FIXED_FILE))
Jens Axboecf3040c2020-02-06 21:31:40 -07004117 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004118
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004119 /* open.how should be already initialised */
4120 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
Jens Axboe08a1d26eb2020-04-08 09:20:54 -06004121 req->open.how.flags |= O_LARGEFILE;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004122
Pavel Begunkov25e72d12020-06-03 18:03:23 +03004123 req->open.dfd = READ_ONCE(sqe->fd);
4124 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboef8748882020-01-08 17:47:02 -07004125 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004126 if (IS_ERR(req->open.filename)) {
4127 ret = PTR_ERR(req->open.filename);
4128 req->open.filename = NULL;
4129 return ret;
4130 }
Jens Axboe4022e7a2020-03-19 19:23:18 -06004131 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004132 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004133 return 0;
4134}
4135
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004136static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4137{
4138 u64 flags, mode;
4139
Jens Axboe14587a462020-09-05 11:36:08 -06004140 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004141 return -EINVAL;
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004142 mode = READ_ONCE(sqe->len);
4143 flags = READ_ONCE(sqe->open_flags);
4144 req->open.how = build_open_how(flags, mode);
4145 return __io_openat_prep(req, sqe);
4146}
4147
Jens Axboecebdb982020-01-08 17:59:24 -07004148static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4149{
4150 struct open_how __user *how;
Jens Axboecebdb982020-01-08 17:59:24 -07004151 size_t len;
4152 int ret;
4153
Jens Axboe14587a462020-09-05 11:36:08 -06004154 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe4eb8dde2020-09-18 19:36:24 -06004155 return -EINVAL;
Jens Axboecebdb982020-01-08 17:59:24 -07004156 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4157 len = READ_ONCE(sqe->len);
Jens Axboecebdb982020-01-08 17:59:24 -07004158 if (len < OPEN_HOW_SIZE_VER0)
4159 return -EINVAL;
4160
4161 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
4162 len);
4163 if (ret)
4164 return ret;
4165
Pavel Begunkovec65fea2020-06-03 18:03:24 +03004166 return __io_openat_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07004167}
4168
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004169static int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe15b71ab2019-12-11 11:20:36 -07004170{
4171 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004172 struct file *file;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004173 bool nonblock_set;
4174 bool resolve_nonblock;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004175 int ret;
4176
Jens Axboecebdb982020-01-08 17:59:24 -07004177 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004178 if (ret)
4179 goto err;
Jens Axboe3a81fd02020-12-10 12:25:36 -07004180 nonblock_set = op.open_flag & O_NONBLOCK;
4181 resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004182 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004183 /*
4184 * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
4185 * it'll always -EAGAIN
4186 */
4187 if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE))
4188 return -EAGAIN;
4189 op.lookup_flags |= LOOKUP_CACHED;
4190 op.open_flag |= O_NONBLOCK;
4191 }
Jens Axboe15b71ab2019-12-11 11:20:36 -07004192
Jens Axboe4022e7a2020-03-19 19:23:18 -06004193 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004194 if (ret < 0)
4195 goto err;
4196
4197 file = do_filp_open(req->open.dfd, req->open.filename, &op);
Jens Axboe3a81fd02020-12-10 12:25:36 -07004198 /* only retry if RESOLVE_CACHED wasn't already set by application */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004199 if ((!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK)) &&
4200 file == ERR_PTR(-EAGAIN)) {
Jens Axboe3a81fd02020-12-10 12:25:36 -07004201 /*
4202 * We could hang on to this 'fd', but seems like marginal
4203 * gain for something that is now known to be a slower path.
4204 * So just put it, and we'll get a new one when we retry.
4205 */
4206 put_unused_fd(ret);
4207 return -EAGAIN;
4208 }
4209
Jens Axboe15b71ab2019-12-11 11:20:36 -07004210 if (IS_ERR(file)) {
4211 put_unused_fd(ret);
4212 ret = PTR_ERR(file);
4213 } else {
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004214 if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
Jens Axboe3a81fd02020-12-10 12:25:36 -07004215 file->f_flags &= ~O_NONBLOCK;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004216 fsnotify_open(file);
4217 fd_install(ret, file);
4218 }
4219err:
4220 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004221 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004222 if (ret < 0)
4223 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004224 io_req_complete(req, ret);
Jens Axboe15b71ab2019-12-11 11:20:36 -07004225 return 0;
4226}
4227
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004228static int io_openat(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboecebdb982020-01-08 17:59:24 -07004229{
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004230 return io_openat2(req, issue_flags & IO_URING_F_NONBLOCK);
Jens Axboecebdb982020-01-08 17:59:24 -07004231}
4232
Jens Axboe067524e2020-03-02 16:32:28 -07004233static int io_remove_buffers_prep(struct io_kiocb *req,
4234 const struct io_uring_sqe *sqe)
4235{
4236 struct io_provide_buf *p = &req->pbuf;
4237 u64 tmp;
4238
4239 if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off)
4240 return -EINVAL;
4241
4242 tmp = READ_ONCE(sqe->fd);
4243 if (!tmp || tmp > USHRT_MAX)
4244 return -EINVAL;
4245
4246 memset(p, 0, sizeof(*p));
4247 p->nbufs = tmp;
4248 p->bgid = READ_ONCE(sqe->buf_group);
4249 return 0;
4250}
4251
4252static int __io_remove_buffers(struct io_ring_ctx *ctx, struct io_buffer *buf,
4253 int bgid, unsigned nbufs)
4254{
4255 unsigned i = 0;
4256
4257 /* shouldn't happen */
4258 if (!nbufs)
4259 return 0;
4260
4261 /* the head kbuf is the list itself */
4262 while (!list_empty(&buf->list)) {
4263 struct io_buffer *nxt;
4264
4265 nxt = list_first_entry(&buf->list, struct io_buffer, list);
4266 list_del(&nxt->list);
4267 kfree(nxt);
4268 if (++i == nbufs)
4269 return i;
4270 }
4271 i++;
4272 kfree(buf);
4273 idr_remove(&ctx->io_buffer_idr, bgid);
4274
4275 return i;
4276}
4277
Pavel Begunkov889fca72021-02-10 00:03:09 +00004278static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe067524e2020-03-02 16:32:28 -07004279{
4280 struct io_provide_buf *p = &req->pbuf;
4281 struct io_ring_ctx *ctx = req->ctx;
4282 struct io_buffer *head;
4283 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004284 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe067524e2020-03-02 16:32:28 -07004285
4286 io_ring_submit_lock(ctx, !force_nonblock);
4287
4288 lockdep_assert_held(&ctx->uring_lock);
4289
4290 ret = -ENOENT;
4291 head = idr_find(&ctx->io_buffer_idr, p->bgid);
4292 if (head)
4293 ret = __io_remove_buffers(ctx, head, p->bgid, p->nbufs);
Jens Axboe067524e2020-03-02 16:32:28 -07004294 if (ret < 0)
4295 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004296
4297 /* need to hold the lock to complete IOPOLL requests */
4298 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004299 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004300 io_ring_submit_unlock(ctx, !force_nonblock);
4301 } else {
4302 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004303 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004304 }
Jens Axboe067524e2020-03-02 16:32:28 -07004305 return 0;
4306}
4307
Jens Axboeddf0322d2020-02-23 16:41:33 -07004308static int io_provide_buffers_prep(struct io_kiocb *req,
4309 const struct io_uring_sqe *sqe)
4310{
4311 struct io_provide_buf *p = &req->pbuf;
4312 u64 tmp;
4313
4314 if (sqe->ioprio || sqe->rw_flags)
4315 return -EINVAL;
4316
4317 tmp = READ_ONCE(sqe->fd);
4318 if (!tmp || tmp > USHRT_MAX)
4319 return -E2BIG;
4320 p->nbufs = tmp;
4321 p->addr = READ_ONCE(sqe->addr);
4322 p->len = READ_ONCE(sqe->len);
4323
Bijan Mottahedehefe68c12020-06-04 18:01:52 -07004324 if (!access_ok(u64_to_user_ptr(p->addr), (p->len * p->nbufs)))
Jens Axboeddf0322d2020-02-23 16:41:33 -07004325 return -EFAULT;
4326
4327 p->bgid = READ_ONCE(sqe->buf_group);
4328 tmp = READ_ONCE(sqe->off);
4329 if (tmp > USHRT_MAX)
4330 return -E2BIG;
4331 p->bid = tmp;
4332 return 0;
4333}
4334
4335static int io_add_buffers(struct io_provide_buf *pbuf, struct io_buffer **head)
4336{
4337 struct io_buffer *buf;
4338 u64 addr = pbuf->addr;
4339 int i, bid = pbuf->bid;
4340
4341 for (i = 0; i < pbuf->nbufs; i++) {
4342 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
4343 if (!buf)
4344 break;
4345
4346 buf->addr = addr;
4347 buf->len = pbuf->len;
4348 buf->bid = bid;
4349 addr += pbuf->len;
4350 bid++;
4351 if (!*head) {
4352 INIT_LIST_HEAD(&buf->list);
4353 *head = buf;
4354 } else {
4355 list_add_tail(&buf->list, &(*head)->list);
4356 }
4357 }
4358
4359 return i ? i : -ENOMEM;
4360}
4361
Pavel Begunkov889fca72021-02-10 00:03:09 +00004362static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeddf0322d2020-02-23 16:41:33 -07004363{
4364 struct io_provide_buf *p = &req->pbuf;
4365 struct io_ring_ctx *ctx = req->ctx;
4366 struct io_buffer *head, *list;
4367 int ret = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004368 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboeddf0322d2020-02-23 16:41:33 -07004369
4370 io_ring_submit_lock(ctx, !force_nonblock);
4371
4372 lockdep_assert_held(&ctx->uring_lock);
4373
4374 list = head = idr_find(&ctx->io_buffer_idr, p->bgid);
4375
4376 ret = io_add_buffers(p, &head);
4377 if (ret < 0)
4378 goto out;
4379
4380 if (!list) {
4381 ret = idr_alloc(&ctx->io_buffer_idr, head, p->bgid, p->bgid + 1,
4382 GFP_KERNEL);
4383 if (ret < 0) {
Jens Axboe067524e2020-03-02 16:32:28 -07004384 __io_remove_buffers(ctx, head, p->bgid, -1U);
Jens Axboeddf0322d2020-02-23 16:41:33 -07004385 goto out;
4386 }
4387 }
4388out:
Jens Axboeddf0322d2020-02-23 16:41:33 -07004389 if (ret < 0)
4390 req_set_fail_links(req);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004391
4392 /* need to hold the lock to complete IOPOLL requests */
4393 if (ctx->flags & IORING_SETUP_IOPOLL) {
Pavel Begunkov889fca72021-02-10 00:03:09 +00004394 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004395 io_ring_submit_unlock(ctx, !force_nonblock);
4396 } else {
4397 io_ring_submit_unlock(ctx, !force_nonblock);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004398 __io_req_complete(req, issue_flags, ret, 0);
Pavel Begunkov31bff9a2020-12-06 22:22:43 +00004399 }
Jens Axboeddf0322d2020-02-23 16:41:33 -07004400 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004401}
4402
Jens Axboe3e4827b2020-01-08 15:18:09 -07004403static int io_epoll_ctl_prep(struct io_kiocb *req,
4404 const struct io_uring_sqe *sqe)
4405{
4406#if defined(CONFIG_EPOLL)
4407 if (sqe->ioprio || sqe->buf_index)
4408 return -EINVAL;
Jens Axboe6ca56f82020-09-18 16:51:19 -06004409 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004410 return -EINVAL;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004411
4412 req->epoll.epfd = READ_ONCE(sqe->fd);
4413 req->epoll.op = READ_ONCE(sqe->len);
4414 req->epoll.fd = READ_ONCE(sqe->off);
4415
4416 if (ep_op_has_event(req->epoll.op)) {
4417 struct epoll_event __user *ev;
4418
4419 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
4420 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
4421 return -EFAULT;
4422 }
4423
4424 return 0;
4425#else
4426 return -EOPNOTSUPP;
4427#endif
4428}
4429
Pavel Begunkov889fca72021-02-10 00:03:09 +00004430static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe3e4827b2020-01-08 15:18:09 -07004431{
4432#if defined(CONFIG_EPOLL)
4433 struct io_epoll *ie = &req->epoll;
4434 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004435 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004436
4437 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
4438 if (force_nonblock && ret == -EAGAIN)
4439 return -EAGAIN;
4440
4441 if (ret < 0)
4442 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004443 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe3e4827b2020-01-08 15:18:09 -07004444 return 0;
4445#else
4446 return -EOPNOTSUPP;
4447#endif
4448}
4449
Jens Axboec1ca7572019-12-25 22:18:28 -07004450static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4451{
4452#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4453 if (sqe->ioprio || sqe->buf_index || sqe->off)
4454 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004455 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4456 return -EINVAL;
Jens Axboec1ca7572019-12-25 22:18:28 -07004457
4458 req->madvise.addr = READ_ONCE(sqe->addr);
4459 req->madvise.len = READ_ONCE(sqe->len);
4460 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
4461 return 0;
4462#else
4463 return -EOPNOTSUPP;
4464#endif
4465}
4466
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004467static int io_madvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboec1ca7572019-12-25 22:18:28 -07004468{
4469#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
4470 struct io_madvise *ma = &req->madvise;
4471 int ret;
4472
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004473 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboec1ca7572019-12-25 22:18:28 -07004474 return -EAGAIN;
4475
Minchan Kim0726b012020-10-17 16:14:50 -07004476 ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice);
Jens Axboec1ca7572019-12-25 22:18:28 -07004477 if (ret < 0)
4478 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004479 io_req_complete(req, ret);
Jens Axboec1ca7572019-12-25 22:18:28 -07004480 return 0;
4481#else
4482 return -EOPNOTSUPP;
4483#endif
4484}
4485
Jens Axboe4840e412019-12-25 22:03:45 -07004486static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4487{
4488 if (sqe->ioprio || sqe->buf_index || sqe->addr)
4489 return -EINVAL;
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004490 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4491 return -EINVAL;
Jens Axboe4840e412019-12-25 22:03:45 -07004492
4493 req->fadvise.offset = READ_ONCE(sqe->off);
4494 req->fadvise.len = READ_ONCE(sqe->len);
4495 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
4496 return 0;
4497}
4498
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004499static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe4840e412019-12-25 22:03:45 -07004500{
4501 struct io_fadvise *fa = &req->fadvise;
4502 int ret;
4503
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004504 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe3e694262020-02-01 09:22:49 -07004505 switch (fa->advice) {
4506 case POSIX_FADV_NORMAL:
4507 case POSIX_FADV_RANDOM:
4508 case POSIX_FADV_SEQUENTIAL:
4509 break;
4510 default:
4511 return -EAGAIN;
4512 }
4513 }
Jens Axboe4840e412019-12-25 22:03:45 -07004514
4515 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
4516 if (ret < 0)
4517 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004518 io_req_complete(req, ret);
Jens Axboe4840e412019-12-25 22:03:45 -07004519 return 0;
4520}
4521
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004522static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4523{
Jens Axboe6ca56f82020-09-18 16:51:19 -06004524 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL)))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004525 return -EINVAL;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004526 if (sqe->ioprio || sqe->buf_index)
4527 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004528 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004529 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004530
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004531 req->statx.dfd = READ_ONCE(sqe->fd);
4532 req->statx.mask = READ_ONCE(sqe->len);
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004533 req->statx.filename = u64_to_user_ptr(READ_ONCE(sqe->addr));
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004534 req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
4535 req->statx.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004536
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004537 return 0;
4538}
4539
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004540static int io_statx(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004541{
Bijan Mottahedeh1d9e1282020-05-22 21:31:16 -07004542 struct io_statx *ctx = &req->statx;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004543 int ret;
4544
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004545 if (issue_flags & IO_URING_F_NONBLOCK) {
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004546 /* only need file table for an actual valid fd */
4547 if (ctx->dfd == -1 || ctx->dfd == AT_FDCWD)
4548 req->flags |= REQ_F_NO_FILE_TABLE;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004549 return -EAGAIN;
Jens Axboe5b0bbee2020-04-27 10:41:22 -06004550 }
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004551
Bijan Mottahedehe62753e2020-05-22 21:31:18 -07004552 ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask,
4553 ctx->buffer);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004554
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004555 if (ret < 0)
4556 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004557 io_req_complete(req, ret);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004558 return 0;
4559}
4560
Jens Axboeb5dba592019-12-11 14:02:38 -07004561static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4562{
Jens Axboe14587a462020-09-05 11:36:08 -06004563 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Pavel Begunkov3232dd02020-06-03 18:03:22 +03004564 return -EINVAL;
Jens Axboeb5dba592019-12-11 14:02:38 -07004565 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
4566 sqe->rw_flags || sqe->buf_index)
4567 return -EINVAL;
Pavel Begunkov9c280f92020-04-08 08:58:46 +03004568 if (req->flags & REQ_F_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07004569 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07004570
4571 req->close.fd = READ_ONCE(sqe->fd);
Jens Axboeb5dba592019-12-11 14:02:38 -07004572 return 0;
4573}
4574
Pavel Begunkov889fca72021-02-10 00:03:09 +00004575static int io_close(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeb5dba592019-12-11 14:02:38 -07004576{
Jens Axboe9eac1902021-01-19 15:50:37 -07004577 struct files_struct *files = current->files;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004578 struct io_close *close = &req->close;
Jens Axboe9eac1902021-01-19 15:50:37 -07004579 struct fdtable *fdt;
4580 struct file *file;
Jens Axboeb5dba592019-12-11 14:02:38 -07004581 int ret;
4582
Jens Axboe9eac1902021-01-19 15:50:37 -07004583 file = NULL;
4584 ret = -EBADF;
4585 spin_lock(&files->file_lock);
4586 fdt = files_fdtable(files);
4587 if (close->fd >= fdt->max_fds) {
4588 spin_unlock(&files->file_lock);
4589 goto err;
4590 }
4591 file = fdt->fd[close->fd];
4592 if (!file) {
4593 spin_unlock(&files->file_lock);
4594 goto err;
4595 }
4596
4597 if (file->f_op == &io_uring_fops) {
4598 spin_unlock(&files->file_lock);
4599 file = NULL;
4600 goto err;
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004601 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004602
4603 /* if the file has a flush method, be safe and punt to async */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004604 if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
Jens Axboe9eac1902021-01-19 15:50:37 -07004605 spin_unlock(&files->file_lock);
Pavel Begunkov0bf0eef2020-05-26 20:34:06 +03004606 return -EAGAIN;
Pavel Begunkova2100672020-03-02 23:45:16 +03004607 }
Jens Axboeb5dba592019-12-11 14:02:38 -07004608
Jens Axboe9eac1902021-01-19 15:50:37 -07004609 ret = __close_fd_get_file(close->fd, &file);
4610 spin_unlock(&files->file_lock);
4611 if (ret < 0) {
4612 if (ret == -ENOENT)
4613 ret = -EBADF;
4614 goto err;
4615 }
4616
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004617 /* No ->flush() or already async, safely close from here */
Jens Axboe9eac1902021-01-19 15:50:37 -07004618 ret = filp_close(file, current->files);
4619err:
Pavel Begunkov3af73b22020-06-08 21:08:17 +03004620 if (ret < 0)
4621 req_set_fail_links(req);
Jens Axboe9eac1902021-01-19 15:50:37 -07004622 if (file)
4623 fput(file);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004624 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe1a417f42020-01-31 17:16:48 -07004625 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07004626}
4627
Pavel Begunkov1155c762021-02-18 18:29:38 +00004628static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004629{
4630 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004631
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004632 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
4633 return -EINVAL;
4634 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
4635 return -EINVAL;
4636
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004637 req->sync.off = READ_ONCE(sqe->off);
4638 req->sync.len = READ_ONCE(sqe->len);
4639 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004640 return 0;
4641}
4642
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004643static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004644{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004645 int ret;
4646
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004647 /* sync_file_range always requires a blocking context */
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004648 if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkovac45abc2020-06-08 21:08:18 +03004649 return -EAGAIN;
4650
Jens Axboe9adbd452019-12-20 08:45:55 -07004651 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004652 req->sync.flags);
4653 if (ret < 0)
4654 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06004655 io_req_complete(req, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004656 return 0;
4657}
4658
YueHaibing469956e2020-03-04 15:53:52 +08004659#if defined(CONFIG_NET)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004660static int io_setup_async_msg(struct io_kiocb *req,
4661 struct io_async_msghdr *kmsg)
4662{
Jens Axboee8c2bc12020-08-15 18:44:09 -07004663 struct io_async_msghdr *async_msg = req->async_data;
4664
4665 if (async_msg)
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004666 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004667 if (io_alloc_async_data(req)) {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004668 kfree(kmsg->free_iov);
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004669 return -ENOMEM;
4670 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07004671 async_msg = req->async_data;
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004672 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboee8c2bc12020-08-15 18:44:09 -07004673 memcpy(async_msg, kmsg, sizeof(*kmsg));
Pavel Begunkov2a780802021-02-05 00:57:58 +00004674 async_msg->msg.msg_name = &async_msg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004675 /* if were using fast_iov, set it to the new one */
4676 if (!async_msg->free_iov)
4677 async_msg->msg.msg_iter.iov = async_msg->fast_iov;
4678
Pavel Begunkov02d27d82020-02-28 10:36:36 +03004679 return -EAGAIN;
4680}
4681
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004682static int io_sendmsg_copy_hdr(struct io_kiocb *req,
4683 struct io_async_msghdr *iomsg)
4684{
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004685 iomsg->msg.msg_name = &iomsg->addr;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004686 iomsg->free_iov = iomsg->fast_iov;
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004687 return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004688 req->sr_msg.msg_flags, &iomsg->free_iov);
Pavel Begunkov2ae523e2020-07-12 20:41:06 +03004689}
4690
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004691static int io_sendmsg_prep_async(struct io_kiocb *req)
4692{
4693 int ret;
4694
4695 if (!io_op_defs[req->opcode].needs_async_data)
4696 return 0;
4697 ret = io_sendmsg_copy_hdr(req, req->async_data);
4698 if (!ret)
4699 req->flags |= REQ_F_NEED_CLEANUP;
4700 return ret;
4701}
4702
Jens Axboe3529d8c2019-12-19 18:24:38 -07004703static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06004704{
Jens Axboee47293f2019-12-20 08:58:21 -07004705 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07004706
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004707 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4708 return -EINVAL;
4709
Jens Axboee47293f2019-12-20 08:58:21 -07004710 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004711 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07004712 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004713
Jens Axboed8768362020-02-27 14:17:49 -07004714#ifdef CONFIG_COMPAT
4715 if (req->ctx->compat)
4716 sr->msg_flags |= MSG_CMSG_COMPAT;
4717#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004718 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004719}
4720
Pavel Begunkov889fca72021-02-10 00:03:09 +00004721static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004722{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004723 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe03b12302019-12-02 18:50:25 -07004724 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004725 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004726 int ret;
4727
Florent Revestdba4a922020-12-04 12:36:04 +01004728 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004729 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004730 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004731
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004732 kmsg = req->async_data;
4733 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004734 ret = io_sendmsg_copy_hdr(req, &iomsg);
Jens Axboefddafac2020-01-04 20:19:44 -07004735 if (ret)
4736 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004737 kmsg = &iomsg;
Jens Axboefddafac2020-01-04 20:19:44 -07004738 }
4739
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004740 flags = req->sr_msg.msg_flags;
4741 if (flags & MSG_DONTWAIT)
4742 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004743 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004744 flags |= MSG_DONTWAIT;
4745
4746 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004747 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004748 return io_setup_async_msg(req, kmsg);
4749 if (ret == -ERESTARTSYS)
4750 ret = -EINTR;
4751
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004752 /* fast path, check for non-NULL to avoid function call */
4753 if (kmsg->free_iov)
4754 kfree(kmsg->free_iov);
Jens Axboe03b12302019-12-02 18:50:25 -07004755 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboefddafac2020-01-04 20:19:44 -07004756 if (ret < 0)
4757 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004758 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboefddafac2020-01-04 20:19:44 -07004759 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07004760}
4761
Pavel Begunkov889fca72021-02-10 00:03:09 +00004762static int io_send(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004763{
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004764 struct io_sr_msg *sr = &req->sr_msg;
4765 struct msghdr msg;
4766 struct iovec iov;
Jens Axboe03b12302019-12-02 18:50:25 -07004767 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004768 unsigned flags;
Jens Axboe03b12302019-12-02 18:50:25 -07004769 int ret;
4770
Florent Revestdba4a922020-12-04 12:36:04 +01004771 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004772 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004773 return -ENOTSOCK;
Jens Axboe03b12302019-12-02 18:50:25 -07004774
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004775 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
4776 if (unlikely(ret))
Zheng Bin14db8412020-09-09 20:12:37 +08004777 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07004778
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004779 msg.msg_name = NULL;
4780 msg.msg_control = NULL;
4781 msg.msg_controllen = 0;
4782 msg.msg_namelen = 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004783
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004784 flags = req->sr_msg.msg_flags;
4785 if (flags & MSG_DONTWAIT)
4786 req->flags |= REQ_F_NOWAIT;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004787 else if (issue_flags & IO_URING_F_NONBLOCK)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004788 flags |= MSG_DONTWAIT;
Jens Axboe03b12302019-12-02 18:50:25 -07004789
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004790 msg.msg_flags = flags;
4791 ret = sock_sendmsg(sock, &msg);
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004792 if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004793 return -EAGAIN;
4794 if (ret == -ERESTARTSYS)
4795 ret = -EINTR;
Jens Axboe03b12302019-12-02 18:50:25 -07004796
Jens Axboe03b12302019-12-02 18:50:25 -07004797 if (ret < 0)
4798 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004799 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe03b12302019-12-02 18:50:25 -07004800 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004801}
4802
Pavel Begunkov1400e692020-07-12 20:41:05 +03004803static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
4804 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004805{
4806 struct io_sr_msg *sr = &req->sr_msg;
4807 struct iovec __user *uiov;
4808 size_t iov_len;
4809 int ret;
4810
Pavel Begunkov1400e692020-07-12 20:41:05 +03004811 ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg,
4812 &iomsg->uaddr, &uiov, &iov_len);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004813 if (ret)
4814 return ret;
4815
4816 if (req->flags & REQ_F_BUFFER_SELECT) {
4817 if (iov_len > 1)
4818 return -EINVAL;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004819 if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov)))
Jens Axboe52de1fe2020-02-27 10:15:42 -07004820 return -EFAULT;
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004821 sr->len = iomsg->fast_iov[0].iov_len;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004822 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004823 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004824 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004825 ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004826 &iomsg->free_iov, &iomsg->msg.msg_iter,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004827 false);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004828 if (ret > 0)
4829 ret = 0;
4830 }
4831
4832 return ret;
4833}
4834
4835#ifdef CONFIG_COMPAT
4836static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
Pavel Begunkov1400e692020-07-12 20:41:05 +03004837 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004838{
4839 struct compat_msghdr __user *msg_compat;
4840 struct io_sr_msg *sr = &req->sr_msg;
4841 struct compat_iovec __user *uiov;
4842 compat_uptr_t ptr;
4843 compat_size_t len;
4844 int ret;
4845
Pavel Begunkov270a5942020-07-12 20:41:04 +03004846 msg_compat = (struct compat_msghdr __user *) sr->umsg;
Pavel Begunkov1400e692020-07-12 20:41:05 +03004847 ret = __get_compat_msghdr(&iomsg->msg, msg_compat, &iomsg->uaddr,
Jens Axboe52de1fe2020-02-27 10:15:42 -07004848 &ptr, &len);
4849 if (ret)
4850 return ret;
4851
4852 uiov = compat_ptr(ptr);
4853 if (req->flags & REQ_F_BUFFER_SELECT) {
4854 compat_ssize_t clen;
4855
4856 if (len > 1)
4857 return -EINVAL;
4858 if (!access_ok(uiov, sizeof(*uiov)))
4859 return -EFAULT;
4860 if (__get_user(clen, &uiov->iov_len))
4861 return -EFAULT;
4862 if (clen < 0)
4863 return -EINVAL;
Pavel Begunkov2d280bc2020-11-29 18:33:32 +00004864 sr->len = clen;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004865 iomsg->free_iov = NULL;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004866 } else {
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004867 iomsg->free_iov = iomsg->fast_iov;
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004868 ret = __import_iovec(READ, (struct iovec __user *)uiov, len,
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004869 UIO_FASTIOV, &iomsg->free_iov,
Christoph Hellwig89cd35c2020-09-25 06:51:41 +02004870 &iomsg->msg.msg_iter, true);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004871 if (ret < 0)
4872 return ret;
4873 }
4874
4875 return 0;
4876}
Jens Axboe03b12302019-12-02 18:50:25 -07004877#endif
Jens Axboe52de1fe2020-02-27 10:15:42 -07004878
Pavel Begunkov1400e692020-07-12 20:41:05 +03004879static int io_recvmsg_copy_hdr(struct io_kiocb *req,
4880 struct io_async_msghdr *iomsg)
Jens Axboe52de1fe2020-02-27 10:15:42 -07004881{
Pavel Begunkov1400e692020-07-12 20:41:05 +03004882 iomsg->msg.msg_name = &iomsg->addr;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004883
4884#ifdef CONFIG_COMPAT
4885 if (req->ctx->compat)
Pavel Begunkov1400e692020-07-12 20:41:05 +03004886 return __io_compat_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004887#endif
4888
Pavel Begunkov1400e692020-07-12 20:41:05 +03004889 return __io_recvmsg_copy_hdr(req, iomsg);
Jens Axboe52de1fe2020-02-27 10:15:42 -07004890}
4891
Jens Axboebcda7ba2020-02-23 16:42:51 -07004892static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req,
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004893 bool needs_lock)
Jens Axboebcda7ba2020-02-23 16:42:51 -07004894{
4895 struct io_sr_msg *sr = &req->sr_msg;
4896 struct io_buffer *kbuf;
4897
Jens Axboebcda7ba2020-02-23 16:42:51 -07004898 kbuf = io_buffer_select(req, &sr->len, sr->bgid, sr->kbuf, needs_lock);
4899 if (IS_ERR(kbuf))
4900 return kbuf;
4901
4902 sr->kbuf = kbuf;
4903 req->flags |= REQ_F_BUFFER_SELECTED;
Jens Axboebcda7ba2020-02-23 16:42:51 -07004904 return kbuf;
Jens Axboe03b12302019-12-02 18:50:25 -07004905}
4906
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004907static inline unsigned int io_put_recv_kbuf(struct io_kiocb *req)
4908{
4909 return io_put_kbuf(req, req->sr_msg.kbuf);
4910}
4911
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004912static int io_recvmsg_prep_async(struct io_kiocb *req)
Jens Axboe03b12302019-12-02 18:50:25 -07004913{
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004914 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07004915
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004916 if (!io_op_defs[req->opcode].needs_async_data)
4917 return 0;
4918 ret = io_recvmsg_copy_hdr(req, req->async_data);
4919 if (!ret)
4920 req->flags |= REQ_F_NEED_CLEANUP;
4921 return ret;
4922}
4923
4924static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
4925{
4926 struct io_sr_msg *sr = &req->sr_msg;
4927
Pavel Begunkovd2b6f482020-06-03 18:03:25 +03004928 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
4929 return -EINVAL;
4930
Jens Axboe3529d8c2019-12-19 18:24:38 -07004931 sr->msg_flags = READ_ONCE(sqe->msg_flags);
Pavel Begunkov270a5942020-07-12 20:41:04 +03004932 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07004933 sr->len = READ_ONCE(sqe->len);
Jens Axboebcda7ba2020-02-23 16:42:51 -07004934 sr->bgid = READ_ONCE(sqe->buf_group);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004935
Jens Axboed8768362020-02-27 14:17:49 -07004936#ifdef CONFIG_COMPAT
4937 if (req->ctx->compat)
4938 sr->msg_flags |= MSG_CMSG_COMPAT;
4939#endif
Pavel Begunkov93642ef2021-02-18 18:29:44 +00004940 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07004941}
4942
Pavel Begunkov889fca72021-02-10 00:03:09 +00004943static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe03b12302019-12-02 18:50:25 -07004944{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03004945 struct io_async_msghdr iomsg, *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004946 struct socket *sock;
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004947 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004948 unsigned flags;
Jens Axboe52de1fe2020-02-27 10:15:42 -07004949 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00004950 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004951
Florent Revestdba4a922020-12-04 12:36:04 +01004952 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004953 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01004954 return -ENOTSOCK;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004955
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004956 kmsg = req->async_data;
4957 if (!kmsg) {
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004958 ret = io_recvmsg_copy_hdr(req, &iomsg);
4959 if (ret)
Pavel Begunkov681fda82020-07-15 22:20:45 +03004960 return ret;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004961 kmsg = &iomsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004962 }
4963
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004964 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004965 kbuf = io_recv_buffer_select(req, !force_nonblock);
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03004966 if (IS_ERR(kbuf))
4967 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004968 kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr);
Pavel Begunkov5476dfe2021-02-05 00:57:59 +00004969 kmsg->fast_iov[0].iov_len = req->sr_msg.len;
4970 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov,
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004971 1, req->sr_msg.len);
4972 }
4973
4974 flags = req->sr_msg.msg_flags;
4975 if (flags & MSG_DONTWAIT)
4976 req->flags |= REQ_F_NOWAIT;
4977 else if (force_nonblock)
4978 flags |= MSG_DONTWAIT;
4979
4980 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
4981 kmsg->uaddr, flags);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004982 if (force_nonblock && ret == -EAGAIN)
4983 return io_setup_async_msg(req, kmsg);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03004984 if (ret == -ERESTARTSYS)
4985 ret = -EINTR;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03004986
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03004987 if (req->flags & REQ_F_BUFFER_SELECTED)
4988 cflags = io_put_recv_kbuf(req);
Pavel Begunkov257e84a2021-02-05 00:58:00 +00004989 /* fast path, check for non-NULL to avoid function call */
4990 if (kmsg->free_iov)
4991 kfree(kmsg->free_iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004992 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004993 if (ret < 0)
4994 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00004995 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004996 return 0;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004997}
4998
Pavel Begunkov889fca72021-02-10 00:03:09 +00004999static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefddafac2020-01-04 20:19:44 -07005000{
Pavel Begunkov6b754c82020-07-16 23:28:00 +03005001 struct io_buffer *kbuf;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005002 struct io_sr_msg *sr = &req->sr_msg;
5003 struct msghdr msg;
5004 void __user *buf = sr->buf;
Jens Axboefddafac2020-01-04 20:19:44 -07005005 struct socket *sock;
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005006 struct iovec iov;
5007 unsigned flags;
Jens Axboebcda7ba2020-02-23 16:42:51 -07005008 int ret, cflags = 0;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005009 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005010
Florent Revestdba4a922020-12-04 12:36:04 +01005011 sock = sock_from_file(req->file);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005012 if (unlikely(!sock))
Florent Revestdba4a922020-12-04 12:36:04 +01005013 return -ENOTSOCK;
Jens Axboefddafac2020-01-04 20:19:44 -07005014
Pavel Begunkovbc02ef32020-07-16 23:28:03 +03005015 if (req->flags & REQ_F_BUFFER_SELECT) {
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005016 kbuf = io_recv_buffer_select(req, !force_nonblock);
Jens Axboebcda7ba2020-02-23 16:42:51 -07005017 if (IS_ERR(kbuf))
5018 return PTR_ERR(kbuf);
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005019 buf = u64_to_user_ptr(kbuf->addr);
Jens Axboefddafac2020-01-04 20:19:44 -07005020 }
5021
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005022 ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter);
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005023 if (unlikely(ret))
5024 goto out_free;
Jens Axboefddafac2020-01-04 20:19:44 -07005025
Pavel Begunkov7a7cacb2020-07-16 23:27:59 +03005026 msg.msg_name = NULL;
5027 msg.msg_control = NULL;
5028 msg.msg_controllen = 0;
5029 msg.msg_namelen = 0;
5030 msg.msg_iocb = NULL;
5031 msg.msg_flags = 0;
5032
5033 flags = req->sr_msg.msg_flags;
5034 if (flags & MSG_DONTWAIT)
5035 req->flags |= REQ_F_NOWAIT;
5036 else if (force_nonblock)
5037 flags |= MSG_DONTWAIT;
5038
5039 ret = sock_recvmsg(sock, &msg, flags);
5040 if (force_nonblock && ret == -EAGAIN)
5041 return -EAGAIN;
5042 if (ret == -ERESTARTSYS)
5043 ret = -EINTR;
Pavel Begunkov14c32ee2020-07-16 23:28:01 +03005044out_free:
Pavel Begunkov7fbb1b52020-07-16 23:28:05 +03005045 if (req->flags & REQ_F_BUFFER_SELECTED)
5046 cflags = io_put_recv_kbuf(req);
Jens Axboefddafac2020-01-04 20:19:44 -07005047 if (ret < 0)
5048 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005049 __io_req_complete(req, issue_flags, ret, cflags);
Jens Axboefddafac2020-01-04 20:19:44 -07005050 return 0;
Jens Axboefddafac2020-01-04 20:19:44 -07005051}
5052
Jens Axboe3529d8c2019-12-19 18:24:38 -07005053static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005054{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005055 struct io_accept *accept = &req->accept;
5056
Jens Axboe14587a462020-09-05 11:36:08 -06005057 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe17f2fe32019-10-17 14:42:58 -06005058 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05005059 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005060 return -EINVAL;
5061
Jens Axboed55e5f52019-12-11 16:12:15 -07005062 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5063 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005064 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06005065 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005066 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005067}
Jens Axboe17f2fe32019-10-17 14:42:58 -06005068
Pavel Begunkov889fca72021-02-10 00:03:09 +00005069static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005070{
5071 struct io_accept *accept = &req->accept;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005072 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005073 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005074 int ret;
5075
Jiufei Xuee697dee2020-06-10 13:41:59 +08005076 if (req->file->f_flags & O_NONBLOCK)
5077 req->flags |= REQ_F_NOWAIT;
5078
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005079 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06005080 accept->addr_len, accept->flags,
5081 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005082 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06005083 return -EAGAIN;
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005084 if (ret < 0) {
5085 if (ret == -ERESTARTSYS)
5086 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005087 req_set_fail_links(req);
Pavel Begunkovac45abc2020-06-08 21:08:18 +03005088 }
Pavel Begunkov889fca72021-02-10 00:03:09 +00005089 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe17f2fe32019-10-17 14:42:58 -06005090 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07005091}
5092
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005093static int io_connect_prep_async(struct io_kiocb *req)
5094{
5095 struct io_async_connect *io = req->async_data;
5096 struct io_connect *conn = &req->connect;
5097
5098 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
5099}
5100
Jens Axboe3529d8c2019-12-19 18:24:38 -07005101static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07005102{
Jens Axboe3529d8c2019-12-19 18:24:38 -07005103 struct io_connect *conn = &req->connect;
Jens Axboef499a022019-12-02 16:28:46 -07005104
Jens Axboe14587a462020-09-05 11:36:08 -06005105 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005106 return -EINVAL;
5107 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
5108 return -EINVAL;
5109
Jens Axboe3529d8c2019-12-19 18:24:38 -07005110 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
5111 conn->addr_len = READ_ONCE(sqe->addr2);
Pavel Begunkov93642ef2021-02-18 18:29:44 +00005112 return 0;
Jens Axboef499a022019-12-02 16:28:46 -07005113}
5114
Pavel Begunkov889fca72021-02-10 00:03:09 +00005115static int io_connect(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboef8e85cf2019-11-23 14:24:24 -07005116{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005117 struct io_async_connect __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005118 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005119 int ret;
Pavel Begunkov45d189c2021-02-10 00:03:07 +00005120 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005121
Jens Axboee8c2bc12020-08-15 18:44:09 -07005122 if (req->async_data) {
5123 io = req->async_data;
Jens Axboef499a022019-12-02 16:28:46 -07005124 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005125 ret = move_addr_to_kernel(req->connect.addr,
5126 req->connect.addr_len,
Jens Axboee8c2bc12020-08-15 18:44:09 -07005127 &__io.address);
Jens Axboef499a022019-12-02 16:28:46 -07005128 if (ret)
5129 goto out;
5130 io = &__io;
5131 }
5132
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005133 file_flags = force_nonblock ? O_NONBLOCK : 0;
5134
Jens Axboee8c2bc12020-08-15 18:44:09 -07005135 ret = __sys_connect_file(req->file, &io->address,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07005136 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07005137 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07005138 if (req->async_data)
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005139 return -EAGAIN;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005140 if (io_alloc_async_data(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07005141 ret = -ENOMEM;
5142 goto out;
5143 }
Jens Axboee8c2bc12020-08-15 18:44:09 -07005144 io = req->async_data;
5145 memcpy(req->async_data, &__io, sizeof(__io));
Jens Axboef8e85cf2019-11-23 14:24:24 -07005146 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07005147 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07005148 if (ret == -ERESTARTSYS)
5149 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07005150out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005151 if (ret < 0)
5152 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00005153 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboef8e85cf2019-11-23 14:24:24 -07005154 return 0;
Jens Axboef8e85cf2019-11-23 14:24:24 -07005155}
YueHaibing469956e2020-03-04 15:53:52 +08005156#else /* !CONFIG_NET */
Jens Axboe99a10082021-02-19 09:35:19 -07005157#define IO_NETOP_FN(op) \
5158static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \
5159{ \
5160 return -EOPNOTSUPP; \
Jens Axboef8e85cf2019-11-23 14:24:24 -07005161}
5162
Jens Axboe99a10082021-02-19 09:35:19 -07005163#define IO_NETOP_PREP(op) \
5164IO_NETOP_FN(op) \
5165static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \
5166{ \
5167 return -EOPNOTSUPP; \
5168} \
5169
5170#define IO_NETOP_PREP_ASYNC(op) \
5171IO_NETOP_PREP(op) \
5172static int io_##op##_prep_async(struct io_kiocb *req) \
5173{ \
5174 return -EOPNOTSUPP; \
YueHaibing469956e2020-03-04 15:53:52 +08005175}
5176
Jens Axboe99a10082021-02-19 09:35:19 -07005177IO_NETOP_PREP_ASYNC(sendmsg);
5178IO_NETOP_PREP_ASYNC(recvmsg);
5179IO_NETOP_PREP_ASYNC(connect);
5180IO_NETOP_PREP(accept);
5181IO_NETOP_FN(send);
5182IO_NETOP_FN(recv);
YueHaibing469956e2020-03-04 15:53:52 +08005183#endif /* CONFIG_NET */
Jens Axboe17f2fe32019-10-17 14:42:58 -06005184
Jens Axboed7718a92020-02-14 22:23:12 -07005185struct io_poll_table {
5186 struct poll_table_struct pt;
5187 struct io_kiocb *req;
5188 int error;
5189};
5190
Jens Axboed7718a92020-02-14 22:23:12 -07005191static int __io_async_wake(struct io_kiocb *req, struct io_poll_iocb *poll,
5192 __poll_t mask, task_work_func_t func)
5193{
Jens Axboeaa96bf82020-04-03 11:26:26 -06005194 int ret;
Jens Axboed7718a92020-02-14 22:23:12 -07005195
5196 /* for instances that support it check for an event match first: */
5197 if (mask && !(mask & poll->events))
5198 return 0;
5199
5200 trace_io_uring_task_add(req->ctx, req->opcode, req->user_data, mask);
5201
5202 list_del_init(&poll->wait.entry);
5203
Jens Axboed7718a92020-02-14 22:23:12 -07005204 req->result = mask;
Jens Axboe7cbf1722021-02-10 00:03:20 +00005205 req->task_work.func = func;
Jens Axboe6d816e02020-08-11 08:04:14 -06005206 percpu_ref_get(&req->ctx->refs);
5207
Jens Axboed7718a92020-02-14 22:23:12 -07005208 /*
Jens Axboee3aabf92020-05-18 11:04:17 -06005209 * If this fails, then the task is exiting. When a task exits, the
5210 * work gets canceled, so just cancel this request as well instead
5211 * of executing it. We can't safely execute it anyway, as we may not
5212 * have the needed state needed for it anyway.
Jens Axboed7718a92020-02-14 22:23:12 -07005213 */
Jens Axboe355fb9e2020-10-22 20:19:35 -06005214 ret = io_req_task_work_add(req);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005215 if (unlikely(ret)) {
Jens Axboee3aabf92020-05-18 11:04:17 -06005216 WRITE_ONCE(poll->canceled, true);
Pavel Begunkoveab30c42021-01-19 13:32:42 +00005217 io_req_task_work_add_fallback(req, func);
Jens Axboeaa96bf82020-04-03 11:26:26 -06005218 }
Jens Axboed7718a92020-02-14 22:23:12 -07005219 return 1;
5220}
5221
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005222static bool io_poll_rewait(struct io_kiocb *req, struct io_poll_iocb *poll)
5223 __acquires(&req->ctx->completion_lock)
5224{
5225 struct io_ring_ctx *ctx = req->ctx;
5226
5227 if (!req->result && !READ_ONCE(poll->canceled)) {
5228 struct poll_table_struct pt = { ._key = poll->events };
5229
5230 req->result = vfs_poll(req->file, &pt) & poll->events;
5231 }
5232
5233 spin_lock_irq(&ctx->completion_lock);
5234 if (!req->result && !READ_ONCE(poll->canceled)) {
5235 add_wait_queue(poll->head, &poll->wait);
5236 return true;
5237 }
5238
5239 return false;
5240}
5241
Jens Axboed4e7cd32020-08-15 11:44:50 -07005242static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req)
Jens Axboe18bceab2020-05-15 11:56:54 -06005243{
Jens Axboee8c2bc12020-08-15 18:44:09 -07005244 /* pure poll stashes this in ->async_data, poll driven retry elsewhere */
Jens Axboed4e7cd32020-08-15 11:44:50 -07005245 if (req->opcode == IORING_OP_POLL_ADD)
Jens Axboee8c2bc12020-08-15 18:44:09 -07005246 return req->async_data;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005247 return req->apoll->double_poll;
5248}
5249
5250static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req)
5251{
5252 if (req->opcode == IORING_OP_POLL_ADD)
5253 return &req->poll;
5254 return &req->apoll->poll;
5255}
5256
5257static void io_poll_remove_double(struct io_kiocb *req)
5258{
5259 struct io_poll_iocb *poll = io_poll_get_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005260
5261 lockdep_assert_held(&req->ctx->completion_lock);
5262
5263 if (poll && poll->head) {
5264 struct wait_queue_head *head = poll->head;
5265
5266 spin_lock(&head->lock);
5267 list_del_init(&poll->wait.entry);
5268 if (poll->wait.private)
5269 refcount_dec(&req->refs);
5270 poll->head = NULL;
5271 spin_unlock(&head->lock);
5272 }
5273}
5274
5275static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
5276{
5277 struct io_ring_ctx *ctx = req->ctx;
5278
Jens Axboed4e7cd32020-08-15 11:44:50 -07005279 io_poll_remove_double(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005280 req->poll.done = true;
5281 io_cqring_fill_event(req, error ? error : mangle_poll(mask));
5282 io_commit_cqring(ctx);
5283}
5284
Jens Axboe18bceab2020-05-15 11:56:54 -06005285static void io_poll_task_func(struct callback_head *cb)
5286{
5287 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
Jens Axboe6d816e02020-08-11 08:04:14 -06005288 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005289 struct io_kiocb *nxt;
Jens Axboe18bceab2020-05-15 11:56:54 -06005290
Pavel Begunkovdd221f462020-10-18 10:17:42 +01005291 if (io_poll_rewait(req, &req->poll)) {
5292 spin_unlock_irq(&ctx->completion_lock);
5293 } else {
5294 hash_del(&req->hash_node);
5295 io_poll_complete(req, req->result, 0);
5296 spin_unlock_irq(&ctx->completion_lock);
5297
5298 nxt = io_put_req_find_next(req);
5299 io_cqring_ev_posted(ctx);
5300 if (nxt)
5301 __io_req_task_submit(nxt);
5302 }
5303
Jens Axboe6d816e02020-08-11 08:04:14 -06005304 percpu_ref_put(&ctx->refs);
Jens Axboe18bceab2020-05-15 11:56:54 -06005305}
5306
5307static int io_poll_double_wake(struct wait_queue_entry *wait, unsigned mode,
5308 int sync, void *key)
5309{
5310 struct io_kiocb *req = wait->private;
Jens Axboed4e7cd32020-08-15 11:44:50 -07005311 struct io_poll_iocb *poll = io_poll_get_single(req);
Jens Axboe18bceab2020-05-15 11:56:54 -06005312 __poll_t mask = key_to_poll(key);
5313
5314 /* for instances that support it check for an event match first: */
5315 if (mask && !(mask & poll->events))
5316 return 0;
5317
Jens Axboe8706e042020-09-28 08:38:54 -06005318 list_del_init(&wait->entry);
5319
Jens Axboe807abcb2020-07-17 17:09:27 -06005320 if (poll && poll->head) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005321 bool done;
5322
Jens Axboe807abcb2020-07-17 17:09:27 -06005323 spin_lock(&poll->head->lock);
5324 done = list_empty(&poll->wait.entry);
Jens Axboe18bceab2020-05-15 11:56:54 -06005325 if (!done)
Jens Axboe807abcb2020-07-17 17:09:27 -06005326 list_del_init(&poll->wait.entry);
Jens Axboed4e7cd32020-08-15 11:44:50 -07005327 /* make sure double remove sees this as being gone */
5328 wait->private = NULL;
Jens Axboe807abcb2020-07-17 17:09:27 -06005329 spin_unlock(&poll->head->lock);
Jens Axboec8b5e262020-10-25 13:53:26 -06005330 if (!done) {
5331 /* use wait func handler, so it matches the rq type */
5332 poll->wait.func(&poll->wait, mode, sync, key);
5333 }
Jens Axboe18bceab2020-05-15 11:56:54 -06005334 }
5335 refcount_dec(&req->refs);
5336 return 1;
5337}
5338
5339static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
5340 wait_queue_func_t wake_func)
5341{
5342 poll->head = NULL;
5343 poll->done = false;
5344 poll->canceled = false;
5345 poll->events = events;
5346 INIT_LIST_HEAD(&poll->wait.entry);
5347 init_waitqueue_func_entry(&poll->wait, wake_func);
5348}
5349
5350static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,
Jens Axboe807abcb2020-07-17 17:09:27 -06005351 struct wait_queue_head *head,
5352 struct io_poll_iocb **poll_ptr)
Jens Axboe18bceab2020-05-15 11:56:54 -06005353{
5354 struct io_kiocb *req = pt->req;
5355
5356 /*
5357 * If poll->head is already set, it's because the file being polled
5358 * uses multiple waitqueues for poll handling (eg one for read, one
5359 * for write). Setup a separate io_poll_iocb if this happens.
5360 */
5361 if (unlikely(poll->head)) {
Pavel Begunkov58852d42020-10-16 20:55:56 +01005362 struct io_poll_iocb *poll_one = poll;
5363
Jens Axboe18bceab2020-05-15 11:56:54 -06005364 /* already have a 2nd entry, fail a third attempt */
Jens Axboe807abcb2020-07-17 17:09:27 -06005365 if (*poll_ptr) {
Jens Axboe18bceab2020-05-15 11:56:54 -06005366 pt->error = -EINVAL;
5367 return;
5368 }
5369 poll = kmalloc(sizeof(*poll), GFP_ATOMIC);
5370 if (!poll) {
5371 pt->error = -ENOMEM;
5372 return;
5373 }
Pavel Begunkov58852d42020-10-16 20:55:56 +01005374 io_init_poll_iocb(poll, poll_one->events, io_poll_double_wake);
Jens Axboe18bceab2020-05-15 11:56:54 -06005375 refcount_inc(&req->refs);
5376 poll->wait.private = req;
Jens Axboe807abcb2020-07-17 17:09:27 -06005377 *poll_ptr = poll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005378 }
5379
5380 pt->error = 0;
5381 poll->head = head;
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005382
5383 if (poll->events & EPOLLEXCLUSIVE)
5384 add_wait_queue_exclusive(head, &poll->wait);
5385 else
5386 add_wait_queue(head, &poll->wait);
Jens Axboe18bceab2020-05-15 11:56:54 -06005387}
5388
5389static void io_async_queue_proc(struct file *file, struct wait_queue_head *head,
5390 struct poll_table_struct *p)
5391{
5392 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
Jens Axboe807abcb2020-07-17 17:09:27 -06005393 struct async_poll *apoll = pt->req->apoll;
Jens Axboe18bceab2020-05-15 11:56:54 -06005394
Jens Axboe807abcb2020-07-17 17:09:27 -06005395 __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll);
Jens Axboe18bceab2020-05-15 11:56:54 -06005396}
5397
Jens Axboed7718a92020-02-14 22:23:12 -07005398static void io_async_task_func(struct callback_head *cb)
5399{
5400 struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
5401 struct async_poll *apoll = req->apoll;
5402 struct io_ring_ctx *ctx = req->ctx;
5403
5404 trace_io_uring_task_run(req->ctx, req->opcode, req->user_data);
5405
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005406 if (io_poll_rewait(req, &apoll->poll)) {
Jens Axboed7718a92020-02-14 22:23:12 -07005407 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe6d816e02020-08-11 08:04:14 -06005408 percpu_ref_put(&ctx->refs);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005409 return;
Jens Axboed7718a92020-02-14 22:23:12 -07005410 }
5411
Jens Axboe31067252020-05-17 17:43:31 -06005412 /* If req is still hashed, it cannot have been canceled. Don't check. */
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005413 if (hash_hashed(&req->hash_node))
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005414 hash_del(&req->hash_node);
Jens Axboe2bae0472020-04-13 11:16:34 -06005415
Jens Axboed4e7cd32020-08-15 11:44:50 -07005416 io_poll_remove_double(req);
Jens Axboe74ce6ce2020-04-13 11:09:12 -06005417 spin_unlock_irq(&ctx->completion_lock);
5418
Pavel Begunkov0be0b0e2020-06-30 15:20:42 +03005419 if (!READ_ONCE(apoll->poll.canceled))
5420 __io_req_task_submit(req);
5421 else
5422 __io_req_task_cancel(req, -ECANCELED);
Dan Carpenteraa340842020-07-08 21:47:11 +03005423
Jens Axboe6d816e02020-08-11 08:04:14 -06005424 percpu_ref_put(&ctx->refs);
Jens Axboe807abcb2020-07-17 17:09:27 -06005425 kfree(apoll->double_poll);
Jens Axboe31067252020-05-17 17:43:31 -06005426 kfree(apoll);
Jens Axboed7718a92020-02-14 22:23:12 -07005427}
5428
5429static int io_async_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5430 void *key)
5431{
5432 struct io_kiocb *req = wait->private;
5433 struct io_poll_iocb *poll = &req->apoll->poll;
5434
5435 trace_io_uring_poll_wake(req->ctx, req->opcode, req->user_data,
5436 key_to_poll(key));
5437
5438 return __io_async_wake(req, poll, key_to_poll(key), io_async_task_func);
5439}
5440
5441static void io_poll_req_insert(struct io_kiocb *req)
5442{
5443 struct io_ring_ctx *ctx = req->ctx;
5444 struct hlist_head *list;
5445
5446 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
5447 hlist_add_head(&req->hash_node, list);
5448}
5449
5450static __poll_t __io_arm_poll_handler(struct io_kiocb *req,
5451 struct io_poll_iocb *poll,
5452 struct io_poll_table *ipt, __poll_t mask,
5453 wait_queue_func_t wake_func)
5454 __acquires(&ctx->completion_lock)
5455{
5456 struct io_ring_ctx *ctx = req->ctx;
5457 bool cancel = false;
5458
Pavel Begunkov4d52f332020-10-18 10:17:43 +01005459 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe18bceab2020-05-15 11:56:54 -06005460 io_init_poll_iocb(poll, mask, wake_func);
Pavel Begunkovb90cd192020-06-21 13:09:52 +03005461 poll->file = req->file;
Jens Axboe18bceab2020-05-15 11:56:54 -06005462 poll->wait.private = req;
Jens Axboed7718a92020-02-14 22:23:12 -07005463
5464 ipt->pt._key = mask;
5465 ipt->req = req;
5466 ipt->error = -EINVAL;
5467
Jens Axboed7718a92020-02-14 22:23:12 -07005468 mask = vfs_poll(req->file, &ipt->pt) & poll->events;
5469
5470 spin_lock_irq(&ctx->completion_lock);
5471 if (likely(poll->head)) {
5472 spin_lock(&poll->head->lock);
5473 if (unlikely(list_empty(&poll->wait.entry))) {
5474 if (ipt->error)
5475 cancel = true;
5476 ipt->error = 0;
5477 mask = 0;
5478 }
5479 if (mask || ipt->error)
5480 list_del_init(&poll->wait.entry);
5481 else if (cancel)
5482 WRITE_ONCE(poll->canceled, true);
5483 else if (!poll->done) /* actually waiting for an event */
5484 io_poll_req_insert(req);
5485 spin_unlock(&poll->head->lock);
5486 }
5487
5488 return mask;
5489}
5490
5491static bool io_arm_poll_handler(struct io_kiocb *req)
5492{
5493 const struct io_op_def *def = &io_op_defs[req->opcode];
5494 struct io_ring_ctx *ctx = req->ctx;
5495 struct async_poll *apoll;
5496 struct io_poll_table ipt;
5497 __poll_t mask, ret;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005498 int rw;
Jens Axboed7718a92020-02-14 22:23:12 -07005499
5500 if (!req->file || !file_can_poll(req->file))
5501 return false;
Pavel Begunkov24c74672020-06-21 13:09:51 +03005502 if (req->flags & REQ_F_POLLED)
Jens Axboed7718a92020-02-14 22:23:12 -07005503 return false;
Jens Axboe9dab14b2020-08-25 12:27:50 -06005504 if (def->pollin)
5505 rw = READ;
5506 else if (def->pollout)
5507 rw = WRITE;
5508 else
5509 return false;
5510 /* if we can't nonblock try, then no point in arming a poll handler */
5511 if (!io_file_supports_async(req->file, rw))
Jens Axboed7718a92020-02-14 22:23:12 -07005512 return false;
5513
5514 apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC);
5515 if (unlikely(!apoll))
5516 return false;
Jens Axboe807abcb2020-07-17 17:09:27 -06005517 apoll->double_poll = NULL;
Jens Axboed7718a92020-02-14 22:23:12 -07005518
5519 req->flags |= REQ_F_POLLED;
Jens Axboed7718a92020-02-14 22:23:12 -07005520 req->apoll = apoll;
Jens Axboed7718a92020-02-14 22:23:12 -07005521
Nathan Chancellor8755d972020-03-02 16:01:19 -07005522 mask = 0;
Jens Axboed7718a92020-02-14 22:23:12 -07005523 if (def->pollin)
Nathan Chancellor8755d972020-03-02 16:01:19 -07005524 mask |= POLLIN | POLLRDNORM;
Jens Axboed7718a92020-02-14 22:23:12 -07005525 if (def->pollout)
5526 mask |= POLLOUT | POLLWRNORM;
Luke Hsiao901341b2020-08-21 21:41:05 -07005527
5528 /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */
5529 if ((req->opcode == IORING_OP_RECVMSG) &&
5530 (req->sr_msg.msg_flags & MSG_ERRQUEUE))
5531 mask &= ~POLLIN;
5532
Jens Axboed7718a92020-02-14 22:23:12 -07005533 mask |= POLLERR | POLLPRI;
5534
5535 ipt.pt._qproc = io_async_queue_proc;
5536
5537 ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask,
5538 io_async_wake);
Jens Axboea36da652020-08-11 09:50:19 -06005539 if (ret || ipt.error) {
Jens Axboed4e7cd32020-08-15 11:44:50 -07005540 io_poll_remove_double(req);
Jens Axboed7718a92020-02-14 22:23:12 -07005541 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe807abcb2020-07-17 17:09:27 -06005542 kfree(apoll->double_poll);
Jens Axboed7718a92020-02-14 22:23:12 -07005543 kfree(apoll);
5544 return false;
5545 }
5546 spin_unlock_irq(&ctx->completion_lock);
5547 trace_io_uring_poll_arm(ctx, req->opcode, req->user_data, mask,
5548 apoll->poll.events);
5549 return true;
5550}
5551
5552static bool __io_poll_remove_one(struct io_kiocb *req,
5553 struct io_poll_iocb *poll)
5554{
Jens Axboeb41e9852020-02-17 09:52:41 -07005555 bool do_complete = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005556
5557 spin_lock(&poll->head->lock);
5558 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07005559 if (!list_empty(&poll->wait.entry)) {
5560 list_del_init(&poll->wait.entry);
Jens Axboeb41e9852020-02-17 09:52:41 -07005561 do_complete = true;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005562 }
5563 spin_unlock(&poll->head->lock);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005564 hash_del(&req->hash_node);
Jens Axboed7718a92020-02-14 22:23:12 -07005565 return do_complete;
5566}
5567
5568static bool io_poll_remove_one(struct io_kiocb *req)
5569{
5570 bool do_complete;
5571
Jens Axboed4e7cd32020-08-15 11:44:50 -07005572 io_poll_remove_double(req);
5573
Jens Axboed7718a92020-02-14 22:23:12 -07005574 if (req->opcode == IORING_OP_POLL_ADD) {
5575 do_complete = __io_poll_remove_one(req, &req->poll);
5576 } else {
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005577 struct async_poll *apoll = req->apoll;
5578
Jens Axboed7718a92020-02-14 22:23:12 -07005579 /* non-poll requests have submit ref still */
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005580 do_complete = __io_poll_remove_one(req, &apoll->poll);
5581 if (do_complete) {
Jens Axboed7718a92020-02-14 22:23:12 -07005582 io_put_req(req);
Jens Axboe807abcb2020-07-17 17:09:27 -06005583 kfree(apoll->double_poll);
Jens Axboe3bfa5bc2020-05-17 13:54:12 -06005584 kfree(apoll);
5585 }
Xiaoguang Wangb1f573b2020-04-12 14:50:54 +08005586 }
5587
Jens Axboeb41e9852020-02-17 09:52:41 -07005588 if (do_complete) {
5589 io_cqring_fill_event(req, -ECANCELED);
5590 io_commit_cqring(req->ctx);
Jens Axboef254ac02020-08-12 17:33:30 -06005591 req_set_fail_links(req);
Pavel Begunkov216578e2020-10-13 09:44:00 +01005592 io_put_req_deferred(req, 1);
Jens Axboeb41e9852020-02-17 09:52:41 -07005593 }
5594
5595 return do_complete;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005596}
5597
Jens Axboe76e1b642020-09-26 15:05:03 -06005598/*
5599 * Returns true if we found and killed one or more poll requests
5600 */
Pavel Begunkov6b819282020-11-06 13:00:25 +00005601static bool io_poll_remove_all(struct io_ring_ctx *ctx, struct task_struct *tsk,
5602 struct files_struct *files)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005603{
Jens Axboe78076bb2019-12-04 19:56:40 -07005604 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005605 struct io_kiocb *req;
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005606 int posted = 0, i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005607
5608 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07005609 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
5610 struct hlist_head *list;
5611
5612 list = &ctx->cancel_hash[i];
Jens Axboef3606e32020-09-22 08:18:24 -06005613 hlist_for_each_entry_safe(req, tmp, list, hash_node) {
Pavel Begunkov6b819282020-11-06 13:00:25 +00005614 if (io_match_task(req, tsk, files))
Jens Axboef3606e32020-09-22 08:18:24 -06005615 posted += io_poll_remove_one(req);
5616 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005617 }
5618 spin_unlock_irq(&ctx->completion_lock);
Jens Axboeb41e9852020-02-17 09:52:41 -07005619
Jens Axboe8e2e1fa2020-04-13 17:05:14 -06005620 if (posted)
5621 io_cqring_ev_posted(ctx);
Jens Axboe76e1b642020-09-26 15:05:03 -06005622
5623 return posted != 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005624}
5625
Jens Axboe47f46762019-11-09 17:43:02 -07005626static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
5627{
Jens Axboe78076bb2019-12-04 19:56:40 -07005628 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07005629 struct io_kiocb *req;
5630
Jens Axboe78076bb2019-12-04 19:56:40 -07005631 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
5632 hlist_for_each_entry(req, list, hash_node) {
Jens Axboeb41e9852020-02-17 09:52:41 -07005633 if (sqe_addr != req->user_data)
5634 continue;
5635 if (io_poll_remove_one(req))
Jens Axboeeac406c2019-11-14 12:09:58 -07005636 return 0;
Jens Axboeb41e9852020-02-17 09:52:41 -07005637 return -EALREADY;
Jens Axboe47f46762019-11-09 17:43:02 -07005638 }
5639
5640 return -ENOENT;
5641}
5642
Jens Axboe3529d8c2019-12-19 18:24:38 -07005643static int io_poll_remove_prep(struct io_kiocb *req,
5644 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005645{
Jens Axboe221c5eb2019-01-17 09:41:58 -07005646 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5647 return -EINVAL;
5648 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
5649 sqe->poll_events)
5650 return -EINVAL;
5651
Pavel Begunkov018043b2020-10-27 23:17:18 +00005652 req->poll_remove.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07005653 return 0;
5654}
5655
5656/*
5657 * Find a running poll command that matches one specified in sqe->addr,
5658 * and remove it if found.
5659 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005660static int io_poll_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005661{
5662 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe0969e782019-12-17 18:40:57 -07005663 int ret;
5664
Jens Axboe221c5eb2019-01-17 09:41:58 -07005665 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov018043b2020-10-27 23:17:18 +00005666 ret = io_poll_cancel(ctx, req->poll_remove.addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005667 spin_unlock_irq(&ctx->completion_lock);
5668
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005669 if (ret < 0)
5670 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06005671 io_req_complete(req, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005672 return 0;
5673}
5674
Jens Axboe221c5eb2019-01-17 09:41:58 -07005675static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
5676 void *key)
5677{
Jens Axboec2f2eb72020-02-10 09:07:05 -07005678 struct io_kiocb *req = wait->private;
5679 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005680
Jens Axboed7718a92020-02-14 22:23:12 -07005681 return __io_async_wake(req, poll, key_to_poll(key), io_poll_task_func);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005682}
5683
Jens Axboe221c5eb2019-01-17 09:41:58 -07005684static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
5685 struct poll_table_struct *p)
5686{
5687 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
5688
Jens Axboee8c2bc12020-08-15 18:44:09 -07005689 __io_queue_proc(&pt->req->poll, pt, head, (struct io_poll_iocb **) &pt->req->async_data);
Jens Axboeeac406c2019-11-14 12:09:58 -07005690}
5691
Jens Axboe3529d8c2019-12-19 18:24:38 -07005692static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07005693{
5694 struct io_poll_iocb *poll = &req->poll;
Jiufei Xue5769a352020-06-17 17:53:55 +08005695 u32 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005696
5697 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5698 return -EINVAL;
5699 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
5700 return -EINVAL;
5701
Jiufei Xue5769a352020-06-17 17:53:55 +08005702 events = READ_ONCE(sqe->poll32_events);
5703#ifdef __BIG_ENDIAN
5704 events = swahw32(events);
5705#endif
Jiufei Xuea31eb4a2020-06-17 17:53:56 +08005706 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP |
5707 (events & EPOLLEXCLUSIVE);
Jens Axboe0969e782019-12-17 18:40:57 -07005708 return 0;
5709}
5710
Pavel Begunkov61e98202021-02-10 00:03:08 +00005711static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe0969e782019-12-17 18:40:57 -07005712{
5713 struct io_poll_iocb *poll = &req->poll;
5714 struct io_ring_ctx *ctx = req->ctx;
5715 struct io_poll_table ipt;
Jens Axboe0969e782019-12-17 18:40:57 -07005716 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07005717
Jens Axboed7718a92020-02-14 22:23:12 -07005718 ipt.pt._qproc = io_poll_queue_proc;
Jens Axboe36703242019-07-25 10:20:18 -06005719
Jens Axboed7718a92020-02-14 22:23:12 -07005720 mask = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events,
5721 io_poll_wake);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005722
Jens Axboe8c838782019-03-12 15:48:16 -06005723 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06005724 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07005725 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06005726 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07005727 spin_unlock_irq(&ctx->completion_lock);
5728
Jens Axboe8c838782019-03-12 15:48:16 -06005729 if (mask) {
5730 io_cqring_ev_posted(ctx);
Pavel Begunkov014db002020-03-03 21:33:12 +03005731 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005732 }
Jens Axboe8c838782019-03-12 15:48:16 -06005733 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07005734}
5735
Jens Axboe5262f562019-09-17 12:26:57 -06005736static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
5737{
Jens Axboead8a48a2019-11-15 08:49:11 -07005738 struct io_timeout_data *data = container_of(timer,
5739 struct io_timeout_data, timer);
5740 struct io_kiocb *req = data->req;
5741 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06005742 unsigned long flags;
5743
Jens Axboe5262f562019-09-17 12:26:57 -06005744 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkova71976f2020-10-10 18:34:11 +01005745 list_del_init(&req->timeout.list);
Pavel Begunkov01cec8c2020-07-30 18:43:50 +03005746 atomic_set(&req->ctx->cq_timeouts,
5747 atomic_read(&req->ctx->cq_timeouts) + 1);
5748
Jens Axboe78e19bb2019-11-06 15:21:34 -07005749 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06005750 io_commit_cqring(ctx);
5751 spin_unlock_irqrestore(&ctx->completion_lock, flags);
5752
5753 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005754 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06005755 io_put_req(req);
5756 return HRTIMER_NORESTART;
5757}
5758
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005759static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx,
5760 __u64 user_data)
Jens Axboe47f46762019-11-09 17:43:02 -07005761{
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005762 struct io_timeout_data *io;
Jens Axboef254ac02020-08-12 17:33:30 -06005763 struct io_kiocb *req;
5764 int ret = -ENOENT;
5765
5766 list_for_each_entry(req, &ctx->timeout_list, timeout.list) {
5767 if (user_data == req->user_data) {
5768 ret = 0;
5769 break;
5770 }
5771 }
5772
5773 if (ret == -ENOENT)
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005774 return ERR_PTR(ret);
Jens Axboef254ac02020-08-12 17:33:30 -06005775
Pavel Begunkovfbd15842020-11-30 19:11:15 +00005776 io = req->async_data;
5777 ret = hrtimer_try_to_cancel(&io->timer);
5778 if (ret == -1)
5779 return ERR_PTR(-EALREADY);
5780 list_del_init(&req->timeout.list);
5781 return req;
5782}
5783
5784static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
5785{
5786 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5787
5788 if (IS_ERR(req))
5789 return PTR_ERR(req);
5790
5791 req_set_fail_links(req);
5792 io_cqring_fill_event(req, -ECANCELED);
5793 io_put_req_deferred(req, 1);
5794 return 0;
Jens Axboef254ac02020-08-12 17:33:30 -06005795}
5796
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005797static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data,
5798 struct timespec64 *ts, enum hrtimer_mode mode)
5799{
5800 struct io_kiocb *req = io_timeout_extract(ctx, user_data);
5801 struct io_timeout_data *data;
5802
5803 if (IS_ERR(req))
5804 return PTR_ERR(req);
5805
5806 req->timeout.off = 0; /* noseq */
5807 data = req->async_data;
5808 list_add_tail(&req->timeout.list, &ctx->timeout_list);
5809 hrtimer_init(&data->timer, CLOCK_MONOTONIC, mode);
5810 data->timer.function = io_timeout_fn;
5811 hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode);
5812 return 0;
Jens Axboe47f46762019-11-09 17:43:02 -07005813}
5814
Jens Axboe3529d8c2019-12-19 18:24:38 -07005815static int io_timeout_remove_prep(struct io_kiocb *req,
5816 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07005817{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005818 struct io_timeout_rem *tr = &req->timeout_rem;
5819
Jens Axboeb29472e2019-12-17 18:50:29 -07005820 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
5821 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06005822 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
5823 return -EINVAL;
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005824 if (sqe->ioprio || sqe->buf_index || sqe->len)
Jens Axboeb29472e2019-12-17 18:50:29 -07005825 return -EINVAL;
5826
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005827 tr->addr = READ_ONCE(sqe->addr);
5828 tr->flags = READ_ONCE(sqe->timeout_flags);
5829 if (tr->flags & IORING_TIMEOUT_UPDATE) {
5830 if (tr->flags & ~(IORING_TIMEOUT_UPDATE|IORING_TIMEOUT_ABS))
5831 return -EINVAL;
5832 if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2)))
5833 return -EFAULT;
5834 } else if (tr->flags) {
5835 /* timeout removal doesn't support flags */
5836 return -EINVAL;
5837 }
5838
Jens Axboeb29472e2019-12-17 18:50:29 -07005839 return 0;
5840}
5841
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005842static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags)
5843{
5844 return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS
5845 : HRTIMER_MODE_REL;
5846}
5847
Jens Axboe11365042019-10-16 09:08:32 -06005848/*
5849 * Remove or update an existing timeout command
5850 */
Pavel Begunkov61e98202021-02-10 00:03:08 +00005851static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe11365042019-10-16 09:08:32 -06005852{
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005853 struct io_timeout_rem *tr = &req->timeout_rem;
Jens Axboe11365042019-10-16 09:08:32 -06005854 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07005855 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06005856
Jens Axboe11365042019-10-16 09:08:32 -06005857 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005858 if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE))
Pavel Begunkov9c8e11b2020-11-30 19:11:16 +00005859 ret = io_timeout_cancel(ctx, tr->addr);
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005860 else
5861 ret = io_timeout_update(ctx, tr->addr, &tr->ts,
5862 io_translate_timeout_mode(tr->flags));
Jens Axboe11365042019-10-16 09:08:32 -06005863
Jens Axboe47f46762019-11-09 17:43:02 -07005864 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06005865 io_commit_cqring(ctx);
5866 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005867 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005868 if (ret < 0)
5869 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08005870 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06005871 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06005872}
5873
Jens Axboe3529d8c2019-12-19 18:24:38 -07005874static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07005875 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06005876{
Jens Axboead8a48a2019-11-15 08:49:11 -07005877 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06005878 unsigned flags;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005879 u32 off = READ_ONCE(sqe->off);
Jens Axboe5262f562019-09-17 12:26:57 -06005880
Jens Axboead8a48a2019-11-15 08:49:11 -07005881 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06005882 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07005883 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06005884 return -EINVAL;
Pavel Begunkov56080b02020-05-26 20:34:04 +03005885 if (off && is_timeout_link)
Jens Axboe2d283902019-12-04 11:08:05 -07005886 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06005887 flags = READ_ONCE(sqe->timeout_flags);
5888 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06005889 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06005890
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005891 req->timeout.off = off;
Jens Axboe26a61672019-12-20 09:02:01 -07005892
Jens Axboee8c2bc12020-08-15 18:44:09 -07005893 if (!req->async_data && io_alloc_async_data(req))
Jens Axboe26a61672019-12-20 09:02:01 -07005894 return -ENOMEM;
5895
Jens Axboee8c2bc12020-08-15 18:44:09 -07005896 data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005897 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07005898
5899 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06005900 return -EFAULT;
5901
Pavel Begunkov8662dae2021-01-19 13:32:44 +00005902 data->mode = io_translate_timeout_mode(flags);
Jens Axboead8a48a2019-11-15 08:49:11 -07005903 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
5904 return 0;
5905}
5906
Pavel Begunkov61e98202021-02-10 00:03:08 +00005907static int io_timeout(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboead8a48a2019-11-15 08:49:11 -07005908{
Jens Axboead8a48a2019-11-15 08:49:11 -07005909 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee8c2bc12020-08-15 18:44:09 -07005910 struct io_timeout_data *data = req->async_data;
Jens Axboead8a48a2019-11-15 08:49:11 -07005911 struct list_head *entry;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005912 u32 tail, off = req->timeout.off;
Jens Axboead8a48a2019-11-15 08:49:11 -07005913
Pavel Begunkov733f5c92020-05-26 20:34:03 +03005914 spin_lock_irq(&ctx->completion_lock);
Jens Axboe93bd25b2019-11-11 23:34:31 -07005915
Jens Axboe5262f562019-09-17 12:26:57 -06005916 /*
5917 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07005918 * timeout event to be satisfied. If it isn't set, then this is
5919 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06005920 */
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005921 if (io_is_timeout_noseq(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -07005922 entry = ctx->timeout_list.prev;
5923 goto add;
5924 }
Jens Axboe5262f562019-09-17 12:26:57 -06005925
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005926 tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts);
5927 req->timeout.target_seq = tail + off;
Jens Axboe5262f562019-09-17 12:26:57 -06005928
Marcelo Diop-Gonzalezf0105052021-01-15 11:54:40 -05005929 /* Update the last seq here in case io_flush_timeouts() hasn't.
5930 * This is safe because ->completion_lock is held, and submissions
5931 * and completions are never mixed in the same ->completion_lock section.
5932 */
5933 ctx->cq_last_tm_flush = tail;
5934
Jens Axboe5262f562019-09-17 12:26:57 -06005935 /*
5936 * Insertion sort, ensuring the first entry in the list is always
5937 * the one we need first.
5938 */
Jens Axboe5262f562019-09-17 12:26:57 -06005939 list_for_each_prev(entry, &ctx->timeout_list) {
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005940 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb,
5941 timeout.list);
Jens Axboe5262f562019-09-17 12:26:57 -06005942
Pavel Begunkov8eb7e2d2020-06-29 13:13:02 +03005943 if (io_is_timeout_noseq(nxt))
Jens Axboe93bd25b2019-11-11 23:34:31 -07005944 continue;
Pavel Begunkovbfe68a22020-05-30 14:54:18 +03005945 /* nxt.seq is behind @tail, otherwise would've been completed */
5946 if (off >= nxt->timeout.target_seq - tail)
Jens Axboe5262f562019-09-17 12:26:57 -06005947 break;
5948 }
Jens Axboe93bd25b2019-11-11 23:34:31 -07005949add:
Pavel Begunkov135fcde2020-07-13 23:37:12 +03005950 list_add(&req->timeout.list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07005951 data->timer.function = io_timeout_fn;
5952 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06005953 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06005954 return 0;
5955}
5956
Jens Axboe62755e32019-10-28 21:49:21 -06005957static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06005958{
Jens Axboe62755e32019-10-28 21:49:21 -06005959 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06005960
Jens Axboe62755e32019-10-28 21:49:21 -06005961 return req->user_data == (unsigned long) data;
5962}
5963
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005964static int io_async_cancel_one(struct io_uring_task *tctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06005965{
Jens Axboe62755e32019-10-28 21:49:21 -06005966 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06005967 int ret = 0;
5968
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005969 if (!tctx->io_wq)
5970 return -ENOENT;
5971
5972 cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, sqe_addr, false);
Jens Axboe62755e32019-10-28 21:49:21 -06005973 switch (cancel_ret) {
5974 case IO_WQ_CANCEL_OK:
5975 ret = 0;
5976 break;
5977 case IO_WQ_CANCEL_RUNNING:
5978 ret = -EALREADY;
5979 break;
5980 case IO_WQ_CANCEL_NOTFOUND:
5981 ret = -ENOENT;
5982 break;
5983 }
5984
Jens Axboee977d6d2019-11-05 12:39:45 -07005985 return ret;
5986}
5987
Jens Axboe47f46762019-11-09 17:43:02 -07005988static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
5989 struct io_kiocb *req, __u64 sqe_addr,
Pavel Begunkov014db002020-03-03 21:33:12 +03005990 int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07005991{
5992 unsigned long flags;
5993 int ret;
5994
Jens Axboe5aa75ed2021-02-16 12:56:50 -07005995 ret = io_async_cancel_one(req->task->io_uring,
5996 (void *) (unsigned long) sqe_addr);
Jens Axboe47f46762019-11-09 17:43:02 -07005997 if (ret != -ENOENT) {
5998 spin_lock_irqsave(&ctx->completion_lock, flags);
5999 goto done;
6000 }
6001
6002 spin_lock_irqsave(&ctx->completion_lock, flags);
6003 ret = io_timeout_cancel(ctx, sqe_addr);
6004 if (ret != -ENOENT)
6005 goto done;
6006 ret = io_poll_cancel(ctx, sqe_addr);
6007done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07006008 if (!ret)
6009 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07006010 io_cqring_fill_event(req, ret);
6011 io_commit_cqring(ctx);
6012 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6013 io_cqring_ev_posted(ctx);
6014
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006015 if (ret < 0)
6016 req_set_fail_links(req);
Pavel Begunkov014db002020-03-03 21:33:12 +03006017 io_put_req(req);
Jens Axboe47f46762019-11-09 17:43:02 -07006018}
6019
Jens Axboe3529d8c2019-12-19 18:24:38 -07006020static int io_async_cancel_prep(struct io_kiocb *req,
6021 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07006022{
Jens Axboefbf23842019-12-17 18:45:56 -07006023 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07006024 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006025 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6026 return -EINVAL;
6027 if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags)
Jens Axboee977d6d2019-11-05 12:39:45 -07006028 return -EINVAL;
6029
Jens Axboefbf23842019-12-17 18:45:56 -07006030 req->cancel.addr = READ_ONCE(sqe->addr);
6031 return 0;
6032}
6033
Pavel Begunkov61e98202021-02-10 00:03:08 +00006034static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboefbf23842019-12-17 18:45:56 -07006035{
6036 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07006037
Pavel Begunkov014db002020-03-03 21:33:12 +03006038 io_async_find_and_cancel(ctx, req, req->cancel.addr, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06006039 return 0;
6040}
6041
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006042static int io_rsrc_update_prep(struct io_kiocb *req,
Jens Axboe05f3fb32019-12-09 11:22:50 -07006043 const struct io_uring_sqe *sqe)
6044{
Jens Axboe6ca56f82020-09-18 16:51:19 -06006045 if (unlikely(req->ctx->flags & IORING_SETUP_SQPOLL))
6046 return -EINVAL;
Daniele Albano61710e42020-07-18 14:15:16 -06006047 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
6048 return -EINVAL;
6049 if (sqe->ioprio || sqe->rw_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006050 return -EINVAL;
6051
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006052 req->rsrc_update.offset = READ_ONCE(sqe->off);
6053 req->rsrc_update.nr_args = READ_ONCE(sqe->len);
6054 if (!req->rsrc_update.nr_args)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006055 return -EINVAL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006056 req->rsrc_update.arg = READ_ONCE(sqe->addr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006057 return 0;
6058}
6059
Pavel Begunkov889fca72021-02-10 00:03:09 +00006060static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006061{
6062 struct io_ring_ctx *ctx = req->ctx;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006063 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006064 int ret;
6065
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006066 if (issue_flags & IO_URING_F_NONBLOCK)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006067 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006068
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006069 up.offset = req->rsrc_update.offset;
6070 up.data = req->rsrc_update.arg;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006071
6072 mutex_lock(&ctx->uring_lock);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006073 ret = __io_sqe_files_update(ctx, &up, req->rsrc_update.nr_args);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006074 mutex_unlock(&ctx->uring_lock);
6075
6076 if (ret < 0)
6077 req_set_fail_links(req);
Pavel Begunkov889fca72021-02-10 00:03:09 +00006078 __io_req_complete(req, issue_flags, ret, 0);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006079 return 0;
6080}
6081
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006082static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07006083{
Jens Axboed625c6e2019-12-17 19:53:05 -07006084 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07006085 case IORING_OP_NOP:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006086 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07006087 case IORING_OP_READV:
6088 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006089 case IORING_OP_READ:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006090 return io_read_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006091 case IORING_OP_WRITEV:
6092 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006093 case IORING_OP_WRITE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006094 return io_write_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006095 case IORING_OP_POLL_ADD:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006096 return io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07006097 case IORING_OP_POLL_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006098 return io_poll_remove_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006099 case IORING_OP_FSYNC:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006100 return io_fsync_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006101 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov1155c762021-02-18 18:29:38 +00006102 return io_sfr_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006103 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006104 case IORING_OP_SEND:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006105 return io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07006106 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07006107 case IORING_OP_RECV:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006108 return io_recvmsg_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07006109 case IORING_OP_CONNECT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006110 return io_connect_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006111 case IORING_OP_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006112 return io_timeout_prep(req, sqe, false);
Jens Axboeb29472e2019-12-17 18:50:29 -07006113 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006114 return io_timeout_remove_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07006115 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006116 return io_async_cancel_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07006117 case IORING_OP_LINK_TIMEOUT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006118 return io_timeout_prep(req, sqe, true);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07006119 case IORING_OP_ACCEPT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006120 return io_accept_prep(req, sqe);
Jens Axboed63d1b52019-12-10 10:38:56 -07006121 case IORING_OP_FALLOCATE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006122 return io_fallocate_prep(req, sqe);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006123 case IORING_OP_OPENAT:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006124 return io_openat_prep(req, sqe);
Jens Axboeb5dba592019-12-11 14:02:38 -07006125 case IORING_OP_CLOSE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006126 return io_close_prep(req, sqe);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006127 case IORING_OP_FILES_UPDATE:
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006128 return io_rsrc_update_prep(req, sqe);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006129 case IORING_OP_STATX:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006130 return io_statx_prep(req, sqe);
Jens Axboe4840e412019-12-25 22:03:45 -07006131 case IORING_OP_FADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006132 return io_fadvise_prep(req, sqe);
Jens Axboec1ca7572019-12-25 22:18:28 -07006133 case IORING_OP_MADVISE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006134 return io_madvise_prep(req, sqe);
Jens Axboecebdb982020-01-08 17:59:24 -07006135 case IORING_OP_OPENAT2:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006136 return io_openat2_prep(req, sqe);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006137 case IORING_OP_EPOLL_CTL:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006138 return io_epoll_ctl_prep(req, sqe);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006139 case IORING_OP_SPLICE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006140 return io_splice_prep(req, sqe);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006141 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006142 return io_provide_buffers_prep(req, sqe);
Jens Axboe067524e2020-03-02 16:32:28 -07006143 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006144 return io_remove_buffers_prep(req, sqe);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006145 case IORING_OP_TEE:
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006146 return io_tee_prep(req, sqe);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006147 case IORING_OP_SHUTDOWN:
6148 return io_shutdown_prep(req, sqe);
Jens Axboe80a261f2020-09-28 14:23:58 -06006149 case IORING_OP_RENAMEAT:
6150 return io_renameat_prep(req, sqe);
Jens Axboe14a11432020-09-28 14:27:37 -06006151 case IORING_OP_UNLINKAT:
6152 return io_unlinkat_prep(req, sqe);
Jens Axboef67676d2019-12-02 11:03:47 -07006153 }
6154
Pavel Begunkovbfe76552020-09-30 22:57:55 +03006155 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
6156 req->opcode);
6157 return-EINVAL;
6158}
6159
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006160static int io_req_prep_async(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006161{
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006162 switch (req->opcode) {
6163 case IORING_OP_READV:
6164 case IORING_OP_READ_FIXED:
6165 case IORING_OP_READ:
6166 return io_rw_prep_async(req, READ);
6167 case IORING_OP_WRITEV:
6168 case IORING_OP_WRITE_FIXED:
6169 case IORING_OP_WRITE:
6170 return io_rw_prep_async(req, WRITE);
6171 case IORING_OP_SENDMSG:
6172 case IORING_OP_SEND:
6173 return io_sendmsg_prep_async(req);
6174 case IORING_OP_RECVMSG:
6175 case IORING_OP_RECV:
6176 return io_recvmsg_prep_async(req);
6177 case IORING_OP_CONNECT:
6178 return io_connect_prep_async(req);
6179 }
6180 return 0;
6181}
6182
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006183static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07006184{
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006185 if (!io_op_defs[req->opcode].needs_async_data)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006186 return 0;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006187 /* some opcodes init it during the inital prep */
Pavel Begunkov93642ef2021-02-18 18:29:44 +00006188 if (req->async_data)
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006189 return 0;
6190 if (__io_alloc_async_data(req))
Jens Axboeb76da702019-11-20 13:05:32 -07006191 return -EAGAIN;
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006192 return io_req_prep_async(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193}
6194
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006195static u32 io_get_sequence(struct io_kiocb *req)
6196{
6197 struct io_kiocb *pos;
6198 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006199 u32 total_submitted, nr_reqs = 0;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006200
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006201 io_for_each_link(pos, req)
6202 nr_reqs++;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006203
6204 total_submitted = ctx->cached_sq_head - ctx->cached_sq_dropped;
6205 return total_submitted - nr_reqs;
6206}
6207
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006208static int io_req_defer(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209{
6210 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006211 struct io_defer_entry *de;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212 int ret;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006213 u32 seq;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006214
6215 /* Still need defer if there is pending req in defer list. */
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006216 if (likely(list_empty_careful(&ctx->defer_list) &&
6217 !(req->flags & REQ_F_IO_DRAIN)))
6218 return 0;
6219
6220 seq = io_get_sequence(req);
6221 /* Still a chance to pass the sequence check */
6222 if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223 return 0;
6224
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006225 ret = io_req_defer_prep(req);
6226 if (ret)
6227 return ret;
Pavel Begunkovcbdcb432020-06-29 19:18:43 +03006228 io_prep_async_link(req);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006229 de = kmalloc(sizeof(*de), GFP_KERNEL);
6230 if (!de)
6231 return -ENOMEM;
Jens Axboe31b51512019-01-18 22:56:34 -07006232
6233 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006234 if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) {
Jens Axboe31b51512019-01-18 22:56:34 -07006235 spin_unlock_irq(&ctx->completion_lock);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006236 kfree(de);
Pavel Begunkovae348172020-07-23 20:25:20 +03006237 io_queue_async_work(req);
6238 return -EIOCBQUEUED;
Jens Axboe31b51512019-01-18 22:56:34 -07006239 }
6240
6241 trace_io_uring_defer(ctx, req, req->user_data);
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006242 de->req = req;
Pavel Begunkov9cf7c102020-07-13 23:37:15 +03006243 de->seq = seq;
Pavel Begunkov27dc8332020-07-13 23:37:14 +03006244 list_add_tail(&de->list, &ctx->defer_list);
Jens Axboe31b51512019-01-18 22:56:34 -07006245 spin_unlock_irq(&ctx->completion_lock);
6246 return -EIOCBQUEUED;
6247}
Jens Axboeedafcce2019-01-09 09:16:05 -07006248
Pavel Begunkov3ca405e2020-07-13 23:37:08 +03006249static void __io_clean_op(struct io_kiocb *req)
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006250{
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006251 if (req->flags & REQ_F_BUFFER_SELECTED) {
6252 switch (req->opcode) {
6253 case IORING_OP_READV:
6254 case IORING_OP_READ_FIXED:
6255 case IORING_OP_READ:
Jens Axboebcda7ba2020-02-23 16:42:51 -07006256 kfree((void *)(unsigned long)req->rw.addr);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006257 break;
6258 case IORING_OP_RECVMSG:
6259 case IORING_OP_RECV:
Jens Axboe52de1fe2020-02-27 10:15:42 -07006260 kfree(req->sr_msg.kbuf);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006261 break;
6262 }
6263 req->flags &= ~REQ_F_BUFFER_SELECTED;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006264 }
6265
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006266 if (req->flags & REQ_F_NEED_CLEANUP) {
6267 switch (req->opcode) {
6268 case IORING_OP_READV:
6269 case IORING_OP_READ_FIXED:
6270 case IORING_OP_READ:
6271 case IORING_OP_WRITEV:
6272 case IORING_OP_WRITE_FIXED:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006273 case IORING_OP_WRITE: {
6274 struct io_async_rw *io = req->async_data;
6275 if (io->free_iovec)
6276 kfree(io->free_iovec);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006277 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006278 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006279 case IORING_OP_RECVMSG:
Jens Axboee8c2bc12020-08-15 18:44:09 -07006280 case IORING_OP_SENDMSG: {
6281 struct io_async_msghdr *io = req->async_data;
Pavel Begunkov257e84a2021-02-05 00:58:00 +00006282
6283 kfree(io->free_iov);
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006284 break;
Jens Axboee8c2bc12020-08-15 18:44:09 -07006285 }
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006286 case IORING_OP_SPLICE:
6287 case IORING_OP_TEE:
6288 io_put_file(req, req->splice.file_in,
6289 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
6290 break;
Jens Axboef3cd48502020-09-24 14:55:54 -06006291 case IORING_OP_OPENAT:
6292 case IORING_OP_OPENAT2:
6293 if (req->open.filename)
6294 putname(req->open.filename);
6295 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006296 case IORING_OP_RENAMEAT:
6297 putname(req->rename.oldpath);
6298 putname(req->rename.newpath);
6299 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006300 case IORING_OP_UNLINKAT:
6301 putname(req->unlink.filename);
6302 break;
Pavel Begunkov0e1b6fe32020-07-16 23:28:02 +03006303 }
6304 req->flags &= ~REQ_F_NEED_CLEANUP;
6305 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03006306}
6307
Pavel Begunkov889fca72021-02-10 00:03:09 +00006308static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
Jens Axboeedafcce2019-01-09 09:16:05 -07006309{
Jens Axboeedafcce2019-01-09 09:16:05 -07006310 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07006311 int ret;
Jens Axboeedafcce2019-01-09 09:16:05 -07006312
Jens Axboed625c6e2019-12-17 19:53:05 -07006313 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 case IORING_OP_NOP:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006315 ret = io_nop(req, issue_flags);
Jens Axboe31b51512019-01-18 22:56:34 -07006316 break;
6317 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07006318 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006319 case IORING_OP_READ:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006320 ret = io_read(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321 break;
6322 case IORING_OP_WRITEV:
Jens Axboe2b188cc2019-01-07 10:46:33 -07006323 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07006324 case IORING_OP_WRITE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006325 ret = io_write(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006326 break;
6327 case IORING_OP_FSYNC:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006328 ret = io_fsync(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006329 break;
6330 case IORING_OP_POLL_ADD:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006331 ret = io_poll_add(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006332 break;
6333 case IORING_OP_POLL_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006334 ret = io_poll_remove(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006335 break;
6336 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006337 ret = io_sync_file_range(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006338 break;
6339 case IORING_OP_SENDMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006340 ret = io_sendmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006341 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006342 case IORING_OP_SEND:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006343 ret = io_send(req, issue_flags);
Jens Axboeb76da702019-11-20 13:05:32 -07006344 break;
6345 case IORING_OP_RECVMSG:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006346 ret = io_recvmsg(req, issue_flags);
Pavel Begunkov062d04d2020-10-10 18:34:12 +01006347 break;
Jens Axboefddafac2020-01-04 20:19:44 -07006348 case IORING_OP_RECV:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006349 ret = io_recv(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006350 break;
6351 case IORING_OP_TIMEOUT:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006352 ret = io_timeout(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006353 break;
6354 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006355 ret = io_timeout_remove(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356 break;
6357 case IORING_OP_ACCEPT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006358 ret = io_accept(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006359 break;
6360 case IORING_OP_CONNECT:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006361 ret = io_connect(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006362 break;
6363 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkov61e98202021-02-10 00:03:08 +00006364 ret = io_async_cancel(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07006366 case IORING_OP_FALLOCATE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006367 ret = io_fallocate(req, issue_flags);
Jens Axboed63d1b52019-12-10 10:38:56 -07006368 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07006369 case IORING_OP_OPENAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006370 ret = io_openat(req, issue_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07006371 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07006372 case IORING_OP_CLOSE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006373 ret = io_close(req, issue_flags);
Jens Axboeb5dba592019-12-11 14:02:38 -07006374 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006375 case IORING_OP_FILES_UPDATE:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006376 ret = io_files_update(req, issue_flags);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006377 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006378 case IORING_OP_STATX:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006379 ret = io_statx(req, issue_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07006380 break;
Jens Axboe4840e412019-12-25 22:03:45 -07006381 case IORING_OP_FADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006382 ret = io_fadvise(req, issue_flags);
Jens Axboe4840e412019-12-25 22:03:45 -07006383 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07006384 case IORING_OP_MADVISE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006385 ret = io_madvise(req, issue_flags);
Jens Axboec1ca7572019-12-25 22:18:28 -07006386 break;
Jens Axboecebdb982020-01-08 17:59:24 -07006387 case IORING_OP_OPENAT2:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006388 ret = io_openat2(req, issue_flags);
Jens Axboecebdb982020-01-08 17:59:24 -07006389 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07006390 case IORING_OP_EPOLL_CTL:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006391 ret = io_epoll_ctl(req, issue_flags);
Jens Axboe3e4827b2020-01-08 15:18:09 -07006392 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006393 case IORING_OP_SPLICE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006394 ret = io_splice(req, issue_flags);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03006395 break;
Jens Axboeddf0322d2020-02-23 16:41:33 -07006396 case IORING_OP_PROVIDE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006397 ret = io_provide_buffers(req, issue_flags);
Jens Axboeddf0322d2020-02-23 16:41:33 -07006398 break;
Jens Axboe067524e2020-03-02 16:32:28 -07006399 case IORING_OP_REMOVE_BUFFERS:
Pavel Begunkov889fca72021-02-10 00:03:09 +00006400 ret = io_remove_buffers(req, issue_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006401 break;
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006402 case IORING_OP_TEE:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006403 ret = io_tee(req, issue_flags);
Pavel Begunkovf2a8d5c2020-05-17 14:18:06 +03006404 break;
Jens Axboe36f4fa62020-09-05 11:14:22 -06006405 case IORING_OP_SHUTDOWN:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006406 ret = io_shutdown(req, issue_flags);
Jens Axboe36f4fa62020-09-05 11:14:22 -06006407 break;
Jens Axboe80a261f2020-09-28 14:23:58 -06006408 case IORING_OP_RENAMEAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006409 ret = io_renameat(req, issue_flags);
Jens Axboe80a261f2020-09-28 14:23:58 -06006410 break;
Jens Axboe14a11432020-09-28 14:27:37 -06006411 case IORING_OP_UNLINKAT:
Pavel Begunkov45d189c2021-02-10 00:03:07 +00006412 ret = io_unlinkat(req, issue_flags);
Jens Axboe14a11432020-09-28 14:27:37 -06006413 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006414 default:
6415 ret = -EINVAL;
6416 break;
Jens Axboe31b51512019-01-18 22:56:34 -07006417 }
6418
6419 if (ret)
Jens Axboeedafcce2019-01-09 09:16:05 -07006420 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006421
Jens Axboeb5325762020-05-19 21:20:27 -06006422 /* If the op doesn't have a file, we're not polling for it */
6423 if ((ctx->flags & IORING_SETUP_IOPOLL) && req->file) {
Jens Axboe11ba8202020-01-15 21:51:17 -07006424 const bool in_async = io_wq_current_is_worker();
6425
Jens Axboe11ba8202020-01-15 21:51:17 -07006426 /* workqueue context doesn't hold uring_lock, grab it now */
6427 if (in_async)
6428 mutex_lock(&ctx->uring_lock);
6429
Xiaoguang Wang2e9dbe92020-11-13 00:44:08 +08006430 io_iopoll_req_issued(req, in_async);
Jens Axboe11ba8202020-01-15 21:51:17 -07006431
6432 if (in_async)
6433 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434 }
6435
6436 return 0;
6437}
6438
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00006439static void io_wq_submit_work(struct io_wq_work *work)
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006440{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006441 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006442 struct io_kiocb *timeout;
Jens Axboe561fb042019-10-24 07:25:42 -06006443 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006444
Pavel Begunkov6df1db62020-07-03 22:15:06 +03006445 timeout = io_prep_linked_timeout(req);
6446 if (timeout)
6447 io_queue_linked_timeout(timeout);
Pavel Begunkovd4c81f32020-06-08 21:08:19 +03006448
Jens Axboe4014d942021-01-19 15:53:54 -07006449 if (work->flags & IO_WQ_WORK_CANCEL)
Jens Axboe561fb042019-10-24 07:25:42 -06006450 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07006451
Jens Axboe561fb042019-10-24 07:25:42 -06006452 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06006453 do {
Pavel Begunkov889fca72021-02-10 00:03:09 +00006454 ret = io_issue_sqe(req, 0);
Jens Axboe561fb042019-10-24 07:25:42 -06006455 /*
6456 * We can get EAGAIN for polled IO even though we're
6457 * forcing a sync submission from here, since we can't
6458 * wait for request slots on the block side.
6459 */
6460 if (ret != -EAGAIN)
6461 break;
6462 cond_resched();
6463 } while (1);
6464 }
Jens Axboe31b51512019-01-18 22:56:34 -07006465
Pavel Begunkova3df76982021-02-18 22:32:52 +00006466 /* avoid locking problems by failing it from a clean context */
Jens Axboe561fb042019-10-24 07:25:42 -06006467 if (ret) {
Pavel Begunkova3df76982021-02-18 22:32:52 +00006468 /* io-wq is going to take one down */
6469 refcount_inc(&req->refs);
6470 io_req_task_queue_fail(req, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006471 }
Jens Axboe31b51512019-01-18 22:56:34 -07006472}
Jens Axboe2b188cc2019-01-07 10:46:33 -07006473
Jens Axboe65e19f52019-10-26 07:20:21 -06006474static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
6475 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06006476{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006477 struct fixed_rsrc_table *table;
Jens Axboe65e19f52019-10-26 07:20:21 -06006478
Jens Axboe05f3fb32019-12-09 11:22:50 -07006479 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
Xiaoming Ni84695082020-05-11 19:25:43 +08006480 return table->files[index & IORING_FILE_TABLE_MASK];
Jens Axboe65e19f52019-10-26 07:20:21 -06006481}
6482
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006483static struct file *io_file_get(struct io_submit_state *state,
6484 struct io_kiocb *req, int fd, bool fixed)
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006485{
6486 struct io_ring_ctx *ctx = req->ctx;
6487 struct file *file;
6488
6489 if (fixed) {
Pavel Begunkov479f5172020-10-10 18:34:07 +01006490 if (unlikely((unsigned int)fd >= ctx->nr_user_files))
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006491 return NULL;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006492 fd = array_index_nospec(fd, ctx->nr_user_files);
6493 file = io_file_from_index(ctx, fd);
Pavel Begunkov36f72fe2020-11-18 19:57:26 +00006494 io_set_resource_node(req);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006495 } else {
6496 trace_io_uring_file_get(ctx, fd);
6497 file = __io_file_get(state, fd);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006498 }
6499
Pavel Begunkovce3d5aa2021-02-01 18:59:55 +00006500 if (file && unlikely(file->f_op == &io_uring_fops))
6501 io_req_track_inflight(req);
Pavel Begunkov8371adf2020-10-10 18:34:08 +01006502 return file;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006503}
6504
Jens Axboe2665abf2019-11-05 12:40:47 -07006505static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
6506{
Jens Axboead8a48a2019-11-15 08:49:11 -07006507 struct io_timeout_data *data = container_of(timer,
6508 struct io_timeout_data, timer);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006509 struct io_kiocb *prev, *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07006510 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07006511 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07006512
6513 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006514 prev = req->timeout.head;
6515 req->timeout.head = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006516
6517 /*
6518 * We don't expect the list to be empty, that will only happen if we
6519 * race with the completion of the linked work.
6520 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006521 if (prev && refcount_inc_not_zero(&prev->refs))
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006522 io_remove_next_linked(prev);
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006523 else
6524 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006525 spin_unlock_irqrestore(&ctx->completion_lock, flags);
6526
6527 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006528 req_set_fail_links(prev);
Pavel Begunkov014db002020-03-03 21:33:12 +03006529 io_async_find_and_cancel(ctx, req, prev->user_data, -ETIME);
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006530 io_put_req_deferred(prev, 1);
Jens Axboe47f46762019-11-09 17:43:02 -07006531 } else {
Pavel Begunkov9ae1f8d2021-02-01 18:59:51 +00006532 io_req_complete_post(req, -ETIME, 0);
6533 io_put_req_deferred(req, 1);
Jens Axboe2665abf2019-11-05 12:40:47 -07006534 }
Jens Axboe2665abf2019-11-05 12:40:47 -07006535 return HRTIMER_NORESTART;
6536}
6537
Jens Axboe7271ef32020-08-10 09:55:22 -06006538static void __io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006539{
Jens Axboe76a46e02019-11-10 23:34:16 -07006540 /*
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006541 * If the back reference is NULL, then our linked request finished
6542 * before we got a chance to setup the timer
Jens Axboe76a46e02019-11-10 23:34:16 -07006543 */
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006544 if (req->timeout.head) {
Jens Axboee8c2bc12020-08-15 18:44:09 -07006545 struct io_timeout_data *data = req->async_data;
Jens Axboe94ae5e72019-11-14 19:39:52 -07006546
Jens Axboead8a48a2019-11-15 08:49:11 -07006547 data->timer.function = io_link_timeout_fn;
6548 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
6549 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07006550 }
Jens Axboe7271ef32020-08-10 09:55:22 -06006551}
6552
6553static void io_queue_linked_timeout(struct io_kiocb *req)
6554{
6555 struct io_ring_ctx *ctx = req->ctx;
6556
6557 spin_lock_irq(&ctx->completion_lock);
6558 __io_queue_linked_timeout(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07006559 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07006560
Jens Axboe2665abf2019-11-05 12:40:47 -07006561 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07006562 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07006563}
6564
Jens Axboead8a48a2019-11-15 08:49:11 -07006565static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07006566{
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006567 struct io_kiocb *nxt = req->link;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006568
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006569 if (!nxt || (req->flags & REQ_F_LINK_TIMEOUT) ||
6570 nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboed7718a92020-02-14 22:23:12 -07006571 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07006572
Pavel Begunkov90cd7e42020-10-27 23:25:36 +00006573 nxt->timeout.head = req;
Pavel Begunkov900fad42020-10-19 16:39:16 +01006574 nxt->flags |= REQ_F_LTIMEOUT_ACTIVE;
Jens Axboe76a46e02019-11-10 23:34:16 -07006575 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07006576 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07006577}
6578
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006579static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006580{
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006581 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
Jens Axboe193155c2020-02-22 23:22:19 -07006582 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006583 int ret;
6584
Pavel Begunkov2e5aa6c2020-10-18 10:17:37 +01006585 if ((req->flags & REQ_F_WORK_INITIALIZED) &&
6586 (req->work.flags & IO_WQ_WORK_CREDS) &&
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006587 req->work.identity->creds != current_cred())
6588 old_creds = override_creds(req->work.identity->creds);
Jens Axboe193155c2020-02-22 23:22:19 -07006589
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006590 ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER);
Jens Axboe491381ce2019-10-17 09:20:46 -06006591
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006592 if (old_creds)
6593 revert_creds(old_creds);
Jens Axboe491381ce2019-10-17 09:20:46 -06006594
6595 /*
6596 * We async punt it if the file wasn't marked NOWAIT, or if the file
6597 * doesn't support non-blocking read/write attempts
6598 */
Pavel Begunkov24c74672020-06-21 13:09:51 +03006599 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006600 if (!io_arm_poll_handler(req)) {
Pavel Begunkovf063c542020-07-25 14:41:59 +03006601 /*
6602 * Queued up for async execution, worker will release
6603 * submit reference when the iocb is actually submitted.
6604 */
6605 io_queue_async_work(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006606 }
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006607 } else if (likely(!ret)) {
6608 /* drop submission reference */
Pavel Begunkove342c802021-01-19 13:32:47 +00006609 if (req->flags & REQ_F_COMPLETE_INLINE) {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006610 struct io_ring_ctx *ctx = req->ctx;
6611 struct io_comp_state *cs = &ctx->submit_state.comp;
Jens Axboee65ef562019-03-12 10:16:44 -06006612
Pavel Begunkov6dd0be12021-02-10 00:03:13 +00006613 cs->reqs[cs->nr++] = req;
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006614 if (cs->nr == ARRAY_SIZE(cs->reqs))
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006615 io_submit_flush_completions(cs, ctx);
Pavel Begunkov9affd662021-01-19 13:32:46 +00006616 } else {
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006617 io_put_req(req);
Pavel Begunkov0d63c142020-10-22 16:47:18 +01006618 }
6619 } else {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006620 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06006621 io_put_req(req);
Pavel Begunkov652532a2020-07-03 22:15:07 +03006622 io_req_complete(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06006623 }
Pavel Begunkovd3d72982021-02-12 03:23:51 +00006624 if (linked_timeout)
6625 io_queue_linked_timeout(linked_timeout);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006626}
6627
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006628static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08006629{
6630 int ret;
6631
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006632 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006633 if (ret) {
6634 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03006635fail_req:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07006636 req_set_fail_links(req);
Jens Axboee1e16092020-06-22 09:17:17 -06006637 io_put_req(req);
6638 io_req_complete(req, ret);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006639 }
Pavel Begunkov25508782019-12-30 21:24:47 +03006640 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006641 ret = io_req_defer_prep(req);
6642 if (unlikely(ret))
6643 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07006644 io_queue_async_work(req);
6645 } else {
Pavel Begunkovc5eef2b2021-02-10 00:03:22 +00006646 __io_queue_sqe(req);
Jens Axboece35a472019-12-17 08:04:44 -07006647 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006648}
6649
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006650/*
6651 * Check SQE restrictions (opcode and flags).
6652 *
6653 * Returns 'true' if SQE is allowed, 'false' otherwise.
6654 */
6655static inline bool io_check_restriction(struct io_ring_ctx *ctx,
6656 struct io_kiocb *req,
6657 unsigned int sqe_flags)
6658{
6659 if (!ctx->restricted)
6660 return true;
6661
6662 if (!test_bit(req->opcode, ctx->restrictions.sqe_op))
6663 return false;
6664
6665 if ((sqe_flags & ctx->restrictions.sqe_flags_required) !=
6666 ctx->restrictions.sqe_flags_required)
6667 return false;
6668
6669 if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed |
6670 ctx->restrictions.sqe_flags_required))
6671 return false;
6672
6673 return true;
6674}
6675
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006676static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006677 const struct io_uring_sqe *sqe)
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006678{
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006679 struct io_submit_state *state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006680 unsigned int sqe_flags;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006681 int id, ret = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006682
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006683 req->opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006684 /* same numerical values with corresponding REQ_F_*, safe to copy */
6685 req->flags = sqe_flags = READ_ONCE(sqe->flags);
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006686 req->user_data = READ_ONCE(sqe->user_data);
Jens Axboee8c2bc12020-08-15 18:44:09 -07006687 req->async_data = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006688 req->file = NULL;
6689 req->ctx = ctx;
Pavel Begunkovf2f87372020-10-27 23:25:37 +00006690 req->link = NULL;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00006691 req->fixed_rsrc_refs = NULL;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006692 /* one is dropped after submission, the other at completion */
6693 refcount_set(&req->refs, 2);
Pavel Begunkov4dd28242020-06-15 10:33:13 +03006694 req->task = current;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006695 req->result = 0;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006696
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006697 /* enforce forwards compatibility on users */
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006698 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
6699 req->flags = 0;
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006700 return -EINVAL;
Pavel Begunkovebf4a5d2021-02-20 01:39:53 +00006701 }
Pavel Begunkov5be9ad12021-02-12 18:41:17 +00006702
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006703 if (unlikely(req->opcode >= IORING_OP_LAST))
6704 return -EINVAL;
6705
Jens Axboe28cea78a2020-09-14 10:51:17 -06006706 if (unlikely(io_sq_thread_acquire_mm_files(ctx, req)))
Jens Axboe9d8426a2020-06-16 18:42:49 -06006707 return -EFAULT;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006708
Stefano Garzarella21b55db2020-08-27 16:58:30 +02006709 if (unlikely(!io_check_restriction(ctx, req, sqe_flags)))
6710 return -EACCES;
6711
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006712 if ((sqe_flags & IOSQE_BUFFER_SELECT) &&
6713 !io_op_defs[req->opcode].buffer_select)
6714 return -EOPNOTSUPP;
6715
6716 id = READ_ONCE(sqe->personality);
6717 if (id) {
Jens Axboe1e6fa522020-10-15 08:46:24 -06006718 struct io_identity *iod;
6719
Jens Axboe1e6fa522020-10-15 08:46:24 -06006720 iod = idr_find(&ctx->personality_idr, id);
6721 if (unlikely(!iod))
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006722 return -EINVAL;
Jens Axboe1e6fa522020-10-15 08:46:24 -06006723 refcount_inc(&iod->count);
Pavel Begunkovec99ca62020-10-18 10:17:38 +01006724
6725 __io_req_init_async(req);
Jens Axboe1e6fa522020-10-15 08:46:24 -06006726 get_cred(iod->creds);
6727 req->work.identity = iod;
Jens Axboedfead8a2020-10-14 10:12:37 -06006728 req->work.flags |= IO_WQ_WORK_CREDS;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006729 }
6730
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006731 state = &ctx->submit_state;
Pavel Begunkovef4ff582020-04-12 02:05:05 +03006732
Jens Axboe27926b62020-10-28 09:33:23 -06006733 /*
6734 * Plug now if we have more than 1 IO left after this, and the target
6735 * is potentially a read/write to block based storage.
6736 */
6737 if (!state->plug_started && state->ios_left > 1 &&
6738 io_op_defs[req->opcode].plug) {
6739 blk_start_plug(&state->plug);
6740 state->plug_started = true;
6741 }
Jens Axboe63ff8222020-05-07 14:56:15 -06006742
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006743 if (io_op_defs[req->opcode].needs_file) {
6744 bool fixed = req->flags & REQ_F_FIXED_FILE;
Jens Axboe63ff8222020-05-07 14:56:15 -06006745
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006746 req->file = io_file_get(state, req, READ_ONCE(sqe->fd), fixed);
Pavel Begunkovba13e232021-02-01 18:59:52 +00006747 if (unlikely(!req->file))
Pavel Begunkovbd5bbda2020-11-20 15:50:51 +00006748 ret = -EBADF;
6749 }
6750
Pavel Begunkov71b547c2020-10-10 18:34:09 +01006751 state->ios_left--;
6752 return ret;
Pavel Begunkov0553b8b2020-04-08 08:58:45 +03006753}
6754
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006755static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006756 const struct io_uring_sqe *sqe)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006757{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006758 struct io_submit_link *link = &ctx->submit_state.link;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006759 int ret;
6760
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006761 ret = io_init_req(ctx, req, sqe);
6762 if (unlikely(ret)) {
6763fail_req:
6764 io_put_req(req);
6765 io_req_complete(req, ret);
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006766 if (link->head) {
6767 /* fail even hard links since we don't submit */
Pavel Begunkovcf109602021-02-18 18:29:43 +00006768 link->head->flags |= REQ_F_FAIL_LINK;
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006769 io_put_req(link->head);
6770 io_req_complete(link->head, -ECANCELED);
6771 link->head = NULL;
6772 }
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006773 return ret;
6774 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006775 ret = io_req_prep(req, sqe);
6776 if (unlikely(ret))
6777 goto fail_req;
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006778
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006779 /* don't need @sqe from now on */
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006780 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
6781 true, ctx->flags & IORING_SETUP_SQPOLL);
6782
Jens Axboe6c271ce2019-01-10 11:22:30 -07006783 /*
6784 * If we already have a head request, queue this one for async
6785 * submittal once the head completes. If we don't have a head but
6786 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
6787 * submitted sync once the chain is complete. If none of those
6788 * conditions are true (normal request), then just queue it.
6789 */
6790 if (link->head) {
6791 struct io_kiocb *head = link->head;
6792
6793 /*
6794 * Taking sequential execution of a link, draining both sides
6795 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
6796 * requests in the link. So, it drains the head and the
6797 * next after the link request. The last one is done via
6798 * drain_next flag to persist the effect across calls.
6799 */
6800 if (req->flags & REQ_F_IO_DRAIN) {
6801 head->flags |= REQ_F_IO_DRAIN;
6802 ctx->drain_next = 1;
6803 }
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006804 ret = io_req_defer_prep(req);
Pavel Begunkovcf109602021-02-18 18:29:43 +00006805 if (unlikely(ret))
Pavel Begunkova6b8cadc2021-02-18 18:29:41 +00006806 goto fail_req;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006807 trace_io_uring_link(ctx, req, head);
6808 link->last->link = req;
6809 link->last = req;
6810
6811 /* last request of a link, enqueue the link */
6812 if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) {
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006813 io_queue_sqe(head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006814 link->head = NULL;
6815 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08006816 } else {
6817 if (unlikely(ctx->drain_next)) {
6818 req->flags |= REQ_F_IO_DRAIN;
6819 ctx->drain_next = 0;
6820 }
6821 if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08006822 link->head = req;
6823 link->last = req;
6824 } else {
Pavel Begunkovbe7053b2021-02-18 18:29:45 +00006825 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08006826 }
6827 }
6828
6829 return 0;
6830}
6831
6832/*
6833 * Batched submission is done, ensure local IO is flushed out.
6834 */
6835static void io_submit_state_end(struct io_submit_state *state,
6836 struct io_ring_ctx *ctx)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03006837{
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006838 if (state->link.head)
Pavel Begunkovde59bc12021-02-18 18:29:47 +00006839 io_queue_sqe(state->link.head);
Jens Axboe3529d8c2019-12-19 18:24:38 -07006840 if (state->comp.nr)
Jens Axboe9e645e112019-05-10 16:07:28 -06006841 io_submit_flush_completions(&state->comp, ctx);
Jackie Liua197f662019-11-08 08:09:12 -07006842 if (state->plug_started)
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006843 blk_finish_plug(&state->plug);
Jens Axboe75c6a032020-01-28 10:15:23 -07006844 io_state_file_put(state);
Jens Axboe9e645e112019-05-10 16:07:28 -06006845}
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006846
Jens Axboe9e645e112019-05-10 16:07:28 -06006847/*
6848 * Start submission side cache.
Pavel Begunkov32fe5252019-12-17 22:26:58 +03006849 */
Jens Axboe9e645e112019-05-10 16:07:28 -06006850static void io_submit_state_start(struct io_submit_state *state,
Pavel Begunkov196be952019-11-07 01:41:06 +03006851 unsigned int max_ios)
Jens Axboe9e645e112019-05-10 16:07:28 -06006852{
6853 state->plug_started = false;
Jens Axboebcda7ba2020-02-23 16:42:51 -07006854 state->ios_left = max_ios;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006855 /* set only head, no need to init link_last in advance */
6856 state->link.head = NULL;
Jens Axboe75c6a032020-01-28 10:15:23 -07006857}
6858
Jens Axboe193155c2020-02-22 23:22:19 -07006859static void io_commit_sqring(struct io_ring_ctx *ctx)
6860{
Jens Axboe75c6a032020-01-28 10:15:23 -07006861 struct io_rings *rings = ctx->rings;
6862
6863 /*
Jens Axboe193155c2020-02-22 23:22:19 -07006864 * Ensure any loads from the SQEs are done at this point,
Jens Axboe75c6a032020-01-28 10:15:23 -07006865 * since once we write the new head, the application could
6866 * write new data to them.
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03006867 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03006868 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboebcda7ba2020-02-23 16:42:51 -07006869}
6870
Jens Axboe9e645e112019-05-10 16:07:28 -06006871/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07006872 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe9e645e112019-05-10 16:07:28 -06006873 * that is mapped by userspace. This means that care needs to be taken to
6874 * ensure that reads are stable, as we cannot rely on userspace always
Jens Axboe78e19bb2019-11-06 15:21:34 -07006875 * being a good citizen. If members of the sqe are validated and then later
6876 * used, it's important that those reads are done through READ_ONCE() to
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03006877 * prevent a re-load down the line.
Jens Axboe9e645e112019-05-10 16:07:28 -06006878 */
6879static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx)
Jens Axboe9e645e112019-05-10 16:07:28 -06006880{
6881 u32 *sq_array = ctx->sq_array;
6882 unsigned head;
6883
6884 /*
6885 * The cached sq head (or cq tail) serves two purposes:
6886 *
6887 * 1) allows us to batch the cost of updating the user visible
Pavel Begunkov9d763772019-12-17 02:22:07 +03006888 * head updates.
Jens Axboe9e645e112019-05-10 16:07:28 -06006889 * 2) allows the kernel side to track the head on its own, even
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03006890 * though the application is the one updating it.
6891 */
6892 head = READ_ONCE(sq_array[ctx->cached_sq_head++ & ctx->sq_mask]);
6893 if (likely(head < ctx->sq_entries))
6894 return &ctx->sq_sqes[head];
6895
6896 /* drop invalid entries */
Pavel Begunkov711be032020-01-17 03:57:59 +03006897 ctx->cached_sq_dropped++;
6898 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
6899 return NULL;
6900}
Jens Axboeb7bb4f72019-12-15 22:13:43 -07006901
Jens Axboe0f212202020-09-13 13:09:39 -06006902static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006903{
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006904 int submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006905
Jens Axboec4a2ed72019-11-21 21:01:26 -07006906 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07006907 if (test_bit(0, &ctx->sq_check_overflow)) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00006908 if (!__io_cqring_overflow_flush(ctx, false, NULL, NULL))
Jens Axboead3eb2c2019-12-18 17:12:20 -07006909 return -EBUSY;
6910 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006911
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03006912 /* make sure SQ entry isn't read before tail */
6913 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03006914
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03006915 if (!percpu_ref_tryget_many(&ctx->refs, nr))
6916 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006917
Jens Axboed8a6df12020-10-15 16:24:45 -06006918 percpu_counter_add(&current->io_uring->inflight, nr);
Jens Axboefaf7b512020-10-07 12:48:53 -06006919 refcount_add(nr, &current->usage);
Pavel Begunkovba88ff12021-02-10 00:03:11 +00006920 io_submit_state_start(&ctx->submit_state, nr);
Pavel Begunkovb14cca02020-01-17 04:45:59 +03006921
Pavel Begunkov46c4e162021-02-18 18:29:37 +00006922 while (submitted < nr) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07006923 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03006924 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006925
Pavel Begunkov258b29a2021-02-10 00:03:10 +00006926 req = io_alloc_req(ctx);
Pavel Begunkov196be952019-11-07 01:41:06 +03006927 if (unlikely(!req)) {
6928 if (!submitted)
6929 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03006930 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06006931 }
Pavel Begunkov4fccfcb2021-02-12 11:55:17 +00006932 sqe = io_get_sqe(ctx);
6933 if (unlikely(!sqe)) {
6934 kmem_cache_free(req_cachep, req);
6935 break;
6936 }
Jens Axboed3656342019-12-18 09:50:26 -07006937 /* will complete beyond this point, count as submitted */
6938 submitted++;
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006939 if (io_submit_sqe(ctx, req, sqe))
Jens Axboed3656342019-12-18 09:50:26 -07006940 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006941 }
6942
Pavel Begunkov9466f432020-01-25 22:34:01 +03006943 if (unlikely(submitted != nr)) {
6944 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
Jens Axboed8a6df12020-10-15 16:24:45 -06006945 struct io_uring_task *tctx = current->io_uring;
6946 int unused = nr - ref_used;
Pavel Begunkov9466f432020-01-25 22:34:01 +03006947
Jens Axboed8a6df12020-10-15 16:24:45 -06006948 percpu_ref_put_many(&ctx->refs, unused);
6949 percpu_counter_sub(&tctx->inflight, unused);
6950 put_task_struct_many(current, unused);
Pavel Begunkov9466f432020-01-25 22:34:01 +03006951 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07006952
Pavel Begunkova1ab7b32021-02-18 18:29:42 +00006953 io_submit_state_end(&ctx->submit_state, ctx);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006954 /* Commit SQ ring head once we've consumed and submitted all SQEs */
6955 io_commit_sqring(ctx);
6956
Jens Axboe6c271ce2019-01-10 11:22:30 -07006957 return submitted;
6958}
6959
Xiaoguang Wang23b36282020-07-23 20:57:24 +08006960static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx)
6961{
6962 /* Tell userspace we may need a wakeup call */
6963 spin_lock_irq(&ctx->completion_lock);
6964 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
6965 spin_unlock_irq(&ctx->completion_lock);
6966}
6967
6968static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx)
6969{
6970 spin_lock_irq(&ctx->completion_lock);
6971 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
6972 spin_unlock_irq(&ctx->completion_lock);
6973}
6974
Xiaoguang Wang08369242020-11-03 14:15:59 +08006975static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
Jens Axboe6c271ce2019-01-10 11:22:30 -07006976{
Jens Axboec8d1ba52020-09-14 11:07:26 -06006977 unsigned int to_submit;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08006978 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006979
Jens Axboec8d1ba52020-09-14 11:07:26 -06006980 to_submit = io_sqring_entries(ctx);
Jens Axboee95eee22020-09-08 09:11:32 -06006981 /* if we're handling multiple rings, cap submit size for fairness */
6982 if (cap_entries && to_submit > 8)
6983 to_submit = 8;
6984
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006985 if (!list_empty(&ctx->iopoll_list) || to_submit) {
6986 unsigned nr_events = 0;
6987
Xiaoguang Wang08369242020-11-03 14:15:59 +08006988 mutex_lock(&ctx->uring_lock);
Xiaoguang Wang906a3c62020-11-12 14:56:00 +08006989 if (!list_empty(&ctx->iopoll_list))
6990 io_do_iopoll(ctx, &nr_events, 0);
6991
Pavel Begunkovd9d05212021-01-08 20:57:25 +00006992 if (to_submit && !ctx->sqo_dead &&
6993 likely(!percpu_ref_is_dying(&ctx->refs)))
Xiaoguang Wang08369242020-11-03 14:15:59 +08006994 ret = io_submit_sqes(ctx, to_submit);
6995 mutex_unlock(&ctx->uring_lock);
6996 }
Jens Axboe90554202020-09-03 12:12:41 -06006997
6998 if (!io_sqring_full(ctx) && wq_has_sleeper(&ctx->sqo_sq_wait))
6999 wake_up(&ctx->sqo_sq_wait);
7000
Xiaoguang Wang08369242020-11-03 14:15:59 +08007001 return ret;
7002}
7003
7004static void io_sqd_update_thread_idle(struct io_sq_data *sqd)
7005{
7006 struct io_ring_ctx *ctx;
7007 unsigned sq_thread_idle = 0;
7008
7009 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7010 if (sq_thread_idle < ctx->sq_thread_idle)
7011 sq_thread_idle = ctx->sq_thread_idle;
7012 }
7013
7014 sqd->sq_thread_idle = sq_thread_idle;
Jens Axboec8d1ba52020-09-14 11:07:26 -06007015}
7016
Jens Axboe69fb2132020-09-14 11:16:23 -06007017static void io_sqd_init_new(struct io_sq_data *sqd)
7018{
7019 struct io_ring_ctx *ctx;
7020
7021 while (!list_empty(&sqd->ctx_new_list)) {
7022 ctx = list_first_entry(&sqd->ctx_new_list, struct io_ring_ctx, sqd_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007023 list_move_tail(&ctx->sqd_list, &sqd->ctx_list);
7024 complete(&ctx->sq_thread_comp);
7025 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007026
7027 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007028}
7029
Jens Axboe6c271ce2019-01-10 11:22:30 -07007030static int io_sq_thread(void *data)
7031{
Dennis Zhou91d8f512020-09-16 13:41:05 -07007032 struct cgroup_subsys_state *cur_css = NULL;
Jens Axboe28cea78a2020-09-14 10:51:17 -06007033 struct files_struct *old_files = current->files;
7034 struct nsproxy *old_nsproxy = current->nsproxy;
Jens Axboe69fb2132020-09-14 11:16:23 -06007035 const struct cred *old_cred = NULL;
7036 struct io_sq_data *sqd = data;
7037 struct io_ring_ctx *ctx;
Xiaoguang Wanga0d92052020-11-12 14:55:59 +08007038 unsigned long timeout = 0;
Xiaoguang Wang08369242020-11-03 14:15:59 +08007039 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007040
Jens Axboe28cea78a2020-09-14 10:51:17 -06007041 task_lock(current);
7042 current->files = NULL;
7043 current->nsproxy = NULL;
7044 task_unlock(current);
7045
Jens Axboe69fb2132020-09-14 11:16:23 -06007046 while (!kthread_should_stop()) {
Xiaoguang Wang08369242020-11-03 14:15:59 +08007047 int ret;
7048 bool cap_entries, sqt_spin, needs_sched;
Jens Axboec1edbf52019-11-10 16:56:04 -07007049
7050 /*
Jens Axboe69fb2132020-09-14 11:16:23 -06007051 * Any changes to the sqd lists are synchronized through the
7052 * kthread parking. This synchronizes the thread vs users,
7053 * the users are synchronized on the sqd->ctx_lock.
Jens Axboec1edbf52019-11-10 16:56:04 -07007054 */
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007055 if (kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007056 kthread_parkme();
Xiaoguang Wang65b2b212020-11-19 17:44:46 +08007057 /*
7058 * When sq thread is unparked, in case the previous park operation
7059 * comes from io_put_sq_data(), which means that sq thread is going
7060 * to be stopped, so here needs to have a check.
7061 */
7062 if (kthread_should_stop())
7063 break;
7064 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007065
Xiaoguang Wang08369242020-11-03 14:15:59 +08007066 if (unlikely(!list_empty(&sqd->ctx_new_list))) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007067 io_sqd_init_new(sqd);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007068 timeout = jiffies + sqd->sq_thread_idle;
7069 }
Jens Axboe69fb2132020-09-14 11:16:23 -06007070
Xiaoguang Wang08369242020-11-03 14:15:59 +08007071 sqt_spin = false;
Jens Axboee95eee22020-09-08 09:11:32 -06007072 cap_entries = !list_is_singular(&sqd->ctx_list);
Jens Axboe69fb2132020-09-14 11:16:23 -06007073 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7074 if (current->cred != ctx->creds) {
7075 if (old_cred)
7076 revert_creds(old_cred);
7077 old_cred = override_creds(ctx->creds);
7078 }
Dennis Zhou91d8f512020-09-16 13:41:05 -07007079 io_sq_thread_associate_blkcg(ctx, &cur_css);
Jens Axboe4ea33a92020-10-15 13:46:44 -06007080#ifdef CONFIG_AUDIT
7081 current->loginuid = ctx->loginuid;
7082 current->sessionid = ctx->sessionid;
7083#endif
Jens Axboe69fb2132020-09-14 11:16:23 -06007084
Xiaoguang Wang08369242020-11-03 14:15:59 +08007085 ret = __io_sq_thread(ctx, cap_entries);
7086 if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
7087 sqt_spin = true;
Jens Axboe69fb2132020-09-14 11:16:23 -06007088
Jens Axboe28cea78a2020-09-14 10:51:17 -06007089 io_sq_thread_drop_mm_files();
Jens Axboe6c271ce2019-01-10 11:22:30 -07007090 }
7091
Xiaoguang Wang08369242020-11-03 14:15:59 +08007092 if (sqt_spin || !time_after(jiffies, timeout)) {
Jens Axboec8d1ba52020-09-14 11:07:26 -06007093 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007094 io_sq_thread_drop_mm_files();
Jens Axboec8d1ba52020-09-14 11:07:26 -06007095 cond_resched();
Xiaoguang Wang08369242020-11-03 14:15:59 +08007096 if (sqt_spin)
7097 timeout = jiffies + sqd->sq_thread_idle;
7098 continue;
7099 }
7100
Xiaoguang Wang08369242020-11-03 14:15:59 +08007101 needs_sched = true;
7102 prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
7103 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
7104 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
7105 !list_empty_careful(&ctx->iopoll_list)) {
7106 needs_sched = false;
7107 break;
7108 }
7109 if (io_sqring_entries(ctx)) {
7110 needs_sched = false;
7111 break;
7112 }
7113 }
7114
Hao Xu8b28fdf2021-01-31 22:39:04 +08007115 if (needs_sched && !kthread_should_park()) {
Jens Axboe69fb2132020-09-14 11:16:23 -06007116 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7117 io_ring_set_wakeup_flag(ctx);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007118
Jens Axboe69fb2132020-09-14 11:16:23 -06007119 schedule();
Jens Axboe69fb2132020-09-14 11:16:23 -06007120 list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
7121 io_ring_clear_wakeup_flag(ctx);
Jens Axboe6c271ce2019-01-10 11:22:30 -07007122 }
Xiaoguang Wang08369242020-11-03 14:15:59 +08007123
7124 finish_wait(&sqd->wait, &wait);
7125 timeout = jiffies + sqd->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007126 }
7127
Jens Axboe4c6e2772020-07-01 11:29:10 -06007128 io_run_task_work();
Pavel Begunkovd434ab62021-01-11 04:00:30 +00007129 io_sq_thread_drop_mm_files();
Jens Axboeb41e9852020-02-17 09:52:41 -07007130
Dennis Zhou91d8f512020-09-16 13:41:05 -07007131 if (cur_css)
7132 io_sq_thread_unassociate_blkcg();
Jens Axboe69fb2132020-09-14 11:16:23 -06007133 if (old_cred)
7134 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06007135
Jens Axboe28cea78a2020-09-14 10:51:17 -06007136 task_lock(current);
7137 current->files = old_files;
7138 current->nsproxy = old_nsproxy;
7139 task_unlock(current);
7140
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007141 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06007142
Jens Axboe6c271ce2019-01-10 11:22:30 -07007143 return 0;
7144}
7145
Jens Axboebda52162019-09-24 13:47:15 -06007146struct io_wait_queue {
7147 struct wait_queue_entry wq;
7148 struct io_ring_ctx *ctx;
7149 unsigned to_wait;
7150 unsigned nr_timeouts;
7151};
7152
Pavel Begunkov6c503152021-01-04 20:36:36 +00007153static inline bool io_should_wake(struct io_wait_queue *iowq)
Jens Axboebda52162019-09-24 13:47:15 -06007154{
7155 struct io_ring_ctx *ctx = iowq->ctx;
7156
7157 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08007158 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06007159 * started waiting. For timeouts, we always want to return to userspace,
7160 * regardless of event count.
7161 */
Pavel Begunkov6c503152021-01-04 20:36:36 +00007162 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06007163 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
7164}
7165
7166static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
7167 int wake_flags, void *key)
7168{
7169 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
7170 wq);
7171
Pavel Begunkov6c503152021-01-04 20:36:36 +00007172 /*
7173 * Cannot safely flush overflowed CQEs from here, ensure we wake up
7174 * the task, and the next invocation will do it.
7175 */
7176 if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->cq_check_overflow))
7177 return autoremove_wake_function(curr, mode, wake_flags, key);
7178 return -1;
Jens Axboebda52162019-09-24 13:47:15 -06007179}
7180
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007181static int io_run_task_work_sig(void)
7182{
7183 if (io_run_task_work())
7184 return 1;
7185 if (!signal_pending(current))
7186 return 0;
Jens Axboe792ee0f62020-10-22 20:17:18 -06007187 if (test_tsk_thread_flag(current, TIF_NOTIFY_SIGNAL))
7188 return -ERESTARTSYS;
Jens Axboeaf9c1a42020-09-24 13:32:18 -06007189 return -EINTR;
7190}
7191
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007192/* when returns >0, the caller should retry */
7193static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx,
7194 struct io_wait_queue *iowq,
7195 signed long *timeout)
7196{
7197 int ret;
7198
7199 /* make sure we run task_work before checking for signals */
7200 ret = io_run_task_work_sig();
7201 if (ret || io_should_wake(iowq))
7202 return ret;
7203 /* let the caller flush overflows, retry */
7204 if (test_bit(0, &ctx->cq_check_overflow))
7205 return 1;
7206
7207 *timeout = schedule_timeout(*timeout);
7208 return !*timeout ? -ETIME : 1;
7209}
7210
Jens Axboe2b188cc2019-01-07 10:46:33 -07007211/*
7212 * Wait until events become available, if we don't already have some. The
7213 * application must reap them itself, as they reside on the shared cq ring.
7214 */
7215static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
Hao Xuc73ebb62020-11-03 10:54:37 +08007216 const sigset_t __user *sig, size_t sigsz,
7217 struct __kernel_timespec __user *uts)
Jens Axboe2b188cc2019-01-07 10:46:33 -07007218{
Jens Axboebda52162019-09-24 13:47:15 -06007219 struct io_wait_queue iowq = {
7220 .wq = {
7221 .private = current,
7222 .func = io_wake_function,
7223 .entry = LIST_HEAD_INIT(iowq.wq.entry),
7224 },
7225 .ctx = ctx,
7226 .to_wait = min_events,
7227 };
Hristo Venev75b28af2019-08-26 17:23:46 +00007228 struct io_rings *rings = ctx->rings;
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007229 signed long timeout = MAX_SCHEDULE_TIMEOUT;
7230 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007231
Jens Axboeb41e9852020-02-17 09:52:41 -07007232 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007233 io_cqring_overflow_flush(ctx, false, NULL, NULL);
7234 if (io_cqring_events(ctx) >= min_events)
Jens Axboeb41e9852020-02-17 09:52:41 -07007235 return 0;
Jens Axboe4c6e2772020-07-01 11:29:10 -06007236 if (!io_run_task_work())
Jens Axboeb41e9852020-02-17 09:52:41 -07007237 break;
Jens Axboeb41e9852020-02-17 09:52:41 -07007238 } while (1);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007239
7240 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007241#ifdef CONFIG_COMPAT
7242 if (in_compat_syscall())
7243 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07007244 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007245 else
7246#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07007247 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01007248
Jens Axboe2b188cc2019-01-07 10:46:33 -07007249 if (ret)
7250 return ret;
7251 }
7252
Hao Xuc73ebb62020-11-03 10:54:37 +08007253 if (uts) {
Pavel Begunkovc1d5a222021-02-04 13:51:57 +00007254 struct timespec64 ts;
7255
Hao Xuc73ebb62020-11-03 10:54:37 +08007256 if (get_timespec64(&ts, uts))
7257 return -EFAULT;
7258 timeout = timespec64_to_jiffies(&ts);
7259 }
7260
Jens Axboebda52162019-09-24 13:47:15 -06007261 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007262 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06007263 do {
Pavel Begunkov6c503152021-01-04 20:36:36 +00007264 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Jens Axboebda52162019-09-24 13:47:15 -06007265 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
7266 TASK_INTERRUPTIBLE);
Pavel Begunkoveeb60b92021-02-04 13:51:58 +00007267 ret = io_cqring_wait_schedule(ctx, &iowq, &timeout);
7268 finish_wait(&ctx->wait, &iowq.wq);
7269 } while (ret > 0);
Jens Axboebda52162019-09-24 13:47:15 -06007270
Jens Axboeb7db41c2020-07-04 08:55:50 -06007271 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007272
Hristo Venev75b28af2019-08-26 17:23:46 +00007273 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007274}
7275
Jens Axboe6b063142019-01-10 22:13:58 -07007276static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
7277{
7278#if defined(CONFIG_UNIX)
7279 if (ctx->ring_sock) {
7280 struct sock *sock = ctx->ring_sock->sk;
7281 struct sk_buff *skb;
7282
7283 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
7284 kfree_skb(skb);
7285 }
7286#else
7287 int i;
7288
Jens Axboe65e19f52019-10-26 07:20:21 -06007289 for (i = 0; i < ctx->nr_user_files; i++) {
7290 struct file *file;
7291
7292 file = io_file_from_index(ctx, i);
7293 if (file)
7294 fput(file);
7295 }
Jens Axboe6b063142019-01-10 22:13:58 -07007296#endif
7297}
7298
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007299static void io_rsrc_data_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007300{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007301 struct fixed_rsrc_data *data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007302
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007303 data = container_of(ref, struct fixed_rsrc_data, refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007304 complete(&data->done);
7305}
7306
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007307static inline void io_rsrc_ref_lock(struct io_ring_ctx *ctx)
Pavel Begunkov1642b442020-12-30 21:34:14 +00007308{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007309 spin_lock_bh(&ctx->rsrc_ref_lock);
Pavel Begunkov1642b442020-12-30 21:34:14 +00007310}
7311
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007312static inline void io_rsrc_ref_unlock(struct io_ring_ctx *ctx)
Jens Axboe6b063142019-01-10 22:13:58 -07007313{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007314 spin_unlock_bh(&ctx->rsrc_ref_lock);
7315}
7316
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007317static void io_sqe_rsrc_set_node(struct io_ring_ctx *ctx,
7318 struct fixed_rsrc_data *rsrc_data,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007319 struct fixed_rsrc_ref_node *ref_node)
Jens Axboe6b063142019-01-10 22:13:58 -07007320{
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007321 io_rsrc_ref_lock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007322 rsrc_data->node = ref_node;
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007323 list_add_tail(&ref_node->node, &ctx->rsrc_ref_list);
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007324 io_rsrc_ref_unlock(ctx);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007325 percpu_ref_get(&rsrc_data->refs);
Jens Axboe6b063142019-01-10 22:13:58 -07007326}
7327
Hao Xu8bad28d2021-02-19 17:19:36 +08007328static void io_sqe_rsrc_kill_node(struct io_ring_ctx *ctx, struct fixed_rsrc_data *data)
Jens Axboe6b063142019-01-10 22:13:58 -07007329{
Hao Xu8bad28d2021-02-19 17:19:36 +08007330 struct fixed_rsrc_ref_node *ref_node = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06007331
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007332 io_rsrc_ref_lock(ctx);
Pavel Begunkov1e5d7702020-11-18 14:56:25 +00007333 ref_node = data->node;
Pavel Begunkove6cb0072021-02-20 18:03:47 +00007334 data->node = NULL;
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007335 io_rsrc_ref_unlock(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007336 if (ref_node)
7337 percpu_ref_kill(&ref_node->refs);
Hao Xu8bad28d2021-02-19 17:19:36 +08007338}
Xiaoguang Wang05589552020-03-31 14:05:18 +08007339
Hao Xu8bad28d2021-02-19 17:19:36 +08007340static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
7341 struct io_ring_ctx *ctx,
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007342 void (*rsrc_put)(struct io_ring_ctx *ctx,
7343 struct io_rsrc_put *prsrc))
Hao Xu8bad28d2021-02-19 17:19:36 +08007344{
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007345 struct fixed_rsrc_ref_node *backup_node;
Hao Xu8bad28d2021-02-19 17:19:36 +08007346 int ret;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007347
Hao Xu8bad28d2021-02-19 17:19:36 +08007348 if (data->quiesce)
7349 return -ENXIO;
7350
7351 data->quiesce = true;
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007352 do {
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007353 ret = -ENOMEM;
7354 backup_node = alloc_fixed_rsrc_ref_node(ctx);
7355 if (!backup_node)
Pavel Begunkov1ffc5422020-12-30 21:34:15 +00007356 break;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007357 backup_node->rsrc_data = data;
7358 backup_node->rsrc_put = rsrc_put;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007359
Hao Xu8bad28d2021-02-19 17:19:36 +08007360 io_sqe_rsrc_kill_node(ctx, data);
7361 percpu_ref_kill(&data->refs);
7362 flush_delayed_work(&ctx->rsrc_put_work);
7363
Jens Axboe6b063142019-01-10 22:13:58 -07007364 ret = wait_for_completion_interruptible(&data->done);
Pavel Begunkov88f171a2021-02-20 18:03:50 +00007365 if (!ret || !io_refs_resurrect(&data->refs, &data->done))
Jens Axboe6b063142019-01-10 22:13:58 -07007366 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007367
Hao Xu8bad28d2021-02-19 17:19:36 +08007368 io_sqe_rsrc_set_node(ctx, data, backup_node);
7369 backup_node = NULL;
Hao Xu8bad28d2021-02-19 17:19:36 +08007370 mutex_unlock(&ctx->uring_lock);
7371 ret = io_run_task_work_sig();
7372 mutex_lock(&ctx->uring_lock);
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007373 } while (ret >= 0);
Hao Xu8bad28d2021-02-19 17:19:36 +08007374 data->quiesce = false;
7375
7376 if (backup_node)
7377 destroy_fixed_rsrc_ref_node(backup_node);
7378 return ret;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007379}
7380
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007381static struct fixed_rsrc_data *alloc_fixed_rsrc_data(struct io_ring_ctx *ctx)
7382{
7383 struct fixed_rsrc_data *data;
7384
7385 data = kzalloc(sizeof(*data), GFP_KERNEL);
7386 if (!data)
7387 return NULL;
7388
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007389 if (percpu_ref_init(&data->refs, io_rsrc_data_ref_zero,
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007390 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
7391 kfree(data);
7392 return NULL;
7393 }
7394 data->ctx = ctx;
7395 init_completion(&data->done);
7396 return data;
7397}
7398
7399static void free_fixed_rsrc_data(struct fixed_rsrc_data *data)
7400{
7401 percpu_ref_exit(&data->refs);
7402 kfree(data->table);
7403 kfree(data);
7404}
7405
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007406static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
7407{
7408 struct fixed_rsrc_data *data = ctx->file_data;
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007409 unsigned nr_tables, i;
7410 int ret;
7411
Hao Xu8bad28d2021-02-19 17:19:36 +08007412 /*
7413 * percpu_ref_is_dying() is to stop parallel files unregister
7414 * Since we possibly drop uring lock later in this function to
7415 * run task work.
7416 */
7417 if (!data || percpu_ref_is_dying(&data->refs))
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007418 return -ENXIO;
Pavel Begunkovf2303b12021-02-20 18:03:49 +00007419 ret = io_rsrc_ref_quiesce(data, ctx, io_ring_file_put);
Bijan Mottahedehd7954b22021-01-15 17:37:50 +00007420 if (ret)
7421 return ret;
7422
Jens Axboe6b063142019-01-10 22:13:58 -07007423 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06007424 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
7425 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007426 kfree(data->table[i].files);
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007427 free_fixed_rsrc_data(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007428 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07007429 ctx->nr_user_files = 0;
7430 return 0;
7431}
7432
Jens Axboe534ca6d2020-09-02 13:52:19 -06007433static void io_put_sq_data(struct io_sq_data *sqd)
Jens Axboe6c271ce2019-01-10 11:22:30 -07007434{
Jens Axboe534ca6d2020-09-02 13:52:19 -06007435 if (refcount_dec_and_test(&sqd->refs)) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02007436 /*
7437 * The park is a bit of a work-around, without it we get
7438 * warning spews on shutdown with SQPOLL set and affinity
7439 * set to a single CPU.
7440 */
Jens Axboe534ca6d2020-09-02 13:52:19 -06007441 if (sqd->thread) {
7442 kthread_park(sqd->thread);
7443 kthread_stop(sqd->thread);
7444 }
7445
7446 kfree(sqd);
7447 }
7448}
7449
Jens Axboeaa061652020-09-02 14:50:27 -06007450static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
7451{
7452 struct io_ring_ctx *ctx_attach;
7453 struct io_sq_data *sqd;
7454 struct fd f;
7455
7456 f = fdget(p->wq_fd);
7457 if (!f.file)
7458 return ERR_PTR(-ENXIO);
7459 if (f.file->f_op != &io_uring_fops) {
7460 fdput(f);
7461 return ERR_PTR(-EINVAL);
7462 }
7463
7464 ctx_attach = f.file->private_data;
7465 sqd = ctx_attach->sq_data;
7466 if (!sqd) {
7467 fdput(f);
7468 return ERR_PTR(-EINVAL);
7469 }
7470
7471 refcount_inc(&sqd->refs);
7472 fdput(f);
7473 return sqd;
7474}
7475
Jens Axboe534ca6d2020-09-02 13:52:19 -06007476static struct io_sq_data *io_get_sq_data(struct io_uring_params *p)
7477{
7478 struct io_sq_data *sqd;
7479
Jens Axboeaa061652020-09-02 14:50:27 -06007480 if (p->flags & IORING_SETUP_ATTACH_WQ)
7481 return io_attach_sq_data(p);
7482
Jens Axboe534ca6d2020-09-02 13:52:19 -06007483 sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
7484 if (!sqd)
7485 return ERR_PTR(-ENOMEM);
7486
7487 refcount_set(&sqd->refs, 1);
Jens Axboe69fb2132020-09-14 11:16:23 -06007488 INIT_LIST_HEAD(&sqd->ctx_list);
7489 INIT_LIST_HEAD(&sqd->ctx_new_list);
7490 mutex_init(&sqd->ctx_lock);
7491 mutex_init(&sqd->lock);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007492 init_waitqueue_head(&sqd->wait);
7493 return sqd;
7494}
7495
Jens Axboe69fb2132020-09-14 11:16:23 -06007496static void io_sq_thread_unpark(struct io_sq_data *sqd)
7497 __releases(&sqd->lock)
7498{
7499 if (!sqd->thread)
7500 return;
7501 kthread_unpark(sqd->thread);
7502 mutex_unlock(&sqd->lock);
7503}
7504
7505static void io_sq_thread_park(struct io_sq_data *sqd)
7506 __acquires(&sqd->lock)
7507{
7508 if (!sqd->thread)
7509 return;
7510 mutex_lock(&sqd->lock);
7511 kthread_park(sqd->thread);
7512}
7513
Jens Axboe534ca6d2020-09-02 13:52:19 -06007514static void io_sq_thread_stop(struct io_ring_ctx *ctx)
7515{
7516 struct io_sq_data *sqd = ctx->sq_data;
7517
7518 if (sqd) {
7519 if (sqd->thread) {
7520 /*
7521 * We may arrive here from the error branch in
7522 * io_sq_offload_create() where the kthread is created
7523 * without being waked up, thus wake it up now to make
7524 * sure the wait will complete.
7525 */
7526 wake_up_process(sqd->thread);
7527 wait_for_completion(&ctx->sq_thread_comp);
Jens Axboe69fb2132020-09-14 11:16:23 -06007528
7529 io_sq_thread_park(sqd);
7530 }
7531
7532 mutex_lock(&sqd->ctx_lock);
7533 list_del(&ctx->sqd_list);
Xiaoguang Wang08369242020-11-03 14:15:59 +08007534 io_sqd_update_thread_idle(sqd);
Jens Axboe69fb2132020-09-14 11:16:23 -06007535 mutex_unlock(&sqd->ctx_lock);
7536
Xiaoguang Wang08369242020-11-03 14:15:59 +08007537 if (sqd->thread)
Jens Axboe69fb2132020-09-14 11:16:23 -06007538 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06007539
7540 io_put_sq_data(sqd);
7541 ctx->sq_data = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07007542 }
7543}
7544
Jens Axboe6b063142019-01-10 22:13:58 -07007545#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07007546/*
7547 * Ensure the UNIX gc is aware of our file set, so we are certain that
7548 * the io_uring can be safely unregistered on process exit, even if we have
7549 * loops in the file referencing.
7550 */
7551static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
7552{
7553 struct sock *sk = ctx->ring_sock->sk;
7554 struct scm_fp_list *fpl;
7555 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06007556 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07007557
Jens Axboe6b063142019-01-10 22:13:58 -07007558 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
7559 if (!fpl)
7560 return -ENOMEM;
7561
7562 skb = alloc_skb(0, GFP_KERNEL);
7563 if (!skb) {
7564 kfree(fpl);
7565 return -ENOMEM;
7566 }
7567
7568 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07007569
Jens Axboe08a45172019-10-03 08:11:03 -06007570 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07007571 fpl->user = get_uid(ctx->user);
7572 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007573 struct file *file = io_file_from_index(ctx, i + offset);
7574
7575 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06007576 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06007577 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06007578 unix_inflight(fpl->user, fpl->fp[nr_files]);
7579 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07007580 }
7581
Jens Axboe08a45172019-10-03 08:11:03 -06007582 if (nr_files) {
7583 fpl->max = SCM_MAX_FD;
7584 fpl->count = nr_files;
7585 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007586 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06007587 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
7588 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07007589
Jens Axboe08a45172019-10-03 08:11:03 -06007590 for (i = 0; i < nr_files; i++)
7591 fput(fpl->fp[i]);
7592 } else {
7593 kfree_skb(skb);
7594 kfree(fpl);
7595 }
Jens Axboe6b063142019-01-10 22:13:58 -07007596
7597 return 0;
7598}
7599
7600/*
7601 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
7602 * causes regular reference counting to break down. We rely on the UNIX
7603 * garbage collection to take care of this problem for us.
7604 */
7605static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7606{
7607 unsigned left, total;
7608 int ret = 0;
7609
7610 total = 0;
7611 left = ctx->nr_user_files;
7612 while (left) {
7613 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07007614
7615 ret = __io_sqe_files_scm(ctx, this_files, total);
7616 if (ret)
7617 break;
7618 left -= this_files;
7619 total += this_files;
7620 }
7621
7622 if (!ret)
7623 return 0;
7624
7625 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06007626 struct file *file = io_file_from_index(ctx, total);
7627
7628 if (file)
7629 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07007630 total++;
7631 }
7632
7633 return ret;
7634}
7635#else
7636static int io_sqe_files_scm(struct io_ring_ctx *ctx)
7637{
7638 return 0;
7639}
7640#endif
7641
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007642static int io_sqe_alloc_file_tables(struct fixed_rsrc_data *file_data,
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007643 unsigned nr_tables, unsigned nr_files)
Jens Axboe65e19f52019-10-26 07:20:21 -06007644{
7645 int i;
7646
7647 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007648 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007649 unsigned this_files;
7650
7651 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
7652 table->files = kcalloc(this_files, sizeof(struct file *),
7653 GFP_KERNEL);
7654 if (!table->files)
7655 break;
7656 nr_files -= this_files;
7657 }
7658
7659 if (i == nr_tables)
7660 return 0;
7661
7662 for (i = 0; i < nr_tables; i++) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007663 struct fixed_rsrc_table *table = &file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06007664 kfree(table->files);
7665 }
7666 return 1;
7667}
7668
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007669static void io_ring_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
Jens Axboec3a31e62019-10-03 13:59:56 -06007670{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007671 struct file *file = prsrc->file;
Jens Axboec3a31e62019-10-03 13:59:56 -06007672#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06007673 struct sock *sock = ctx->ring_sock->sk;
7674 struct sk_buff_head list, *head = &sock->sk_receive_queue;
7675 struct sk_buff *skb;
7676 int i;
7677
7678 __skb_queue_head_init(&list);
7679
7680 /*
7681 * Find the skb that holds this file in its SCM_RIGHTS. When found,
7682 * remove this entry and rearrange the file array.
7683 */
7684 skb = skb_dequeue(head);
7685 while (skb) {
7686 struct scm_fp_list *fp;
7687
7688 fp = UNIXCB(skb).fp;
7689 for (i = 0; i < fp->count; i++) {
7690 int left;
7691
7692 if (fp->fp[i] != file)
7693 continue;
7694
7695 unix_notinflight(fp->user, fp->fp[i]);
7696 left = fp->count - 1 - i;
7697 if (left) {
7698 memmove(&fp->fp[i], &fp->fp[i + 1],
7699 left * sizeof(struct file *));
7700 }
7701 fp->count--;
7702 if (!fp->count) {
7703 kfree_skb(skb);
7704 skb = NULL;
7705 } else {
7706 __skb_queue_tail(&list, skb);
7707 }
7708 fput(file);
7709 file = NULL;
7710 break;
7711 }
7712
7713 if (!file)
7714 break;
7715
7716 __skb_queue_tail(&list, skb);
7717
7718 skb = skb_dequeue(head);
7719 }
7720
7721 if (skb_peek(&list)) {
7722 spin_lock_irq(&head->lock);
7723 while ((skb = __skb_dequeue(&list)) != NULL)
7724 __skb_queue_tail(head, skb);
7725 spin_unlock_irq(&head->lock);
7726 }
7727#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07007728 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06007729#endif
7730}
7731
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007732static void __io_rsrc_put_work(struct fixed_rsrc_ref_node *ref_node)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007733{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007734 struct fixed_rsrc_data *rsrc_data = ref_node->rsrc_data;
7735 struct io_ring_ctx *ctx = rsrc_data->ctx;
7736 struct io_rsrc_put *prsrc, *tmp;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007737
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007738 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
7739 list_del(&prsrc->list);
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007740 ref_node->rsrc_put(ctx, prsrc);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007741 kfree(prsrc);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007742 }
7743
Xiaoguang Wang05589552020-03-31 14:05:18 +08007744 percpu_ref_exit(&ref_node->refs);
7745 kfree(ref_node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007746 percpu_ref_put(&rsrc_data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007747}
7748
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007749static void io_rsrc_put_work(struct work_struct *work)
Jens Axboe4a38aed22020-05-14 17:21:15 -06007750{
7751 struct io_ring_ctx *ctx;
7752 struct llist_node *node;
7753
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007754 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
7755 node = llist_del_all(&ctx->rsrc_put_llist);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007756
7757 while (node) {
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007758 struct fixed_rsrc_ref_node *ref_node;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007759 struct llist_node *next = node->next;
7760
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007761 ref_node = llist_entry(node, struct fixed_rsrc_ref_node, llist);
7762 __io_rsrc_put_work(ref_node);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007763 node = next;
7764 }
7765}
7766
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007767static struct file **io_fixed_file_slot(struct fixed_rsrc_data *file_data,
7768 unsigned i)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007769{
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007770 struct fixed_rsrc_table *table;
7771
7772 table = &file_data->table[i >> IORING_FILE_TABLE_SHIFT];
7773 return &table->files[i & IORING_FILE_TABLE_MASK];
7774}
7775
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007776static void io_rsrc_node_ref_zero(struct percpu_ref *ref)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007777{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007778 struct fixed_rsrc_ref_node *ref_node;
7779 struct fixed_rsrc_data *data;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007780 struct io_ring_ctx *ctx;
Pavel Begunkove2978222020-11-18 14:56:26 +00007781 bool first_add = false;
Jens Axboe4a38aed22020-05-14 17:21:15 -06007782 int delay = HZ;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007783
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007784 ref_node = container_of(ref, struct fixed_rsrc_ref_node, refs);
7785 data = ref_node->rsrc_data;
Pavel Begunkove2978222020-11-18 14:56:26 +00007786 ctx = data->ctx;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007787
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007788 io_rsrc_ref_lock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007789 ref_node->done = true;
7790
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007791 while (!list_empty(&ctx->rsrc_ref_list)) {
7792 ref_node = list_first_entry(&ctx->rsrc_ref_list,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007793 struct fixed_rsrc_ref_node, node);
Pavel Begunkove2978222020-11-18 14:56:26 +00007794 /* recycle ref nodes in order */
7795 if (!ref_node->done)
7796 break;
7797 list_del(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007798 first_add |= llist_add(&ref_node->llist, &ctx->rsrc_put_llist);
Pavel Begunkove2978222020-11-18 14:56:26 +00007799 }
Bijan Mottahedeh2a63b2d2021-01-15 17:37:47 +00007800 io_rsrc_ref_unlock(ctx);
Pavel Begunkove2978222020-11-18 14:56:26 +00007801
7802 if (percpu_ref_is_dying(&data->refs))
Jens Axboe4a38aed22020-05-14 17:21:15 -06007803 delay = 0;
7804
Jens Axboe4a38aed22020-05-14 17:21:15 -06007805 if (!delay)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007806 mod_delayed_work(system_wq, &ctx->rsrc_put_work, 0);
Jens Axboe4a38aed22020-05-14 17:21:15 -06007807 else if (first_add)
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007808 queue_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007809}
7810
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007811static struct fixed_rsrc_ref_node *alloc_fixed_rsrc_ref_node(
Xiaoguang Wang05589552020-03-31 14:05:18 +08007812 struct io_ring_ctx *ctx)
7813{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007814 struct fixed_rsrc_ref_node *ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007815
7816 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
7817 if (!ref_node)
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007818 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007819
Bijan Mottahedeh00835dc2021-01-15 17:37:52 +00007820 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
Xiaoguang Wang05589552020-03-31 14:05:18 +08007821 0, GFP_KERNEL)) {
7822 kfree(ref_node);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007823 return NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007824 }
7825 INIT_LIST_HEAD(&ref_node->node);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007826 INIT_LIST_HEAD(&ref_node->rsrc_list);
Pavel Begunkove2978222020-11-18 14:56:26 +00007827 ref_node->done = false;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007828 return ref_node;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007829}
7830
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007831static void init_fixed_file_ref_node(struct io_ring_ctx *ctx,
7832 struct fixed_rsrc_ref_node *ref_node)
Bijan Mottahedeh68025352021-01-15 17:37:48 +00007833{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007834 ref_node->rsrc_data = ctx->file_data;
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007835 ref_node->rsrc_put = io_ring_file_put;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007836}
7837
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007838static void destroy_fixed_rsrc_ref_node(struct fixed_rsrc_ref_node *ref_node)
Xiaoguang Wang05589552020-03-31 14:05:18 +08007839{
7840 percpu_ref_exit(&ref_node->refs);
7841 kfree(ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007842}
7843
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007844
Jens Axboe05f3fb32019-12-09 11:22:50 -07007845static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
7846 unsigned nr_args)
7847{
7848 __s32 __user *fds = (__s32 __user *) arg;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007849 unsigned nr_tables, i;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007850 struct file *file;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007851 int fd, ret = -ENOMEM;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007852 struct fixed_rsrc_ref_node *ref_node;
7853 struct fixed_rsrc_data *file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007854
7855 if (ctx->file_data)
7856 return -EBUSY;
7857 if (!nr_args)
7858 return -EINVAL;
7859 if (nr_args > IORING_MAX_FIXED_FILES)
7860 return -EMFILE;
7861
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007862 file_data = alloc_fixed_rsrc_data(ctx);
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007863 if (!file_data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007864 return -ENOMEM;
Dan Carpenter13770a72021-02-01 15:23:42 +03007865 ctx->file_data = file_data;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007866
7867 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
Colin Ian King035fbaf2020-10-12 15:03:41 +01007868 file_data->table = kcalloc(nr_tables, sizeof(*file_data->table),
Pavel Begunkov5398ae62020-10-10 18:34:14 +01007869 GFP_KERNEL);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007870 if (!file_data->table)
7871 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007872
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007873 if (io_sqe_alloc_file_tables(file_data, nr_tables, nr_args))
Jens Axboe05f3fb32019-12-09 11:22:50 -07007874 goto out_free;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007875
Jens Axboe05f3fb32019-12-09 11:22:50 -07007876 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007877 if (copy_from_user(&fd, &fds[i], sizeof(fd))) {
7878 ret = -EFAULT;
7879 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007880 }
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007881 /* allow sparse sets */
7882 if (fd == -1)
7883 continue;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007884
Jens Axboe05f3fb32019-12-09 11:22:50 -07007885 file = fget(fd);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007886 ret = -EBADF;
7887 if (!file)
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007888 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007889
7890 /*
7891 * Don't allow io_uring instances to be registered. If UNIX
7892 * isn't enabled, then this causes a reference cycle and this
7893 * instance can never get freed. If UNIX is enabled we'll
7894 * handle it just fine, but there's still no point in allowing
7895 * a ring fd as it doesn't support regular read/write anyway.
7896 */
7897 if (file->f_op == &io_uring_fops) {
7898 fput(file);
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007899 goto out_fput;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007900 }
Pavel Begunkovea64ec022021-02-04 13:52:07 +00007901 *io_fixed_file_slot(file_data, i) = file;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007902 }
7903
Jens Axboe05f3fb32019-12-09 11:22:50 -07007904 ret = io_sqe_files_scm(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007905 if (ret) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007906 io_sqe_files_unregister(ctx);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007907 return ret;
7908 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007909
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007910 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007911 if (!ref_node) {
Xiaoguang Wang05589552020-03-31 14:05:18 +08007912 io_sqe_files_unregister(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00007913 return -ENOMEM;
Xiaoguang Wang05589552020-03-31 14:05:18 +08007914 }
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00007915 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007916
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00007917 io_sqe_rsrc_set_node(ctx, file_data, ref_node);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007918 return ret;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007919out_fput:
7920 for (i = 0; i < ctx->nr_user_files; i++) {
7921 file = io_file_from_index(ctx, i);
7922 if (file)
7923 fput(file);
7924 }
7925 for (i = 0; i < nr_tables; i++)
7926 kfree(file_data->table[i].files);
7927 ctx->nr_user_files = 0;
Pavel Begunkov600cf3f2020-10-10 18:34:15 +01007928out_free:
Bijan Mottahedeh1ad555c2021-01-15 17:37:51 +00007929 free_fixed_rsrc_data(ctx->file_data);
Jens Axboe55cbc252020-10-14 07:35:57 -06007930 ctx->file_data = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007931 return ret;
7932}
7933
Jens Axboec3a31e62019-10-03 13:59:56 -06007934static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
7935 int index)
7936{
7937#if defined(CONFIG_UNIX)
7938 struct sock *sock = ctx->ring_sock->sk;
7939 struct sk_buff_head *head = &sock->sk_receive_queue;
7940 struct sk_buff *skb;
7941
7942 /*
7943 * See if we can merge this file into an existing skb SCM_RIGHTS
7944 * file set. If there's no room, fall back to allocating a new skb
7945 * and filling it in.
7946 */
7947 spin_lock_irq(&head->lock);
7948 skb = skb_peek(head);
7949 if (skb) {
7950 struct scm_fp_list *fpl = UNIXCB(skb).fp;
7951
7952 if (fpl->count < SCM_MAX_FD) {
7953 __skb_unlink(skb, head);
7954 spin_unlock_irq(&head->lock);
7955 fpl->fp[fpl->count] = get_file(file);
7956 unix_inflight(fpl->user, fpl->fp[fpl->count]);
7957 fpl->count++;
7958 spin_lock_irq(&head->lock);
7959 __skb_queue_head(head, skb);
7960 } else {
7961 skb = NULL;
7962 }
7963 }
7964 spin_unlock_irq(&head->lock);
7965
7966 if (skb) {
7967 fput(file);
7968 return 0;
7969 }
7970
7971 return __io_sqe_files_scm(ctx, 1, index);
7972#else
7973 return 0;
7974#endif
7975}
7976
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007977static int io_queue_rsrc_removal(struct fixed_rsrc_data *data, void *rsrc)
Jens Axboe05f3fb32019-12-09 11:22:50 -07007978{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007979 struct io_rsrc_put *prsrc;
7980 struct fixed_rsrc_ref_node *ref_node = data->node;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007981
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007982 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
7983 if (!prsrc)
Hillf Dantona5318d32020-03-23 17:47:15 +08007984 return -ENOMEM;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007985
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007986 prsrc->rsrc = rsrc;
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007987 list_add(&prsrc->list, &ref_node->rsrc_list);
Xiaoguang Wang05589552020-03-31 14:05:18 +08007988
Hillf Dantona5318d32020-03-23 17:47:15 +08007989 return 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07007990}
7991
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007992static inline int io_queue_file_removal(struct fixed_rsrc_data *data,
7993 struct file *file)
7994{
Bijan Mottahedeh50238532021-01-15 17:37:45 +00007995 return io_queue_rsrc_removal(data, (void *)file);
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007996}
7997
Jens Axboe05f3fb32019-12-09 11:22:50 -07007998static int __io_sqe_files_update(struct io_ring_ctx *ctx,
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00007999 struct io_uring_rsrc_update *up,
Jens Axboe05f3fb32019-12-09 11:22:50 -07008000 unsigned nr_args)
8001{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008002 struct fixed_rsrc_data *data = ctx->file_data;
8003 struct fixed_rsrc_ref_node *ref_node;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008004 struct file *file, **file_slot;
Jens Axboec3a31e62019-10-03 13:59:56 -06008005 __s32 __user *fds;
8006 int fd, i, err;
8007 __u32 done;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008008 bool needs_switch = false;
Jens Axboec3a31e62019-10-03 13:59:56 -06008009
Jens Axboe05f3fb32019-12-09 11:22:50 -07008010 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06008011 return -EOVERFLOW;
8012 if (done > ctx->nr_user_files)
8013 return -EINVAL;
8014
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008015 ref_node = alloc_fixed_rsrc_ref_node(ctx);
Matthew Wilcox (Oracle)3e2224c2021-01-06 16:09:26 +00008016 if (!ref_node)
8017 return -ENOMEM;
Pavel Begunkovbc9744c2021-01-15 17:37:49 +00008018 init_fixed_file_ref_node(ctx, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008019
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008020 fds = u64_to_user_ptr(up->data);
Pavel Begunkov67973b92021-01-26 13:51:09 +00008021 for (done = 0; done < nr_args; done++) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008022 err = 0;
8023 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
8024 err = -EFAULT;
8025 break;
8026 }
noah4e0377a2021-01-26 15:23:28 -05008027 if (fd == IORING_REGISTER_FILES_SKIP)
8028 continue;
8029
Pavel Begunkov67973b92021-01-26 13:51:09 +00008030 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008031 file_slot = io_fixed_file_slot(ctx->file_data, i);
8032
8033 if (*file_slot) {
8034 err = io_queue_file_removal(data, *file_slot);
Hillf Dantona5318d32020-03-23 17:47:15 +08008035 if (err)
8036 break;
Pavel Begunkovea64ec022021-02-04 13:52:07 +00008037 *file_slot = NULL;
Xiaoguang Wang05589552020-03-31 14:05:18 +08008038 needs_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06008039 }
8040 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06008041 file = fget(fd);
8042 if (!file) {
8043 err = -EBADF;
8044 break;
8045 }
8046 /*
8047 * Don't allow io_uring instances to be registered. If
8048 * UNIX isn't enabled, then this causes a reference
8049 * cycle and this instance can never get freed. If UNIX
8050 * is enabled we'll handle it just fine, but there's
8051 * still no point in allowing a ring fd as it doesn't
8052 * support regular read/write anyway.
8053 */
8054 if (file->f_op == &io_uring_fops) {
8055 fput(file);
8056 err = -EBADF;
8057 break;
8058 }
Jens Axboee68a3ff2021-02-11 07:45:08 -07008059 *file_slot = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06008060 err = io_sqe_file_register(ctx, file, i);
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008061 if (err) {
Jens Axboee68a3ff2021-02-11 07:45:08 -07008062 *file_slot = NULL;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008063 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06008064 break;
Yang Yingliangf3bd9da2020-07-09 10:11:41 +00008065 }
Jens Axboec3a31e62019-10-03 13:59:56 -06008066 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07008067 }
8068
Xiaoguang Wang05589552020-03-31 14:05:18 +08008069 if (needs_switch) {
Pavel Begunkovb2e96852020-10-10 18:34:16 +01008070 percpu_ref_kill(&data->node->refs);
Bijan Mottahedehd67d2262021-01-15 17:37:46 +00008071 io_sqe_rsrc_set_node(ctx, data, ref_node);
Xiaoguang Wang05589552020-03-31 14:05:18 +08008072 } else
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008073 destroy_fixed_rsrc_ref_node(ref_node);
Jens Axboec3a31e62019-10-03 13:59:56 -06008074
8075 return done ? done : err;
8076}
Xiaoguang Wang05589552020-03-31 14:05:18 +08008077
Jens Axboe05f3fb32019-12-09 11:22:50 -07008078static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
8079 unsigned nr_args)
8080{
Bijan Mottahedeh269bbe52021-01-15 17:37:44 +00008081 struct io_uring_rsrc_update up;
Jens Axboe05f3fb32019-12-09 11:22:50 -07008082
8083 if (!ctx->file_data)
8084 return -ENXIO;
8085 if (!nr_args)
8086 return -EINVAL;
8087 if (copy_from_user(&up, arg, sizeof(up)))
8088 return -EFAULT;
8089 if (up.resv)
8090 return -EINVAL;
8091
8092 return __io_sqe_files_update(ctx, &up, nr_args);
8093}
Jens Axboec3a31e62019-10-03 13:59:56 -06008094
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008095static struct io_wq_work *io_free_work(struct io_wq_work *work)
Jens Axboe7d723062019-11-12 22:31:31 -07008096{
8097 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
8098
Pavel Begunkov5280f7e2021-02-04 13:52:08 +00008099 req = io_put_req_find_next(req);
8100 return req ? &req->work : NULL;
Jens Axboe7d723062019-11-12 22:31:31 -07008101}
8102
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008103static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx)
Pavel Begunkov24369c22020-01-28 03:15:48 +03008104{
8105 struct io_wq_data data;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008106 unsigned int concurrency;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008107
8108 data.user = ctx->user;
Pavel Begunkove9fd9392020-03-04 16:14:12 +03008109 data.free_work = io_free_work;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +03008110 data.do_work = io_wq_submit_work;
Pavel Begunkov24369c22020-01-28 03:15:48 +03008111
Jens Axboed25e3a32021-02-16 11:41:41 -07008112 /* Do QD, or 4 * CPUS, whatever is smallest */
8113 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Pavel Begunkov24369c22020-01-28 03:15:48 +03008114
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008115 return io_wq_create(concurrency, &data);
Pavel Begunkov24369c22020-01-28 03:15:48 +03008116}
8117
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008118static int io_uring_alloc_task_context(struct task_struct *task,
8119 struct io_ring_ctx *ctx)
Jens Axboe0f212202020-09-13 13:09:39 -06008120{
8121 struct io_uring_task *tctx;
Jens Axboed8a6df12020-10-15 16:24:45 -06008122 int ret;
Jens Axboe0f212202020-09-13 13:09:39 -06008123
8124 tctx = kmalloc(sizeof(*tctx), GFP_KERNEL);
8125 if (unlikely(!tctx))
8126 return -ENOMEM;
8127
Jens Axboed8a6df12020-10-15 16:24:45 -06008128 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
8129 if (unlikely(ret)) {
8130 kfree(tctx);
8131 return ret;
8132 }
8133
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008134 tctx->io_wq = io_init_wq_offload(ctx);
8135 if (IS_ERR(tctx->io_wq)) {
8136 ret = PTR_ERR(tctx->io_wq);
8137 percpu_counter_destroy(&tctx->inflight);
8138 kfree(tctx);
8139 return ret;
8140 }
8141
Jens Axboe0f212202020-09-13 13:09:39 -06008142 xa_init(&tctx->xa);
8143 init_waitqueue_head(&tctx->wait);
8144 tctx->last = NULL;
Jens Axboefdaf0832020-10-30 09:37:30 -06008145 atomic_set(&tctx->in_idle, 0);
8146 tctx->sqpoll = false;
Jens Axboe500a3732020-10-15 17:38:03 -06008147 io_init_identity(&tctx->__identity);
8148 tctx->identity = &tctx->__identity;
Jens Axboe0f212202020-09-13 13:09:39 -06008149 task->io_uring = tctx;
Jens Axboe7cbf1722021-02-10 00:03:20 +00008150 spin_lock_init(&tctx->task_lock);
8151 INIT_WQ_LIST(&tctx->task_list);
8152 tctx->task_state = 0;
8153 init_task_work(&tctx->task_work, tctx_task_work);
Jens Axboe0f212202020-09-13 13:09:39 -06008154 return 0;
8155}
8156
8157void __io_uring_free(struct task_struct *tsk)
8158{
8159 struct io_uring_task *tctx = tsk->io_uring;
8160
8161 WARN_ON_ONCE(!xa_empty(&tctx->xa));
Jens Axboe500a3732020-10-15 17:38:03 -06008162 WARN_ON_ONCE(refcount_read(&tctx->identity->count) != 1);
8163 if (tctx->identity != &tctx->__identity)
8164 kfree(tctx->identity);
Jens Axboed8a6df12020-10-15 16:24:45 -06008165 percpu_counter_destroy(&tctx->inflight);
Jens Axboe0f212202020-09-13 13:09:39 -06008166 kfree(tctx);
8167 tsk->io_uring = NULL;
8168}
8169
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008170static int io_sq_offload_create(struct io_ring_ctx *ctx,
8171 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008172{
8173 int ret;
8174
Jens Axboed25e3a32021-02-16 11:41:41 -07008175 /* Retain compatibility with failing for an invalid attach attempt */
8176 if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
8177 IORING_SETUP_ATTACH_WQ) {
8178 struct fd f;
8179
8180 f = fdget(p->wq_fd);
8181 if (!f.file)
8182 return -ENXIO;
8183 if (f.file->f_op != &io_uring_fops) {
8184 fdput(f);
8185 return -EINVAL;
8186 }
8187 fdput(f);
8188 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07008189 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe534ca6d2020-09-02 13:52:19 -06008190 struct io_sq_data *sqd;
8191
Jens Axboe3ec482d2019-04-08 10:51:01 -06008192 ret = -EPERM;
Jens Axboece59fc62020-09-02 13:28:09 -06008193 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_NICE))
Jens Axboe3ec482d2019-04-08 10:51:01 -06008194 goto err;
8195
Jens Axboe534ca6d2020-09-02 13:52:19 -06008196 sqd = io_get_sq_data(p);
8197 if (IS_ERR(sqd)) {
8198 ret = PTR_ERR(sqd);
8199 goto err;
8200 }
Jens Axboe69fb2132020-09-14 11:16:23 -06008201
Jens Axboe534ca6d2020-09-02 13:52:19 -06008202 ctx->sq_data = sqd;
Jens Axboe69fb2132020-09-14 11:16:23 -06008203 io_sq_thread_park(sqd);
8204 mutex_lock(&sqd->ctx_lock);
8205 list_add(&ctx->sqd_list, &sqd->ctx_new_list);
8206 mutex_unlock(&sqd->ctx_lock);
8207 io_sq_thread_unpark(sqd);
Jens Axboe534ca6d2020-09-02 13:52:19 -06008208
Jens Axboe917257d2019-04-13 09:28:55 -06008209 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
8210 if (!ctx->sq_thread_idle)
8211 ctx->sq_thread_idle = HZ;
8212
Jens Axboeaa061652020-09-02 14:50:27 -06008213 if (sqd->thread)
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008214 return 0;
Jens Axboeaa061652020-09-02 14:50:27 -06008215
Jens Axboe6c271ce2019-01-10 11:22:30 -07008216 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06008217 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008218
Jens Axboe917257d2019-04-13 09:28:55 -06008219 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06008220 if (cpu >= nr_cpu_ids)
8221 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08008222 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06008223 goto err;
8224
Jens Axboe69fb2132020-09-14 11:16:23 -06008225 sqd->thread = kthread_create_on_cpu(io_sq_thread, sqd,
Jens Axboe534ca6d2020-09-02 13:52:19 -06008226 cpu, "io_uring-sq");
Jens Axboe6c271ce2019-01-10 11:22:30 -07008227 } else {
Jens Axboe69fb2132020-09-14 11:16:23 -06008228 sqd->thread = kthread_create(io_sq_thread, sqd,
Jens Axboe6c271ce2019-01-10 11:22:30 -07008229 "io_uring-sq");
8230 }
Jens Axboe534ca6d2020-09-02 13:52:19 -06008231 if (IS_ERR(sqd->thread)) {
8232 ret = PTR_ERR(sqd->thread);
8233 sqd->thread = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008234 goto err;
8235 }
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008236 ret = io_uring_alloc_task_context(sqd->thread, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06008237 if (ret)
8238 goto err;
Jens Axboe6c271ce2019-01-10 11:22:30 -07008239 } else if (p->flags & IORING_SETUP_SQ_AFF) {
8240 /* Can't have SQ_AFF without SQPOLL */
8241 ret = -EINVAL;
8242 goto err;
8243 }
8244
Jens Axboe2b188cc2019-01-07 10:46:33 -07008245 return 0;
8246err:
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008247 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008248 return ret;
8249}
8250
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008251static void io_sq_offload_start(struct io_ring_ctx *ctx)
8252{
Jens Axboe534ca6d2020-09-02 13:52:19 -06008253 struct io_sq_data *sqd = ctx->sq_data;
8254
8255 if ((ctx->flags & IORING_SETUP_SQPOLL) && sqd->thread)
8256 wake_up_process(sqd->thread);
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02008257}
8258
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008259static inline void __io_unaccount_mem(struct user_struct *user,
8260 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008261{
8262 atomic_long_sub(nr_pages, &user->locked_vm);
8263}
8264
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008265static inline int __io_account_mem(struct user_struct *user,
8266 unsigned long nr_pages)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008267{
8268 unsigned long page_limit, cur_pages, new_pages;
8269
8270 /* Don't allow more pages than we can safely lock */
8271 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
8272
8273 do {
8274 cur_pages = atomic_long_read(&user->locked_vm);
8275 new_pages = cur_pages + nr_pages;
8276 if (new_pages > page_limit)
8277 return -ENOMEM;
8278 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
8279 new_pages) != cur_pages);
8280
8281 return 0;
8282}
8283
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008284static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008285{
Bijan Mottahedehaad5d8d2020-06-16 16:36:08 -07008286 if (ctx->limit_mem)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008287 __io_unaccount_mem(ctx->user, nr_pages);
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008288
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008289 if (ctx->mm_account)
8290 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008291}
8292
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008293static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008294{
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008295 int ret;
8296
8297 if (ctx->limit_mem) {
8298 ret = __io_account_mem(ctx->user, nr_pages);
8299 if (ret)
8300 return ret;
8301 }
8302
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008303 if (ctx->mm_account)
8304 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
Bijan Mottahedeha087e2b2020-06-16 16:36:07 -07008305
8306 return 0;
8307}
8308
Jens Axboe2b188cc2019-01-07 10:46:33 -07008309static void io_mem_free(void *ptr)
8310{
Mark Rutland52e04ef2019-04-30 17:30:21 +01008311 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008312
Mark Rutland52e04ef2019-04-30 17:30:21 +01008313 if (!ptr)
8314 return;
8315
8316 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008317 if (put_page_testzero(page))
8318 free_compound_page(page);
8319}
8320
8321static void *io_mem_alloc(size_t size)
8322{
8323 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008324 __GFP_NORETRY | __GFP_ACCOUNT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07008325
8326 return (void *) __get_free_pages(gfp_flags, get_order(size));
8327}
8328
Hristo Venev75b28af2019-08-26 17:23:46 +00008329static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
8330 size_t *sq_offset)
8331{
8332 struct io_rings *rings;
8333 size_t off, sq_array_size;
8334
8335 off = struct_size(rings, cqes, cq_entries);
8336 if (off == SIZE_MAX)
8337 return SIZE_MAX;
8338
8339#ifdef CONFIG_SMP
8340 off = ALIGN(off, SMP_CACHE_BYTES);
8341 if (off == 0)
8342 return SIZE_MAX;
8343#endif
8344
Dmitry Vyukovb36200f2020-07-11 11:31:11 +02008345 if (sq_offset)
8346 *sq_offset = off;
8347
Hristo Venev75b28af2019-08-26 17:23:46 +00008348 sq_array_size = array_size(sizeof(u32), sq_entries);
8349 if (sq_array_size == SIZE_MAX)
8350 return SIZE_MAX;
8351
8352 if (check_add_overflow(off, sq_array_size, &off))
8353 return SIZE_MAX;
8354
Hristo Venev75b28af2019-08-26 17:23:46 +00008355 return off;
8356}
8357
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008358static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
Jens Axboeedafcce2019-01-09 09:16:05 -07008359{
8360 int i, j;
8361
8362 if (!ctx->user_bufs)
8363 return -ENXIO;
8364
8365 for (i = 0; i < ctx->nr_user_bufs; i++) {
8366 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8367
8368 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08008369 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07008370
Jens Axboede293932020-09-17 16:19:16 -06008371 if (imu->acct_pages)
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008372 io_unaccount_mem(ctx, imu->acct_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01008373 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07008374 imu->nr_bvecs = 0;
8375 }
8376
8377 kfree(ctx->user_bufs);
8378 ctx->user_bufs = NULL;
8379 ctx->nr_user_bufs = 0;
8380 return 0;
8381}
8382
8383static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
8384 void __user *arg, unsigned index)
8385{
8386 struct iovec __user *src;
8387
8388#ifdef CONFIG_COMPAT
8389 if (ctx->compat) {
8390 struct compat_iovec __user *ciovs;
8391 struct compat_iovec ciov;
8392
8393 ciovs = (struct compat_iovec __user *) arg;
8394 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
8395 return -EFAULT;
8396
Jens Axboed55e5f52019-12-11 16:12:15 -07008397 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07008398 dst->iov_len = ciov.iov_len;
8399 return 0;
8400 }
8401#endif
8402 src = (struct iovec __user *) arg;
8403 if (copy_from_user(dst, &src[index], sizeof(*dst)))
8404 return -EFAULT;
8405 return 0;
8406}
8407
Jens Axboede293932020-09-17 16:19:16 -06008408/*
8409 * Not super efficient, but this is just a registration time. And we do cache
8410 * the last compound head, so generally we'll only do a full search if we don't
8411 * match that one.
8412 *
8413 * We check if the given compound head page has already been accounted, to
8414 * avoid double accounting it. This allows us to account the full size of the
8415 * page, not just the constituent pages of a huge page.
8416 */
8417static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
8418 int nr_pages, struct page *hpage)
8419{
8420 int i, j;
8421
8422 /* check current page array */
8423 for (i = 0; i < nr_pages; i++) {
8424 if (!PageCompound(pages[i]))
8425 continue;
8426 if (compound_head(pages[i]) == hpage)
8427 return true;
8428 }
8429
8430 /* check previously registered pages */
8431 for (i = 0; i < ctx->nr_user_bufs; i++) {
8432 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
8433
8434 for (j = 0; j < imu->nr_bvecs; j++) {
8435 if (!PageCompound(imu->bvec[j].bv_page))
8436 continue;
8437 if (compound_head(imu->bvec[j].bv_page) == hpage)
8438 return true;
8439 }
8440 }
8441
8442 return false;
8443}
8444
8445static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
8446 int nr_pages, struct io_mapped_ubuf *imu,
8447 struct page **last_hpage)
8448{
8449 int i, ret;
8450
8451 for (i = 0; i < nr_pages; i++) {
8452 if (!PageCompound(pages[i])) {
8453 imu->acct_pages++;
8454 } else {
8455 struct page *hpage;
8456
8457 hpage = compound_head(pages[i]);
8458 if (hpage == *last_hpage)
8459 continue;
8460 *last_hpage = hpage;
8461 if (headpage_already_acct(ctx, pages, i, hpage))
8462 continue;
8463 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
8464 }
8465 }
8466
8467 if (!imu->acct_pages)
8468 return 0;
8469
Jens Axboe26bfa89e2021-02-09 20:14:12 -07008470 ret = io_account_mem(ctx, imu->acct_pages);
Jens Axboede293932020-09-17 16:19:16 -06008471 if (ret)
8472 imu->acct_pages = 0;
8473 return ret;
8474}
8475
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008476static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
8477 struct io_mapped_ubuf *imu,
8478 struct page **last_hpage)
Jens Axboeedafcce2019-01-09 09:16:05 -07008479{
8480 struct vm_area_struct **vmas = NULL;
8481 struct page **pages = NULL;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008482 unsigned long off, start, end, ubuf;
8483 size_t size;
8484 int ret, pret, nr_pages, i;
Jens Axboeedafcce2019-01-09 09:16:05 -07008485
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008486 ubuf = (unsigned long) iov->iov_base;
8487 end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
8488 start = ubuf >> PAGE_SHIFT;
8489 nr_pages = end - start;
8490
8491 ret = -ENOMEM;
8492
8493 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
8494 if (!pages)
8495 goto done;
8496
8497 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
8498 GFP_KERNEL);
8499 if (!vmas)
8500 goto done;
8501
8502 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
8503 GFP_KERNEL);
8504 if (!imu->bvec)
8505 goto done;
8506
8507 ret = 0;
8508 mmap_read_lock(current->mm);
8509 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
8510 pages, vmas);
8511 if (pret == nr_pages) {
8512 /* don't support file backed memory */
8513 for (i = 0; i < nr_pages; i++) {
8514 struct vm_area_struct *vma = vmas[i];
8515
8516 if (vma->vm_file &&
8517 !is_file_hugepages(vma->vm_file)) {
8518 ret = -EOPNOTSUPP;
8519 break;
8520 }
8521 }
8522 } else {
8523 ret = pret < 0 ? pret : -EFAULT;
8524 }
8525 mmap_read_unlock(current->mm);
8526 if (ret) {
8527 /*
8528 * if we did partial map, or found file backed vmas,
8529 * release any pages we did get
8530 */
8531 if (pret > 0)
8532 unpin_user_pages(pages, pret);
8533 kvfree(imu->bvec);
8534 goto done;
8535 }
8536
8537 ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage);
8538 if (ret) {
8539 unpin_user_pages(pages, pret);
8540 kvfree(imu->bvec);
8541 goto done;
8542 }
8543
8544 off = ubuf & ~PAGE_MASK;
8545 size = iov->iov_len;
8546 for (i = 0; i < nr_pages; i++) {
8547 size_t vec_len;
8548
8549 vec_len = min_t(size_t, size, PAGE_SIZE - off);
8550 imu->bvec[i].bv_page = pages[i];
8551 imu->bvec[i].bv_len = vec_len;
8552 imu->bvec[i].bv_offset = off;
8553 off = 0;
8554 size -= vec_len;
8555 }
8556 /* store original address for later verification */
8557 imu->ubuf = ubuf;
8558 imu->len = iov->iov_len;
8559 imu->nr_bvecs = nr_pages;
8560 ret = 0;
8561done:
8562 kvfree(pages);
8563 kvfree(vmas);
8564 return ret;
8565}
8566
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008567static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008568{
Jens Axboeedafcce2019-01-09 09:16:05 -07008569 if (ctx->user_bufs)
8570 return -EBUSY;
8571 if (!nr_args || nr_args > UIO_MAXIOV)
8572 return -EINVAL;
8573
8574 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
8575 GFP_KERNEL);
8576 if (!ctx->user_bufs)
8577 return -ENOMEM;
8578
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008579 return 0;
8580}
8581
8582static int io_buffer_validate(struct iovec *iov)
8583{
8584 /*
8585 * Don't impose further limits on the size and buffer
8586 * constraints here, we'll -EINVAL later when IO is
8587 * submitted if they are wrong.
8588 */
8589 if (!iov->iov_base || !iov->iov_len)
8590 return -EFAULT;
8591
8592 /* arbitrary limit, but we need something */
8593 if (iov->iov_len > SZ_1G)
8594 return -EFAULT;
8595
8596 return 0;
8597}
8598
8599static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
8600 unsigned int nr_args)
8601{
8602 int i, ret;
8603 struct iovec iov;
8604 struct page *last_hpage = NULL;
8605
8606 ret = io_buffers_map_alloc(ctx, nr_args);
8607 if (ret)
8608 return ret;
8609
Jens Axboeedafcce2019-01-09 09:16:05 -07008610 for (i = 0; i < nr_args; i++) {
8611 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
Jens Axboeedafcce2019-01-09 09:16:05 -07008612
8613 ret = io_copy_iov(ctx, &iov, arg, i);
8614 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008615 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008616
Bijan Mottahedeh2b358602021-01-06 12:39:11 -08008617 ret = io_buffer_validate(&iov);
8618 if (ret)
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008619 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008620
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008621 ret = io_sqe_buffer_register(ctx, &iov, imu, &last_hpage);
8622 if (ret)
8623 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07008624
8625 ctx->nr_user_bufs++;
8626 }
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008627
8628 if (ret)
8629 io_sqe_buffers_unregister(ctx);
8630
Jens Axboeedafcce2019-01-09 09:16:05 -07008631 return ret;
8632}
8633
Jens Axboe9b402842019-04-11 11:45:41 -06008634static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
8635{
8636 __s32 __user *fds = arg;
8637 int fd;
8638
8639 if (ctx->cq_ev_fd)
8640 return -EBUSY;
8641
8642 if (copy_from_user(&fd, fds, sizeof(*fds)))
8643 return -EFAULT;
8644
8645 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
8646 if (IS_ERR(ctx->cq_ev_fd)) {
8647 int ret = PTR_ERR(ctx->cq_ev_fd);
8648 ctx->cq_ev_fd = NULL;
8649 return ret;
8650 }
8651
8652 return 0;
8653}
8654
8655static int io_eventfd_unregister(struct io_ring_ctx *ctx)
8656{
8657 if (ctx->cq_ev_fd) {
8658 eventfd_ctx_put(ctx->cq_ev_fd);
8659 ctx->cq_ev_fd = NULL;
8660 return 0;
8661 }
8662
8663 return -ENXIO;
8664}
8665
Jens Axboe5a2e7452020-02-23 16:23:11 -07008666static int __io_destroy_buffers(int id, void *p, void *data)
8667{
8668 struct io_ring_ctx *ctx = data;
8669 struct io_buffer *buf = p;
8670
Jens Axboe067524e2020-03-02 16:32:28 -07008671 __io_remove_buffers(ctx, buf, id, -1U);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008672 return 0;
8673}
8674
8675static void io_destroy_buffers(struct io_ring_ctx *ctx)
8676{
8677 idr_for_each(&ctx->io_buffer_idr, __io_destroy_buffers, ctx);
8678 idr_destroy(&ctx->io_buffer_idr);
8679}
8680
Jens Axboe68e68ee2021-02-13 09:00:02 -07008681static void io_req_cache_free(struct list_head *list, struct task_struct *tsk)
Jens Axboe1b4c3512021-02-10 00:03:19 +00008682{
Jens Axboe68e68ee2021-02-13 09:00:02 -07008683 struct io_kiocb *req, *nxt;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008684
Jens Axboe68e68ee2021-02-13 09:00:02 -07008685 list_for_each_entry_safe(req, nxt, list, compl.list) {
8686 if (tsk && req->task != tsk)
8687 continue;
Jens Axboe1b4c3512021-02-10 00:03:19 +00008688 list_del(&req->compl.list);
8689 kmem_cache_free(req_cachep, req);
8690 }
8691}
8692
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008693static void io_req_caches_free(struct io_ring_ctx *ctx, struct task_struct *tsk)
Jens Axboe2b188cc2019-01-07 10:46:33 -07008694{
Pavel Begunkovbf019da2021-02-10 00:03:17 +00008695 struct io_submit_state *submit_state = &ctx->submit_state;
8696
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008697 mutex_lock(&ctx->uring_lock);
8698
8699 if (submit_state->free_reqs)
8700 kmem_cache_free_bulk(req_cachep, submit_state->free_reqs,
8701 submit_state->reqs);
8702
8703 io_req_cache_free(&submit_state->comp.free_list, NULL);
8704
8705 spin_lock_irq(&ctx->completion_lock);
8706 io_req_cache_free(&submit_state->comp.locked_free_list, NULL);
8707 spin_unlock_irq(&ctx->completion_lock);
8708
8709 mutex_unlock(&ctx->uring_lock);
8710}
8711
Jens Axboe2b188cc2019-01-07 10:46:33 -07008712static void io_ring_ctx_free(struct io_ring_ctx *ctx)
8713{
Pavel Begunkov04fc6c82021-02-12 03:23:54 +00008714 /*
8715 * Some may use context even when all refs and requests have been put,
8716 * and they are free to do so while still holding uring_lock, see
8717 * __io_req_task_submit(). Wait for them to finish.
8718 */
8719 mutex_lock(&ctx->uring_lock);
8720 mutex_unlock(&ctx->uring_lock);
8721
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008722 io_sq_thread_stop(ctx);
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -08008723 io_sqe_buffers_unregister(ctx);
Jens Axboe2aede0e2020-09-14 10:45:53 -06008724
8725 if (ctx->sqo_task) {
8726 put_task_struct(ctx->sqo_task);
8727 ctx->sqo_task = NULL;
8728 mmdrop(ctx->mm_account);
8729 ctx->mm_account = NULL;
Bijan Mottahedeh30975822020-06-16 16:36:09 -07008730 }
Jens Axboedef596e2019-01-09 08:59:42 -07008731
Dennis Zhou91d8f512020-09-16 13:41:05 -07008732#ifdef CONFIG_BLK_CGROUP
8733 if (ctx->sqo_blkcg_css)
8734 css_put(ctx->sqo_blkcg_css);
8735#endif
8736
Hao Xu8bad28d2021-02-19 17:19:36 +08008737 mutex_lock(&ctx->uring_lock);
Jens Axboe6b063142019-01-10 22:13:58 -07008738 io_sqe_files_unregister(ctx);
Hao Xu8bad28d2021-02-19 17:19:36 +08008739 mutex_unlock(&ctx->uring_lock);
Jens Axboe9b402842019-04-11 11:45:41 -06008740 io_eventfd_unregister(ctx);
Jens Axboe5a2e7452020-02-23 16:23:11 -07008741 io_destroy_buffers(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07008742 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07008743
Jens Axboe2b188cc2019-01-07 10:46:33 -07008744#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07008745 if (ctx->ring_sock) {
8746 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008747 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07008748 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07008749#endif
8750
Hristo Venev75b28af2019-08-26 17:23:46 +00008751 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008752 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008753
8754 percpu_ref_exit(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008755 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07008756 put_cred(ctx->creds);
Jens Axboe9a4fdbd2021-02-13 09:09:44 -07008757 io_req_caches_free(ctx, NULL);
Jens Axboe78076bb2019-12-04 19:56:40 -07008758 kfree(ctx->cancel_hash);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008759 kfree(ctx);
8760}
8761
8762static __poll_t io_uring_poll(struct file *file, poll_table *wait)
8763{
8764 struct io_ring_ctx *ctx = file->private_data;
8765 __poll_t mask = 0;
8766
8767 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02008768 /*
8769 * synchronizes with barrier from wq_has_sleeper call in
8770 * io_commit_cqring
8771 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07008772 smp_rmb();
Jens Axboe90554202020-09-03 12:12:41 -06008773 if (!io_sqring_full(ctx))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008774 mask |= EPOLLOUT | EPOLLWRNORM;
Hao Xued670c32021-02-05 16:34:21 +08008775
8776 /*
8777 * Don't flush cqring overflow list here, just do a simple check.
8778 * Otherwise there could possible be ABBA deadlock:
8779 * CPU0 CPU1
8780 * ---- ----
8781 * lock(&ctx->uring_lock);
8782 * lock(&ep->mtx);
8783 * lock(&ctx->uring_lock);
8784 * lock(&ep->mtx);
8785 *
8786 * Users may get EPOLLIN meanwhile seeing nothing in cqring, this
8787 * pushs them to do the flush.
8788 */
8789 if (io_cqring_events(ctx) || test_bit(0, &ctx->cq_check_overflow))
Jens Axboe2b188cc2019-01-07 10:46:33 -07008790 mask |= EPOLLIN | EPOLLRDNORM;
8791
8792 return mask;
8793}
8794
8795static int io_uring_fasync(int fd, struct file *file, int on)
8796{
8797 struct io_ring_ctx *ctx = file->private_data;
8798
8799 return fasync_helper(fd, file, on, &ctx->cq_fasync);
8800}
8801
Yejune Deng0bead8c2020-12-24 11:02:20 +08008802static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
Jens Axboe071698e2020-01-28 10:04:42 -07008803{
Jens Axboe1e6fa522020-10-15 08:46:24 -06008804 struct io_identity *iod;
Jens Axboe071698e2020-01-28 10:04:42 -07008805
Jens Axboe1e6fa522020-10-15 08:46:24 -06008806 iod = idr_remove(&ctx->personality_idr, id);
8807 if (iod) {
8808 put_cred(iod->creds);
8809 if (refcount_dec_and_test(&iod->count))
8810 kfree(iod);
Yejune Deng0bead8c2020-12-24 11:02:20 +08008811 return 0;
Jens Axboe1e6fa522020-10-15 08:46:24 -06008812 }
Yejune Deng0bead8c2020-12-24 11:02:20 +08008813
8814 return -EINVAL;
8815}
8816
8817static int io_remove_personalities(int id, void *p, void *data)
8818{
8819 struct io_ring_ctx *ctx = data;
8820
8821 io_unregister_personality(ctx, id);
Jens Axboe071698e2020-01-28 10:04:42 -07008822 return 0;
8823}
8824
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008825static void io_run_ctx_fallback(struct io_ring_ctx *ctx)
8826{
8827 struct callback_head *work, *head, *next;
8828
8829 do {
8830 do {
8831 head = NULL;
8832 work = READ_ONCE(ctx->exit_task_work);
8833 } while (cmpxchg(&ctx->exit_task_work, work, head) != work);
8834
8835 if (!work)
8836 break;
8837
8838 do {
8839 next = work->next;
8840 work->func(work);
8841 work = next;
8842 cond_resched();
8843 } while (work);
8844 } while (1);
8845}
8846
Jens Axboe85faa7b2020-04-09 18:14:00 -06008847static void io_ring_exit_work(struct work_struct *work)
8848{
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008849 struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx,
8850 exit_work);
Jens Axboe85faa7b2020-04-09 18:14:00 -06008851
Jens Axboe56952e92020-06-17 15:00:04 -06008852 /*
8853 * If we're doing polled IO and end up having requests being
8854 * submitted async (out-of-line), then completions can come in while
8855 * we're waiting for refs to drop. We need to reap these manually,
8856 * as nobody else will be looking for them.
8857 */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008858 do {
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008859 io_uring_try_cancel_requests(ctx, NULL, NULL);
Jens Axboe7c25c0d2021-02-16 07:17:00 -07008860 io_run_ctx_fallback(ctx);
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008861 } while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
Jens Axboe85faa7b2020-04-09 18:14:00 -06008862 io_ring_ctx_free(ctx);
8863}
8864
Jens Axboe2b188cc2019-01-07 10:46:33 -07008865static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
8866{
8867 mutex_lock(&ctx->uring_lock);
8868 percpu_ref_kill(&ctx->refs);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00008869
8870 if (WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) && !ctx->sqo_dead))
8871 ctx->sqo_dead = 1;
8872
Pavel Begunkovcda286f2020-12-17 00:24:35 +00008873 /* if force is set, the ring is going away. always drop after that */
8874 ctx->cq_overflow_flushed = 1;
Pavel Begunkov634578f2020-12-06 22:22:44 +00008875 if (ctx->rings)
Pavel Begunkov6c503152021-01-04 20:36:36 +00008876 __io_cqring_overflow_flush(ctx, true, NULL, NULL);
Pavel Begunkov5c766a92021-01-19 13:32:36 +00008877 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008878 mutex_unlock(&ctx->uring_lock);
8879
Pavel Begunkov6b819282020-11-06 13:00:25 +00008880 io_kill_timeouts(ctx, NULL, NULL);
8881 io_poll_remove_all(ctx, NULL, NULL);
Jens Axboe561fb042019-10-24 07:25:42 -06008882
Jens Axboe15dff282019-11-13 09:09:23 -07008883 /* if we failed setting up the ctx, we might not have any rings */
Pavel Begunkovb2edc0a2020-07-07 16:36:22 +03008884 io_iopoll_try_reap_events(ctx);
Jens Axboe309fc032020-07-10 09:13:34 -06008885
Jens Axboe85faa7b2020-04-09 18:14:00 -06008886 INIT_WORK(&ctx->exit_work, io_ring_exit_work);
Jens Axboefc666772020-08-19 11:10:51 -06008887 /*
8888 * Use system_unbound_wq to avoid spawning tons of event kworkers
8889 * if we're exiting a ton of rings at the same time. It just adds
8890 * noise and overhead, there's no discernable change in runtime
8891 * over using system_wq.
8892 */
8893 queue_work(system_unbound_wq, &ctx->exit_work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07008894}
8895
8896static int io_uring_release(struct inode *inode, struct file *file)
8897{
8898 struct io_ring_ctx *ctx = file->private_data;
8899
8900 file->private_data = NULL;
8901 io_ring_ctx_wait_and_kill(ctx);
8902 return 0;
8903}
8904
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008905struct io_task_cancel {
8906 struct task_struct *task;
8907 struct files_struct *files;
8908};
Pavel Begunkov67c4d9e2020-06-15 10:24:05 +03008909
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008910static bool io_cancel_task_cb(struct io_wq_work *work, void *data)
Jens Axboeb711d4e2020-08-16 08:23:05 -07008911{
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008912 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008913 struct io_task_cancel *cancel = data;
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008914 bool ret;
8915
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008916 if (cancel->files && (req->flags & REQ_F_LINK_TIMEOUT)) {
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008917 unsigned long flags;
8918 struct io_ring_ctx *ctx = req->ctx;
8919
8920 /* protect against races with linked timeouts */
8921 spin_lock_irqsave(&ctx->completion_lock, flags);
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008922 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008923 spin_unlock_irqrestore(&ctx->completion_lock, flags);
8924 } else {
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00008925 ret = io_match_task(req, cancel->task, cancel->files);
Pavel Begunkov9a472ef2020-11-05 22:31:37 +00008926 }
8927 return ret;
Jens Axboeb711d4e2020-08-16 08:23:05 -07008928}
8929
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008930static void io_cancel_defer_files(struct io_ring_ctx *ctx,
Pavel Begunkovef9865a2020-11-05 14:06:19 +00008931 struct task_struct *task,
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008932 struct files_struct *files)
8933{
8934 struct io_defer_entry *de = NULL;
8935 LIST_HEAD(list);
8936
8937 spin_lock_irq(&ctx->completion_lock);
8938 list_for_each_entry_reverse(de, &ctx->defer_list, list) {
Pavel Begunkov08d23632020-11-06 13:00:22 +00008939 if (io_match_task(de->req, task, files)) {
Pavel Begunkovb7ddce32020-09-06 00:45:14 +03008940 list_cut_position(&list, &ctx->defer_list, &de->list);
8941 break;
8942 }
8943 }
8944 spin_unlock_irq(&ctx->completion_lock);
8945
8946 while (!list_empty(&list)) {
8947 de = list_first_entry(&list, struct io_defer_entry, list);
8948 list_del_init(&de->list);
8949 req_set_fail_links(de->req);
8950 io_put_req(de->req);
8951 io_req_complete(de->req, -ECANCELED);
8952 kfree(de);
8953 }
8954}
8955
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008956static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx,
8957 struct task_struct *task,
8958 struct files_struct *files)
8959{
8960 struct io_task_cancel cancel = { .task = task, .files = files, };
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008961 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008962
8963 while (1) {
8964 enum io_wq_cancel cret;
8965 bool ret = false;
8966
Jens Axboe5aa75ed2021-02-16 12:56:50 -07008967 if (tctx && tctx->io_wq) {
8968 cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb,
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00008969 &cancel, true);
8970 ret |= (cret != IO_WQ_CANCEL_NOTFOUND);
8971 }
8972
8973 /* SQPOLL thread does its own polling */
8974 if (!(ctx->flags & IORING_SETUP_SQPOLL) && !files) {
8975 while (!list_empty_careful(&ctx->iopoll_list)) {
8976 io_iopoll_try_reap_events(ctx);
8977 ret = true;
8978 }
8979 }
8980
8981 ret |= io_poll_remove_all(ctx, task, files);
8982 ret |= io_kill_timeouts(ctx, task, files);
8983 ret |= io_run_task_work();
8984 io_cqring_overflow_flush(ctx, true, task, files);
8985 if (!ret)
8986 break;
8987 cond_resched();
8988 }
8989}
8990
Pavel Begunkovca70f002021-01-26 15:28:27 +00008991static int io_uring_count_inflight(struct io_ring_ctx *ctx,
8992 struct task_struct *task,
8993 struct files_struct *files)
8994{
8995 struct io_kiocb *req;
8996 int cnt = 0;
8997
8998 spin_lock_irq(&ctx->inflight_lock);
8999 list_for_each_entry(req, &ctx->inflight_list, inflight_entry)
9000 cnt += io_match_task(req, task, files);
9001 spin_unlock_irq(&ctx->inflight_lock);
9002 return cnt;
9003}
9004
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009005static void io_uring_cancel_files(struct io_ring_ctx *ctx,
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009006 struct task_struct *task,
Jens Axboefcb323c2019-10-24 12:39:47 -06009007 struct files_struct *files)
9008{
Jens Axboefcb323c2019-10-24 12:39:47 -06009009 while (!list_empty_careful(&ctx->inflight_list)) {
Xiaoguang Wangd8f1b972020-04-26 15:54:43 +08009010 DEFINE_WAIT(wait);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009011 int inflight;
Jens Axboefcb323c2019-10-24 12:39:47 -06009012
Pavel Begunkovca70f002021-01-26 15:28:27 +00009013 inflight = io_uring_count_inflight(ctx, task, files);
9014 if (!inflight)
Jens Axboefcb323c2019-10-24 12:39:47 -06009015 break;
Pavel Begunkovf6edbab2020-11-06 13:00:26 +00009016
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009017 io_uring_try_cancel_requests(ctx, task, files);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009018
Pavel Begunkov34343782021-02-10 11:45:42 +00009019 if (ctx->sq_data)
9020 io_sq_thread_unpark(ctx->sq_data);
Pavel Begunkovca70f002021-01-26 15:28:27 +00009021 prepare_to_wait(&task->io_uring->wait, &wait,
9022 TASK_UNINTERRUPTIBLE);
9023 if (inflight == io_uring_count_inflight(ctx, task, files))
9024 schedule();
Pavel Begunkovc98de082020-11-15 12:56:32 +00009025 finish_wait(&task->io_uring->wait, &wait);
Pavel Begunkov34343782021-02-10 11:45:42 +00009026 if (ctx->sq_data)
9027 io_sq_thread_park(ctx->sq_data);
Jens Axboe0f212202020-09-13 13:09:39 -06009028 }
Jens Axboe0f212202020-09-13 13:09:39 -06009029}
9030
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009031static void io_disable_sqo_submit(struct io_ring_ctx *ctx)
9032{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009033 mutex_lock(&ctx->uring_lock);
9034 ctx->sqo_dead = 1;
9035 mutex_unlock(&ctx->uring_lock);
9036
9037 /* make sure callers enter the ring to get error */
Pavel Begunkovb4411612021-01-13 12:42:24 +00009038 if (ctx->rings)
9039 io_ring_set_wakeup_flag(ctx);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009040}
9041
Jens Axboe0f212202020-09-13 13:09:39 -06009042/*
9043 * We need to iteratively cancel requests, in case a request has dependent
9044 * hard links. These persist even for failure of cancelations, hence keep
9045 * looping until none are found.
9046 */
9047static void io_uring_cancel_task_requests(struct io_ring_ctx *ctx,
9048 struct files_struct *files)
9049{
9050 struct task_struct *task = current;
9051
Jens Axboefdaf0832020-10-30 09:37:30 -06009052 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009053 io_disable_sqo_submit(ctx);
Jens Axboe534ca6d2020-09-02 13:52:19 -06009054 task = ctx->sq_data->thread;
Jens Axboefdaf0832020-10-30 09:37:30 -06009055 atomic_inc(&task->io_uring->in_idle);
9056 io_sq_thread_park(ctx->sq_data);
9057 }
Jens Axboe0f212202020-09-13 13:09:39 -06009058
Pavel Begunkovdf9923f2020-11-06 13:00:23 +00009059 io_cancel_defer_files(ctx, task, files);
Jens Axboe0f212202020-09-13 13:09:39 -06009060
Pavel Begunkov3a7efd12021-01-28 23:23:42 +00009061 io_uring_cancel_files(ctx, task, files);
Pavel Begunkovb52fda02020-11-06 13:00:24 +00009062 if (!files)
Pavel Begunkov9936c7c2021-02-04 13:51:56 +00009063 io_uring_try_cancel_requests(ctx, task, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009064
9065 if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sq_data) {
9066 atomic_dec(&task->io_uring->in_idle);
Jens Axboefdaf0832020-10-30 09:37:30 -06009067 io_sq_thread_unpark(ctx->sq_data);
9068 }
Jens Axboe0f212202020-09-13 13:09:39 -06009069}
9070
9071/*
9072 * Note that this task has used io_uring. We use it for cancelation purposes.
9073 */
Jens Axboefdaf0832020-10-30 09:37:30 -06009074static int io_uring_add_task_file(struct io_ring_ctx *ctx, struct file *file)
Jens Axboe0f212202020-09-13 13:09:39 -06009075{
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009076 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkova528b042020-12-21 18:34:04 +00009077 int ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009078
9079 if (unlikely(!tctx)) {
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009080 ret = io_uring_alloc_task_context(current, ctx);
Jens Axboe0f212202020-09-13 13:09:39 -06009081 if (unlikely(ret))
9082 return ret;
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009083 tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009084 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009085 if (tctx->last != file) {
9086 void *old = xa_load(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009087
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009088 if (!old) {
Jens Axboe0f212202020-09-13 13:09:39 -06009089 get_file(file);
Pavel Begunkova528b042020-12-21 18:34:04 +00009090 ret = xa_err(xa_store(&tctx->xa, (unsigned long)file,
9091 file, GFP_KERNEL));
9092 if (ret) {
9093 fput(file);
9094 return ret;
9095 }
Pavel Begunkovecfc8492021-01-25 11:42:20 +00009096
9097 /* one and only SQPOLL file note, held by sqo_task */
9098 WARN_ON_ONCE((ctx->flags & IORING_SETUP_SQPOLL) &&
9099 current != ctx->sqo_task);
Jens Axboe0f212202020-09-13 13:09:39 -06009100 }
Matthew Wilcox (Oracle)236434c2020-10-09 13:49:52 +01009101 tctx->last = file;
Jens Axboe0f212202020-09-13 13:09:39 -06009102 }
9103
Jens Axboefdaf0832020-10-30 09:37:30 -06009104 /*
9105 * This is race safe in that the task itself is doing this, hence it
9106 * cannot be going through the exit/cancel paths at the same time.
9107 * This cannot be modified while exit/cancel is running.
9108 */
9109 if (!tctx->sqpoll && (ctx->flags & IORING_SETUP_SQPOLL))
9110 tctx->sqpoll = true;
9111
Jens Axboe0f212202020-09-13 13:09:39 -06009112 return 0;
9113}
9114
9115/*
9116 * Remove this io_uring_file -> task mapping.
9117 */
9118static void io_uring_del_task_file(struct file *file)
9119{
9120 struct io_uring_task *tctx = current->io_uring;
Jens Axboe0f212202020-09-13 13:09:39 -06009121
9122 if (tctx->last == file)
9123 tctx->last = NULL;
Matthew Wilcox (Oracle)5e2ed8c2020-10-09 13:49:53 +01009124 file = xa_erase(&tctx->xa, (unsigned long)file);
Jens Axboe0f212202020-09-13 13:09:39 -06009125 if (file)
9126 fput(file);
9127}
9128
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009129static void io_uring_remove_task_files(struct io_uring_task *tctx)
9130{
9131 struct file *file;
9132 unsigned long index;
9133
9134 xa_for_each(&tctx->xa, index, file)
9135 io_uring_del_task_file(file);
9136}
9137
Jens Axboe0f212202020-09-13 13:09:39 -06009138void __io_uring_files_cancel(struct files_struct *files)
9139{
9140 struct io_uring_task *tctx = current->io_uring;
Matthew Wilcox (Oracle)ce765372020-10-09 13:49:51 +01009141 struct file *file;
9142 unsigned long index;
Jens Axboe0f212202020-09-13 13:09:39 -06009143
9144 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009145 atomic_inc(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009146 xa_for_each(&tctx->xa, index, file)
9147 io_uring_cancel_task_requests(file->private_data, files);
Jens Axboefdaf0832020-10-30 09:37:30 -06009148 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009149
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009150 if (files) {
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009151 io_uring_remove_task_files(tctx);
Jens Axboe5aa75ed2021-02-16 12:56:50 -07009152 } else if (tctx->io_wq && current->flags & PF_EXITING) {
9153 io_wq_destroy(tctx->io_wq);
9154 tctx->io_wq = NULL;
9155 }
Jens Axboefdaf0832020-10-30 09:37:30 -06009156}
9157
9158static s64 tctx_inflight(struct io_uring_task *tctx)
9159{
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009160 return percpu_counter_sum(&tctx->inflight);
9161}
9162
9163static void io_uring_cancel_sqpoll(struct io_ring_ctx *ctx)
9164{
9165 struct io_uring_task *tctx;
Jens Axboefdaf0832020-10-30 09:37:30 -06009166 s64 inflight;
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009167 DEFINE_WAIT(wait);
Jens Axboefdaf0832020-10-30 09:37:30 -06009168
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009169 if (!ctx->sq_data)
9170 return;
9171 tctx = ctx->sq_data->thread->io_uring;
9172 io_disable_sqo_submit(ctx);
Jens Axboefdaf0832020-10-30 09:37:30 -06009173
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009174 atomic_inc(&tctx->in_idle);
9175 do {
9176 /* read completions before cancelations */
9177 inflight = tctx_inflight(tctx);
9178 if (!inflight)
9179 break;
9180 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboefdaf0832020-10-30 09:37:30 -06009181
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009182 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9183 /*
9184 * If we've seen completions, retry without waiting. This
9185 * avoids a race where a completion comes in before we did
9186 * prepare_to_wait().
9187 */
9188 if (inflight == tctx_inflight(tctx))
9189 schedule();
9190 finish_wait(&tctx->wait, &wait);
9191 } while (1);
9192 atomic_dec(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009193}
9194
Jens Axboe0f212202020-09-13 13:09:39 -06009195/*
9196 * Find any io_uring fd that this task has registered or done IO on, and cancel
9197 * requests.
9198 */
9199void __io_uring_task_cancel(void)
9200{
9201 struct io_uring_task *tctx = current->io_uring;
9202 DEFINE_WAIT(wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009203 s64 inflight;
Jens Axboe0f212202020-09-13 13:09:39 -06009204
9205 /* make sure overflow events are dropped */
Jens Axboefdaf0832020-10-30 09:37:30 -06009206 atomic_inc(&tctx->in_idle);
Jens Axboe0f212202020-09-13 13:09:39 -06009207
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009208 /* trigger io_disable_sqo_submit() */
Pavel Begunkov0e9ddb32021-02-07 22:34:26 +00009209 if (tctx->sqpoll) {
9210 struct file *file;
9211 unsigned long index;
9212
9213 xa_for_each(&tctx->xa, index, file)
9214 io_uring_cancel_sqpoll(file->private_data);
9215 }
Pavel Begunkov0b5cd6c2021-01-17 02:29:56 +00009216
Jens Axboed8a6df12020-10-15 16:24:45 -06009217 do {
Jens Axboe0f212202020-09-13 13:09:39 -06009218 /* read completions before cancelations */
Jens Axboefdaf0832020-10-30 09:37:30 -06009219 inflight = tctx_inflight(tctx);
Jens Axboed8a6df12020-10-15 16:24:45 -06009220 if (!inflight)
9221 break;
Jens Axboe0f212202020-09-13 13:09:39 -06009222 __io_uring_files_cancel(NULL);
9223
9224 prepare_to_wait(&tctx->wait, &wait, TASK_UNINTERRUPTIBLE);
9225
9226 /*
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009227 * If we've seen completions, retry without waiting. This
9228 * avoids a race where a completion comes in before we did
9229 * prepare_to_wait().
Jens Axboe0f212202020-09-13 13:09:39 -06009230 */
Pavel Begunkova1bb3cd2021-01-26 15:28:26 +00009231 if (inflight == tctx_inflight(tctx))
9232 schedule();
Pavel Begunkovf57555e2020-12-20 13:21:44 +00009233 finish_wait(&tctx->wait, &wait);
Jens Axboed8a6df12020-10-15 16:24:45 -06009234 } while (1);
Jens Axboe0f212202020-09-13 13:09:39 -06009235
Jens Axboefdaf0832020-10-30 09:37:30 -06009236 atomic_dec(&tctx->in_idle);
Pavel Begunkovde7f1d92021-01-04 20:43:29 +00009237
9238 io_uring_remove_task_files(tctx);
Pavel Begunkov44e728b2020-06-15 10:24:04 +03009239}
9240
Jens Axboefcb323c2019-10-24 12:39:47 -06009241static int io_uring_flush(struct file *file, void *data)
9242{
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009243 struct io_uring_task *tctx = current->io_uring;
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009244 struct io_ring_ctx *ctx = file->private_data;
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009245
Jens Axboe3bfe6102021-02-16 14:15:30 -07009246 /* Ignore helper thread files exit */
9247 if (current->flags & PF_IO_WORKER)
9248 return 0;
9249
Jens Axboe41be53e2021-02-13 09:11:04 -07009250 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
Jens Axboe84965ff2021-01-23 15:51:11 -07009251 io_uring_cancel_task_requests(ctx, NULL);
Jens Axboe41be53e2021-02-13 09:11:04 -07009252 io_req_caches_free(ctx, current);
9253 }
Jens Axboe84965ff2021-01-23 15:51:11 -07009254
Jens Axboe7c25c0d2021-02-16 07:17:00 -07009255 io_run_ctx_fallback(ctx);
9256
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009257 if (!tctx)
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009258 return 0;
9259
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009260 /* we should have cancelled and erased it before PF_EXITING */
9261 WARN_ON_ONCE((current->flags & PF_EXITING) &&
9262 xa_load(&tctx->xa, (unsigned long)file));
9263
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009264 /*
9265 * fput() is pending, will be 2 if the only other ref is our potential
9266 * task file note. If the task is exiting, drop regardless of count.
9267 */
Pavel Begunkov6b5733e2021-01-08 20:57:24 +00009268 if (atomic_long_read(&file->f_count) != 2)
9269 return 0;
Pavel Begunkov4f793dc2021-01-08 20:57:23 +00009270
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009271 if (ctx->flags & IORING_SETUP_SQPOLL) {
9272 /* there is only one file note, which is owned by sqo_task */
Pavel Begunkov4325cb42021-01-16 05:32:30 +00009273 WARN_ON_ONCE(ctx->sqo_task != current &&
9274 xa_load(&tctx->xa, (unsigned long)file));
9275 /* sqo_dead check is for when this happens after cancellation */
9276 WARN_ON_ONCE(ctx->sqo_task == current && !ctx->sqo_dead &&
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009277 !xa_load(&tctx->xa, (unsigned long)file));
9278
9279 io_disable_sqo_submit(ctx);
9280 }
9281
9282 if (!(ctx->flags & IORING_SETUP_SQPOLL) || ctx->sqo_task == current)
9283 io_uring_del_task_file(file);
Jens Axboefcb323c2019-10-24 12:39:47 -06009284 return 0;
9285}
9286
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009287static void *io_uring_validate_mmap_request(struct file *file,
9288 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009289{
Jens Axboe2b188cc2019-01-07 10:46:33 -07009290 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009291 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009292 struct page *page;
9293 void *ptr;
9294
9295 switch (offset) {
9296 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00009297 case IORING_OFF_CQ_RING:
9298 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009299 break;
9300 case IORING_OFF_SQES:
9301 ptr = ctx->sq_sqes;
9302 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009303 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009304 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009305 }
9306
9307 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07009308 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009309 return ERR_PTR(-EINVAL);
9310
9311 return ptr;
9312}
9313
9314#ifdef CONFIG_MMU
9315
9316static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9317{
9318 size_t sz = vma->vm_end - vma->vm_start;
9319 unsigned long pfn;
9320 void *ptr;
9321
9322 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
9323 if (IS_ERR(ptr))
9324 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009325
9326 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
9327 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
9328}
9329
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009330#else /* !CONFIG_MMU */
9331
9332static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
9333{
9334 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
9335}
9336
9337static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
9338{
9339 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
9340}
9341
9342static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
9343 unsigned long addr, unsigned long len,
9344 unsigned long pgoff, unsigned long flags)
9345{
9346 void *ptr;
9347
9348 ptr = io_uring_validate_mmap_request(file, pgoff, len);
9349 if (IS_ERR(ptr))
9350 return PTR_ERR(ptr);
9351
9352 return (unsigned long) ptr;
9353}
9354
9355#endif /* !CONFIG_MMU */
9356
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009357static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
Jens Axboe90554202020-09-03 12:12:41 -06009358{
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009359 int ret = 0;
Jens Axboe90554202020-09-03 12:12:41 -06009360 DEFINE_WAIT(wait);
9361
9362 do {
9363 if (!io_sqring_full(ctx))
9364 break;
9365
9366 prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
9367
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009368 if (unlikely(ctx->sqo_dead)) {
9369 ret = -EOWNERDEAD;
9370 goto out;
9371 }
9372
Jens Axboe90554202020-09-03 12:12:41 -06009373 if (!io_sqring_full(ctx))
9374 break;
9375
9376 schedule();
9377 } while (!signal_pending(current));
9378
9379 finish_wait(&ctx->sqo_sq_wait, &wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009380out:
9381 return ret;
Jens Axboe90554202020-09-03 12:12:41 -06009382}
9383
Hao Xuc73ebb62020-11-03 10:54:37 +08009384static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz,
9385 struct __kernel_timespec __user **ts,
9386 const sigset_t __user **sig)
9387{
9388 struct io_uring_getevents_arg arg;
9389
9390 /*
9391 * If EXT_ARG isn't set, then we have no timespec and the argp pointer
9392 * is just a pointer to the sigset_t.
9393 */
9394 if (!(flags & IORING_ENTER_EXT_ARG)) {
9395 *sig = (const sigset_t __user *) argp;
9396 *ts = NULL;
9397 return 0;
9398 }
9399
9400 /*
9401 * EXT_ARG is set - ensure we agree on the size of it and copy in our
9402 * timespec and sigset_t pointers if good.
9403 */
9404 if (*argsz != sizeof(arg))
9405 return -EINVAL;
9406 if (copy_from_user(&arg, argp, sizeof(arg)))
9407 return -EFAULT;
9408 *sig = u64_to_user_ptr(arg.sigmask);
9409 *argsz = arg.sigmask_sz;
9410 *ts = u64_to_user_ptr(arg.ts);
9411 return 0;
9412}
9413
Jens Axboe2b188cc2019-01-07 10:46:33 -07009414SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
Hao Xuc73ebb62020-11-03 10:54:37 +08009415 u32, min_complete, u32, flags, const void __user *, argp,
9416 size_t, argsz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009417{
9418 struct io_ring_ctx *ctx;
9419 long ret = -EBADF;
9420 int submitted = 0;
9421 struct fd f;
9422
Jens Axboe4c6e2772020-07-01 11:29:10 -06009423 io_run_task_work();
Jens Axboeb41e9852020-02-17 09:52:41 -07009424
Jens Axboe90554202020-09-03 12:12:41 -06009425 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP |
Hao Xuc73ebb62020-11-03 10:54:37 +08009426 IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009427 return -EINVAL;
9428
9429 f = fdget(fd);
9430 if (!f.file)
9431 return -EBADF;
9432
9433 ret = -EOPNOTSUPP;
9434 if (f.file->f_op != &io_uring_fops)
9435 goto out_fput;
9436
9437 ret = -ENXIO;
9438 ctx = f.file->private_data;
9439 if (!percpu_ref_tryget(&ctx->refs))
9440 goto out_fput;
9441
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009442 ret = -EBADFD;
9443 if (ctx->flags & IORING_SETUP_R_DISABLED)
9444 goto out;
9445
Jens Axboe6c271ce2019-01-10 11:22:30 -07009446 /*
9447 * For SQ polling, the thread will do all submissions and completions.
9448 * Just return the requested submit count, and wake the thread if
9449 * we were asked to.
9450 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009451 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009452 if (ctx->flags & IORING_SETUP_SQPOLL) {
Pavel Begunkov6c503152021-01-04 20:36:36 +00009453 io_cqring_overflow_flush(ctx, false, NULL, NULL);
Pavel Begunkov89448c42020-12-17 00:24:39 +00009454
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009455 ret = -EOWNERDEAD;
9456 if (unlikely(ctx->sqo_dead))
9457 goto out;
Jens Axboe6c271ce2019-01-10 11:22:30 -07009458 if (flags & IORING_ENTER_SQ_WAKEUP)
Jens Axboe534ca6d2020-09-02 13:52:19 -06009459 wake_up(&ctx->sq_data->wait);
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009460 if (flags & IORING_ENTER_SQ_WAIT) {
9461 ret = io_sqpoll_wait_sq(ctx);
9462 if (ret)
9463 goto out;
9464 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07009465 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06009466 } else if (to_submit) {
Jens Axboefdaf0832020-10-30 09:37:30 -06009467 ret = io_uring_add_task_file(ctx, f.file);
Jens Axboe0f212202020-09-13 13:09:39 -06009468 if (unlikely(ret))
9469 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009470 mutex_lock(&ctx->uring_lock);
Jens Axboe0f212202020-09-13 13:09:39 -06009471 submitted = io_submit_sqes(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009472 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009473
9474 if (submitted != to_submit)
9475 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009476 }
9477 if (flags & IORING_ENTER_GETEVENTS) {
Hao Xuc73ebb62020-11-03 10:54:37 +08009478 const sigset_t __user *sig;
9479 struct __kernel_timespec __user *ts;
9480
9481 ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig);
9482 if (unlikely(ret))
9483 goto out;
9484
Jens Axboe2b188cc2019-01-07 10:46:33 -07009485 min_complete = min(min_complete, ctx->cq_entries);
9486
Xiaoguang Wang32b22442020-03-11 09:26:09 +08009487 /*
9488 * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user
9489 * space applications don't need to do io completion events
9490 * polling again, they can rely on io_sq_thread to do polling
9491 * work, which can reduce cpu usage and uring_lock contention.
9492 */
9493 if (ctx->flags & IORING_SETUP_IOPOLL &&
9494 !(ctx->flags & IORING_SETUP_SQPOLL)) {
Pavel Begunkov7668b922020-07-07 16:36:21 +03009495 ret = io_iopoll_check(ctx, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07009496 } else {
Hao Xuc73ebb62020-11-03 10:54:37 +08009497 ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts);
Jens Axboedef596e2019-01-09 08:59:42 -07009498 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009499 }
9500
Pavel Begunkov7c504e652019-12-18 19:53:45 +03009501out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03009502 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009503out_fput:
9504 fdput(f);
9505 return submitted ? submitted : ret;
9506}
9507
Tobias Klauserbebdb652020-02-26 18:38:32 +01009508#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009509static int io_uring_show_cred(int id, void *p, void *data)
9510{
Jens Axboe6b47ab82020-11-05 09:50:16 -07009511 struct io_identity *iod = p;
9512 const struct cred *cred = iod->creds;
Jens Axboe87ce9552020-01-30 08:25:34 -07009513 struct seq_file *m = data;
9514 struct user_namespace *uns = seq_user_ns(m);
9515 struct group_info *gi;
9516 kernel_cap_t cap;
9517 unsigned __capi;
9518 int g;
9519
9520 seq_printf(m, "%5d\n", id);
9521 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
9522 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
9523 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
9524 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
9525 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
9526 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
9527 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
9528 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
9529 seq_puts(m, "\n\tGroups:\t");
9530 gi = cred->group_info;
9531 for (g = 0; g < gi->ngroups; g++) {
9532 seq_put_decimal_ull(m, g ? " " : "",
9533 from_kgid_munged(uns, gi->gid[g]));
9534 }
9535 seq_puts(m, "\n\tCapEff:\t");
9536 cap = cred->cap_effective;
9537 CAP_FOR_EACH_U32(__capi)
9538 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
9539 seq_putc(m, '\n');
9540 return 0;
9541}
9542
9543static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
9544{
Joseph Qidbbe9c62020-09-29 09:01:22 -06009545 struct io_sq_data *sq = NULL;
Jens Axboefad8e0d2020-09-28 08:57:48 -06009546 bool has_lock;
Jens Axboe87ce9552020-01-30 08:25:34 -07009547 int i;
9548
Jens Axboefad8e0d2020-09-28 08:57:48 -06009549 /*
9550 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
9551 * since fdinfo case grabs it in the opposite direction of normal use
9552 * cases. If we fail to get the lock, we just don't iterate any
9553 * structures that could be going away outside the io_uring mutex.
9554 */
9555 has_lock = mutex_trylock(&ctx->uring_lock);
9556
Joseph Qidbbe9c62020-09-29 09:01:22 -06009557 if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL))
9558 sq = ctx->sq_data;
9559
9560 seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
9561 seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
Jens Axboe87ce9552020-01-30 08:25:34 -07009562 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009563 for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
Pavel Begunkovea64ec022021-02-04 13:52:07 +00009564 struct file *f = *io_fixed_file_slot(ctx->file_data, i);
Jens Axboe87ce9552020-01-30 08:25:34 -07009565
Jens Axboe87ce9552020-01-30 08:25:34 -07009566 if (f)
9567 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
9568 else
9569 seq_printf(m, "%5u: <none>\n", i);
9570 }
9571 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009572 for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009573 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
9574
9575 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
9576 (unsigned int) buf->len);
9577 }
Jens Axboefad8e0d2020-09-28 08:57:48 -06009578 if (has_lock && !idr_is_empty(&ctx->personality_idr)) {
Jens Axboe87ce9552020-01-30 08:25:34 -07009579 seq_printf(m, "Personalities:\n");
9580 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
9581 }
Jens Axboed7718a92020-02-14 22:23:12 -07009582 seq_printf(m, "PollList:\n");
9583 spin_lock_irq(&ctx->completion_lock);
9584 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
9585 struct hlist_head *list = &ctx->cancel_hash[i];
9586 struct io_kiocb *req;
9587
9588 hlist_for_each_entry(req, list, hash_node)
9589 seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
9590 req->task->task_works != NULL);
9591 }
9592 spin_unlock_irq(&ctx->completion_lock);
Jens Axboefad8e0d2020-09-28 08:57:48 -06009593 if (has_lock)
9594 mutex_unlock(&ctx->uring_lock);
Jens Axboe87ce9552020-01-30 08:25:34 -07009595}
9596
9597static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
9598{
9599 struct io_ring_ctx *ctx = f->private_data;
9600
9601 if (percpu_ref_tryget(&ctx->refs)) {
9602 __io_uring_show_fdinfo(ctx, m);
9603 percpu_ref_put(&ctx->refs);
9604 }
9605}
Tobias Klauserbebdb652020-02-26 18:38:32 +01009606#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07009607
Jens Axboe2b188cc2019-01-07 10:46:33 -07009608static const struct file_operations io_uring_fops = {
9609 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06009610 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07009611 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01009612#ifndef CONFIG_MMU
9613 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
9614 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
9615#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009616 .poll = io_uring_poll,
9617 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009618#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07009619 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01009620#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009621};
9622
9623static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
9624 struct io_uring_params *p)
9625{
Hristo Venev75b28af2019-08-26 17:23:46 +00009626 struct io_rings *rings;
9627 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009628
Jens Axboebd740482020-08-05 12:58:23 -06009629 /* make sure these are sane, as we already accounted them */
9630 ctx->sq_entries = p->sq_entries;
9631 ctx->cq_entries = p->cq_entries;
9632
Hristo Venev75b28af2019-08-26 17:23:46 +00009633 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
9634 if (size == SIZE_MAX)
9635 return -EOVERFLOW;
9636
9637 rings = io_mem_alloc(size);
9638 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009639 return -ENOMEM;
9640
Hristo Venev75b28af2019-08-26 17:23:46 +00009641 ctx->rings = rings;
9642 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
9643 rings->sq_ring_mask = p->sq_entries - 1;
9644 rings->cq_ring_mask = p->cq_entries - 1;
9645 rings->sq_ring_entries = p->sq_entries;
9646 rings->cq_ring_entries = p->cq_entries;
9647 ctx->sq_mask = rings->sq_ring_mask;
9648 ctx->cq_mask = rings->cq_ring_mask;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009649
9650 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07009651 if (size == SIZE_MAX) {
9652 io_mem_free(ctx->rings);
9653 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009654 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07009655 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009656
9657 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07009658 if (!ctx->sq_sqes) {
9659 io_mem_free(ctx->rings);
9660 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009661 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07009662 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009663
Jens Axboe2b188cc2019-01-07 10:46:33 -07009664 return 0;
9665}
9666
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009667static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file)
9668{
9669 int ret, fd;
9670
9671 fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
9672 if (fd < 0)
9673 return fd;
9674
9675 ret = io_uring_add_task_file(ctx, file);
9676 if (ret) {
9677 put_unused_fd(fd);
9678 return ret;
9679 }
9680 fd_install(fd, file);
9681 return fd;
9682}
9683
Jens Axboe2b188cc2019-01-07 10:46:33 -07009684/*
9685 * Allocate an anonymous fd, this is what constitutes the application
9686 * visible backing of an io_uring instance. The application mmaps this
9687 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
9688 * we have to tie this fd to a socket for file garbage collection purposes.
9689 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009690static struct file *io_uring_get_file(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009691{
9692 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009693#if defined(CONFIG_UNIX)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009694 int ret;
9695
Jens Axboe2b188cc2019-01-07 10:46:33 -07009696 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
9697 &ctx->ring_sock);
9698 if (ret)
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009699 return ERR_PTR(ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009700#endif
9701
Jens Axboe2b188cc2019-01-07 10:46:33 -07009702 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
9703 O_RDWR | O_CLOEXEC);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009704#if defined(CONFIG_UNIX)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009705 if (IS_ERR(file)) {
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009706 sock_release(ctx->ring_sock);
9707 ctx->ring_sock = NULL;
9708 } else {
9709 ctx->ring_sock->file = file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009710 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009711#endif
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009712 return file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009713}
9714
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009715static int io_uring_create(unsigned entries, struct io_uring_params *p,
9716 struct io_uring_params __user *params)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009717{
9718 struct user_struct *user = NULL;
9719 struct io_ring_ctx *ctx;
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009720 struct file *file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009721 int ret;
9722
Jens Axboe8110c1a2019-12-28 15:39:54 -07009723 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07009724 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009725 if (entries > IORING_MAX_ENTRIES) {
9726 if (!(p->flags & IORING_SETUP_CLAMP))
9727 return -EINVAL;
9728 entries = IORING_MAX_ENTRIES;
9729 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009730
9731 /*
9732 * Use twice as many entries for the CQ ring. It's possible for the
9733 * application to drive a higher depth than the size of the SQ ring,
9734 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06009735 * some flexibility in overcommitting a bit. If the application has
9736 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
9737 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07009738 */
9739 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06009740 if (p->flags & IORING_SETUP_CQSIZE) {
9741 /*
9742 * If IORING_SETUP_CQSIZE is set, we do the same roundup
9743 * to a power-of-two, if it isn't already. We do NOT impose
9744 * any cq vs sq ring sizing.
9745 */
Joseph Qieb2667b32020-11-24 15:03:03 +08009746 if (!p->cq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06009747 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07009748 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
9749 if (!(p->flags & IORING_SETUP_CLAMP))
9750 return -EINVAL;
9751 p->cq_entries = IORING_MAX_CQ_ENTRIES;
9752 }
Joseph Qieb2667b32020-11-24 15:03:03 +08009753 p->cq_entries = roundup_pow_of_two(p->cq_entries);
9754 if (p->cq_entries < p->sq_entries)
9755 return -EINVAL;
Jens Axboe33a107f2019-10-04 12:10:03 -06009756 } else {
9757 p->cq_entries = 2 * p->sq_entries;
9758 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07009759
9760 user = get_uid(current_user());
Jens Axboe2b188cc2019-01-07 10:46:33 -07009761
9762 ctx = io_ring_ctx_alloc(p);
9763 if (!ctx) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07009764 free_uid(user);
9765 return -ENOMEM;
9766 }
9767 ctx->compat = in_compat_syscall();
Jens Axboe26bfa89e2021-02-09 20:14:12 -07009768 ctx->limit_mem = !capable(CAP_IPC_LOCK);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009769 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07009770 ctx->creds = get_current_cred();
Jens Axboe4ea33a92020-10-15 13:46:44 -06009771#ifdef CONFIG_AUDIT
9772 ctx->loginuid = current->loginuid;
9773 ctx->sessionid = current->sessionid;
9774#endif
Jens Axboe2aede0e2020-09-14 10:45:53 -06009775 ctx->sqo_task = get_task_struct(current);
9776
9777 /*
9778 * This is just grabbed for accounting purposes. When a process exits,
9779 * the mm is exited and dropped before the files, hence we need to hang
9780 * on to this mm purely for the purposes of being able to unaccount
9781 * memory (locked/pinned vm). It's not used for anything else.
9782 */
Jens Axboe6b7898e2020-08-25 07:58:00 -06009783 mmgrab(current->mm);
Jens Axboe2aede0e2020-09-14 10:45:53 -06009784 ctx->mm_account = current->mm;
Jens Axboe6b7898e2020-08-25 07:58:00 -06009785
Dennis Zhou91d8f512020-09-16 13:41:05 -07009786#ifdef CONFIG_BLK_CGROUP
9787 /*
9788 * The sq thread will belong to the original cgroup it was inited in.
9789 * If the cgroup goes offline (e.g. disabling the io controller), then
9790 * issued bios will be associated with the closest cgroup later in the
9791 * block layer.
9792 */
9793 rcu_read_lock();
9794 ctx->sqo_blkcg_css = blkcg_css();
9795 ret = css_tryget_online(ctx->sqo_blkcg_css);
9796 rcu_read_unlock();
9797 if (!ret) {
9798 /* don't init against a dying cgroup, have the user try again */
9799 ctx->sqo_blkcg_css = NULL;
9800 ret = -ENODEV;
9801 goto err;
9802 }
9803#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07009804 ret = io_allocate_scq_urings(ctx, p);
9805 if (ret)
9806 goto err;
9807
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009808 ret = io_sq_offload_create(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009809 if (ret)
9810 goto err;
9811
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009812 if (!(p->flags & IORING_SETUP_R_DISABLED))
9813 io_sq_offload_start(ctx);
9814
Jens Axboe2b188cc2019-01-07 10:46:33 -07009815 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009816 p->sq_off.head = offsetof(struct io_rings, sq.head);
9817 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
9818 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
9819 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
9820 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
9821 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
9822 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009823
9824 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00009825 p->cq_off.head = offsetof(struct io_rings, cq.head);
9826 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
9827 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
9828 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
9829 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
9830 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Stefano Garzarella0d9b5b32020-05-15 18:38:04 +02009831 p->cq_off.flags = offsetof(struct io_rings, cq_flags);
Jens Axboeac90f242019-09-06 10:26:21 -06009832
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009833 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
9834 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
Jiufei Xue5769a352020-06-17 17:53:55 +08009835 IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL |
Hao Xuc73ebb62020-11-03 10:54:37 +08009836 IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED |
9837 IORING_FEAT_EXT_ARG;
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009838
9839 if (copy_to_user(params, p, sizeof(*p))) {
9840 ret = -EFAULT;
9841 goto err;
9842 }
Jens Axboed1719f72020-07-30 13:43:53 -06009843
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009844 file = io_uring_get_file(ctx);
9845 if (IS_ERR(file)) {
9846 ret = PTR_ERR(file);
9847 goto err;
9848 }
9849
Jens Axboed1719f72020-07-30 13:43:53 -06009850 /*
Jens Axboe044c1ab2019-10-28 09:15:33 -06009851 * Install ring fd as the very last thing, so we don't risk someone
9852 * having closed it before we finish setup
9853 */
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009854 ret = io_uring_install_fd(ctx, file);
9855 if (ret < 0) {
Pavel Begunkov06585c42021-01-13 12:42:25 +00009856 io_disable_sqo_submit(ctx);
Pavel Begunkov9faadcc2020-12-21 18:34:05 +00009857 /* fput will clean it up */
9858 fput(file);
9859 return ret;
9860 }
Jens Axboe044c1ab2019-10-28 09:15:33 -06009861
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02009862 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009863 return ret;
9864err:
Pavel Begunkovd9d05212021-01-08 20:57:25 +00009865 io_disable_sqo_submit(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009866 io_ring_ctx_wait_and_kill(ctx);
9867 return ret;
9868}
9869
9870/*
9871 * Sets up an aio uring context, and returns the fd. Applications asks for a
9872 * ring size, we return the actual sq/cq ring sizes (among other things) in the
9873 * params structure passed in.
9874 */
9875static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
9876{
9877 struct io_uring_params p;
Jens Axboe2b188cc2019-01-07 10:46:33 -07009878 int i;
9879
9880 if (copy_from_user(&p, params, sizeof(p)))
9881 return -EFAULT;
9882 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
9883 if (p.resv[i])
9884 return -EINVAL;
9885 }
9886
Jens Axboe6c271ce2019-01-10 11:22:30 -07009887 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07009888 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009889 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ |
9890 IORING_SETUP_R_DISABLED))
Jens Axboe2b188cc2019-01-07 10:46:33 -07009891 return -EINVAL;
9892
Xiaoguang Wang7f136572020-05-05 16:28:53 +08009893 return io_uring_create(entries, &p, params);
Jens Axboe2b188cc2019-01-07 10:46:33 -07009894}
9895
9896SYSCALL_DEFINE2(io_uring_setup, u32, entries,
9897 struct io_uring_params __user *, params)
9898{
9899 return io_uring_setup(entries, params);
9900}
9901
Jens Axboe66f4af92020-01-16 15:36:52 -07009902static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
9903{
9904 struct io_uring_probe *p;
9905 size_t size;
9906 int i, ret;
9907
9908 size = struct_size(p, ops, nr_args);
9909 if (size == SIZE_MAX)
9910 return -EOVERFLOW;
9911 p = kzalloc(size, GFP_KERNEL);
9912 if (!p)
9913 return -ENOMEM;
9914
9915 ret = -EFAULT;
9916 if (copy_from_user(p, arg, size))
9917 goto out;
9918 ret = -EINVAL;
9919 if (memchr_inv(p, 0, size))
9920 goto out;
9921
9922 p->last_op = IORING_OP_LAST - 1;
9923 if (nr_args > IORING_OP_LAST)
9924 nr_args = IORING_OP_LAST;
9925
9926 for (i = 0; i < nr_args; i++) {
9927 p->ops[i].op = i;
9928 if (!io_op_defs[i].not_supported)
9929 p->ops[i].flags = IO_URING_OP_SUPPORTED;
9930 }
9931 p->ops_len = i;
9932
9933 ret = 0;
9934 if (copy_to_user(arg, p, size))
9935 ret = -EFAULT;
9936out:
9937 kfree(p);
9938 return ret;
9939}
9940
Jens Axboe071698e2020-01-28 10:04:42 -07009941static int io_register_personality(struct io_ring_ctx *ctx)
9942{
Jens Axboe1e6fa522020-10-15 08:46:24 -06009943 struct io_identity *id;
9944 int ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009945
Jens Axboe1e6fa522020-10-15 08:46:24 -06009946 id = kmalloc(sizeof(*id), GFP_KERNEL);
9947 if (unlikely(!id))
9948 return -ENOMEM;
9949
9950 io_init_identity(id);
9951 id->creds = get_current_cred();
9952
9953 ret = idr_alloc_cyclic(&ctx->personality_idr, id, 1, USHRT_MAX, GFP_KERNEL);
9954 if (ret < 0) {
9955 put_cred(id->creds);
9956 kfree(id);
9957 }
9958 return ret;
Jens Axboe071698e2020-01-28 10:04:42 -07009959}
9960
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009961static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
9962 unsigned int nr_args)
9963{
9964 struct io_uring_restriction *res;
9965 size_t size;
9966 int i, ret;
9967
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009968 /* Restrictions allowed only if rings started disabled */
9969 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
9970 return -EBADFD;
9971
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009972 /* We allow only a single restrictions registration */
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +02009973 if (ctx->restrictions.registered)
Stefano Garzarella21b55db2020-08-27 16:58:30 +02009974 return -EBUSY;
9975
9976 if (!arg || nr_args > IORING_MAX_RESTRICTIONS)
9977 return -EINVAL;
9978
9979 size = array_size(nr_args, sizeof(*res));
9980 if (size == SIZE_MAX)
9981 return -EOVERFLOW;
9982
9983 res = memdup_user(arg, size);
9984 if (IS_ERR(res))
9985 return PTR_ERR(res);
9986
9987 ret = 0;
9988
9989 for (i = 0; i < nr_args; i++) {
9990 switch (res[i].opcode) {
9991 case IORING_RESTRICTION_REGISTER_OP:
9992 if (res[i].register_op >= IORING_REGISTER_LAST) {
9993 ret = -EINVAL;
9994 goto out;
9995 }
9996
9997 __set_bit(res[i].register_op,
9998 ctx->restrictions.register_op);
9999 break;
10000 case IORING_RESTRICTION_SQE_OP:
10001 if (res[i].sqe_op >= IORING_OP_LAST) {
10002 ret = -EINVAL;
10003 goto out;
10004 }
10005
10006 __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op);
10007 break;
10008 case IORING_RESTRICTION_SQE_FLAGS_ALLOWED:
10009 ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags;
10010 break;
10011 case IORING_RESTRICTION_SQE_FLAGS_REQUIRED:
10012 ctx->restrictions.sqe_flags_required = res[i].sqe_flags;
10013 break;
10014 default:
10015 ret = -EINVAL;
10016 goto out;
10017 }
10018 }
10019
10020out:
10021 /* Reset all restrictions if an error happened */
10022 if (ret != 0)
10023 memset(&ctx->restrictions, 0, sizeof(ctx->restrictions));
10024 else
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010025 ctx->restrictions.registered = true;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010026
10027 kfree(res);
10028 return ret;
10029}
10030
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010031static int io_register_enable_rings(struct io_ring_ctx *ctx)
10032{
10033 if (!(ctx->flags & IORING_SETUP_R_DISABLED))
10034 return -EBADFD;
10035
10036 if (ctx->restrictions.registered)
10037 ctx->restricted = 1;
10038
10039 ctx->flags &= ~IORING_SETUP_R_DISABLED;
10040
10041 io_sq_offload_start(ctx);
10042
10043 return 0;
10044}
10045
Jens Axboe071698e2020-01-28 10:04:42 -070010046static bool io_register_op_must_quiesce(int op)
10047{
10048 switch (op) {
10049 case IORING_UNREGISTER_FILES:
10050 case IORING_REGISTER_FILES_UPDATE:
10051 case IORING_REGISTER_PROBE:
10052 case IORING_REGISTER_PERSONALITY:
10053 case IORING_UNREGISTER_PERSONALITY:
10054 return false;
10055 default:
10056 return true;
10057 }
10058}
10059
Jens Axboeedafcce2019-01-09 09:16:05 -070010060static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
10061 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -060010062 __releases(ctx->uring_lock)
10063 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -070010064{
10065 int ret;
10066
Jens Axboe35fa71a2019-04-22 10:23:23 -060010067 /*
10068 * We're inside the ring mutex, if the ref is already dying, then
10069 * someone else killed the ctx or is already going through
10070 * io_uring_register().
10071 */
10072 if (percpu_ref_is_dying(&ctx->refs))
10073 return -ENXIO;
10074
Jens Axboe071698e2020-01-28 10:04:42 -070010075 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010076 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -060010077
Jens Axboe05f3fb32019-12-09 11:22:50 -070010078 /*
10079 * Drop uring mutex before waiting for references to exit. If
10080 * another thread is currently inside io_uring_enter() it might
10081 * need to grab the uring_lock to make progress. If we hold it
10082 * here across the drain wait, then we can deadlock. It's safe
10083 * to drop the mutex here, since no new references will come in
10084 * after we've killed the percpu ref.
10085 */
10086 mutex_unlock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010087 do {
10088 ret = wait_for_completion_interruptible(&ctx->ref_comp);
10089 if (!ret)
10090 break;
Jens Axboeed6930c2020-10-08 19:09:46 -060010091 ret = io_run_task_work_sig();
10092 if (ret < 0)
10093 break;
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010094 } while (1);
10095
Jens Axboe05f3fb32019-12-09 11:22:50 -070010096 mutex_lock(&ctx->uring_lock);
Jens Axboeaf9c1a42020-09-24 13:32:18 -060010097
Pavel Begunkov88f171a2021-02-20 18:03:50 +000010098 if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp))
10099 return ret;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010100 }
10101
10102 if (ctx->restricted) {
10103 if (opcode >= IORING_REGISTER_LAST) {
10104 ret = -EINVAL;
10105 goto out;
10106 }
10107
10108 if (!test_bit(opcode, ctx->restrictions.register_op)) {
10109 ret = -EACCES;
Jens Axboec1503682020-01-08 08:26:07 -070010110 goto out;
10111 }
Jens Axboe05f3fb32019-12-09 11:22:50 -070010112 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010113
10114 switch (opcode) {
10115 case IORING_REGISTER_BUFFERS:
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010116 ret = io_sqe_buffers_register(ctx, arg, nr_args);
Jens Axboeedafcce2019-01-09 09:16:05 -070010117 break;
10118 case IORING_UNREGISTER_BUFFERS:
10119 ret = -EINVAL;
10120 if (arg || nr_args)
10121 break;
Bijan Mottahedeh0a96bbe2021-01-06 12:39:10 -080010122 ret = io_sqe_buffers_unregister(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -070010123 break;
Jens Axboe6b063142019-01-10 22:13:58 -070010124 case IORING_REGISTER_FILES:
10125 ret = io_sqe_files_register(ctx, arg, nr_args);
10126 break;
10127 case IORING_UNREGISTER_FILES:
10128 ret = -EINVAL;
10129 if (arg || nr_args)
10130 break;
10131 ret = io_sqe_files_unregister(ctx);
10132 break;
Jens Axboec3a31e62019-10-03 13:59:56 -060010133 case IORING_REGISTER_FILES_UPDATE:
10134 ret = io_sqe_files_update(ctx, arg, nr_args);
10135 break;
Jens Axboe9b402842019-04-11 11:45:41 -060010136 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -070010137 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -060010138 ret = -EINVAL;
10139 if (nr_args != 1)
10140 break;
10141 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -070010142 if (ret)
10143 break;
10144 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
10145 ctx->eventfd_async = 1;
10146 else
10147 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -060010148 break;
10149 case IORING_UNREGISTER_EVENTFD:
10150 ret = -EINVAL;
10151 if (arg || nr_args)
10152 break;
10153 ret = io_eventfd_unregister(ctx);
10154 break;
Jens Axboe66f4af92020-01-16 15:36:52 -070010155 case IORING_REGISTER_PROBE:
10156 ret = -EINVAL;
10157 if (!arg || nr_args > 256)
10158 break;
10159 ret = io_probe(ctx, arg, nr_args);
10160 break;
Jens Axboe071698e2020-01-28 10:04:42 -070010161 case IORING_REGISTER_PERSONALITY:
10162 ret = -EINVAL;
10163 if (arg || nr_args)
10164 break;
10165 ret = io_register_personality(ctx);
10166 break;
10167 case IORING_UNREGISTER_PERSONALITY:
10168 ret = -EINVAL;
10169 if (arg)
10170 break;
10171 ret = io_unregister_personality(ctx, nr_args);
10172 break;
Stefano Garzarella7e84e1c2020-08-27 16:58:31 +020010173 case IORING_REGISTER_ENABLE_RINGS:
10174 ret = -EINVAL;
10175 if (arg || nr_args)
10176 break;
10177 ret = io_register_enable_rings(ctx);
10178 break;
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010179 case IORING_REGISTER_RESTRICTIONS:
10180 ret = io_register_restrictions(ctx, arg, nr_args);
10181 break;
Jens Axboeedafcce2019-01-09 09:16:05 -070010182 default:
10183 ret = -EINVAL;
10184 break;
10185 }
10186
Stefano Garzarella21b55db2020-08-27 16:58:30 +020010187out:
Jens Axboe071698e2020-01-28 10:04:42 -070010188 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -070010189 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -070010190 percpu_ref_reinit(&ctx->refs);
Jens Axboe0f158b42020-05-14 17:18:39 -060010191 reinit_completion(&ctx->ref_comp);
Jens Axboe05f3fb32019-12-09 11:22:50 -070010192 }
Jens Axboeedafcce2019-01-09 09:16:05 -070010193 return ret;
10194}
10195
10196SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
10197 void __user *, arg, unsigned int, nr_args)
10198{
10199 struct io_ring_ctx *ctx;
10200 long ret = -EBADF;
10201 struct fd f;
10202
10203 f = fdget(fd);
10204 if (!f.file)
10205 return -EBADF;
10206
10207 ret = -EOPNOTSUPP;
10208 if (f.file->f_op != &io_uring_fops)
10209 goto out_fput;
10210
10211 ctx = f.file->private_data;
10212
Pavel Begunkovb6c23dd2021-02-20 15:17:18 +000010213 io_run_task_work();
10214
Jens Axboeedafcce2019-01-09 09:16:05 -070010215 mutex_lock(&ctx->uring_lock);
10216 ret = __io_uring_register(ctx, opcode, arg, nr_args);
10217 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020010218 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
10219 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -070010220out_fput:
10221 fdput(f);
10222 return ret;
10223}
10224
Jens Axboe2b188cc2019-01-07 10:46:33 -070010225static int __init io_uring_init(void)
10226{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010227#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
10228 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
10229 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
10230} while (0)
10231
10232#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
10233 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
10234 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
10235 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
10236 BUILD_BUG_SQE_ELEM(1, __u8, flags);
10237 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
10238 BUILD_BUG_SQE_ELEM(4, __s32, fd);
10239 BUILD_BUG_SQE_ELEM(8, __u64, off);
10240 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
10241 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010242 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010243 BUILD_BUG_SQE_ELEM(24, __u32, len);
10244 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
10245 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
10246 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
10247 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
Jiufei Xue5769a352020-06-17 17:53:55 +080010248 BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events);
10249 BUILD_BUG_SQE_ELEM(28, __u32, poll32_events);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010250 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
10251 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
10252 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
10253 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
10254 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
10255 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
10256 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
10257 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010258 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010259 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
10260 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
10261 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +030010262 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +010010263
Jens Axboed3656342019-12-18 09:50:26 -070010264 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe84557872020-03-03 15:28:17 -070010265 BUILD_BUG_ON(__REQ_F_LAST_BIT >= 8 * sizeof(int));
Jens Axboe91f245d2021-02-09 13:48:50 -070010266 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC |
10267 SLAB_ACCOUNT);
Jens Axboe2b188cc2019-01-07 10:46:33 -070010268 return 0;
10269};
10270__initcall(io_uring_init);